2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build XML dumps of tables
6 * @package PhpMyAdmin-Export
9 if (! defined('PHPMYADMIN')) {
12 if (! strlen($GLOBALS['db'])) { /* Can't do server export */
13 $GLOBALS['skip_import'] = true;
17 /* Get the export interface */
18 require_once 'libraries/plugins/ExportPlugin.class.php';
21 * Handles the export for the XML class
23 * @package PhpMyAdmin-Export
26 class ExportXml
extends ExportPlugin
45 public function __construct()
47 $this->setProperties();
51 * Initialize the local variables that are used for export PDF
55 protected function initSpecificVariables()
57 global $table, $tables;
58 $this->_setTable($table);
59 $this->_setTables($tables);
63 * Sets the export XML properties
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
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);
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);
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
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'];
185 $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
187 . '- phpMyAdmin XML Dump' . $crlf
188 . '- version ' . PMA_VERSION
. $crlf
189 . '- http://www.phpmyadmin.net' . $crlf
191 . '- ' . __('Host') . ': ' . $cfg['Server']['host'];
192 if (! empty($cfg['Server']['port'])) {
193 $head .= ':' . $cfg['Server']['port'];
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"'
204 ?
' xmlns:pma="http://www.phpmyadmin.net/some_doc_url/"'
208 if ($export_struct) {
210 $result = PMA_DBI_fetch_result(
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) . "'"
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
236 if (count($tables) == 0) {
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);
257 if ($is_view && ! isset($GLOBALS['xml_export_views'])) {
261 if (! $is_view && ! isset($GLOBALS['xml_export_tables'])) {
265 $head .= ' <pma:' . $type . ' name="' . $table . '">'
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']
278 $triggers = PMA_DBI_get_triggers($db, $table);
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;
300 if (isset($GLOBALS['xml_export_functions'])
301 && $GLOBALS['xml_export_functions']
304 $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
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);
313 $sql = " " . htmlspecialchars($sql);
314 $sql = str_replace("\n", "\n ", $sql);
316 $head .= $sql . $crlf;
317 $head .= ' </pma:function>' . $crlf;
325 if (isset($GLOBALS['xml_export_procedures'])
326 && $GLOBALS['xml_export_procedures']
329 $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
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);
338 $sql = " " . htmlspecialchars($sql);
339 $sql = str_replace("\n", "\n ", $sql);
341 $head .= $sql . $crlf;
342 $head .= ' </pma:procedure>' . $crlf;
352 $head .= ' </pma:database>' . $crlf;
353 $head .= ' </pma:structure_schemas>' . $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)
386 if (isset($GLOBALS['xml_export_contents'])
387 && $GLOBALS['xml_export_contents']
389 $head = ' <!--' . $crlf
390 . ' - ' . __('Database') . ': ' . '\'' . $db . '\'' . $crlf
392 . ' <database name="' . htmlspecialchars($db) . '">' . $crlf;
394 return PMA_exportOutputHandler($head);
401 * Outputs database footer
403 * @param string $db Database name
405 * @return bool Whether it succeeded
407 public function exportDBFooter ($db)
411 if (isset($GLOBALS['xml_export_contents'])
412 && $GLOBALS['xml_export_contents']
414 return PMA_exportOutputHandler(' </database>' . $crlf);
421 * Outputs CREATE DATABASE statement
423 * @param string $db Database name
425 * @return bool Whether it succeeded
427 public function exportDBCreate($db)
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);
452 for ($i = 0; $i < $columns_cnt; $i++
) {
453 $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
457 $buffer = ' <!-- ' . __('Table') . ' ' . $table . ' -->' . $crlf;
458 if (! PMA_exportOutputHandler($buffer)) {
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
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)) {
482 PMA_DBI_free_result($result);
489 /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
493 * Gets the table name
497 private function _getTable()
499 return $this->_table
;
503 * Sets the table name
505 * @param string $table table name
509 private function _setTable($table)
511 $this->_table
= $table;
515 * Gets the table names
519 private function _getTables()
521 return $this->_tables
;
525 * Sets the table names
527 * @param array $tables table names
531 private function _setTables($tables)
533 $this->_tables
= $tables;