PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/public/phpmyadmin/libraries/plugins/import/ImportXml.class.php

https://gitlab.com/qbarbosa/klindev
PHP | 371 lines | 189 code | 50 blank | 132 comment | 31 complexity | 254f71235f62a6f308e1cfaca3e46080 MD5 | raw file
  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. if (! defined('PHPMYADMIN')) {
  11. exit;
  12. }
  13. /**
  14. * We need way to disable external XML entities processing.
  15. */
  16. if (! function_exists('libxml_disable_entity_loader')) {
  17. $GLOBALS['skip_import'] = true;
  18. return;
  19. }
  20. /* Get the import interface */
  21. require_once 'libraries/plugins/ImportPlugin.class.php';
  22. /**
  23. * Handles the import for the XML format
  24. *
  25. * @package PhpMyAdmin-Import
  26. * @subpackage XML
  27. */
  28. class ImportXml extends ImportPlugin
  29. {
  30. /**
  31. * Constructor
  32. */
  33. public function __construct()
  34. {
  35. $this->setProperties();
  36. }
  37. /**
  38. * Sets the import plugin properties.
  39. * Called in the constructor.
  40. *
  41. * @return void
  42. */
  43. protected function setProperties()
  44. {
  45. $props = 'libraries/properties/';
  46. include_once "$props/plugins/ImportPluginProperties.class.php";
  47. $importPluginProperties = new ImportPluginProperties();
  48. $importPluginProperties->setText(__('XML'));
  49. $importPluginProperties->setExtension('xml');
  50. $importPluginProperties->setMimeType('text/xml');
  51. $importPluginProperties->setOptions(array());
  52. $importPluginProperties->setOptionsText(__('Options'));
  53. $this->properties = $importPluginProperties;
  54. }
  55. /**
  56. * Handles the whole import logic
  57. *
  58. * @return void
  59. */
  60. public function doImport()
  61. {
  62. global $error, $timeout_passed, $finished, $db;
  63. $i = 0;
  64. $len = 0;
  65. $buffer = "";
  66. /**
  67. * Read in the file via PMA_importGetNextChunk so that
  68. * it can process compressed files
  69. */
  70. while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) {
  71. $data = PMA_importGetNextChunk();
  72. if ($data === false) {
  73. /* subtract data we didn't handle yet and stop processing */
  74. $GLOBALS['offset'] -= strlen($buffer);
  75. break;
  76. } elseif ($data === true) {
  77. /* Handle rest of buffer */
  78. } else {
  79. /* Append new data to buffer */
  80. $buffer .= $data;
  81. unset($data);
  82. }
  83. }
  84. unset($data);
  85. /**
  86. * Disable loading of external XML entities.
  87. */
  88. libxml_disable_entity_loader();
  89. /**
  90. * Load the XML string
  91. *
  92. * The option LIBXML_COMPACT is specified because it can
  93. * result in increased performance without the need to
  94. * alter the code in any way. It's basically a freebee.
  95. */
  96. $xml = simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT);
  97. unset($buffer);
  98. /**
  99. * The XML was malformed
  100. */
  101. if ($xml === false) {
  102. PMA_Message::error(
  103. __(
  104. 'The XML file specified was either malformed or incomplete.'
  105. . ' Please correct the issue and try again.'
  106. )
  107. )->display();
  108. unset($xml);
  109. $GLOBALS['finished'] = false;
  110. return;
  111. }
  112. /**
  113. * Table accumulator
  114. */
  115. $tables = array();
  116. /**
  117. * Row accumulator
  118. */
  119. $rows = array();
  120. /**
  121. * Temp arrays
  122. */
  123. $tempRow = array();
  124. $tempCells = array();
  125. /**
  126. * CREATE code included (by default: no)
  127. */
  128. $struct_present = false;
  129. /**
  130. * Analyze the data in each table
  131. */
  132. $namespaces = $xml->getNameSpaces(true);
  133. /**
  134. * Get the database name, collation and charset
  135. */
  136. $db_attr = $xml->children($namespaces['pma'])
  137. ->{'structure_schemas'}->{'database'};
  138. if ($db_attr instanceof SimpleXMLElement) {
  139. $db_attr = $db_attr->attributes();
  140. $db_name = (string)$db_attr['name'];
  141. $collation = (string)$db_attr['collation'];
  142. $charset = (string)$db_attr['charset'];
  143. } else {
  144. /**
  145. * If the structure section is not present
  146. * get the database name from the data section
  147. */
  148. $db_attr = $xml->children()->attributes();
  149. $db_name = (string)$db_attr['name'];
  150. $collation = null;
  151. $charset = null;
  152. }
  153. /**
  154. * The XML was malformed
  155. */
  156. if ($db_name === null) {
  157. PMA_Message::error(
  158. __(
  159. 'The XML file specified was either malformed or incomplete.'
  160. . ' Please correct the issue and try again.'
  161. )
  162. )->display();
  163. unset($xml);
  164. $GLOBALS['finished'] = false;
  165. return;
  166. }
  167. /**
  168. * Retrieve the structure information
  169. */
  170. if (isset($namespaces['pma'])) {
  171. /**
  172. * Get structures for all tables
  173. * @var SimpleXMLElement $struct
  174. */
  175. $struct = $xml->children($namespaces['pma']);
  176. $create = array();
  177. /** @var SimpleXMLElement $val1 */
  178. foreach ($struct as $val1) {
  179. /** @var SimpleXMLElement $val2 */
  180. foreach ($val1 as $val2) {
  181. // Need to select the correct database for the creation of
  182. // tables, views, triggers, etc.
  183. /**
  184. * @todo Generating a USE here blocks importing of a table
  185. * into another database.
  186. */
  187. $attrs = $val2->attributes();
  188. $create[] = "USE "
  189. . PMA_Util::backquote(
  190. $attrs["name"]
  191. );
  192. foreach ($val2 as $val3) {
  193. /**
  194. * Remove the extra cosmetic spacing
  195. */
  196. $val3 = str_replace(" ", "", (string)$val3);
  197. $create[] = $val3;
  198. }
  199. }
  200. }
  201. $struct_present = true;
  202. }
  203. /**
  204. * Move down the XML tree to the actual data
  205. */
  206. $xml = $xml->children()->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][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][TBL_NAME], $rows[$j][TBL_NAME])) {
  251. if (! isset($tables[$i][COL_NAMES])) {
  252. $tables[$i][] = $rows[$j][COL_NAMES];
  253. }
  254. $tables[$i][ROWS][] = $rows[$j][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[] = PMA_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 PMA_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. PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
  315. unset($analyses);
  316. unset($tables);
  317. unset($create);
  318. /* Commit any possible data in buffers */
  319. PMA_importRunQuery();
  320. }
  321. }