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')) {
23 * The possible scopes for $plugin_param are: 'table', 'database', and 'server'
26 if (isset($plugin_list)) {
27 $plugin_list['xml'] = array(
32 'options_text' => __('Options'),
34 /* We do not define function when plugin is just queried for information above */
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);
52 } elseif ($data === true) {
53 /* Handle rest of buffer */
55 /* Append new data to buffer */
64 * Disable loading of external XML entities.
66 libxml_disable_entity_loader();
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
);
80 * The XML was malformed
83 PMA_Message
::error(__('The XML file specified was either malformed or incomplete. Please correct the issue and try again.'))->display();
85 $GLOBALS['finished'] = false;
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'];
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'];
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();
141 $GLOBALS['finished'] = false;
146 * Retrieve the structure information
148 if (isset($namespaces['pma'])) {
150 * Get structures for all tables
152 $struct = $xml->children($namespaces['pma']);
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);
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 (@count
($xml->children())) {
190 $data_present = true;
193 * Process all database content
195 foreach ($xml as $k1 => $v1) {
196 $tbl_attr = $v1->attributes();
199 for ($i = 0; $i < count($tables); ++
$i) {
200 if (! strcmp($tables[$i][TBL_NAME
], (string)$tbl_attr['name'])) {
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);
221 $tempCells = array();
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
];
246 if (! $struct_present) {
249 $len = count($tables);
250 for ($i = 0; $i < $len; ++
$i) {
251 $analyses[] = PMA_analyzeTable($tables[$i]);
262 * Only build SQL from data if there is data present
266 * Set values to NULL if they were not present
267 * to maintain PMA_buildSQL() call integrity
269 if (! isset($analyses)) {
271 if (! $struct_present) {
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 */
293 /* Override the database name in the XML file, if one is selected */
295 $options = array('create_db' => false);
297 if ($db_name === null) {
301 /* Set database collation/charset */
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);
315 /* Commit any possible data in buffers */
316 PMA_importRunQuery();