2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * XML import plugin for phpMyAdmin
6 * @todo Improve efficiency
8 * @package phpMyAdmin-Import
11 if (! defined('PHPMYADMIN')) {
16 * The possible scopes for $plugin_param are: 'table', 'database', and 'server'
19 if (isset($plugin_list)) {
20 $plugin_list['xml'] = array(
25 'options_text' => __('Options'),
27 /* We do not define function when plugin is just queried for information above */
31 ini_set('memory_limit', '128M');
39 * Read in the file via PMA_importGetNextChunk so that
40 * it can process compressed files
42 while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) {
43 $data = PMA_importGetNextChunk();
44 if ($data === FALSE) {
45 /* subtract data we didn't handle yet and stop processing */
46 $offset -= strlen($buffer);
48 } elseif ($data === TRUE) {
49 /* Handle rest of buffer */
51 /* Append new data to buffer */
62 * The option LIBXML_COMPACT is specified because it can
63 * result in increased performance without the need to
64 * alter the code in any way. It's basically a freebee.
66 $xml = simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT
);
71 * The XML was malformed
74 PMA_Message
::error(__('The XML file specified was either malformed or incomplete. Please correct the issue and try again.'))->display();
76 $GLOBALS['finished'] = false;
96 * CREATE code included (by default: no)
98 $struct_present = false;
101 * Analyze the data in each table
103 $namespaces = $xml->getNameSpaces(true);
106 * Get the database name, collation and charset
108 $db_attr = $xml->children($namespaces['pma'])->{'structure_schemas'}->{'database'};
110 if ($db_attr instanceof SimpleXMLElement
) {
111 $db_attr = $db_attr->attributes();
112 $db_name = (string)$db_attr['name'];
113 $collation = (string)$db_attr['collation'];
114 $charset = (string)$db_attr['charset'];
117 * If the structure section is not present
118 * get the database name from the data section
120 $db_attr = $xml->children()->attributes();
121 $db_name = (string)$db_attr['name'];
127 * The XML was malformed
129 if ($db_name === NULL) {
130 PMA_Message
::error(__('The XML file specified was either malformed or incomplete. Please correct the issue and try again.'))->display();
132 $GLOBALS['finished'] = false;
137 * Retrieve the structure information
139 if (isset($namespaces['pma'])) {
141 * Get structures for all tables
143 $struct = $xml->children($namespaces['pma']);
147 foreach ($struct as $tier1 => $val1) {
148 foreach($val1 as $tier2 => $val2) {
149 /* Need to select the correct database for the creation of tables, views, triggers, etc. */
151 * @todo Generating a USE here blocks importing of a table
152 * into another database.
154 $attrs = $val2->attributes();
155 $create[] = "USE " . PMA_backquote($attrs["name"]);
157 foreach ($val2 as $val3) {
159 * Remove the extra cosmetic spacing
161 $val3 = str_replace(" ", "", (string)$val3);
167 $struct_present = true;
171 * Move down the XML tree to the actual data
173 $xml = $xml->children()->children();
175 $data_present = false;
178 * Only attempt to analyze/collect data if there is data present
180 if (@count
($xml->children())) {
181 $data_present = true;
184 * Process all database content
186 foreach ($xml as $k1 => $v1) {
187 $tbl_attr = $v1->attributes();
190 for ($i = 0; $i < count($tables); ++
$i) {
191 if (! strcmp($tables[$i][TBL_NAME
], (string)$tbl_attr['name'])) {
197 if ($isInTables == false) {
198 $tables[] = array((string)$tbl_attr['name']);
201 foreach ($v1 as $k2 => $v2) {
202 $row_attr = $v2->attributes();
203 if (! array_search((string)$row_attr['name'], $tempRow))
205 $tempRow[] = (string)$row_attr['name'];
207 $tempCells[] = (string)$v2;
210 $rows[] = array((string)$tbl_attr['name'], $tempRow, $tempCells);
213 $tempCells = array();
221 * Bring accumulated rows into the corresponding table
223 $num_tbls = count($tables);
224 for ($i = 0; $i < $num_tbls; ++
$i) {
225 for ($j = 0; $j < count($rows); ++
$j) {
226 if (! strcmp($tables[$i][TBL_NAME
], $rows[$j][TBL_NAME
])) {
227 if (! isset($tables[$i][COL_NAMES
])) {
228 $tables[$i][] = $rows[$j][COL_NAMES
];
231 $tables[$i][ROWS
][] = $rows[$j][ROWS
];
238 if (! $struct_present) {
241 $len = count($tables);
242 for ($i = 0; $i < $len; ++
$i) {
243 $analyses[] = PMA_analyzeTable($tables[$i]);
254 * Only build SQL from data if there is data present
258 * Set values to NULL if they were not present
259 * to maintain PMA_buildSQL() call integrity
261 if (! isset($analyses)) {
263 if (! $struct_present) {
270 * string $db_name (no backquotes)
272 * array $table = array(table_name, array() column_names, array()() rows)
273 * array $tables = array of "$table"s
275 * array $analysis = array(array() column_types, array() column_sizes)
276 * array $analyses = array of "$analysis"s
278 * array $create = array of SQL strings
280 * array $options = an associative array of options
283 /* Set database name to the currently selected one, if applicable */
285 /* Override the database name in the XML file, if one is selected */
287 $options = array('create_db' => false);
289 if ($db_name === NULL) {
293 /* Set database collation/charset */
295 'db_collation' => $collation,
296 'db_charset' => $charset,
300 /* Created and execute necessary SQL statements from data */
301 PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
307 /* Commit any possible data in buffers */
308 PMA_importRunQuery();