Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin.git] / libraries / export / xml.php
blobe60999f9ba372a87db0230302383f45023980553
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 * @package phpMyAdmin-Export
7 * @subpackage XML
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 if (strlen($GLOBALS['db'])) { /* Can't do server export */
14 return;
17 if (isset($plugin_list)) {
18 $plugin_list['xml'] = array(
19 'text' => __('XML'),
20 'extension' => 'xml',
21 'mime_type' => 'text/xml',
22 'options' => array(
23 array('type' => 'begin_group', 'name' => 'general_opts'),
24 array('type' => 'hidden', 'name' => 'structure_or_data'),
25 array('type' => 'end_group')
27 'options_text' => __('Options')
30 /* Export structure */
31 $plugin_list['xml']['options'][] = array(
32 'type' => 'begin_group',
33 'name' => 'structure',
34 'text' => __('Object creation options (all are recommended)')
36 if (!PMA_DRIZZLE) {
37 $plugin_list['xml']['options'][] = array(
38 'type' => 'bool',
39 'name' => 'export_functions',
40 'text' => __('Functions')
42 $plugin_list['xml']['options'][] = array(
43 'type' => 'bool',
44 'name' => 'export_procedures',
45 'text' => __('Procedures')
48 $plugin_list['xml']['options'][] = array(
49 'type' => 'bool',
50 'name' => 'export_tables',
51 'text' => __('Tables')
53 if (!PMA_DRIZZLE) {
54 $plugin_list['xml']['options'][] = array(
55 'type' => 'bool',
56 'name' => 'export_triggers',
57 'text' => __('Triggers')
59 $plugin_list['xml']['options'][] = array(
60 'type' => 'bool',
61 'name' => 'export_views',
62 'text' => __('Views')
65 $plugin_list['xml']['options'][] = array(
66 'type' => 'end_group'
69 /* Data */
70 $plugin_list['xml']['options'][] = array(
71 'type' => 'begin_group',
72 'name' => 'data',
73 'text' => __('Data dump options')
75 $plugin_list['xml']['options'][] = array(
76 'type' => 'bool',
77 'name' => 'export_contents',
78 'text' => __('Export contents')
80 $plugin_list['xml']['options'][] = array(
81 'type' => 'end_group'
83 } else {
85 /**
86 * Outputs export footer
88 * @return bool Whether it suceeded
90 * @access public
92 function PMA_exportFooter()
94 $foot = '</pma_xml_export>';
96 return PMA_exportOutputHandler($foot);
99 /**
100 * Outputs export header
102 * @return bool Whether it suceeded
104 * @access public
106 function PMA_exportHeader()
108 global $crlf;
109 global $cfg;
110 global $db;
111 global $table;
112 global $tables;
114 $export_struct = isset($GLOBALS['xml_export_functions']) || isset($GLOBALS['xml_export_procedures'])
115 || isset($GLOBALS['xml_export_tables']) || isset($GLOBALS['xml_export_triggers'])
116 || isset($GLOBALS['xml_export_views']);
117 $export_data = isset($GLOBALS['xml_export_contents']) ? true : false;
119 if ($GLOBALS['output_charset_conversion']) {
120 $charset = $GLOBALS['charset_of_file'];
121 } else {
122 $charset = 'utf-8';
125 $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
126 . '<!--' . $crlf
127 . '- phpMyAdmin XML Dump' . $crlf
128 . '- version ' . PMA_VERSION . $crlf
129 . '- http://www.phpmyadmin.net' . $crlf
130 . '-' . $crlf
131 . '- ' . __('Host') . ': ' . $cfg['Server']['host'];
132 if (!empty($cfg['Server']['port'])) {
133 $head .= ':' . $cfg['Server']['port'];
135 $head .= $crlf
136 . '- ' . __('Generation Time') . ': ' . PMA_localisedDate() . $crlf
137 . '- ' . __('Server version') . ': ' . PMA_MYSQL_STR_VERSION . $crlf
138 . '- ' . __('PHP Version') . ': ' . phpversion() . $crlf
139 . '-->' . $crlf . $crlf;
141 $head .= '<pma_xml_export version="1.0"' . (($export_struct) ? ' xmlns:pma="http://www.phpmyadmin.net/some_doc_url/"' : '') . '>' . $crlf;
143 if ($export_struct) {
144 if (PMA_DRIZZLE) {
145 $result = PMA_DBI_fetch_result("
146 SELECT
147 'utf8' AS DEFAULT_CHARACTER_SET_NAME,
148 DEFAULT_COLLATION_NAME
149 FROM data_dictionary.SCHEMAS
150 WHERE SCHEMA_NAME = '" . PMA_sqlAddSlashes($db) . "'");
151 } else {
152 $result = PMA_DBI_fetch_result('SELECT `DEFAULT_CHARACTER_SET_NAME`, `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME` = \''.PMA_sqlAddSlashes($db).'\' LIMIT 1');
154 $db_collation = $result[0]['DEFAULT_COLLATION_NAME'];
155 $db_charset = $result[0]['DEFAULT_CHARACTER_SET_NAME'];
157 $head .= ' <!--' . $crlf;
158 $head .= ' - Structure schemas' . $crlf;
159 $head .= ' -->' . $crlf;
160 $head .= ' <pma:structure_schemas>' . $crlf;
161 $head .= ' <pma:database name="' . htmlspecialchars($db) . '" collation="' . $db_collation . '" charset="' . $db_charset . '">' . $crlf;
163 if (count($tables) == 0) {
164 $tables[] = $table;
167 foreach ($tables as $table) {
168 // Export tables and views
169 $result = PMA_DBI_fetch_result('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), 0);
170 $tbl = $result[$table][1];
172 $is_view = PMA_isView($db, $table);
174 if ($is_view) {
175 $type = 'view';
176 } else {
177 $type = 'table';
180 if ($is_view && ! isset($GLOBALS['xml_export_views'])) {
181 continue;
184 if (! $is_view && ! isset($GLOBALS['xml_export_tables'])) {
185 continue;
188 $head .= ' <pma:' . $type . ' name="' . $table . '">' . $crlf;
190 $tbl = " " . htmlspecialchars($tbl);
191 $tbl = str_replace("\n", "\n ", $tbl);
193 $head .= $tbl . ';' . $crlf;
194 $head .= ' </pma:' . $type . '>' . $crlf;
196 if (isset($GLOBALS['xml_export_triggers']) && $GLOBALS['xml_export_triggers']) {
197 // Export triggers
198 $triggers = PMA_DBI_get_triggers($db, $table);
199 if ($triggers) {
200 foreach ($triggers as $trigger) {
201 $code = $trigger['create'];
202 $head .= ' <pma:trigger name="' . $trigger['name'] . '">' . $crlf;
204 // Do some formatting
205 $code = substr(rtrim($code), 0, -3);
206 $code = " " . htmlspecialchars($code);
207 $code = str_replace("\n", "\n ", $code);
209 $head .= $code . $crlf;
210 $head .= ' </pma:trigger>' . $crlf;
213 unset($trigger);
214 unset($triggers);
219 if (isset($GLOBALS['xml_export_functions']) && $GLOBALS['xml_export_functions']) {
220 // Export functions
221 $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
222 if ($functions) {
223 foreach ($functions as $function) {
224 $head .= ' <pma:function name="' . $function . '">' . $crlf;
226 // Do some formatting
227 $sql = PMA_DBI_get_definition($db, 'FUNCTION', $function);
228 $sql = rtrim($sql);
229 $sql = " " . htmlspecialchars($sql);
230 $sql = str_replace("\n", "\n ", $sql);
232 $head .= $sql . $crlf;
233 $head .= ' </pma:function>' . $crlf;
236 unset($create_func);
237 unset($function);
238 unset($functions);
242 if (isset($GLOBALS['xml_export_procedures']) && $GLOBALS['xml_export_procedures']) {
243 // Export procedures
244 $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
245 if ($procedures) {
246 foreach ($procedures as $procedure) {
247 $head .= ' <pma:procedure name="' . $procedure . '">' . $crlf;
249 // Do some formatting
250 $sql = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure);
251 $sql = rtrim($sql);
252 $sql = " " . htmlspecialchars($sql);
253 $sql = str_replace("\n", "\n ", $sql);
255 $head .= $sql . $crlf;
256 $head .= ' </pma:procedure>' . $crlf;
259 unset($create_proc);
260 unset($procedure);
261 unset($procedures);
265 unset($result);
267 $head .= ' </pma:database>' . $crlf;
268 $head .= ' </pma:structure_schemas>' . $crlf;
270 if ($export_data) {
271 $head .= $crlf;
275 return PMA_exportOutputHandler($head);
279 * Outputs database header
281 * @param string $db Database name
282 * @return bool Whether it suceeded
284 * @access public
286 function PMA_exportDBHeader($db)
288 global $crlf;
290 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
291 $head = ' <!--' . $crlf
292 . ' - ' . __('Database') . ': ' . '\'' . $db . '\'' . $crlf
293 . ' -->' . $crlf
294 . ' <database name="' . htmlspecialchars($db) . '">' . $crlf;
296 return PMA_exportOutputHandler($head);
297 } else {
298 return true;
303 * Outputs database footer
305 * @param string $db Database name
306 * @return bool Whether it suceeded
308 * @access public
310 function PMA_exportDBFooter($db)
312 global $crlf;
314 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
315 return PMA_exportOutputHandler(' </database>' . $crlf);
316 } else {
317 return true;
322 * Outputs CREATE DATABASE statement
324 * @param string $db Database name
325 * @return bool Whether it suceeded
327 * @access public
329 function PMA_exportDBCreate($db)
331 return true;
335 * Outputs the content of a table in XML format
337 * @param string $db database name
338 * @param string $table table name
339 * @param string $crlf the end of line sequence
340 * @param string $error_url the url to go back in case of error
341 * @param string $sql_query SQL query for obtaining data
342 * @return bool Whether it suceeded
344 * @access public
346 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
349 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
350 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
352 $columns_cnt = PMA_DBI_num_fields($result);
353 $columns = array();
354 for ($i = 0; $i < $columns_cnt; $i++) {
355 $columns[$i] = stripslashes(str_replace(' ', '_', PMA_DBI_field_name($result, $i)));
357 unset($i);
359 $buffer = ' <!-- ' . __('Table') . ' ' . $table . ' -->' . $crlf;
360 if (!PMA_exportOutputHandler($buffer)) {
361 return false;
364 while ($record = PMA_DBI_fetch_row($result)) {
365 $buffer = ' <table name="' . htmlspecialchars($table) . '">' . $crlf;
366 for ($i = 0; $i < $columns_cnt; $i++) {
367 // If a cell is NULL, still export it to preserve the XML structure
368 if (!isset($record[$i]) || is_null($record[$i])) {
369 $record[$i] = 'NULL';
371 $buffer .= ' <column name="' . htmlspecialchars($columns[$i]) . '">' . htmlspecialchars((string)$record[$i])
372 . '</column>' . $crlf;
374 $buffer .= ' </table>' . $crlf;
376 if (!PMA_exportOutputHandler($buffer)) {
377 return false;
380 PMA_DBI_free_result($result);
383 return true;
384 } // end of the 'PMA_getTableXML()' function