Translation update done using Pootle.
[phpmyadmin/alexukf.git] / libraries / export / xml.php
blob9bafb0998df4cfeab2a1f444d650475f5a7dd87a
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 * @package phpMyAdmin-Export-XML
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 if (strlen($GLOBALS['db'])) { /* Can't do server export */
15 if (isset($plugin_list)) {
16 $plugin_list['xml'] = array(
17 'text' => __('XML'),
18 'extension' => 'xml',
19 'mime_type' => 'text/xml',
20 'options' => array(
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');
43 /* Data */
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');
49 } else {
51 /**
52 * Outputs comment
54 * @param string Text of comment
56 * @return bool Whether it suceeded
58 function PMA_exportComment($text) {
59 return PMA_exportOutputHandler('<!-- ' . $text . ' -->' . $GLOBALS['crlf']);
62 /**
63 * Outputs export footer
65 * @return bool Whether it suceeded
67 * @access public
69 function PMA_exportFooter() {
70 $foot = '</pma_xml_export>';
72 return PMA_exportOutputHandler($foot);
75 /**
76 * Outputs export header
78 * @return bool Whether it suceeded
80 * @access public
82 function PMA_exportHeader() {
83 global $crlf;
84 global $cfg;
85 global $what;
86 global $db;
87 global $table;
88 global $tables;
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'];
95 } else {
96 $charset = $GLOBALS['charset'];
99 $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
100 . '<!--' . $crlf
101 . '- phpMyAdmin XML Dump' . $crlf
102 . '- version ' . PMA_VERSION . $crlf
103 . '- http://www.phpmyadmin.net' . $crlf
104 . '-' . $crlf
105 . '- ' . __('Host') . ': ' . $cfg['Server']['host'];
106 if (!empty($cfg['Server']['port'])) {
107 $head .= ':' . $cfg['Server']['port'];
109 $head .= $crlf
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) {
129 $tables[] = $table;
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);
139 if ($is_view) {
140 $type = 'view';
141 } else {
142 $type = 'table';
145 if ($is_view && ! isset($GLOBALS[$what . '_export_views'])) {
146 continue;
149 if (! $is_view && ! isset($GLOBALS[$what . '_export_tables'])) {
150 continue;
153 $head .= ' <pma:' . $type . ' name="' . $table . '">' . $crlf;
155 $tbl = " " . $tbl;
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']) {
162 // Export triggers
163 $triggers = PMA_DBI_get_triggers($db, $table);
164 if ($triggers) {
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);
171 $code = " " . $code;
172 $code = str_replace("\n", "\n ", $code);
174 $head .= $code . $crlf;
175 $head .= ' </pma:trigger>' . $crlf;
178 unset($trigger);
179 unset($triggers);
184 if (isset($GLOBALS[$what . '_export_functions']) && $GLOBALS[$what . '_export_functions']) {
185 // Export functions
186 $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
187 if ($functions) {
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);
193 $sql = rtrim($sql);
194 $sql = " " . $sql;
195 $sql = str_replace("\n", "\n ", $sql);
197 $head .= $sql . $crlf;
198 $head .= ' </pma:function>' . $crlf;
201 unset($create_func);
202 unset($function);
203 unset($functions);
207 if (isset($GLOBALS[$what . '_export_procedures']) && $GLOBALS[$what . '_export_procedures']) {
208 // Export procedures
209 $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
210 if ($procedures) {
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);
216 $sql = rtrim($sql);
217 $sql = " " . $sql;
218 $sql = str_replace("\n", "\n ", $sql);
220 $head .= $sql . $crlf;
221 $head .= ' </pma:procedure>' . $crlf;
224 unset($create_proc);
225 unset($procedure);
226 unset($procedures);
230 unset($result);
232 $head .= ' </pma:database>' . $crlf;
233 $head .= ' </pma:structure_schemas>' . $crlf;
235 if ($export_data) {
236 $head .= $crlf;
240 return PMA_exportOutputHandler($head);
244 * Outputs database header
246 * @param string Database name
248 * @return bool Whether it suceeded
250 * @access public
252 function PMA_exportDBHeader($db) {
253 global $crlf;
254 global $what;
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
259 . ' -->' . $crlf
260 . ' <database name="' . $db . '">' . $crlf;
262 return PMA_exportOutputHandler($head);
264 else
266 return TRUE;
271 * Outputs database footer
273 * @param string Database name
275 * @return bool Whether it suceeded
277 * @access public
279 function PMA_exportDBFooter($db) {
280 global $crlf;
281 global $what;
283 if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
284 return PMA_exportOutputHandler(' </database>' . $crlf);
286 else
288 return TRUE;
293 * Outputs create database database
295 * @param string Database name
297 * @return bool Whether it suceeded
299 * @access public
301 function PMA_exportDBCreate($db) {
302 return TRUE;
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
317 * @access public
319 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
320 global $what;
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)));
329 unset($i);
331 $buffer = ' <!-- ' . __('Table') . ' ' . $table . ' -->' . $crlf;
332 if (!PMA_exportOutputHandler($buffer)) {
333 return FALSE;
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)) {
349 return FALSE;
352 PMA_DBI_free_result($result);
355 return TRUE;
356 } // end of the 'PMA_getTableXML()' function