2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * XML import plugin for phpMyAdmin
6 * @todo Improve efficiency
7 * @package PhpMyAdmin-Import
11 if (! defined('PHPMYADMIN')) {
16 * We need way to disable external XML entities processing.
18 if (!function_exists('libxml_disable_entity_loader')) {
22 /* Get the import interface */
23 require_once 'libraries/plugins/ImportPlugin.class.php';
26 * Handles the import for the XML format
28 * @package PhpMyAdmin-Import
31 class ImportXml
extends ImportPlugin
36 public function __construct()
38 $this->setProperties();
42 * Sets the import plugin properties.
43 * Called in the constructor.
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;
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
71 public function update (SplSubject
$subject)
76 * Handles the whole import logic
80 public function doImport()
82 global $error, $timeout_passed, $finished, $db;
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);
98 } elseif ($data === true) {
99 /* Handle rest of buffer */
101 /* Append new data to buffer */
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
);
126 * The XML was malformed
128 if ($xml === false) {
131 'The XML file specified was either malformed or incomplete.'
132 . ' Please correct the issue and try again.'
136 $GLOBALS['finished'] = false;
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'];
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'];
188 * The XML was malformed
190 if ($db_name === null) {
193 'The XML file specified was either malformed or incomplete.'
194 . ' Please correct the issue and try again.'
198 $GLOBALS['finished'] = false;
203 * Retrieve the structure information
205 if (isset($namespaces['pma'])) {
207 * Get structures for all tables
209 $struct = $xml->children($namespaces['pma']);
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();
223 . PMA_Util
::backquote(
227 foreach ($val2 as $val3) {
229 * Remove the extra cosmetic spacing
231 $val3 = str_replace(" ", "", (string)$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();
260 for ($i = 0; $i < count($tables); ++
$i) {
261 if (! strcmp($tables[$i][TBL_NAME
], (string)$tbl_attr['name'])) {
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);
282 $tempCells = array();
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
];
307 if (! $struct_present) {
310 $len = count($tables);
311 for ($i = 0; $i < $len; ++
$i) {
312 $analyses[] = PMA_analyzeTable($tables[$i]);
323 * Only build SQL from data if there is data present
327 * Set values to NULL if they were not present
328 * to maintain PMA_buildSQL() call integrity
330 if (! isset($analyses)) {
332 if (! $struct_present) {
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 */
354 /* Override the database name in the XML file, if one is selected */
356 $options = array('create_db' => false);
358 if ($db_name === null) {
362 /* Set database collation/charset */
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);
376 /* Commit any possible data in buffers */
377 PMA_importRunQuery();