Fix XML export so it actually can export table structure
[phpmyadmin.git] / libraries / export / xml.php
blob83b51ee4c32ae4cf436bbcd2cfa613cc553b48be
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build XML dumps of tables
6 * @todo
7 * @package phpMyAdmin-Export-XML
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 if (strlen($GLOBALS['db'])) { /* Can't do server export */
15 if (isset($plugin_list)) {
16 $plugin_list['xml'] = array(
17 'text' => __('XML'),
18 'extension' => 'xml',
19 'mime_type' => 'text/xml',
20 'options' => array(
21 array('type' => 'begin_group', 'name' => 'general_opts'),
22 array('type' => 'hidden', 'name' => 'structure_or_data'),
23 array('type' => 'end_group')
25 'options_text' => __('Options')
28 /* Export structure */
29 $plugin_list['xml']['options'][] =
30 array('type' => 'begin_group', 'name' => 'structure', 'text' => __('Object creation options (all are recommended)'));
31 $plugin_list['xml']['options'][] =
32 array('type' => 'bool', 'name' => 'export_functions', 'text' => __('Functions'));
33 $plugin_list['xml']['options'][] =
34 array('type' => 'bool', 'name' => 'export_procedures', 'text' => __('Procedures'));
35 $plugin_list['xml']['options'][] =
36 array('type' => 'bool', 'name' => 'export_tables', 'text' => __('Tables'));
37 $plugin_list['xml']['options'][] =
38 array('type' => 'bool', 'name' => 'export_triggers', 'text' => __('Triggers'));
39 $plugin_list['xml']['options'][] =
40 array('type' => 'bool', 'name' => 'export_views', 'text' => __('Views'));
41 $plugin_list['xml']['options'][] = array('type' => 'end_group');
43 /* Data */
44 $plugin_list['xml']['options'][] =
45 array('type' => 'begin_group', 'name' => 'data', 'text' => __('Data dump options'));
46 $plugin_list['xml']['options'][] =
47 array('type' => 'bool', 'name' => 'export_contents', 'text' => __('Export contents'));
48 $plugin_list['xml']['options'][] = array('type' => 'end_group');
49 } else {
51 /**
52 * Outputs comment
54 * @param string Text of comment
56 * @return bool Whether it suceeded
58 function PMA_exportComment($text) {
59 return PMA_exportOutputHandler('<!-- ' . $text . ' -->' . $GLOBALS['crlf']);
62 /**
63 * Outputs export footer
65 * @return bool Whether it suceeded
67 * @access public
69 function PMA_exportFooter() {
70 $foot = '</pma_xml_export>';
72 return PMA_exportOutputHandler($foot);
75 /**
76 * Outputs export header
78 * @return bool Whether it suceeded
80 * @access public
82 function PMA_exportHeader() {
83 global $crlf;
84 global $cfg;
85 global $db;
86 global $table;
87 global $tables;
89 $export_struct = isset($GLOBALS['xml_export_functions']) || isset($GLOBALS['xml_export_procedures'])
90 || isset($GLOBALS['xml_export_tables']) || isset($GLOBALS['xml_export_triggers'])
91 || isset($GLOBALS['xml_export_views']);
92 $export_data = isset($GLOBALS['xml_export_contents']) ? true : false;
94 if ($GLOBALS['output_charset_conversion']) {
95 $charset = $GLOBALS['charset_of_file'];
96 } else {
97 $charset = $GLOBALS['charset'];
100 $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
101 . '<!--' . $crlf
102 . '- phpMyAdmin XML Dump' . $crlf
103 . '- version ' . PMA_VERSION . $crlf
104 . '- http://www.phpmyadmin.net' . $crlf
105 . '-' . $crlf
106 . '- ' . __('Host') . ': ' . $cfg['Server']['host'];
107 if (!empty($cfg['Server']['port'])) {
108 $head .= ':' . $cfg['Server']['port'];
110 $head .= $crlf
111 . '- ' . __('Generation Time') . ': ' . PMA_localisedDate() . $crlf
112 . '- ' . __('Server version') . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
113 . '- ' . __('PHP Version') . ': ' . phpversion() . $crlf
114 . '-->' . $crlf . $crlf;
116 $head .= '<pma_xml_export version="1.0"' . (($export_struct) ? ' xmlns:pma="http://www.phpmyadmin.net/some_doc_url/"' : '') . '>' . $crlf;
118 if ($export_struct) {
119 $result = PMA_DBI_fetch_result('SELECT `DEFAULT_CHARACTER_SET_NAME`, `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME` = \''.$db.'\' LIMIT 1');
120 $db_collation = $result[0]['DEFAULT_COLLATION_NAME'];
121 $db_charset = $result[0]['DEFAULT_CHARACTER_SET_NAME'];
123 $head .= ' <!--' . $crlf;
124 $head .= ' - Structure schemas' . $crlf;
125 $head .= ' -->' . $crlf;
126 $head .= ' <pma:structure_schemas>' . $crlf;
127 $head .= ' <pma:database name="' . htmlspecialchars($db) . '" collation="' . $db_collation . '" charset="' . $db_charset . '">' . $crlf;
129 if (count($tables) == 0) {
130 $tables[] = $table;
133 foreach ($tables as $table) {
134 // Export tables and views
135 $result = PMA_DBI_fetch_result('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), 0);
136 $tbl = $result[$table][1];
138 $is_view = PMA_isView($db, $table);
140 if ($is_view) {
141 $type = 'view';
142 } else {
143 $type = 'table';
146 if ($is_view && ! isset($GLOBALS['xml_export_views'])) {
147 continue;
150 if (! $is_view && ! isset($GLOBALS['xml_export_tables'])) {
151 continue;
154 $head .= ' <pma:' . $type . ' name="' . $table . '">' . $crlf;
156 $tbl = " " . htmlspecialchars($tbl);
157 $tbl = str_replace("\n", "\n ", $tbl);
159 $head .= $tbl . ';' . $crlf;
160 $head .= ' </pma:' . $type . '>' . $crlf;
162 if (isset($GLOBALS['xml_export_triggers']) && $GLOBALS['xml_export_triggers']) {
163 // Export triggers
164 $triggers = PMA_DBI_get_triggers($db, $table);
165 if ($triggers) {
166 foreach ($triggers as $trigger) {
167 $code = $trigger['create'];
168 $head .= ' <pma:trigger name="' . $trigger['name'] . '">' . $crlf;
170 // Do some formatting
171 $code = substr(rtrim($code), 0, -3);
172 $code = " " . htmlspecialchars($code);
173 $code = str_replace("\n", "\n ", $code);
175 $head .= $code . $crlf;
176 $head .= ' </pma:trigger>' . $crlf;
179 unset($trigger);
180 unset($triggers);
185 if (isset($GLOBALS['xml_export_functions']) && $GLOBALS['xml_export_functions']) {
186 // Export functions
187 $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
188 if ($functions) {
189 foreach ($functions as $function) {
190 $head .= ' <pma:function name="' . $function . '">' . $crlf;
192 // Do some formatting
193 $sql = PMA_DBI_get_definition($db, 'FUNCTION', $function);
194 $sql = rtrim($sql);
195 $sql = " " . htmlspecialchars($sql);
196 $sql = str_replace("\n", "\n ", $sql);
198 $head .= $sql . $crlf;
199 $head .= ' </pma:function>' . $crlf;
202 unset($create_func);
203 unset($function);
204 unset($functions);
208 if (isset($GLOBALS['xml_export_procedures']) && $GLOBALS['xml_export_procedures']) {
209 // Export procedures
210 $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
211 if ($procedures) {
212 foreach ($procedures as $procedure) {
213 $head .= ' <pma:procedure name="' . $procedure . '">' . $crlf;
215 // Do some formatting
216 $sql = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure);
217 $sql = rtrim($sql);
218 $sql = " " . htmlspecialchars($sql);
219 $sql = str_replace("\n", "\n ", $sql);
221 $head .= $sql . $crlf;
222 $head .= ' </pma:procedure>' . $crlf;
225 unset($create_proc);
226 unset($procedure);
227 unset($procedures);
231 unset($result);
233 $head .= ' </pma:database>' . $crlf;
234 $head .= ' </pma:structure_schemas>' . $crlf;
236 if ($export_data) {
237 $head .= $crlf;
241 return PMA_exportOutputHandler($head);
245 * Outputs database header
247 * @param string Database name
249 * @return bool Whether it suceeded
251 * @access public
253 function PMA_exportDBHeader($db) {
254 global $crlf;
256 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
257 $head = ' <!--' . $crlf
258 . ' - ' . __('Database') . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
259 . ' -->' . $crlf
260 . ' <database name="' . htmlspecialchars($db) . '">' . $crlf;
262 return PMA_exportOutputHandler($head);
264 else
266 return TRUE;
271 * Outputs database footer
273 * @param string Database name
275 * @return bool Whether it suceeded
277 * @access public
279 function PMA_exportDBFooter($db) {
280 global $crlf;
282 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
283 return PMA_exportOutputHandler(' </database>' . $crlf);
285 else
287 return TRUE;
292 * Outputs create database database
294 * @param string Database name
296 * @return bool Whether it suceeded
298 * @access public
300 function PMA_exportDBCreate($db) {
301 return TRUE;
306 * Outputs the content of a table
308 * @param string the database name
309 * @param string the table name
310 * @param string the end of line sequence
311 * @param string the url to go back in case of error
312 * @param string SQL query for obtaining data
314 * @return bool Whether it suceeded
316 * @access public
318 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
320 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
321 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
323 $columns_cnt = PMA_DBI_num_fields($result);
324 $columns = array();
325 for ($i = 0; $i < $columns_cnt; $i++) {
326 $columns[$i] = stripslashes(str_replace(' ', '_', PMA_DBI_field_name($result, $i)));
328 unset($i);
330 $buffer = ' <!-- ' . __('Table') . ' ' . $table . ' -->' . $crlf;
331 if (!PMA_exportOutputHandler($buffer)) {
332 return FALSE;
335 while ($record = PMA_DBI_fetch_row($result)) {
336 $buffer = ' <table name="' . htmlspecialchars($table) . '">' . $crlf;
337 for ($i = 0; $i < $columns_cnt; $i++) {
338 // If a cell is NULL, still export it to preserve the XML structure
339 if (!isset($record[$i]) || is_null($record[$i])) {
340 $record[$i] = 'NULL';
342 $buffer .= ' <column name="' . htmlspecialchars($columns[$i]) . '">' . htmlspecialchars((string)$record[$i])
343 . '</column>' . $crlf;
345 $buffer .= ' </table>' . $crlf;
347 if (!PMA_exportOutputHandler($buffer)) {
348 return FALSE;
351 PMA_DBI_free_result($result);
354 return TRUE;
355 } // end of the 'PMA_getTableXML()' function