Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugins / import / ImportXml.class.php
blob9c0e9995927bd7f26833321cb42f54d70ede06e5
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 /* Get the import interface */
23 require_once 'libraries/plugins/ImportPlugin.class.php';
25 /**
26 * Handles the import for the XML format
28 * @package PhpMyAdmin-Import
29 * @subpackage XML
31 class ImportXml extends ImportPlugin
33 /**
34 * Constructor
36 public function __construct()
38 $this->setProperties();
41 /**
42 * Sets the import plugin properties.
43 * Called in the constructor.
45 * @return void
47 protected function setProperties()
49 $props = 'libraries/properties/';
50 include_once "$props/plugins/ImportPluginProperties.class.php";
52 $importPluginProperties = new ImportPluginProperties();
53 $importPluginProperties->setText(__('XML'));
54 $importPluginProperties->setExtension('xml');
55 $importPluginProperties->setMimeType('text/xml');
56 $importPluginProperties->setOptions(array());
57 $importPluginProperties->setOptionsText(__('Options'));
59 $this->properties = $importPluginProperties;
62 /**
63 * This method is called when any PluginManager to which the observer
64 * is attached calls PluginManager::notify()
66 * @param SplSubject $subject The PluginManager notifying the observer
67 * of an update.
69 * @return void
71 public function update (SplSubject $subject)
75 /**
76 * Handles the whole import logic
78 * @return void
80 public function doImport()
82 global $error, $timeout_passed, $finished, $db;
84 $i = 0;
85 $len = 0;
86 $buffer = "";
88 /**
89 * Read in the file via PMA_importGetNextChunk so that
90 * it can process compressed files
92 while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) {
93 $data = PMA_importGetNextChunk();
94 if ($data === false) {
95 /* subtract data we didn't handle yet and stop processing */
96 $offset -= strlen($buffer);
97 break;
98 } elseif ($data === true) {
99 /* Handle rest of buffer */
100 } else {
101 /* Append new data to buffer */
102 $buffer .= $data;
103 unset($data);
107 unset($data);
110 * Disable loading of external XML entities.
112 libxml_disable_entity_loader();
115 * Load the XML string
117 * The option LIBXML_COMPACT is specified because it can
118 * result in increased performance without the need to
119 * alter the code in any way. It's basically a freebee.
121 $xml = simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT);
123 unset($buffer);
126 * The XML was malformed
128 if ($xml === false) {
129 PMA_Message::error(
131 'The XML file specified was either malformed or incomplete.'
132 . ' Please correct the issue and try again.'
134 )->display();
135 unset($xml);
136 $GLOBALS['finished'] = false;
137 return;
141 * Table accumulator
143 $tables = array();
145 * Row accumulator
147 $rows = array();
150 * Temp arrays
152 $tempRow = array();
153 $tempCells = array();
156 * CREATE code included (by default: no)
158 $struct_present = false;
161 * Analyze the data in each table
163 $namespaces = $xml->getNameSpaces(true);
166 * Get the database name, collation and charset
168 $db_attr = $xml->children($namespaces['pma'])
169 ->{'structure_schemas'}->{'database'};
171 if ($db_attr instanceof SimpleXMLElement) {
172 $db_attr = $db_attr->attributes();
173 $db_name = (string)$db_attr['name'];
174 $collation = (string)$db_attr['collation'];
175 $charset = (string)$db_attr['charset'];
176 } else {
178 * If the structure section is not present
179 * get the database name from the data section
181 $db_attr = $xml->children()->attributes();
182 $db_name = (string)$db_attr['name'];
183 $collation = null;
184 $charset = null;
188 * The XML was malformed
190 if ($db_name === null) {
191 PMA_Message::error(
193 'The XML file specified was either malformed or incomplete.'
194 . ' Please correct the issue and try again.'
196 )->display();
197 unset($xml);
198 $GLOBALS['finished'] = false;
199 return;
203 * Retrieve the structure information
205 if (isset($namespaces['pma'])) {
207 * Get structures for all tables
209 $struct = $xml->children($namespaces['pma']);
211 $create = array();
213 foreach ($struct as $tier1 => $val1) {
214 foreach ($val1 as $tier2 => $val2) {
215 // Need to select the correct database for the creation of
216 // tables, views, triggers, etc.
218 * @todo Generating a USE here blocks importing of a table
219 * into another database.
221 $attrs = $val2->attributes();
222 $create[] = "USE "
223 . PMA_Util::backquote(
224 $attrs["name"]
227 foreach ($val2 as $val3) {
229 * Remove the extra cosmetic spacing
231 $val3 = str_replace(" ", "", (string)$val3);
232 $create[] = $val3;
237 $struct_present = true;
241 * Move down the XML tree to the actual data
243 $xml = $xml->children()->children();
245 $data_present = false;
248 * Only attempt to analyze/collect data if there is data present
250 if ($xml && @count($xml->children())) {
251 $data_present = true;
254 * Process all database content
256 foreach ($xml as $k1 => $v1) {
257 $tbl_attr = $v1->attributes();
259 $isInTables = false;
260 for ($i = 0; $i < count($tables); ++$i) {
261 if (! strcmp($tables[$i][TBL_NAME], (string)$tbl_attr['name'])) {
262 $isInTables = true;
263 break;
267 if ($isInTables == false) {
268 $tables[] = array((string)$tbl_attr['name']);
271 foreach ($v1 as $k2 => $v2) {
272 $row_attr = $v2->attributes();
273 if (! array_search((string)$row_attr['name'], $tempRow)) {
274 $tempRow[] = (string)$row_attr['name'];
276 $tempCells[] = (string)$v2;
279 $rows[] = array((string)$tbl_attr['name'], $tempRow, $tempCells);
281 $tempRow = array();
282 $tempCells = array();
285 unset($tempRow);
286 unset($tempCells);
287 unset($xml);
290 * Bring accumulated rows into the corresponding table
292 $num_tbls = count($tables);
293 for ($i = 0; $i < $num_tbls; ++$i) {
294 for ($j = 0; $j < count($rows); ++$j) {
295 if (! strcmp($tables[$i][TBL_NAME], $rows[$j][TBL_NAME])) {
296 if (! isset($tables[$i][COL_NAMES])) {
297 $tables[$i][] = $rows[$j][COL_NAMES];
300 $tables[$i][ROWS][] = $rows[$j][ROWS];
305 unset($rows);
307 if (! $struct_present) {
308 $analyses = array();
310 $len = count($tables);
311 for ($i = 0; $i < $len; ++$i) {
312 $analyses[] = PMA_analyzeTable($tables[$i]);
317 unset($xml);
318 unset($tempRows);
319 unset($tempCells);
320 unset($rows);
323 * Only build SQL from data if there is data present
325 if ($data_present) {
327 * Set values to NULL if they were not present
328 * to maintain PMA_buildSQL() call integrity
330 if (! isset($analyses)) {
331 $analyses = null;
332 if (! $struct_present) {
333 $create = null;
339 * string $db_name (no backquotes)
341 * array $table = array(table_name, array() column_names, array()() rows)
342 * array $tables = array of "$table"s
344 * array $analysis = array(array() column_types, array() column_sizes)
345 * array $analyses = array of "$analysis"s
347 * array $create = array of SQL strings
349 * array $options = an associative array of options
352 /* Set database name to the currently selected one, if applicable */
353 if (strlen($db)) {
354 /* Override the database name in the XML file, if one is selected */
355 $db_name = $db;
356 $options = array('create_db' => false);
357 } else {
358 if ($db_name === null) {
359 $db_name = 'XML_DB';
362 /* Set database collation/charset */
363 $options = array(
364 'db_collation' => $collation,
365 'db_charset' => $charset,
369 /* Created and execute necessary SQL statements from data */
370 PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
372 unset($analyses);
373 unset($tables);
374 unset($create);
376 /* Commit any possible data in buffers */
377 PMA_importRunQuery();