add backquotes to improve look and feel of output
[phpmyadmin.git] / libraries / export / php_array.php
blob06c4a1018fffcc7212647b665b16789f6d0760b2
1 <?php
2 /**
3 * Set of functions used to build dumps of tables as PHP Arrays
5 * @version 0.2b (20090704)
6 */
7 if (! defined('PHPMYADMIN')) {
8 exit;
11 /**
14 if (isset($plugin_list)) {
15 $plugin_list['php_array'] = array(
16 'text' => __('PHP array'),
17 'extension' => 'php',
18 'mime_type' => 'text/plain',
19 'options' => array(
20 array('type' => 'begin_group', 'name' => 'general_opts'),
21 array(
22 'type' => 'hidden',
23 'name' => 'structure_or_data',
25 array('type' => 'end_group')
27 'options_text' => __('Options'),
29 } else {
31 /**
32 * Set of functions used to build exports of tables
35 /**
36 * Outputs comment
38 * @param string Text of comment
40 * @return bool Whether it suceeded
42 function PMA_exportComment($text)
44 PMA_exportOutputHandler('// ' . $text . $GLOBALS['crlf']);
45 return true;
48 /**
49 * Outputs export footer
51 * @return bool Whether it suceeded
53 * @access public
55 function PMA_exportFooter()
57 return true;
60 /**
61 * Outputs export header
63 * @return bool Whether it suceeded
65 * @access public
67 function PMA_exportHeader()
69 PMA_exportOutputHandler(
70 '<?php' . $GLOBALS['crlf']
71 . '/**' . $GLOBALS['crlf']
72 . ' * Export to PHP Array plugin for PHPMyAdmin' . $GLOBALS['crlf']
73 . ' * @version 0.2b' . $GLOBALS['crlf']
74 . ' */' . $GLOBALS['crlf'] . $GLOBALS['crlf']
76 return true;
79 /**
80 * Outputs database header
82 * @param string Database name
84 * @return bool Whether it suceeded
86 * @access public
88 function PMA_exportDBHeader($db)
90 PMA_exportOutputHandler('//' . $GLOBALS['crlf'] . '// Database ' . PMA_backquote($db) . $GLOBALS['crlf'] . '//' . $GLOBALS['crlf']);
91 return true;
94 /**
95 * Outputs database footer
97 * @param string Database name
99 * @return bool Whether it suceeded
101 * @access public
103 function PMA_exportDBFooter($db)
105 return true;
109 * Outputs create database database
111 * @param string Database name
113 * @return bool Whether it suceeded
115 * @access public
117 function PMA_exportDBCreate($db)
119 return true;
123 * Outputs the content of a table in YAML format
125 * @param string the database name
126 * @param string the table name
127 * @param string the end of line sequence
128 * @param string the url to go back in case of error
129 * @param string SQL query for obtaining data
131 * @return bool Whether it suceeded
133 * @access public
135 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
137 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
139 $columns_cnt = PMA_DBI_num_fields($result);
140 for ($i = 0; $i < $columns_cnt; $i++) {
141 $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
143 unset($i);
145 // fix variable names (based on http://www.php.net/manual/language.variables.basics.php)
146 if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $table) == false) {
147 // fix invalid chars in variable names by replacing them with underscores
148 $tablefixed = preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '_', $table);
150 // variable name must not start with a number or dash...
151 if (preg_match('/^[a-zA-Z_\x7f-\xff]/', $tablefixed) == false) {
152 $tablefixed = '_' . $tablefixed;
154 } else {
155 $table = $tablefixed;
158 $buffer = '';
159 $record_cnt = 0;
160 while ($record = PMA_DBI_fetch_row($result)) {
162 $record_cnt++;
164 // Output table name as comment if this is the first record of the table
165 if ($record_cnt == 1) {
166 $buffer .= $crlf . '// ' . PMA_backquote($db) . '.' . PMA_backquote($table) . $crlf;
167 $buffer .= '$' . $tablefixed . ' = array(' . $crlf;
168 $buffer .= ' array(';
169 } else {
170 $buffer .= ',' . $crlf . ' array(';
173 for ($i = 0; $i < $columns_cnt; $i++) {
174 $buffer .= var_export($columns[$i], true) . "=>" . var_export($record[$i], true) . (($i + 1 >= $columns_cnt) ? '' : ',');
177 $buffer .= ')';
180 $buffer .= $crlf . ');' . $crlf;
181 if (! PMA_exportOutputHandler($buffer)) {
182 return FALSE;
185 PMA_DBI_free_result($result);
187 return true;