Converting number of Excel column names no longer limited
[phpmyadmin/arisferyanto.git] / libraries / import / xml.php
blobf62328eaa956cfa42a1febc96b19d413c28193da
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * XML import plugin for phpMyAdmin
6 * @todo Improve efficiency
7 * @version 0.5-beta
8 * @package phpMyAdmin-Import
9 */
11 if (! defined('PHPMYADMIN')) {
12 exit;
15 /**
16 * The possible scopes for $plugin_param are: 'table', 'database', and 'server'
19 if (isset($plugin_list)) {
20 $plugin_list['xml'] = array(
21 'text' => 'strXML',
22 'extension' => 'xml',
23 'options' => array(
25 'options_text' => 'strOptions',
27 /* We do not define function when plugin is just queried for information above */
28 return;
31 ini_set('memory_limit', '128M');
32 set_time_limit(120);
34 $i = 0;
35 $len = 0;
36 $buffer = "";
38 /**
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);
47 break;
48 } elseif ($data === TRUE) {
49 /* Handle rest of buffer */
50 } else {
51 /* Append new data to buffer */
52 $buffer .= $data;
53 unset($data);
57 unset($data);
59 /**
60 * Load the XML string
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(utf8_encode($buffer), "SimpleXMLElement", LIBXML_COMPACT);
68 unset($buffer);
70 /**
71 * The XML was malformed
73 if ($xml === FALSE) {
74 PMA_Message::error('strXMLError')->display();
75 unset($xml);
76 $GLOBALS['finished'] = false;
77 return;
80 /**
81 * Table accumulator
83 $tables = array();
84 /**
85 * Row accumulator
87 $rows = array();
89 /**
90 * Temp arrays
92 $tempRow = array();
93 $tempCells = array();
95 /**
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'];
115 } else {
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'];
122 $collation = NULL;
123 $charset = NULL;
127 * The XML was malformed
129 if ($db_name === NULL) {
130 PMA_Message::error('strXMLError')->display();
131 unset($xml);
132 $GLOBALS['finished'] = false;
133 return;
137 * Retrieve the structure information
139 if (isset($namespaces['pma'])) {
141 * Get structures for all tables
143 $struct = $xml->children($namespaces['pma']);
145 $create = array();
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);
162 $create[] = $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();
189 $isInTables = false;
190 for ($i = 0; $i < count($tables); ++$i) {
191 if (! strcmp($tables[$i][TBL_NAME], (string)$tbl_attr['name'])) {
192 $isInTables = true;
193 break;
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);
212 $tempRow = array();
213 $tempCells = array();
216 unset($tempRow);
217 unset($tempCells);
218 unset($xml);
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];
236 unset($rows);
238 if (! $struct_present) {
239 $analyses = array();
241 $len = count($tables);
242 for ($i = 0; $i < $len; ++$i) {
243 $analyses[] = PMA_analyzeTable($tables[$i]);
248 unset($xml);
249 unset($tempRows);
250 unset($tempCells);
251 unset($rows);
254 * Only build SQL from data if there is data present
256 if ($data_present) {
258 * Set values to NULL if they were not present
259 * to maintain PMA_buildSQL() call integrity
261 if (! isset($analyses)) {
262 $analyses = NULL;
263 if (! $struct_present) {
264 $create = NULL;
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 */
284 if (strlen($db)) {
285 /* Override the database name in the XML file, if one is selected */
286 $db_name = $db;
287 $options = array('create_db' => false);
288 } else {
289 if ($db_name === NULL) {
290 $db_name = 'XML_DB';
293 /* Set database collation/charset */
294 $options = array(
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);
303 unset($analyses);
304 unset($tables);
305 unset($create);
307 /* Commit any possible data in buffers */
308 PMA_importRunQuery();