2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build dumps of tables as PHP Arrays
6 * @package PhpMyAdmin-Export
9 if (! defined('PHPMYADMIN')) {
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
17 * Handles the export for the PHP Array class
19 * @package PhpMyAdmin-Export
22 class ExportPhparray
extends ExportPlugin
27 public function __construct()
29 $this->setProperties();
33 * Sets the export PHP Array properties
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;
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
81 public function update (SplSubject
$subject)
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']
103 * Outputs export footer
105 * @return bool Whether it succeeded
107 public function exportFooter ()
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']
130 * Outputs database footer
132 * @param string $db Database name
134 * @return bool Whether it succeeded
136 public function exportDBFooter ($db)
142 * Outputs CREATE DATABASE statement
144 * @param string $db Database name
146 * @return bool Whether it succeeded
148 public function exportDBCreate($db)
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));
174 // fix variable names (based on
175 // http://www.php.net/manual/language.variables.basics.php)
177 '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/',
180 // fix invalid characters in variable names by replacing them with
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;
189 $tablefixed = $table;
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)) {
203 if ($record_cnt == 1) {
204 $buffer .= $crlf . ' array(';
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) ?
'' : ',');
218 $buffer .= $crlf . ');' . $crlf;
219 if (! PMA_exportOutputHandler($buffer)) {
223 PMA_DBI_free_result($result);