Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugins / export / ExportXml.class.php
blob711824c320d2613e896ebf3a89b46c7770d86efb
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;
12 if (! strlen($GLOBALS['db'])) { /* Can't do server export */
13 $GLOBALS['skip_import'] = true;
14 return;
17 /* Get the export interface */
18 require_once 'libraries/plugins/ExportPlugin.class.php';
20 /**
21 * Handles the export for the XML class
23 * @package PhpMyAdmin-Export
24 * @subpackage XML
26 class ExportXml extends ExportPlugin
28 /**
29 * Table name
31 * @var string
33 private $_table;
35 /**
36 * Table names
38 * @var array
40 private $_tables;
42 /**
43 * Constructor
45 public function __construct()
47 $this->setProperties();
50 /**
51 * Initialize the local variables that are used for export PDF
53 * @return void
55 protected function initSpecificVariables()
57 global $table, $tables;
58 $this->_setTable($table);
59 $this->_setTables($tables);
62 /**
63 * Sets the export XML properties
65 * @return void
67 protected function setProperties()
69 $props = 'libraries/properties/';
70 include_once "$props/plugins/ExportPluginProperties.class.php";
71 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
72 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
73 include_once "$props/options/items/HiddenPropertyItem.class.php";
74 include_once "$props/options/items/BoolPropertyItem.class.php";
76 // create the export plugin property item
77 $exportPluginProperties = new ExportPluginProperties();
78 $exportPluginProperties->setText('XML');
79 $exportPluginProperties->setExtension('xml');
80 $exportPluginProperties->setMimeType('text/xml');
81 $exportPluginProperties->setOptionsText(__('Options'));
83 // create the root group that will be the options field for
84 // $exportPluginProperties
85 // this will be shown as "Format specific options"
86 $exportSpecificOptions = new OptionsPropertyRootGroup();
87 $exportSpecificOptions->setName("Format Specific Options");
89 // general options main group
90 $generalOptions = new OptionsPropertyMainGroup();
91 $generalOptions->setName("general_opts");
92 // create primary items and add them to the group
93 $leaf = new HiddenPropertyItem();
94 $leaf->setName("structure_or_data");
95 $generalOptions->addProperty($leaf);
96 // add the main group to the root group
97 $exportSpecificOptions->addProperty($generalOptions);
99 // export structure main group
100 $structure = new OptionsPropertyMainGroup();
101 $structure->setName("structure");
102 $structure->setText(__('Object creation options (all are recommended)'));
103 // create primary items and add them to the group
104 if (! PMA_DRIZZLE) {
105 $leaf = new BoolPropertyItem();
106 $leaf->setName("export_functions");
107 $leaf->setText(__('Functions'));
108 $structure->addProperty($leaf);
109 $leaf = new BoolPropertyItem();
110 $leaf->setName("export_procedures");
111 $leaf->setText(__('Procedures'));
112 $structure->addProperty($leaf);
114 $leaf = new BoolPropertyItem();
115 $leaf->setName("export_tables");
116 $leaf->setText(__('Tables'));
117 $structure->addProperty($leaf);
118 if (! PMA_DRIZZLE) {
119 $leaf = new BoolPropertyItem();
120 $leaf->setName("export_triggers");
121 $leaf->setText(__('Triggers'));
122 $structure->addProperty($leaf);
123 $leaf = new BoolPropertyItem();
124 $leaf->setName("export_views");
125 $leaf->setText(__('Views'));
126 $structure->addProperty($leaf);
128 $exportSpecificOptions->addProperty($structure);
130 // data main group
131 $data = new OptionsPropertyMainGroup();
132 $data->setName("data");
133 $data->setText(__('Data dump options'));
134 // create primary items and add them to the group
135 $leaf = new BoolPropertyItem();
136 $leaf->setName("export_contents");
137 $leaf->setText(__('Export contents'));
138 $data->addProperty($leaf);
139 $exportSpecificOptions->addProperty($data);
141 // set the options for the export plugin property item
142 $exportPluginProperties->setOptions($exportSpecificOptions);
143 $this->properties = $exportPluginProperties;
147 * This method is called when any PluginManager to which the observer
148 * is attached calls PluginManager::notify()
150 * @param SplSubject $subject The PluginManager notifying the observer
151 * of an update.
153 * @return void
155 public function update (SplSubject $subject)
160 * Outputs export header. It is the first method to be called, so all
161 * the required variables are initialized here.
163 * @return bool Whether it succeeded
165 public function exportHeader ()
167 $this->initSpecificVariables();
168 global $crlf, $cfg, $db;
169 $table = $this->_getTable();
170 $tables = $this->_getTables();
172 $export_struct = isset($GLOBALS['xml_export_functions'])
173 || isset($GLOBALS['xml_export_procedures'])
174 || isset($GLOBALS['xml_export_tables'])
175 || isset($GLOBALS['xml_export_triggers'])
176 || isset($GLOBALS['xml_export_views']);
177 $export_data = isset($GLOBALS['xml_export_contents']) ? true : false;
179 if ($GLOBALS['output_charset_conversion']) {
180 $charset = $GLOBALS['charset_of_file'];
181 } else {
182 $charset = 'utf-8';
185 $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
186 . '<!--' . $crlf
187 . '- phpMyAdmin XML Dump' . $crlf
188 . '- version ' . PMA_VERSION . $crlf
189 . '- http://www.phpmyadmin.net' . $crlf
190 . '-' . $crlf
191 . '- ' . __('Host') . ': ' . $cfg['Server']['host'];
192 if (! empty($cfg['Server']['port'])) {
193 $head .= ':' . $cfg['Server']['port'];
195 $head .= $crlf
196 . '- ' . __('Generation Time') . ': '
197 . PMA_Util::localisedDate() . $crlf
198 . '- ' . __('Server version') . ': ' . PMA_MYSQL_STR_VERSION . $crlf
199 . '- ' . __('PHP Version') . ': ' . phpversion() . $crlf
200 . '-->' . $crlf . $crlf;
202 $head .= '<pma_xml_export version="1.0"'
203 . (($export_struct)
204 ? ' xmlns:pma="http://www.phpmyadmin.net/some_doc_url/"'
205 : '')
206 . '>' . $crlf;
208 if ($export_struct) {
209 if (PMA_DRIZZLE) {
210 $result = PMA_DBI_fetch_result(
211 "SELECT
212 'utf8' AS DEFAULT_CHARACTER_SET_NAME,
213 DEFAULT_COLLATION_NAME
214 FROM data_dictionary.SCHEMAS
215 WHERE SCHEMA_NAME = '"
216 . PMA_Util::sqlAddSlashes($db) . "'"
218 } else {
219 $result = PMA_DBI_fetch_result(
220 'SELECT `DEFAULT_CHARACTER_SET_NAME`, `DEFAULT_COLLATION_NAME`'
221 . ' FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME`'
222 . ' = \''.PMA_Util::sqlAddSlashes($db).'\' LIMIT 1'
225 $db_collation = $result[0]['DEFAULT_COLLATION_NAME'];
226 $db_charset = $result[0]['DEFAULT_CHARACTER_SET_NAME'];
228 $head .= ' <!--' . $crlf;
229 $head .= ' - Structure schemas' . $crlf;
230 $head .= ' -->' . $crlf;
231 $head .= ' <pma:structure_schemas>' . $crlf;
232 $head .= ' <pma:database name="' . htmlspecialchars($db)
233 . '" collation="' . $db_collation . '" charset="' . $db_charset
234 . '">' . $crlf;
236 if (count($tables) == 0) {
237 $tables[] = $table;
240 foreach ($tables as $table) {
241 // Export tables and views
242 $result = PMA_DBI_fetch_result(
243 'SHOW CREATE TABLE ' . PMA_Util::backquote($db) . '.'
244 . PMA_Util::backquote($table),
247 $tbl = $result[$table][1];
249 $is_view = PMA_Table::isView($db, $table);
251 if ($is_view) {
252 $type = 'view';
253 } else {
254 $type = 'table';
257 if ($is_view && ! isset($GLOBALS['xml_export_views'])) {
258 continue;
261 if (! $is_view && ! isset($GLOBALS['xml_export_tables'])) {
262 continue;
265 $head .= ' <pma:' . $type . ' name="' . $table . '">'
266 . $crlf;
268 $tbl = " " . htmlspecialchars($tbl);
269 $tbl = str_replace("\n", "\n ", $tbl);
271 $head .= $tbl . ';' . $crlf;
272 $head .= ' </pma:' . $type . '>' . $crlf;
274 if (isset($GLOBALS['xml_export_triggers'])
275 && $GLOBALS['xml_export_triggers']
277 // Export triggers
278 $triggers = PMA_DBI_get_triggers($db, $table);
279 if ($triggers) {
280 foreach ($triggers as $trigger) {
281 $code = $trigger['create'];
282 $head .= ' <pma:trigger name="'
283 . $trigger['name'] . '">' . $crlf;
285 // Do some formatting
286 $code = substr(rtrim($code), 0, -3);
287 $code = " " . htmlspecialchars($code);
288 $code = str_replace("\n", "\n ", $code);
290 $head .= $code . $crlf;
291 $head .= ' </pma:trigger>' . $crlf;
294 unset($trigger);
295 unset($triggers);
300 if (isset($GLOBALS['xml_export_functions'])
301 && $GLOBALS['xml_export_functions']
303 // Export functions
304 $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
305 if ($functions) {
306 foreach ($functions as $function) {
307 $head .= ' <pma:function name="'
308 . $function . '">' . $crlf;
310 // Do some formatting
311 $sql = PMA_DBI_get_definition($db, 'FUNCTION', $function);
312 $sql = rtrim($sql);
313 $sql = " " . htmlspecialchars($sql);
314 $sql = str_replace("\n", "\n ", $sql);
316 $head .= $sql . $crlf;
317 $head .= ' </pma:function>' . $crlf;
320 unset($function);
321 unset($functions);
325 if (isset($GLOBALS['xml_export_procedures'])
326 && $GLOBALS['xml_export_procedures']
328 // Export procedures
329 $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
330 if ($procedures) {
331 foreach ($procedures as $procedure) {
332 $head .= ' <pma:procedure name="'
333 . $procedure . '">' . $crlf;
335 // Do some formatting
336 $sql = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure);
337 $sql = rtrim($sql);
338 $sql = " " . htmlspecialchars($sql);
339 $sql = str_replace("\n", "\n ", $sql);
341 $head .= $sql . $crlf;
342 $head .= ' </pma:procedure>' . $crlf;
345 unset($procedure);
346 unset($procedures);
350 unset($result);
352 $head .= ' </pma:database>' . $crlf;
353 $head .= ' </pma:structure_schemas>' . $crlf;
355 if ($export_data) {
356 $head .= $crlf;
360 return PMA_exportOutputHandler($head);
364 * Outputs export footer
366 * @return bool Whether it succeeded
368 public function exportFooter ()
370 $foot = '</pma_xml_export>';
372 return PMA_exportOutputHandler($foot);
376 * Outputs database header
378 * @param string $db Database name
380 * @return bool Whether it succeeded
382 public function exportDBHeader ($db)
384 global $crlf;
386 if (isset($GLOBALS['xml_export_contents'])
387 && $GLOBALS['xml_export_contents']
389 $head = ' <!--' . $crlf
390 . ' - ' . __('Database') . ': ' . '\'' . $db . '\'' . $crlf
391 . ' -->' . $crlf
392 . ' <database name="' . htmlspecialchars($db) . '">' . $crlf;
394 return PMA_exportOutputHandler($head);
395 } else {
396 return true;
401 * Outputs database footer
403 * @param string $db Database name
405 * @return bool Whether it succeeded
407 public function exportDBFooter ($db)
409 global $crlf;
411 if (isset($GLOBALS['xml_export_contents'])
412 && $GLOBALS['xml_export_contents']
414 return PMA_exportOutputHandler(' </database>' . $crlf);
415 } else {
416 return true;
421 * Outputs CREATE DATABASE statement
423 * @param string $db Database name
425 * @return bool Whether it succeeded
427 public function exportDBCreate($db)
429 return true;
433 * Outputs the content of a table in XML format
435 * @param string $db database name
436 * @param string $table table name
437 * @param string $crlf the end of line sequence
438 * @param string $error_url the url to go back in case of error
439 * @param string $sql_query SQL query for obtaining data
441 * @return bool Whether it succeeded
443 public function exportData ($db, $table, $crlf, $error_url, $sql_query)
445 if (isset($GLOBALS['xml_export_contents'])
446 && $GLOBALS['xml_export_contents']
448 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
450 $columns_cnt = PMA_DBI_num_fields($result);
451 $columns = array();
452 for ($i = 0; $i < $columns_cnt; $i++) {
453 $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
455 unset($i);
457 $buffer = ' <!-- ' . __('Table') . ' ' . $table . ' -->' . $crlf;
458 if (! PMA_exportOutputHandler($buffer)) {
459 return false;
462 while ($record = PMA_DBI_fetch_row($result)) {
463 $buffer = ' <table name="'
464 . htmlspecialchars($table) . '">' . $crlf;
465 for ($i = 0; $i < $columns_cnt; $i++) {
466 // If a cell is NULL, still export it to preserve
467 // the XML structure
468 if (! isset($record[$i]) || is_null($record[$i])) {
469 $record[$i] = 'NULL';
471 $buffer .= ' <column name="'
472 . htmlspecialchars($columns[$i]) . '">'
473 . htmlspecialchars((string)$record[$i])
474 . '</column>' . $crlf;
476 $buffer .= ' </table>' . $crlf;
478 if (! PMA_exportOutputHandler($buffer)) {
479 return false;
482 PMA_DBI_free_result($result);
485 return true;
489 /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
493 * Gets the table name
495 * @return void
497 private function _getTable()
499 return $this->_table;
503 * Sets the table name
505 * @param string $table table name
507 * @return void
509 private function _setTable($table)
511 $this->_table = $table;
515 * Gets the table names
517 * @return array
519 private function _getTables()
521 return $this->_tables;
525 * Sets the table names
527 * @param array $tables table names
529 * @return void
531 private function _setTables($tables)
533 $this->_tables = $tables;