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')) {
13 if (strlen($GLOBALS['db'])) { /* Can't do server export */
17 if (isset($plugin_list)) {
18 $plugin_list['xml'] = array(
21 'mime_type' => 'text/xml',
23 array('type' => 'begin_group', 'name' => 'general_opts'),
24 array('type' => 'hidden', 'name' => 'structure_or_data'),
25 array('type' => 'end_group')
27 'options_text' => __('Options')
30 /* Export structure */
31 $plugin_list['xml']['options'][] = array(
32 'type' => 'begin_group',
33 'name' => 'structure',
34 'text' => __('Object creation options (all are recommended)')
36 $plugin_list['xml']['options'][] = array(
38 'name' => 'export_functions',
39 'text' => __('Functions')
41 $plugin_list['xml']['options'][] = array(
43 'name' => 'export_procedures',
44 'text' => __('Procedures')
46 $plugin_list['xml']['options'][] = array(
48 'name' => 'export_tables',
49 'text' => __('Tables')
51 $plugin_list['xml']['options'][] = array(
53 'name' => 'export_triggers',
54 'text' => __('Triggers')
56 $plugin_list['xml']['options'][] = array(
58 'name' => 'export_views',
61 $plugin_list['xml']['options'][] = array(
66 $plugin_list['xml']['options'][] = array(
67 'type' => 'begin_group',
69 'text' => __('Data dump options')
71 $plugin_list['xml']['options'][] = array(
73 'name' => 'export_contents',
74 'text' => __('Export contents')
76 $plugin_list['xml']['options'][] = array(
82 * Outputs export footer
84 * @return bool Whether it suceeded
88 function PMA_exportFooter()
90 $foot = '</pma_xml_export>';
92 return PMA_exportOutputHandler($foot);
96 * Outputs export header
98 * @return bool Whether it suceeded
102 function PMA_exportHeader()
110 $export_struct = isset($GLOBALS['xml_export_functions']) ||
isset($GLOBALS['xml_export_procedures'])
111 ||
isset($GLOBALS['xml_export_tables']) ||
isset($GLOBALS['xml_export_triggers'])
112 ||
isset($GLOBALS['xml_export_views']);
113 $export_data = isset($GLOBALS['xml_export_contents']) ?
true : false;
115 if ($GLOBALS['output_charset_conversion']) {
116 $charset = $GLOBALS['charset_of_file'];
121 $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
123 . '- phpMyAdmin XML Dump' . $crlf
124 . '- version ' . PMA_VERSION
. $crlf
125 . '- http://www.phpmyadmin.net' . $crlf
127 . '- ' . __('Host') . ': ' . $cfg['Server']['host'];
128 if (!empty($cfg['Server']['port'])) {
129 $head .= ':' . $cfg['Server']['port'];
132 . '- ' . __('Generation Time') . ': ' . PMA_localisedDate() . $crlf
133 . '- ' . __('Server version') . ': ' . PMA_MYSQL_STR_VERSION
. $crlf
134 . '- ' . __('PHP Version') . ': ' . phpversion() . $crlf
135 . '-->' . $crlf . $crlf;
137 $head .= '<pma_xml_export version="1.0"' . (($export_struct) ?
' xmlns:pma="http://www.phpmyadmin.net/some_doc_url/"' : '') . '>' . $crlf;
139 if ($export_struct) {
140 $result = PMA_DBI_fetch_result('SELECT `DEFAULT_CHARACTER_SET_NAME`, `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME` = \''.PMA_sqlAddSlashes($db).'\' LIMIT 1');
141 $db_collation = $result[0]['DEFAULT_COLLATION_NAME'];
142 $db_charset = $result[0]['DEFAULT_CHARACTER_SET_NAME'];
144 $head .= ' <!--' . $crlf;
145 $head .= ' - Structure schemas' . $crlf;
146 $head .= ' -->' . $crlf;
147 $head .= ' <pma:structure_schemas>' . $crlf;
148 $head .= ' <pma:database name="' . htmlspecialchars($db) . '" collation="' . $db_collation . '" charset="' . $db_charset . '">' . $crlf;
150 if (count($tables) == 0) {
154 foreach ($tables as $table) {
155 // Export tables and views
156 $result = PMA_DBI_fetch_result('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), 0);
157 $tbl = $result[$table][1];
159 $is_view = PMA_isView($db, $table);
167 if ($is_view && ! isset($GLOBALS['xml_export_views'])) {
171 if (! $is_view && ! isset($GLOBALS['xml_export_tables'])) {
175 $head .= ' <pma:' . $type . ' name="' . $table . '">' . $crlf;
177 $tbl = " " . htmlspecialchars($tbl);
178 $tbl = str_replace("\n", "\n ", $tbl);
180 $head .= $tbl . ';' . $crlf;
181 $head .= ' </pma:' . $type . '>' . $crlf;
183 if (isset($GLOBALS['xml_export_triggers']) && $GLOBALS['xml_export_triggers']) {
185 $triggers = PMA_DBI_get_triggers($db, $table);
187 foreach ($triggers as $trigger) {
188 $code = $trigger['create'];
189 $head .= ' <pma:trigger name="' . $trigger['name'] . '">' . $crlf;
191 // Do some formatting
192 $code = substr(rtrim($code), 0, -3);
193 $code = " " . htmlspecialchars($code);
194 $code = str_replace("\n", "\n ", $code);
196 $head .= $code . $crlf;
197 $head .= ' </pma:trigger>' . $crlf;
206 if (isset($GLOBALS['xml_export_functions']) && $GLOBALS['xml_export_functions']) {
208 $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
210 foreach ($functions as $function) {
211 $head .= ' <pma:function name="' . $function . '">' . $crlf;
213 // Do some formatting
214 $sql = PMA_DBI_get_definition($db, 'FUNCTION', $function);
216 $sql = " " . htmlspecialchars($sql);
217 $sql = str_replace("\n", "\n ", $sql);
219 $head .= $sql . $crlf;
220 $head .= ' </pma:function>' . $crlf;
229 if (isset($GLOBALS['xml_export_procedures']) && $GLOBALS['xml_export_procedures']) {
231 $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
233 foreach ($procedures as $procedure) {
234 $head .= ' <pma:procedure name="' . $procedure . '">' . $crlf;
236 // Do some formatting
237 $sql = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure);
239 $sql = " " . htmlspecialchars($sql);
240 $sql = str_replace("\n", "\n ", $sql);
242 $head .= $sql . $crlf;
243 $head .= ' </pma:procedure>' . $crlf;
254 $head .= ' </pma:database>' . $crlf;
255 $head .= ' </pma:structure_schemas>' . $crlf;
262 return PMA_exportOutputHandler($head);
266 * Outputs database header
268 * @param string $db Database name
269 * @return bool Whether it suceeded
273 function PMA_exportDBHeader($db)
277 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
278 $head = ' <!--' . $crlf
279 . ' - ' . __('Database') . ': ' . '\'' . $db . '\'' . $crlf
281 . ' <database name="' . htmlspecialchars($db) . '">' . $crlf;
283 return PMA_exportOutputHandler($head);
290 * Outputs database footer
292 * @param string $db Database name
293 * @return bool Whether it suceeded
297 function PMA_exportDBFooter($db)
301 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
302 return PMA_exportOutputHandler(' </database>' . $crlf);
309 * Outputs CREATE DATABASE statement
311 * @param string $db Database name
312 * @return bool Whether it suceeded
316 function PMA_exportDBCreate($db)
322 * Outputs the content of a table in XML format
324 * @param string $db database name
325 * @param string $table table name
326 * @param string $crlf the end of line sequence
327 * @param string $error_url the url to go back in case of error
328 * @param string $sql_query SQL query for obtaining data
329 * @return bool Whether it suceeded
333 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
336 if (isset($GLOBALS['xml_export_contents']) && $GLOBALS['xml_export_contents']) {
337 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
339 $columns_cnt = PMA_DBI_num_fields($result);
341 for ($i = 0; $i < $columns_cnt; $i++
) {
342 $columns[$i] = stripslashes(str_replace(' ', '_', PMA_DBI_field_name($result, $i)));
346 $buffer = ' <!-- ' . __('Table') . ' ' . $table . ' -->' . $crlf;
347 if (!PMA_exportOutputHandler($buffer)) {
351 while ($record = PMA_DBI_fetch_row($result)) {
352 $buffer = ' <table name="' . htmlspecialchars($table) . '">' . $crlf;
353 for ($i = 0; $i < $columns_cnt; $i++
) {
354 // If a cell is NULL, still export it to preserve the XML structure
355 if (!isset($record[$i]) ||
is_null($record[$i])) {
356 $record[$i] = 'NULL';
358 $buffer .= ' <column name="' . htmlspecialchars($columns[$i]) . '">' . htmlspecialchars((string)$record[$i])
359 . '</column>' . $crlf;
361 $buffer .= ' </table>' . $crlf;
363 if (!PMA_exportOutputHandler($buffer)) {
367 PMA_DBI_free_result($result);
371 } // end of the 'PMA_getTableXML()' function