ImportXml.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * XML import plugin for phpMyAdmin
  5. *
  6. * @todo Improve efficiency
  7. * @package PhpMyAdmin-Import
  8. * @subpackage XML
  9. */
  10. namespace PhpMyAdmin\Plugins\Import;
  11. use PhpMyAdmin\Import;
  12. use PhpMyAdmin\Message;
  13. use PhpMyAdmin\Plugins\ImportPlugin;
  14. use PhpMyAdmin\Properties\Plugins\ImportPluginProperties;
  15. use PhpMyAdmin\Util;
  16. use SimpleXMLElement;
  17. /**
  18. * Handles the import for the XML format
  19. *
  20. * @package PhpMyAdmin-Import
  21. * @subpackage XML
  22. */
  23. class ImportXml extends ImportPlugin
  24. {
  25. /**
  26. * Constructor
  27. */
  28. public function __construct()
  29. {
  30. $this->setProperties();
  31. }
  32. /**
  33. * Sets the import plugin properties.
  34. * Called in the constructor.
  35. *
  36. * @return void
  37. */
  38. protected function setProperties()
  39. {
  40. $importPluginProperties = new ImportPluginProperties();
  41. $importPluginProperties->setText(__('XML'));
  42. $importPluginProperties->setExtension('xml');
  43. $importPluginProperties->setMimeType('text/xml');
  44. $importPluginProperties->setOptions(array());
  45. $importPluginProperties->setOptionsText(__('Options'));
  46. $this->properties = $importPluginProperties;
  47. }
  48. /**
  49. * Handles the whole import logic
  50. *
  51. * @param array &$sql_data 2-element array with sql data
  52. *
  53. * @return void
  54. */
  55. public function doImport(array &$sql_data = array())
  56. {
  57. global $error, $timeout_passed, $finished, $db;
  58. $i = 0;
  59. $len = 0;
  60. $buffer = "";
  61. /**
  62. * Read in the file via Import::getNextChunk so that
  63. * it can process compressed files
  64. */
  65. while (!($finished && $i >= $len) && !$error && !$timeout_passed) {
  66. $data = Import::getNextChunk();
  67. if ($data === false) {
  68. /* subtract data we didn't handle yet and stop processing */
  69. $GLOBALS['offset'] -= strlen($buffer);
  70. break;
  71. } elseif ($data === true) {
  72. /* Handle rest of buffer */
  73. } else {
  74. /* Append new data to buffer */
  75. $buffer .= $data;
  76. unset($data);
  77. }
  78. }
  79. unset($data);
  80. /**
  81. * Disable loading of external XML entities.
  82. */
  83. libxml_disable_entity_loader();
  84. /**
  85. * Load the XML string
  86. *
  87. * The option LIBXML_COMPACT is specified because it can
  88. * result in increased performance without the need to
  89. * alter the code in any way. It's basically a freebee.
  90. */
  91. $xml = @simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT);
  92. unset($buffer);
  93. /**
  94. * The XML was malformed
  95. */
  96. if ($xml === false) {
  97. Message::error(
  98. __(
  99. 'The XML file specified was either malformed or incomplete.'
  100. . ' Please correct the issue and try again.'
  101. )
  102. )
  103. ->display();
  104. unset($xml);
  105. $GLOBALS['finished'] = false;
  106. return;
  107. }
  108. /**
  109. * Table accumulator
  110. */
  111. $tables = array();
  112. /**
  113. * Row accumulator
  114. */
  115. $rows = array();
  116. /**
  117. * Temp arrays
  118. */
  119. $tempRow = array();
  120. $tempCells = array();
  121. /**
  122. * CREATE code included (by default: no)
  123. */
  124. $struct_present = false;
  125. /**
  126. * Analyze the data in each table
  127. */
  128. $namespaces = $xml->getNameSpaces(true);
  129. /**
  130. * Get the database name, collation and charset
  131. */
  132. $db_attr = $xml->children(isset($namespaces['pma']) ? $namespaces['pma'] : null)
  133. ->{'structure_schemas'}->{'database'};
  134. if ($db_attr instanceof SimpleXMLElement) {
  135. $db_attr = $db_attr->attributes();
  136. $db_name = (string)$db_attr['name'];
  137. $collation = (string)$db_attr['collation'];
  138. $charset = (string)$db_attr['charset'];
  139. } else {
  140. /**
  141. * If the structure section is not present
  142. * get the database name from the data section
  143. */
  144. $db_attr = $xml->children()
  145. ->attributes();
  146. $db_name = (string)$db_attr['name'];
  147. $collation = null;
  148. $charset = null;
  149. }
  150. /**
  151. * The XML was malformed
  152. */
  153. if ($db_name === null) {
  154. Message::error(
  155. __(
  156. 'The XML file specified was either malformed or incomplete.'
  157. . ' Please correct the issue and try again.'
  158. )
  159. )
  160. ->display();
  161. unset($xml);
  162. $GLOBALS['finished'] = false;
  163. return;
  164. }
  165. /**
  166. * Retrieve the structure information
  167. */
  168. if (isset($namespaces['pma'])) {
  169. /**
  170. * Get structures for all tables
  171. *
  172. * @var SimpleXMLElement $struct
  173. */
  174. $struct = $xml->children($namespaces['pma']);
  175. $create = array();
  176. /** @var SimpleXMLElement $val1 */
  177. foreach ($struct as $val1) {
  178. /** @var SimpleXMLElement $val2 */
  179. foreach ($val1 as $val2) {
  180. // Need to select the correct database for the creation of
  181. // tables, views, triggers, etc.
  182. /**
  183. * @todo Generating a USE here blocks importing of a table
  184. * into another database.
  185. */
  186. $attrs = $val2->attributes();
  187. $create[] = "USE "
  188. . Util::backquote(
  189. $attrs["name"]
  190. );
  191. foreach ($val2 as $val3) {
  192. /**
  193. * Remove the extra cosmetic spacing
  194. */
  195. $val3 = str_replace(" ", "", (string)$val3);
  196. $create[] = $val3;
  197. }
  198. }
  199. }
  200. $struct_present = true;
  201. }
  202. /**
  203. * Move down the XML tree to the actual data
  204. */
  205. $xml = $xml->children()
  206. ->children();
  207. $data_present = false;
  208. /**
  209. * Only attempt to analyze/collect data if there is data present
  210. */
  211. if ($xml && @count($xml->children())) {
  212. $data_present = true;
  213. /**
  214. * Process all database content
  215. */
  216. foreach ($xml as $v1) {
  217. $tbl_attr = $v1->attributes();
  218. $isInTables = false;
  219. $num_tables = count($tables);
  220. for ($i = 0; $i < $num_tables; ++$i) {
  221. if (!strcmp($tables[$i][Import::TBL_NAME], (string)$tbl_attr['name'])) {
  222. $isInTables = true;
  223. break;
  224. }
  225. }
  226. if (!$isInTables) {
  227. $tables[] = array((string)$tbl_attr['name']);
  228. }
  229. foreach ($v1 as $v2) {
  230. $row_attr = $v2->attributes();
  231. if (!array_search((string)$row_attr['name'], $tempRow)) {
  232. $tempRow[] = (string)$row_attr['name'];
  233. }
  234. $tempCells[] = (string)$v2;
  235. }
  236. $rows[] = array((string)$tbl_attr['name'], $tempRow, $tempCells);
  237. $tempRow = array();
  238. $tempCells = array();
  239. }
  240. unset($tempRow);
  241. unset($tempCells);
  242. unset($xml);
  243. /**
  244. * Bring accumulated rows into the corresponding table
  245. */
  246. $num_tables = count($tables);
  247. for ($i = 0; $i < $num_tables; ++$i) {
  248. $num_rows = count($rows);
  249. for ($j = 0; $j < $num_rows; ++$j) {
  250. if (!strcmp($tables[$i][Import::TBL_NAME], $rows[$j][Import::TBL_NAME])) {
  251. if (!isset($tables[$i][Import::COL_NAMES])) {
  252. $tables[$i][] = $rows[$j][Import::COL_NAMES];
  253. }
  254. $tables[$i][Import::ROWS][] = $rows[$j][Import::ROWS];
  255. }
  256. }
  257. }
  258. unset($rows);
  259. if (!$struct_present) {
  260. $analyses = array();
  261. $len = count($tables);
  262. for ($i = 0; $i < $len; ++$i) {
  263. $analyses[] = Import::analyzeTable($tables[$i]);
  264. }
  265. }
  266. }
  267. unset($xml);
  268. unset($tempCells);
  269. unset($rows);
  270. /**
  271. * Only build SQL from data if there is data present
  272. */
  273. if ($data_present) {
  274. /**
  275. * Set values to NULL if they were not present
  276. * to maintain Import::buildSql() call integrity
  277. */
  278. if (!isset($analyses)) {
  279. $analyses = null;
  280. if (!$struct_present) {
  281. $create = null;
  282. }
  283. }
  284. }
  285. /**
  286. * string $db_name (no backquotes)
  287. *
  288. * array $table = array(table_name, array() column_names, array()() rows)
  289. * array $tables = array of "$table"s
  290. *
  291. * array $analysis = array(array() column_types, array() column_sizes)
  292. * array $analyses = array of "$analysis"s
  293. *
  294. * array $create = array of SQL strings
  295. *
  296. * array $options = an associative array of options
  297. */
  298. /* Set database name to the currently selected one, if applicable */
  299. if (strlen($db)) {
  300. /* Override the database name in the XML file, if one is selected */
  301. $db_name = $db;
  302. $options = array('create_db' => false);
  303. } else {
  304. if ($db_name === null) {
  305. $db_name = 'XML_DB';
  306. }
  307. /* Set database collation/charset */
  308. $options = array(
  309. 'db_collation' => $collation,
  310. 'db_charset' => $charset,
  311. );
  312. }
  313. /* Created and execute necessary SQL statements from data */
  314. Import::buildSql($db_name, $tables, $analyses, $create, $options, $sql_data);
  315. unset($analyses);
  316. unset($tables);
  317. unset($create);
  318. /* Commit any possible data in buffers */
  319. Import::runQuery('', '', $sql_data);
  320. }
  321. }