Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin.git] / libraries / export / xml.php
blobbb28cacfbb0026e0ad2430d0b4f771e961093dbe
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() {
93 $foot = '</pma_xml_export>';
95 return PMA_exportOutputHandler($foot);
98 /**
99 * Outputs export header
101 * @return bool Whether it suceeded
103 * @access public
105 function PMA_exportHeader() {
106 global $crlf;
107 global $cfg;
108 global $db;
109 global $table;
110 global $tables;
112 $export_struct = isset($GLOBALS['xml_export_functions']) || isset($GLOBALS['xml_export_procedures'])
113 || isset($GLOBALS['xml_export_tables']) || isset($GLOBALS['xml_export_triggers'])
114 || isset($GLOBALS['xml_export_views']);
115 $export_data = isset($GLOBALS['xml_export_contents']) ? true : false;
117 if ($GLOBALS['output_charset_conversion']) {
118 $charset = $GLOBALS['charset_of_file'];
119 } else {
120 $charset = 'utf-8';
123 $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
124 . '<!--' . $crlf
125 . '- phpMyAdmin XML Dump' . $crlf
126 . '- version ' . PMA_VERSION . $crlf
127 . '- http://www.phpmyadmin.net' . $crlf
128 . '-' . $crlf
129 . '- ' . __('Host') . ': ' . $cfg['Server']['host'];
130 if (!empty($cfg['Server']['port'])) {
131 $head .= ':' . $cfg['Server']['port'];
133 $head .= $crlf
134 . '- ' . __('Generation Time') . ': ' . PMA_localisedDate() . $crlf
135 . '- ' . __('Server version') . ': ' . PMA_MYSQL_STR_VERSION . $crlf
136 . '- ' . __('PHP Version') . ': ' . phpversion() . $crlf
137 . '-->' . $crlf . $crlf;
139 $head .= '<pma_xml_export version="1.0"' . (($export_struct) ? ' xmlns:pma="http://www.phpmyadmin.net/some_doc_url/"' : '') . '>' . $crlf;
141 if ($export_struct) {
142 if (PMA_DRIZZLE) {
143 $result = PMA_DBI_fetch_result("
144 SELECT
145 'utf8' AS DEFAULT_CHARACTER_SET_NAME,
146 DEFAULT_COLLATION_NAME
147 FROM data_dictionary.SCHEMAS
148 WHERE SCHEMA_NAME = '" . PMA_sqlAddSlashes($db) . "'");
149 } else {
150 $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');
152 $db_collation = $result[0]['DEFAULT_COLLATION_NAME'];
153 $db_charset = $result[0]['DEFAULT_CHARACTER_SET_NAME'];
155 $head .= ' <!--' . $crlf;
156 $head .= ' - Structure schemas' . $crlf;
157 $head .= ' -->' . $crlf;
158 $head .= ' <pma:structure_schemas>' . $crlf;
159 $head .= ' <pma:database name="' . htmlspecialchars($db) . '" collation="' . $db_collation . '" charset="' . $db_charset . '">' . $crlf;
161 if (count($tables) == 0) {
162 $tables[] = $table;
165 foreach ($tables as $table) {
166 // Export tables and views
167 $result = PMA_DBI_fetch_result('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), 0);
168 $tbl = $result[$table][1];
170 $is_view = PMA_isView($db, $table);
172 if ($is_view) {
173 $type = 'view';
174 } else {
175 $type = 'table';
178 if ($is_view && ! isset($GLOBALS['xml_export_views'])) {
179 continue;
182 if (! $is_view && ! isset($GLOBALS['xml_export_tables'])) {
183 continue;
186 $head .= ' <pma:' . $type . ' name="' . $table . '">' . $crlf;
188 $tbl = " " . htmlspecialchars($tbl);
189 $tbl = str_replace("\n", "\n ", $tbl);
191 $head .= $tbl . ';' . $crlf;
192 $head .= ' </pma:' . $type . '>' . $crlf;
194 if (isset($GLOBALS['xml_export_triggers']) && $GLOBALS['xml_export_triggers']) {
195 // Export triggers
196 $triggers = PMA_DBI_get_triggers($db, $table);
197 if ($triggers) {
198 foreach ($triggers as $trigger) {
199 $code = $trigger['create'];
200 $head .= ' <pma:trigger name="' . $trigger['name'] . '">' . $crlf;
202 // Do some formatting
203 $code = substr(rtrim($code), 0, -3);
204 $code = " " . htmlspecialchars($code);
205 $code = str_replace("\n", "\n ", $code);
207 $head .= $code . $crlf;
208 $head .= ' </pma:trigger>' . $crlf;
211 unset($trigger);
212 unset($triggers);
217 if (isset($GLOBALS['xml_export_functions']) && $GLOBALS['xml_export_functions']) {
218 // Export functions
219 $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
220 if ($functions) {
221 foreach ($functions as $function) {
222 $head .= ' <pma:function name="' . $function . '">' . $crlf;
224 // Do some formatting
225 $sql = PMA_DBI_get_definition($db, 'FUNCTION', $function);
226 $sql = rtrim($sql);
227 $sql = " " . htmlspecialchars($sql);
228 $sql = str_replace("\n", "\n ", $sql);
230 $head .= $sql . $crlf;
231 $head .= ' </pma:function>' . $crlf;
234 unset($create_func);
235 unset($function);
236 unset($functions);
240 if (isset($GLOBALS['xml_export_procedures']) && $GLOBALS['xml_export_procedures']) {
241 // Export procedures
242 $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
243 if ($procedures) {
244 foreach ($procedures as $procedure) {
245 $head .= ' <pma:procedure name="' . $procedure . '">' . $crlf;
247 // Do some formatting
248 $sql = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure);
249 $sql = rtrim($sql);
250 $sql = " " . htmlspecialchars($sql);
251 $sql = str_replace("\n", "\n ", $sql);
253 $head .= $sql . $crlf;
254 $head .= ' </pma:procedure>' . $crlf;
257 unset($create_proc);
258 unset($procedure);
259 unset($procedures);
263 unset($result);
265 $head .= ' </pma:database>' . $crlf;
266 $head .= ' </pma:structure_schemas>' . $crlf;
268 if ($export_data) {
269 $head .= $crlf;
273 return PMA_exportOutputHandler($head);
277 * Outputs database header
279 * @param string $db Database name
280 * @return bool Whether it suceeded
282 * @access public
284 function PMA_exportDBHeader($db) {
285 global $crlf;
287 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
288 $head = ' <!--' . $crlf
289 . ' - ' . __('Database') . ': ' . '\'' . $db . '\'' . $crlf
290 . ' -->' . $crlf
291 . ' <database name="' . htmlspecialchars($db) . '">' . $crlf;
293 return PMA_exportOutputHandler($head);
295 else
297 return true;
302 * Outputs database footer
304 * @param string $db Database name
305 * @return bool Whether it suceeded
307 * @access public
309 function PMA_exportDBFooter($db) {
310 global $crlf;
312 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
313 return PMA_exportOutputHandler(' </database>' . $crlf);
315 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) {
330 return true;
334 * Outputs the content of a table in XML format
336 * @param string $db database name
337 * @param string $table table name
338 * @param string $crlf the end of line sequence
339 * @param string $error_url the url to go back in case of error
340 * @param string $sql_query SQL query for obtaining data
341 * @return bool Whether it suceeded
343 * @access public
345 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
347 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
348 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
350 $columns_cnt = PMA_DBI_num_fields($result);
351 $columns = array();
352 for ($i = 0; $i < $columns_cnt; $i++) {
353 $columns[$i] = stripslashes(str_replace(' ', '_', PMA_DBI_field_name($result, $i)));
355 unset($i);
357 $buffer = ' <!-- ' . __('Table') . ' ' . $table . ' -->' . $crlf;
358 if (!PMA_exportOutputHandler($buffer)) {
359 return false;
362 while ($record = PMA_DBI_fetch_row($result)) {
363 $buffer = ' <table name="' . htmlspecialchars($table) . '">' . $crlf;
364 for ($i = 0; $i < $columns_cnt; $i++) {
365 // If a cell is NULL, still export it to preserve the XML structure
366 if (!isset($record[$i]) || is_null($record[$i])) {
367 $record[$i] = 'NULL';
369 $buffer .= ' <column name="' . htmlspecialchars($columns[$i]) . '">' . htmlspecialchars((string)$record[$i])
370 . '</column>' . $crlf;
372 $buffer .= ' </table>' . $crlf;
374 if (!PMA_exportOutputHandler($buffer)) {
375 return false;
378 PMA_DBI_free_result($result);
381 return true;
382 } // end of the 'PMA_getTableXML()' function