typo
[phpmyadmin.git] / libraries / export / php_array.php
blobe40145df2e5a61d35501acb381d90cb73c4e76e0
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 /**
16 if (isset($plugin_list)) {
17 $plugin_list['php_array'] = array(
18 'text' => __('PHP array'),
19 'extension' => 'php',
20 'mime_type' => 'text/plain',
21 'options' => array(
22 array('type' => 'begin_group', 'name' => 'general_opts'),
23 array(
24 'type' => 'hidden',
25 'name' => 'structure_or_data',
27 array('type' => 'end_group')
29 'options_text' => __('Options'),
31 } else {
33 /**
34 * Set of functions used to build exports of tables
37 /**
38 * Outputs export footer
40 * @return bool Whether it succeeded
42 * @access public
44 function PMA_exportFooter()
46 return true;
49 /**
50 * Outputs export header
52 * @return bool Whether it succeeded
54 * @access public
56 function PMA_exportHeader()
58 PMA_exportOutputHandler(
59 '<?php' . $GLOBALS['crlf']
60 . '/**' . $GLOBALS['crlf']
61 . ' * Export to PHP Array plugin for PHPMyAdmin' . $GLOBALS['crlf']
62 . ' * @version 0.2b' . $GLOBALS['crlf']
63 . ' */' . $GLOBALS['crlf'] . $GLOBALS['crlf']
65 return true;
68 /**
69 * Outputs database header
71 * @param string $db Database name
72 * @return bool Whether it succeeded
74 * @access public
76 function PMA_exportDBHeader($db)
78 PMA_exportOutputHandler('//' . $GLOBALS['crlf'] . '// Database ' . PMA_backquote($db) . $GLOBALS['crlf'] . '//' . $GLOBALS['crlf']);
79 return true;
82 /**
83 * Outputs database footer
85 * @param string $db Database name
86 * @return bool Whether it succeeded
88 * @access public
90 function PMA_exportDBFooter($db)
92 return true;
95 /**
96 * Outputs CREATE DATABASE statement
98 * @param string $db Database name
99 * @return bool Whether it succeeded
101 * @access public
103 function PMA_exportDBCreate($db)
105 return true;
109 * Outputs the content of a table as a fragment of PHP code
111 * @param string $db database name
112 * @param string $table table name
113 * @param string $crlf the end of line sequence
114 * @param string $error_url the url to go back in case of error
115 * @param string $sql_query SQL query for obtaining data
116 * @return bool Whether it succeeded
118 * @access public
120 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
122 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
124 $columns_cnt = PMA_DBI_num_fields($result);
125 for ($i = 0; $i < $columns_cnt; $i++) {
126 $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
128 unset($i);
130 // fix variable names (based on http://www.php.net/manual/language.variables.basics.php)
131 if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $table) == false) {
132 // fix invalid chars in variable names by replacing them with underscores
133 $tablefixed = preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '_', $table);
135 // variable name must not start with a number or dash...
136 if (preg_match('/^[a-zA-Z_\x7f-\xff]/', $tablefixed) == false) {
137 $tablefixed = '_' . $tablefixed;
139 } else {
140 $tablefixed = $table;
143 $buffer = '';
144 $record_cnt = 0;
145 while ($record = PMA_DBI_fetch_row($result)) {
147 $record_cnt++;
149 // Output table name as comment if this is the first record of the table
150 if ($record_cnt == 1) {
151 $buffer .= $crlf . '// ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $crlf;
152 $buffer .= '$' . $tablefixed . ' = array(' . $crlf;
153 $buffer .= ' array(';
154 } else {
155 $buffer .= ',' . $crlf . ' array(';
158 for ($i = 0; $i < $columns_cnt; $i++) {
159 $buffer .= var_export($columns[$i], true) . " => " . var_export($record[$i], true) . (($i + 1 >= $columns_cnt) ? '' : ',');
162 $buffer .= ')';
165 $buffer .= $crlf . ');' . $crlf;
166 if (! PMA_exportOutputHandler($buffer)) {
167 return false;
170 PMA_DBI_free_result($result);
172 return true;