2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build XML dumps of tables
7 * @package phpMyAdmin-Export-XML
9 if (! defined('PHPMYADMIN')) {
13 if (strlen($GLOBALS['db'])) { /* Can't do server export */
15 if (isset($plugin_list)) {
16 $plugin_list['xml'] = array(
19 'mime_type' => 'text/xml',
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');
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');
54 * @param string Text of comment
56 * @return bool Whether it suceeded
58 function PMA_exportComment($text) {
59 return PMA_exportOutputHandler('<!-- ' . $text . ' -->' . $GLOBALS['crlf']);
63 * Outputs export footer
65 * @return bool Whether it suceeded
69 function PMA_exportFooter() {
70 $foot = '</pma_xml_export>';
72 return PMA_exportOutputHandler($foot);
76 * Outputs export header
78 * @return bool Whether it suceeded
82 function PMA_exportHeader() {
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'];
97 $charset = $GLOBALS['charset'];
100 $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
102 . '- phpMyAdmin XML Dump' . $crlf
103 . '- version ' . PMA_VERSION
. $crlf
104 . '- http://www.phpmyadmin.net' . $crlf
106 . '- ' . __('Host') . ': ' . $cfg['Server']['host'];
107 if (!empty($cfg['Server']['port'])) {
108 $head .= ':' . $cfg['Server']['port'];
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) {
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);
146 if ($is_view && ! isset($GLOBALS['xml_export_views'])) {
150 if (! $is_view && ! isset($GLOBALS['xml_export_tables'])) {
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']) {
164 $triggers = PMA_DBI_get_triggers($db, $table);
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;
185 if (isset($GLOBALS['xml_export_functions']) && $GLOBALS['xml_export_functions']) {
187 $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
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);
195 $sql = " " . htmlspecialchars($sql);
196 $sql = str_replace("\n", "\n ", $sql);
198 $head .= $sql . $crlf;
199 $head .= ' </pma:function>' . $crlf;
208 if (isset($GLOBALS['xml_export_procedures']) && $GLOBALS['xml_export_procedures']) {
210 $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
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);
218 $sql = " " . htmlspecialchars($sql);
219 $sql = str_replace("\n", "\n ", $sql);
221 $head .= $sql . $crlf;
222 $head .= ' </pma:procedure>' . $crlf;
233 $head .= ' </pma:database>' . $crlf;
234 $head .= ' </pma:structure_schemas>' . $crlf;
241 return PMA_exportOutputHandler($head);
245 * Outputs database header
247 * @param string Database name
249 * @return bool Whether it suceeded
253 function PMA_exportDBHeader($db) {
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
260 . ' <database name="' . htmlspecialchars($db) . '">' . $crlf;
262 return PMA_exportOutputHandler($head);
271 * Outputs database footer
273 * @param string Database name
275 * @return bool Whether it suceeded
279 function PMA_exportDBFooter($db) {
282 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
283 return PMA_exportOutputHandler(' </database>' . $crlf);
292 * Outputs create database database
294 * @param string Database name
296 * @return bool Whether it suceeded
300 function PMA_exportDBCreate($db) {
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
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);
325 for ($i = 0; $i < $columns_cnt; $i++
) {
326 $columns[$i] = stripslashes(str_replace(' ', '_', PMA_DBI_field_name($result, $i)));
330 $buffer = ' <!-- ' . __('Table') . ' ' . $table . ' -->' . $crlf;
331 if (!PMA_exportOutputHandler($buffer)) {
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)) {
351 PMA_DBI_free_result($result);
355 } // end of the 'PMA_getTableXML()' function