ImportXml.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. declare(strict_types=1);
  11. namespace PhpMyAdmin\Plugins\Import;
  12. use PhpMyAdmin\Import;
  13. use PhpMyAdmin\Message;
  14. use PhpMyAdmin\Plugins\ImportPlugin;
  15. use PhpMyAdmin\Properties\Plugins\ImportPluginProperties;
  16. use PhpMyAdmin\Util;
  17. use SimpleXMLElement;
  18. /**
  19. * Handles the import for the XML format
  20. *
  21. * @package PhpMyAdmin-Import
  22. * @subpackage XML
  23. */
  24. class ImportXml extends ImportPlugin
  25. {
  26. /**
  27. * Constructor
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. $this->setProperties();
  33. }
  34. /**
  35. * Sets the import plugin properties.
  36. * Called in the constructor.
  37. *
  38. * @return void
  39. */
  40. protected function setProperties()
  41. {
  42. $importPluginProperties = new ImportPluginProperties();
  43. $importPluginProperties->setText(__('XML'));
  44. $importPluginProperties->setExtension('xml');
  45. $importPluginProperties->setMimeType('text/xml');
  46. $importPluginProperties->setOptions([]);
  47. $importPluginProperties->setOptionsText(__('Options'));
  48. $this->properties = $importPluginProperties;
  49. }
  50. /**
  51. * Handles the whole import logic
  52. *
  53. * @param array $sql_data 2-element array with sql data
  54. *
  55. * @return void
  56. */
  57. public function doImport(array &$sql_data = [])
  58. {
  59. global $error, $timeout_passed, $finished, $db;
  60. $i = 0;
  61. $len = 0;
  62. $buffer = "";
  63. /**
  64. * Read in the file via Import::getNextChunk so that
  65. * it can process compressed files
  66. */
  67. while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) {
  68. $data = $this->import->getNextChunk();
  69. if ($data === false) {
  70. /* subtract data we didn't handle yet and stop processing */
  71. $GLOBALS['offset'] -= strlen($buffer);
  72. break;
  73. } elseif ($data !== true) {
  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 = [];
  112. /**
  113. * Row accumulator
  114. */
  115. $rows = [];
  116. /**
  117. * Temp arrays
  118. */
  119. $tempRow = [];
  120. $tempCells = [];
  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 = [];
  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[] = [(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[] = [
  237. (string) $tbl_attr['name'],
  238. $tempRow,
  239. $tempCells,
  240. ];
  241. $tempRow = [];
  242. $tempCells = [];
  243. }
  244. unset($tempRow);
  245. unset($tempCells);
  246. unset($xml);
  247. /**
  248. * Bring accumulated rows into the corresponding table
  249. */
  250. $num_tables = count($tables);
  251. for ($i = 0; $i < $num_tables; ++$i) {
  252. $num_rows = count($rows);
  253. for ($j = 0; $j < $num_rows; ++$j) {
  254. if (! strcmp($tables[$i][Import::TBL_NAME], $rows[$j][Import::TBL_NAME])) {
  255. if (! isset($tables[$i][Import::COL_NAMES])) {
  256. $tables[$i][] = $rows[$j][Import::COL_NAMES];
  257. }
  258. $tables[$i][Import::ROWS][] = $rows[$j][Import::ROWS];
  259. }
  260. }
  261. }
  262. unset($rows);
  263. if (! $struct_present) {
  264. $analyses = [];
  265. $len = count($tables);
  266. for ($i = 0; $i < $len; ++$i) {
  267. $analyses[] = $this->import->analyzeTable($tables[$i]);
  268. }
  269. }
  270. }
  271. unset($xml);
  272. unset($tempCells);
  273. unset($rows);
  274. /**
  275. * Only build SQL from data if there is data present
  276. */
  277. if ($data_present) {
  278. /**
  279. * Set values to NULL if they were not present
  280. * to maintain Import::buildSql() call integrity
  281. */
  282. if (! isset($analyses)) {
  283. $analyses = null;
  284. if (! $struct_present) {
  285. $create = null;
  286. }
  287. }
  288. }
  289. /**
  290. * string $db_name (no backquotes)
  291. *
  292. * array $table = array(table_name, array() column_names, array()() rows)
  293. * array $tables = array of "$table"s
  294. *
  295. * array $analysis = array(array() column_types, array() column_sizes)
  296. * array $analyses = array of "$analysis"s
  297. *
  298. * array $create = array of SQL strings
  299. *
  300. * array $options = an associative array of options
  301. */
  302. /* Set database name to the currently selected one, if applicable */
  303. if (strlen((string) $db)) {
  304. /* Override the database name in the XML file, if one is selected */
  305. $db_name = $db;
  306. $options = ['create_db' => false];
  307. } else {
  308. if ($db_name === null) {
  309. $db_name = 'XML_DB';
  310. }
  311. /* Set database collation/charset */
  312. $options = [
  313. 'db_collation' => $collation,
  314. 'db_charset' => $charset,
  315. ];
  316. }
  317. /* Created and execute necessary SQL statements from data */
  318. $this->import->buildSql($db_name, $tables, $analyses, $create, $options, $sql_data);
  319. unset($analyses);
  320. unset($tables);
  321. unset($create);
  322. /* Commit any possible data in buffers */
  323. $this->import->runQuery('', '', $sql_data);
  324. }
  325. }