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() {
90 $export_struct = isset($GLOBALS[$what . '_export_struc']) ?
true : false;
91 $export_data = isset($GLOBALS[$what . '_export_contents']) ?
true : false;
93 if ($GLOBALS['output_charset_conversion']) {
94 $charset = $GLOBALS['charset_of_file'];
96 $charset = $GLOBALS['charset'];
99 $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
101 . '- phpMyAdmin XML Dump' . $crlf
102 . '- version ' . PMA_VERSION
. $crlf
103 . '- http://www.phpmyadmin.net' . $crlf
105 . '- ' . __('Host') . ': ' . $cfg['Server']['host'];
106 if (!empty($cfg['Server']['port'])) {
107 $head .= ':' . $cfg['Server']['port'];
110 . '- ' . __('Generation Time') . ': ' . PMA_localisedDate() . $crlf
111 . '- ' . __('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
112 . '- ' . __('PHP Version') . ': ' . phpversion() . $crlf
113 . '-->' . $crlf . $crlf;
115 $head .= '<pma_xml_export version="1.0"' . (($export_struct) ?
' xmlns:pma="http://www.phpmyadmin.net/some_doc_url/"' : '') . '>' . $crlf;
117 if ($export_struct) {
118 $result = PMA_DBI_fetch_result('SELECT `DEFAULT_CHARACTER_SET_NAME`, `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME` = \''.$db.'\' LIMIT 1');
119 $db_collation = $result[0]['DEFAULT_COLLATION_NAME'];
120 $db_charset = $result[0]['DEFAULT_CHARACTER_SET_NAME'];
122 $head .= ' <!--' . $crlf;
123 $head .= ' - Structure schemas' . $crlf;
124 $head .= ' -->' . $crlf;
125 $head .= ' <pma:structure_schemas>' . $crlf;
126 $head .= ' <pma:database name="' . $db . '" collation="' . $db_collation . '" charset="' . $db_charset . '">' . $crlf;
128 if (count($tables) == 0) {
132 foreach ($tables as $table) {
133 // Export tables and views
134 $result = PMA_DBI_fetch_result('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), 0);
135 $tbl = $result[$table][1];
137 $is_view = PMA_isView($db, $table);
145 if ($is_view && ! isset($GLOBALS[$what . '_export_views'])) {
149 if (! $is_view && ! isset($GLOBALS[$what . '_export_tables'])) {
153 $head .= ' <pma:' . $type . ' name="' . $table . '">' . $crlf;
156 $tbl = str_replace("\n", "\n ", $tbl);
158 $head .= $tbl . ';' . $crlf;
159 $head .= ' </pma:' . $type . '>' . $crlf;
161 if (isset($GLOBALS[$what . '_export_triggers']) && $GLOBALS[$what . '_export_triggers']) {
163 $triggers = PMA_DBI_get_triggers($db, $table);
165 foreach ($triggers as $trigger) {
166 $code = $trigger['create'];
167 $head .= ' <pma:trigger name="' . $trigger['name'] . '">' . $crlf;
169 // Do some formatting
170 $code = substr(rtrim($code), 0, -3);
172 $code = str_replace("\n", "\n ", $code);
174 $head .= $code . $crlf;
175 $head .= ' </pma:trigger>' . $crlf;
184 if (isset($GLOBALS[$what . '_export_functions']) && $GLOBALS[$what . '_export_functions']) {
186 $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
188 foreach ($functions as $function) {
189 $head .= ' <pma:function name="' . $function . '">' . $crlf;
191 // Do some formatting
192 $sql = PMA_DBI_get_definition($db, 'FUNCTION', $function);
195 $sql = str_replace("\n", "\n ", $sql);
197 $head .= $sql . $crlf;
198 $head .= ' </pma:function>' . $crlf;
207 if (isset($GLOBALS[$what . '_export_procedures']) && $GLOBALS[$what . '_export_procedures']) {
209 $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
211 foreach ($procedures as $procedure) {
212 $head .= ' <pma:procedure name="' . $procedure . '">' . $crlf;
214 // Do some formatting
215 $sql = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure);
218 $sql = str_replace("\n", "\n ", $sql);
220 $head .= $sql . $crlf;
221 $head .= ' </pma:procedure>' . $crlf;
232 $head .= ' </pma:database>' . $crlf;
233 $head .= ' </pma:structure_schemas>' . $crlf;
240 return PMA_exportOutputHandler($head);
244 * Outputs database header
246 * @param string Database name
248 * @return bool Whether it suceeded
252 function PMA_exportDBHeader($db) {
256 if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
257 $head = ' <!--' . $crlf
258 . ' - ' . __('Database') . ': ' . (isset($GLOBALS['use_backquotes']) ?
PMA_backquote($db) : '\'' . $db . '\''). $crlf
260 . ' <database name="' . $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) {
283 if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
284 return PMA_exportOutputHandler(' </database>' . $crlf);
293 * Outputs create database database
295 * @param string Database name
297 * @return bool Whether it suceeded
301 function PMA_exportDBCreate($db) {
307 * Outputs the content of a table
309 * @param string the database name
310 * @param string the table name
311 * @param string the end of line sequence
312 * @param string the url to go back in case of error
313 * @param string SQL query for obtaining data
315 * @return bool Whether it suceeded
319 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
322 if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
323 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
325 $columns_cnt = PMA_DBI_num_fields($result);
326 for ($i = 0; $i < $columns_cnt; $i++
) {
327 $columns[$i] = stripslashes(str_replace(' ', '_', PMA_DBI_field_name($result, $i)));
331 $buffer = ' <!-- ' . __('Table') . ' ' . $table . ' -->' . $crlf;
332 if (!PMA_exportOutputHandler($buffer)) {
336 while ($record = PMA_DBI_fetch_row($result)) {
337 $buffer = ' <table name="' . htmlspecialchars($table) . '">' . $crlf;
338 for ($i = 0; $i < $columns_cnt; $i++
) {
339 // If a cell is NULL, still export it to preserve the XML structure
340 if (!isset($record[$i]) ||
is_null($record[$i])) {
341 $record[$i] = 'NULL';
343 $buffer .= ' <column name="' . $columns[$i] . '">' . htmlspecialchars((string)$record[$i])
344 . '</column>' . $crlf;
346 $buffer .= ' </table>' . $crlf;
348 if (!PMA_exportOutputHandler($buffer)) {
352 PMA_DBI_free_result($result);
356 } // end of the 'PMA_getTableXML()' function