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')) {
16 if (isset($plugin_list)) {
17 $plugin_list['php_array'] = array(
18 'text' => __('PHP array'),
20 'mime_type' => 'text/plain',
22 array('type' => 'begin_group', 'name' => 'general_opts'),
25 'name' => 'structure_or_data',
27 array('type' => 'end_group')
29 'options_text' => __('Options'),
34 * Set of functions used to build exports of tables
38 * Outputs export footer
40 * @return bool Whether it succeeded
44 function PMA_exportFooter()
50 * Outputs export header
52 * @return bool Whether it succeeded
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']
69 * Outputs database header
71 * @param string $db Database name
72 * @return bool Whether it succeeded
76 function PMA_exportDBHeader($db)
78 PMA_exportOutputHandler('//' . $GLOBALS['crlf'] . '// Database ' . PMA_backquote($db) . $GLOBALS['crlf'] . '//' . $GLOBALS['crlf']);
83 * Outputs database footer
85 * @param string $db Database name
86 * @return bool Whether it succeeded
90 function PMA_exportDBFooter($db)
96 * Outputs CREATE DATABASE statement
98 * @param string $db Database name
99 * @return bool Whether it succeeded
103 function PMA_exportDBCreate($db)
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
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));
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;
140 $tablefixed = $table;
145 while ($record = PMA_DBI_fetch_row($result)) {
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(';
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) ?
'' : ',');
165 $buffer .= $crlf . ');' . $crlf;
166 if (! PMA_exportOutputHandler($buffer)) {
170 PMA_DBI_free_result($result);