2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build XML dumps of tables
8 * @package phpMyAdmin-Export-XML
10 if (! defined('PHPMYADMIN')) {
14 if (strlen($GLOBALS['db'])) { /* Can't do server export */
16 if (isset($plugin_list)) {
17 $plugin_list['xml'] = array(
20 'mime_type' => 'text/xml',
22 array('type' => 'hidden', 'name' => 'data'),
24 'options_text' => 'strOptions'
27 /* Export structure */
28 $plugin_list['xml']['options'][] =
29 array('type' => 'bgroup', 'name' => 'export_struc', 'text' => 'strXMLExportStructs');
30 $plugin_list['xml']['options'][] =
31 array('type' => 'bool', 'name' => 'export_functions', 'text' => 'strXMLExportFunctions');
32 $plugin_list['xml']['options'][] =
33 array('type' => 'bool', 'name' => 'export_procedures', 'text' => 'strXMLExportProcedures');
34 $plugin_list['xml']['options'][] =
35 array('type' => 'bool', 'name' => 'export_tables', 'text' => 'strXMLExportTables');
36 $plugin_list['xml']['options'][] =
37 array('type' => 'bool', 'name' => 'export_triggers', 'text' => 'strXMLExportTriggers');
38 $plugin_list['xml']['options'][] =
39 array('type' => 'bool', 'name' => 'export_views', 'text' => 'strXMLExportViews');
40 $plugin_list['xml']['options'][] =
41 array('type' => 'egroup');
44 $plugin_list['xml']['options'][] =
45 array('type' => 'bool', 'name' => 'export_contents', 'text' => 'strXMLExportContents');
51 * @param string Text of comment
53 * @return bool Whether it suceeded
55 function PMA_exportComment($text) {
56 return PMA_exportOutputHandler('<!-- ' . $text . ' -->' . $GLOBALS['crlf']);
60 * Outputs export footer
62 * @return bool Whether it suceeded
66 function PMA_exportFooter() {
67 $foot = '</pma_xml_export>';
69 return PMA_exportOutputHandler($foot);
73 * Outputs export header
75 * @return bool Whether it suceeded
79 function PMA_exportHeader() {
87 $export_struct = isset($GLOBALS[$what . '_export_struc']) ?
true : false;
88 $export_data = isset($GLOBALS[$what . '_export_contents']) ?
true : false;
90 if ($GLOBALS['output_charset_conversion']) {
91 $charset = $GLOBALS['charset_of_file'];
93 $charset = $GLOBALS['charset'];
96 $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
98 . '- phpMyAdmin XML Dump' . $crlf
99 . '- version ' . PMA_VERSION
. $crlf
100 . '- http://www.phpmyadmin.net' . $crlf
102 . '- ' . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host'];
103 if (!empty($cfg['Server']['port'])) {
104 $head .= ':' . $cfg['Server']['port'];
107 . '- ' . $GLOBALS['strGenTime'] . ': ' . PMA_localisedDate() . $crlf
108 . '- ' . $GLOBALS['strServerVersion'] . ': ' . substr(PMA_MYSQL_INT_VERSION
, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION
, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION
, 3) . $crlf
109 . '- ' . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf
110 . '-->' . $crlf . $crlf;
112 $head .= '<pma_xml_export version="1.0"' . (($export_struct) ?
' xmlns:pma="http://www.phpmyadmin.net/some_doc_url/"' : '') . '>' . $crlf;
114 if ($export_struct) {
115 $result = PMA_DBI_fetch_result('SELECT `DEFAULT_CHARACTER_SET_NAME`, `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME` = \''.$db.'\' LIMIT 1');
116 $db_collation = $result[0]['DEFAULT_COLLATION_NAME'];
117 $db_charset = $result[0]['DEFAULT_CHARACTER_SET_NAME'];
119 $head .= ' <!--' . $crlf;
120 $head .= ' - Structure schemas' . $crlf;
121 $head .= ' -->' . $crlf;
122 $head .= ' <pma:structure_schemas>' . $crlf;
123 $head .= ' <pma:database name="' . $db . '" collation="' . $db_collation . '" charset="' . $db_charset . '">' . $crlf;
125 if (count($tables) == 0) {
129 foreach ($tables as $table) {
130 // Export tables and views
131 $result = PMA_DBI_fetch_result('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), 0);
132 $tbl = $result[$table][1];
134 $is_view = PMA_isView($db, $table);
142 if ($is_view && ! isset($GLOBALS[$what . '_export_views'])) {
146 if (! $is_view && ! isset($GLOBALS[$what . '_export_tables'])) {
150 $head .= ' <pma:' . $type . ' name="' . $table . '">' . $crlf;
153 $tbl = str_replace("\n", "\n ", $tbl);
155 $head .= $tbl . ';' . $crlf;
156 $head .= ' </pma:' . $type . '>' . $crlf;
158 if (isset($GLOBALS[$what . '_export_triggers']) && $GLOBALS[$what . '_export_triggers']) {
160 $triggers = PMA_DBI_get_triggers($db, $table);
162 foreach ($triggers as $trigger) {
163 $code = $trigger['create'];
164 $head .= ' <pma:trigger name="' . $trigger['name'] . '">' . $crlf;
166 // Do some formatting
167 $code = substr(rtrim($code), 0, -3);
169 $code = str_replace("\n", "\n ", $code);
171 $head .= $code . $crlf;
172 $head .= ' </pma:trigger>' . $crlf;
181 if (isset($GLOBALS[$what . '_export_functions']) && $GLOBALS[$what . '_export_functions']) {
183 $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
185 foreach ($functions as $function) {
186 $head .= ' <pma:function name="' . $function . '">' . $crlf;
188 // Do some formatting
189 $sql = PMA_DBI_get_definition($db, 'FUNCTION', $function);
192 $sql = str_replace("\n", "\n ", $sql);
194 $head .= $sql . $crlf;
195 $head .= ' </pma:function>' . $crlf;
204 if (isset($GLOBALS[$what . '_export_procedures']) && $GLOBALS[$what . '_export_procedures']) {
206 $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
208 foreach ($procedures as $procedure) {
209 $head .= ' <pma:procedure name="' . $procedure . '">' . $crlf;
211 // Do some formatting
212 $sql = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure);
215 $sql = str_replace("\n", "\n ", $sql);
217 $head .= $sql . $crlf;
218 $head .= ' </pma:procedure>' . $crlf;
229 $head .= ' </pma:database>' . $crlf;
230 $head .= ' </pma:structure_schemas>' . $crlf;
237 return PMA_exportOutputHandler($head);
241 * Outputs database header
243 * @param string Database name
245 * @return bool Whether it suceeded
249 function PMA_exportDBHeader($db) {
253 if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
254 $head = ' <!--' . $crlf
255 . ' - ' . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ?
PMA_backquote($db) : '\'' . $db . '\''). $crlf
257 . ' <database name="' . $db . '">' . $crlf;
259 return PMA_exportOutputHandler($head);
268 * Outputs database footer
270 * @param string Database name
272 * @return bool Whether it suceeded
276 function PMA_exportDBFooter($db) {
280 if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
281 return PMA_exportOutputHandler(' </database>' . $crlf);
290 * Outputs create database database
292 * @param string Database name
294 * @return bool Whether it suceeded
298 function PMA_exportDBCreate($db) {
304 * Outputs the content of a table
306 * @param string the database name
307 * @param string the table name
308 * @param string the end of line sequence
309 * @param string the url to go back in case of error
310 * @param string SQL query for obtaining data
312 * @return bool Whether it suceeded
316 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
319 if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
320 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
322 $columns_cnt = PMA_DBI_num_fields($result);
323 for ($i = 0; $i < $columns_cnt; $i++
) {
324 $columns[$i] = stripslashes(str_replace(' ', '_', PMA_DBI_field_name($result, $i)));
328 $buffer = ' <!-- ' . $GLOBALS['strTable'] . ' ' . $table . ' -->' . $crlf;
329 if (!PMA_exportOutputHandler($buffer)) {
333 while ($record = PMA_DBI_fetch_row($result)) {
334 $buffer = ' <table name="' . htmlspecialchars($table) . '">' . $crlf;
335 for ($i = 0; $i < $columns_cnt; $i++
) {
336 // If a cell is NULL, still export it to preserve the XML structure
337 if (!isset($record[$i]) ||
is_null($record[$i])) {
338 $record[$i] = 'NULL';
340 $buffer .= ' <column name="' . $columns[$i] . '">' . htmlspecialchars((string)$record[$i])
341 . '</column>' . $crlf;
343 $buffer .= ' </table>' . $crlf;
345 if (!PMA_exportOutputHandler($buffer)) {
349 PMA_DBI_free_result($result);
353 } // end of the 'PMA_getTableXML()' function