Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugins / export / ExportPhparray.class.php
blob2d3b73c545c7f9104bf888fd6887301a1f146c71
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build dumps of tables as PHP Arrays
6 * @package PhpMyAdmin-Export
7 * @subpackage PHP
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
16 /**
17 * Handles the export for the PHP Array class
19 * @package PhpMyAdmin-Export
20 * @subpackage PHP
22 class ExportPhparray extends ExportPlugin
24 /**
25 * Constructor
27 public function __construct()
29 $this->setProperties();
32 /**
33 * Sets the export PHP Array properties
35 * @return void
37 protected function setProperties()
39 $props = 'libraries/properties/';
40 include_once "$props/plugins/ExportPluginProperties.class.php";
41 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
42 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
43 include_once "$props/options/items/HiddenPropertyItem.class.php";
45 $exportPluginProperties = new ExportPluginProperties();
46 $exportPluginProperties->setText('PHP array');
47 $exportPluginProperties->setExtension('php');
48 $exportPluginProperties->setMimeType('text/plain');
49 $exportPluginProperties->setOptionsText(__('Options'));
51 // create the root group that will be the options field for
52 // $exportPluginProperties
53 // this will be shown as "Format specific options"
54 $exportSpecificOptions = new OptionsPropertyRootGroup();
55 $exportSpecificOptions->setName("Format Specific Options");
57 // general options main group
58 $generalOptions = new OptionsPropertyMainGroup();
59 $generalOptions->setName("general_opts");
60 // create primary items and add them to the group
61 $leaf = new HiddenPropertyItem();
62 $leaf->setName("structure_or_data");
63 $generalOptions->addProperty($leaf);
64 // add the main group to the root group
65 $exportSpecificOptions->addProperty($generalOptions);
67 // set the options for the export plugin property item
68 $exportPluginProperties->setOptions($exportSpecificOptions);
69 $this->properties = $exportPluginProperties;
72 /**
73 * This method is called when any PluginManager to which the observer
74 * is attached calls PluginManager::notify()
76 * @param SplSubject $subject The PluginManager notifying the observer
77 * of an update.
79 * @return void
81 public function update (SplSubject $subject)
85 /**
86 * Outputs export header
88 * @return bool Whether it succeeded
90 public function exportHeader ()
92 PMA_exportOutputHandler(
93 '<?php' . $GLOBALS['crlf']
94 . '/**' . $GLOBALS['crlf']
95 . ' * Export to PHP Array plugin for PHPMyAdmin' . $GLOBALS['crlf']
96 . ' * @version 0.2b' . $GLOBALS['crlf']
97 . ' */' . $GLOBALS['crlf'] . $GLOBALS['crlf']
99 return true;
103 * Outputs export footer
105 * @return bool Whether it succeeded
107 public function exportFooter ()
109 return true;
113 * Outputs database header
115 * @param string $db Database name
117 * @return bool Whether it succeeded
119 public function exportDBHeader ($db)
121 PMA_exportOutputHandler(
122 '//' . $GLOBALS['crlf']
123 . '// Database ' . PMA_Util::backquote($db)
124 . $GLOBALS['crlf'] . '//' . $GLOBALS['crlf']
126 return true;
130 * Outputs database footer
132 * @param string $db Database name
134 * @return bool Whether it succeeded
136 public function exportDBFooter ($db)
138 return true;
142 * Outputs CREATE DATABASE statement
144 * @param string $db Database name
146 * @return bool Whether it succeeded
148 public function exportDBCreate($db)
150 return true;
154 * Outputs the content of a table in NHibernate format
156 * @param string $db database name
157 * @param string $table table name
158 * @param string $crlf the end of line sequence
159 * @param string $error_url the url to go back in case of error
160 * @param string $sql_query SQL query for obtaining data
162 * @return bool Whether it succeeded
164 public function exportData($db, $table, $crlf, $error_url, $sql_query)
166 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
168 $columns_cnt = PMA_DBI_num_fields($result);
169 for ($i = 0; $i < $columns_cnt; $i++) {
170 $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
172 unset($i);
174 // fix variable names (based on
175 // http://www.php.net/manual/language.variables.basics.php)
176 if (! preg_match(
177 '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/',
178 $table
179 )) {
180 // fix invalid characters in variable names by replacing them with
181 // underscores
182 $tablefixed = preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '_', $table);
184 // variable name must not start with a number or dash...
185 if (preg_match('/^[a-zA-Z_\x7f-\xff]/', $tablefixed) == false) {
186 $tablefixed = '_' . $tablefixed;
188 } else {
189 $tablefixed = $table;
192 $buffer = '';
193 $record_cnt = 0;
194 // Output table name as comment
195 $buffer .= $crlf . '// '
196 . PMA_Util::backquote($db) . '.'
197 . PMA_Util::backquote($table) . $crlf;
198 $buffer .= '$' . $tablefixed . ' = array(';
200 while ($record = PMA_DBI_fetch_row($result)) {
201 $record_cnt++;
203 if ($record_cnt == 1) {
204 $buffer .= $crlf . ' array(';
205 } else {
206 $buffer .= ',' . $crlf . ' array(';
209 for ($i = 0; $i < $columns_cnt; $i++) {
210 $buffer .= var_export($columns[$i], true)
211 . " => " . var_export($record[$i], true)
212 . (($i + 1 >= $columns_cnt) ? '' : ',');
215 $buffer .= ')';
218 $buffer .= $crlf . ');' . $crlf;
219 if (! PMA_exportOutputHandler($buffer)) {
220 return false;
223 PMA_DBI_free_result($result);
224 return true;