bug #2042032 Cannot detect PmaAbsoluteUri correctly on Windows
[phpmyadmin/madhuracj.git] / libraries / export / xml.php
blob5b82ad5ecccca8187528c03819cf0c35c23c4a1f
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 * @todo
7 * @version $Id$
8 * @package phpMyAdmin-Export-XML
9 */
10 if (! defined('PHPMYADMIN')) {
11 exit;
14 if (strlen($GLOBALS['db'])) { /* Can't do server export */
16 if (isset($plugin_list)) {
17 $plugin_list['xml'] = array(
18 'text' => 'strXML',
19 'extension' => 'xml',
20 'mime_type' => 'text/xml',
21 'options' => array(
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');
43 /* Data */
44 $plugin_list['xml']['options'][] =
45 array('type' => 'bool', 'name' => 'export_contents', 'text' => 'strXMLExportContents');
46 } else {
48 /**
49 * Outputs comment
51 * @param string Text of comment
53 * @return bool Whether it suceeded
55 function PMA_exportComment($text) {
56 return PMA_exportOutputHandler('<!-- ' . $text . ' -->' . $GLOBALS['crlf']);
59 /**
60 * Outputs export footer
62 * @return bool Whether it suceeded
64 * @access public
66 function PMA_exportFooter() {
67 $foot = '</pma_xml_export>';
69 return PMA_exportOutputHandler($foot);
72 /**
73 * Outputs export header
75 * @return bool Whether it suceeded
77 * @access public
79 function PMA_exportHeader() {
80 global $crlf;
81 global $cfg;
82 global $what;
83 global $db;
84 global $table;
85 global $tables;
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'];
92 } else {
93 $charset = $GLOBALS['charset'];
96 $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
97 . '<!--' . $crlf
98 . '- phpMyAdmin XML Dump' . $crlf
99 . '- version ' . PMA_VERSION . $crlf
100 . '- http://www.phpmyadmin.net' . $crlf
101 . '-' . $crlf
102 . '- ' . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host'];
103 if (!empty($cfg['Server']['port'])) {
104 $head .= ':' . $cfg['Server']['port'];
106 $head .= $crlf
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) {
126 $tables[] = $table;
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);
136 if ($is_view) {
137 $type = 'view';
138 } else {
139 $type = 'table';
142 if ($is_view && ! isset($GLOBALS[$what . '_export_views'])) {
143 continue;
146 if (! $is_view && ! isset($GLOBALS[$what . '_export_tables'])) {
147 continue;
150 $head .= ' <pma:' . $type . ' name="' . $table . '">' . $crlf;
152 $tbl = " " . $tbl;
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']) {
159 // Export triggers
160 $triggers = PMA_DBI_get_triggers($db, $table);
161 if ($triggers) {
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);
168 $code = " " . $code;
169 $code = str_replace("\n", "\n ", $code);
171 $head .= $code . $crlf;
172 $head .= ' </pma:trigger>' . $crlf;
175 unset($trigger);
176 unset($triggers);
181 if (isset($GLOBALS[$what . '_export_functions']) && $GLOBALS[$what . '_export_functions']) {
182 // Export functions
183 $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
184 if ($functions) {
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);
190 $sql = rtrim($sql);
191 $sql = " " . $sql;
192 $sql = str_replace("\n", "\n ", $sql);
194 $head .= $sql . $crlf;
195 $head .= ' </pma:function>' . $crlf;
198 unset($create_func);
199 unset($function);
200 unset($functions);
204 if (isset($GLOBALS[$what . '_export_procedures']) && $GLOBALS[$what . '_export_procedures']) {
205 // Export procedures
206 $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
207 if ($procedures) {
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);
213 $sql = rtrim($sql);
214 $sql = " " . $sql;
215 $sql = str_replace("\n", "\n ", $sql);
217 $head .= $sql . $crlf;
218 $head .= ' </pma:procedure>' . $crlf;
221 unset($create_proc);
222 unset($procedure);
223 unset($procedures);
227 unset($result);
229 $head .= ' </pma:database>' . $crlf;
230 $head .= ' </pma:structure_schemas>' . $crlf;
232 if ($export_data) {
233 $head .= $crlf;
237 return PMA_exportOutputHandler($head);
241 * Outputs database header
243 * @param string Database name
245 * @return bool Whether it suceeded
247 * @access public
249 function PMA_exportDBHeader($db) {
250 global $crlf;
251 global $what;
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
256 . ' -->' . $crlf
257 . ' <database name="' . $db . '">' . $crlf;
259 return PMA_exportOutputHandler($head);
261 else
263 return TRUE;
268 * Outputs database footer
270 * @param string Database name
272 * @return bool Whether it suceeded
274 * @access public
276 function PMA_exportDBFooter($db) {
277 global $crlf;
278 global $what;
280 if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
281 return PMA_exportOutputHandler(' </database>' . $crlf);
283 else
285 return TRUE;
290 * Outputs create database database
292 * @param string Database name
294 * @return bool Whether it suceeded
296 * @access public
298 function PMA_exportDBCreate($db) {
299 return TRUE;
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
314 * @access public
316 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
317 global $what;
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)));
326 unset($i);
328 $buffer = ' <!-- ' . $GLOBALS['strTable'] . ' ' . $table . ' -->' . $crlf;
329 if (!PMA_exportOutputHandler($buffer)) {
330 return FALSE;
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(utf8_encode((string)$record[$i]))
341 . '</column>' . $crlf;
343 $buffer .= ' </table>' . $crlf;
345 if (!PMA_exportOutputHandler($buffer)) {
346 return FALSE;
349 PMA_DBI_free_result($result);
352 return TRUE;
353 } // end of the 'PMA_getTableXML()' function