ImportXml.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. * This method is called when any PluginManager to which the observer
  57. * is attached calls PluginManager::notify()
  58. *
  59. * @param SplSubject $subject The PluginManager notifying the observer
  60. * of an update.
  61. *
  62. * @return void
  63. */
  64. public function update (SplSubject $subject)
  65. {
  66. }
  67. /**
  68. * Handles the whole import logic
  69. *
  70. * @return void
  71. */
  72. public function doImport()
  73. {
  74. global $error, $timeout_passed, $finished, $db;
  75. $i = 0;
  76. $len = 0;
  77. $buffer = "";
  78. /**
  79. * Read in the file via PMA_importGetNextChunk so that
  80. * it can process compressed files
  81. */
  82. while (! ($finished && $i >= $len) && ! $error && ! $timeout_passed) {
  83. $data = PMA_importGetNextChunk();
  84. if ($data === false) {
  85. /* subtract data we didn't handle yet and stop processing */
  86. $offset -= strlen($buffer);
  87. break;
  88. } elseif ($data === true) {
  89. /* Handle rest of buffer */
  90. } else {
  91. /* Append new data to buffer */
  92. $buffer .= $data;
  93. unset($data);
  94. }
  95. }
  96. unset($data);
  97. /**
  98. * Disable loading of external XML entities.
  99. */
  100. libxml_disable_entity_loader();
  101. /**
  102. * Load the XML string
  103. *
  104. * The option LIBXML_COMPACT is specified because it can
  105. * result in increased performance without the need to
  106. * alter the code in any way. It's basically a freebee.
  107. */
  108. $xml = simplexml_load_string($buffer, "SimpleXMLElement", LIBXML_COMPACT);
  109. unset($buffer);
  110. /**
  111. * The XML was malformed
  112. */
  113. if ($xml === false) {
  114. PMA_Message::error(
  115. __(
  116. 'The XML file specified was either malformed or incomplete.'
  117. . ' Please correct the issue and try again.'
  118. )
  119. )->display();
  120. unset($xml);
  121. $GLOBALS['finished'] = false;
  122. return;
  123. }
  124. /**
  125. * Table accumulator
  126. */
  127. $tables = array();
  128. /**
  129. * Row accumulator
  130. */
  131. $rows = array();
  132. /**
  133. * Temp arrays
  134. */
  135. $tempRow = array();
  136. $tempCells = array();
  137. /**
  138. * CREATE code included (by default: no)
  139. */
  140. $struct_present = false;
  141. /**
  142. * Analyze the data in each table
  143. */
  144. $namespaces = $xml->getNameSpaces(true);
  145. /**
  146. * Get the database name, collation and charset
  147. */
  148. $db_attr = $xml->children($namespaces['pma'])
  149. ->{'structure_schemas'}->{'database'};
  150. if ($db_attr instanceof SimpleXMLElement) {
  151. $db_attr = $db_attr->attributes();
  152. $db_name = (string)$db_attr['name'];
  153. $collation = (string)$db_attr['collation'];
  154. $charset = (string)$db_attr['charset'];
  155. } else {
  156. /**
  157. * If the structure section is not present
  158. * get the database name from the data section
  159. */
  160. $db_attr = $xml->children()->attributes();
  161. $db_name = (string)$db_attr['name'];
  162. $collation = null;
  163. $charset = null;
  164. }
  165. /**
  166. * The XML was malformed
  167. */
  168. if ($db_name === null) {
  169. PMA_Message::error(
  170. __(
  171. 'The XML file specified was either malformed or incomplete.'
  172. . ' Please correct the issue and try again.'
  173. )
  174. )->display();
  175. unset($xml);
  176. $GLOBALS['finished'] = false;
  177. return;
  178. }
  179. /**
  180. * Retrieve the structure information
  181. */
  182. if (isset($namespaces['pma'])) {
  183. /**
  184. * Get structures for all tables
  185. */
  186. $struct = $xml->children($namespaces['pma']);
  187. $create = array();
  188. foreach ($struct as $tier1 => $val1) {
  189. foreach ($val1 as $tier2 => $val2) {
  190. // Need to select the correct database for the creation of
  191. // tables, views, triggers, etc.
  192. /**
  193. * @todo Generating a USE here blocks importing of a table
  194. * into another database.
  195. */
  196. $attrs = $val2->attributes();
  197. $create[] = "USE "
  198. . PMA_Util::backquote(
  199. $attrs["name"]
  200. );
  201. foreach ($val2 as $val3) {
  202. /**
  203. * Remove the extra cosmetic spacing
  204. */
  205. $val3 = str_replace(" ", "", (string)$val3);
  206. $create[] = $val3;
  207. }
  208. }
  209. }
  210. $struct_present = true;
  211. }
  212. /**
  213. * Move down the XML tree to the actual data
  214. */
  215. $xml = $xml->children()->children();
  216. $data_present = false;
  217. /**
  218. * Only attempt to analyze/collect data if there is data present
  219. */
  220. if ($xml && @count($xml->children())) {
  221. $data_present = true;
  222. /**
  223. * Process all database content
  224. */
  225. foreach ($xml as $k1 => $v1) {
  226. $tbl_attr = $v1->attributes();
  227. $isInTables = false;
  228. for ($i = 0; $i < count($tables); ++$i) {
  229. if (! strcmp($tables[$i][TBL_NAME], (string)$tbl_attr['name'])) {
  230. $isInTables = true;
  231. break;
  232. }
  233. }
  234. if ($isInTables == false) {
  235. $tables[] = array((string)$tbl_attr['name']);
  236. }
  237. foreach ($v1 as $k2 => $v2) {
  238. $row_attr = $v2->attributes();
  239. if (! array_search((string)$row_attr['name'], $tempRow)) {
  240. $tempRow[] = (string)$row_attr['name'];
  241. }
  242. $tempCells[] = (string)$v2;
  243. }
  244. $rows[] = array((string)$tbl_attr['name'], $tempRow, $tempCells);
  245. $tempRow = array();
  246. $tempCells = array();
  247. }
  248. unset($tempRow);
  249. unset($tempCells);
  250. unset($xml);
  251. /**
  252. * Bring accumulated rows into the corresponding table
  253. */
  254. $num_tbls = count($tables);
  255. for ($i = 0; $i < $num_tbls; ++$i) {
  256. for ($j = 0; $j < count($rows); ++$j) {
  257. if (! strcmp($tables[$i][TBL_NAME], $rows[$j][TBL_NAME])) {
  258. if (! isset($tables[$i][COL_NAMES])) {
  259. $tables[$i][] = $rows[$j][COL_NAMES];
  260. }
  261. $tables[$i][ROWS][] = $rows[$j][ROWS];
  262. }
  263. }
  264. }
  265. unset($rows);
  266. if (! $struct_present) {
  267. $analyses = array();
  268. $len = count($tables);
  269. for ($i = 0; $i < $len; ++$i) {
  270. $analyses[] = PMA_analyzeTable($tables[$i]);
  271. }
  272. }
  273. }
  274. unset($xml);
  275. unset($tempRows);
  276. unset($tempCells);
  277. unset($rows);
  278. /**
  279. * Only build SQL from data if there is data present
  280. */
  281. if ($data_present) {
  282. /**
  283. * Set values to NULL if they were not present
  284. * to maintain PMA_buildSQL() call integrity
  285. */
  286. if (! isset($analyses)) {
  287. $analyses = null;
  288. if (! $struct_present) {
  289. $create = null;
  290. }
  291. }
  292. }
  293. /**
  294. * string $db_name (no backquotes)
  295. *
  296. * array $table = array(table_name, array() column_names, array()() rows)
  297. * array $tables = array of "$table"s
  298. *
  299. * array $analysis = array(array() column_types, array() column_sizes)
  300. * array $analyses = array of "$analysis"s
  301. *
  302. * array $create = array of SQL strings
  303. *
  304. * array $options = an associative array of options
  305. */
  306. /* Set database name to the currently selected one, if applicable */
  307. if (strlen($db)) {
  308. /* Override the database name in the XML file, if one is selected */
  309. $db_name = $db;
  310. $options = array('create_db' => false);
  311. } else {
  312. if ($db_name === null) {
  313. $db_name = 'XML_DB';
  314. }
  315. /* Set database collation/charset */
  316. $options = array(
  317. 'db_collation' => $collation,
  318. 'db_charset' => $charset,
  319. );
  320. }
  321. /* Created and execute necessary SQL statements from data */
  322. PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
  323. unset($analyses);
  324. unset($tables);
  325. unset($create);
  326. /* Commit any possible data in buffers */
  327. PMA_importRunQuery();
  328. }
  329. }