Translated using Weblate.
[phpmyadmin.git] / libraries / import / xml.php
blob38c2054bff6bd1e14702afa064d9357ded412021
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * XML import plugin for phpMyAdmin
6 * @todo Improve efficiency
7 * @package PhpMyAdmin-Import
8 * @subpackage XML
9 */
11 if (! defined('PHPMYADMIN')) {
12 exit;
15 /**
16 * We need way to disable external XML entities processing.
18 if (!function_exists('libxml_disable_entity_loader')) {
19 return;
22 /**
23 * The possible scopes for $plugin_param are: 'table', 'database', and 'server'
26 if (isset($plugin_list)) {
27 $plugin_list['xml'] = array(
28 'text' => __('XML'),
29 'extension' => 'xml',
30 'options' => array(
32 'options_text' => __('Options'),
34 /* We do not define function when plugin is just queried for information above */
35 return;
38 $i = 0;
39 $len = 0;
40 $buffer = "";
42 /**
43 * Read in the file via PMA_importGetNextChunk so that
44 * it can process compressed files
46 while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) {
47 $data = PMA_importGetNextChunk();
48 if ($data === false) {
49 /* subtract data we didn't handle yet and stop processing */
50 $offset -= strlen($buffer);
51 break;
52 } elseif ($data === true) {
53 /* Handle rest of buffer */
54 } else {
55 /* Append new data to buffer */
56 $buffer .= $data;
57 unset($data);
61 unset($data);
63 /**
64 * Disable loading of external XML entities.
66 libxml_disable_entity_loader();
68 /**
69 * Load the XML string
71 * The option LIBXML_COMPACT is specified because it can
72 * result in increased performance without the need to
73 * alter the code in any way. It's basically a freebee.
75 $xml = simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT);
77 unset($buffer);
79 /**
80 * The XML was malformed
82 if ($xml === false) {
83 PMA_Message::error(__('The XML file specified was either malformed or incomplete. Please correct the issue and try again.'))->display();
84 unset($xml);
85 $GLOBALS['finished'] = false;
86 return;
89 /**
90 * Table accumulator
92 $tables = array();
93 /**
94 * Row accumulator
96 $rows = array();
98 /**
99 * Temp arrays
101 $tempRow = array();
102 $tempCells = array();
105 * CREATE code included (by default: no)
107 $struct_present = false;
110 * Analyze the data in each table
112 $namespaces = $xml->getNameSpaces(true);
115 * Get the database name, collation and charset
117 $db_attr = $xml->children($namespaces['pma'])->{'structure_schemas'}->{'database'};
119 if ($db_attr instanceof SimpleXMLElement) {
120 $db_attr = $db_attr->attributes();
121 $db_name = (string)$db_attr['name'];
122 $collation = (string)$db_attr['collation'];
123 $charset = (string)$db_attr['charset'];
124 } else {
126 * If the structure section is not present
127 * get the database name from the data section
129 $db_attr = $xml->children()->attributes();
130 $db_name = (string)$db_attr['name'];
131 $collation = null;
132 $charset = null;
136 * The XML was malformed
138 if ($db_name === null) {
139 PMA_Message::error(__('The XML file specified was either malformed or incomplete. Please correct the issue and try again.'))->display();
140 unset($xml);
141 $GLOBALS['finished'] = false;
142 return;
146 * Retrieve the structure information
148 if (isset($namespaces['pma'])) {
150 * Get structures for all tables
152 $struct = $xml->children($namespaces['pma']);
154 $create = array();
156 foreach ($struct as $tier1 => $val1) {
157 foreach ($val1 as $tier2 => $val2) {
158 /* Need to select the correct database for the creation of tables, views, triggers, etc. */
160 * @todo Generating a USE here blocks importing of a table
161 * into another database.
163 $attrs = $val2->attributes();
164 $create[] = "USE " . PMA_backquote($attrs["name"]);
166 foreach ($val2 as $val3) {
168 * Remove the extra cosmetic spacing
170 $val3 = str_replace(" ", "", (string)$val3);
171 $create[] = $val3;
176 $struct_present = true;
180 * Move down the XML tree to the actual data
182 $xml = $xml->children()->children();
184 $data_present = false;
187 * Only attempt to analyze/collect data if there is data present
189 if ($xml && @$xml->count()) {
190 $data_present = true;
193 * Process all database content
195 foreach ($xml as $k1 => $v1) {
196 $tbl_attr = $v1->attributes();
198 $isInTables = false;
199 for ($i = 0; $i < count($tables); ++$i) {
200 if (! strcmp($tables[$i][TBL_NAME], (string)$tbl_attr['name'])) {
201 $isInTables = true;
202 break;
206 if ($isInTables == false) {
207 $tables[] = array((string)$tbl_attr['name']);
210 foreach ($v1 as $k2 => $v2) {
211 $row_attr = $v2->attributes();
212 if (! array_search((string)$row_attr['name'], $tempRow)) {
213 $tempRow[] = (string)$row_attr['name'];
215 $tempCells[] = (string)$v2;
218 $rows[] = array((string)$tbl_attr['name'], $tempRow, $tempCells);
220 $tempRow = array();
221 $tempCells = array();
224 unset($tempRow);
225 unset($tempCells);
226 unset($xml);
229 * Bring accumulated rows into the corresponding table
231 $num_tbls = count($tables);
232 for ($i = 0; $i < $num_tbls; ++$i) {
233 for ($j = 0; $j < count($rows); ++$j) {
234 if (! strcmp($tables[$i][TBL_NAME], $rows[$j][TBL_NAME])) {
235 if (! isset($tables[$i][COL_NAMES])) {
236 $tables[$i][] = $rows[$j][COL_NAMES];
239 $tables[$i][ROWS][] = $rows[$j][ROWS];
244 unset($rows);
246 if (! $struct_present) {
247 $analyses = array();
249 $len = count($tables);
250 for ($i = 0; $i < $len; ++$i) {
251 $analyses[] = PMA_analyzeTable($tables[$i]);
256 unset($xml);
257 unset($tempRows);
258 unset($tempCells);
259 unset($rows);
262 * Only build SQL from data if there is data present
264 if ($data_present) {
266 * Set values to NULL if they were not present
267 * to maintain PMA_buildSQL() call integrity
269 if (! isset($analyses)) {
270 $analyses = null;
271 if (! $struct_present) {
272 $create = null;
278 * string $db_name (no backquotes)
280 * array $table = array(table_name, array() column_names, array()() rows)
281 * array $tables = array of "$table"s
283 * array $analysis = array(array() column_types, array() column_sizes)
284 * array $analyses = array of "$analysis"s
286 * array $create = array of SQL strings
288 * array $options = an associative array of options
291 /* Set database name to the currently selected one, if applicable */
292 if (strlen($db)) {
293 /* Override the database name in the XML file, if one is selected */
294 $db_name = $db;
295 $options = array('create_db' => false);
296 } else {
297 if ($db_name === null) {
298 $db_name = 'XML_DB';
301 /* Set database collation/charset */
302 $options = array(
303 'db_collation' => $collation,
304 'db_charset' => $charset,
308 /* Created and execute necessary SQL statements from data */
309 PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
311 unset($analyses);
312 unset($tables);
313 unset($create);
315 /* Commit any possible data in buffers */
316 PMA_importRunQuery();