/libraries/plugins/import/ImportLdi.php

https://gitlab.com/trungthao379/phpmyadmin · PHP · 175 lines · 125 code · 18 blank · 32 comment · 27 complexity · 577348bcee7636f3551b49b4f7d08ee9 MD5 · raw file

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * CSV import plugin for phpMyAdmin using LOAD DATA
  5. *
  6. * @package PhpMyAdmin-Import
  7. * @subpackage LDI
  8. */
  9. namespace PMA\libraries\plugins\import;
  10. use PMA\libraries\properties\options\items\BoolPropertyItem;
  11. use PMA;
  12. use PMA\libraries\plugins\import\AbstractImportCsv;
  13. use PMA\libraries\properties\options\items\TextPropertyItem;
  14. if (!defined('PHPMYADMIN')) {
  15. exit;
  16. }
  17. // We need relations enabled and we work only on database
  18. if ($GLOBALS['plugin_param'] !== 'table') {
  19. $GLOBALS['skip_import'] = true;
  20. return;
  21. }
  22. /**
  23. * Handles the import for the CSV format using load data
  24. *
  25. * @package PhpMyAdmin-Import
  26. * @subpackage LDI
  27. */
  28. class ImportLdi extends AbstractImportCsv
  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. if ($GLOBALS['cfg']['Import']['ldi_local_option'] == 'auto') {
  46. $GLOBALS['cfg']['Import']['ldi_local_option'] = false;
  47. $result = $GLOBALS['dbi']->tryQuery(
  48. 'SELECT @@local_infile;'
  49. );
  50. if ($result != false && $GLOBALS['dbi']->numRows($result) > 0) {
  51. $tmp = $GLOBALS['dbi']->fetchRow($result);
  52. if ($tmp[0] == 'ON') {
  53. $GLOBALS['cfg']['Import']['ldi_local_option'] = true;
  54. }
  55. }
  56. $GLOBALS['dbi']->freeResult($result);
  57. unset($result);
  58. }
  59. $generalOptions = parent::setProperties();
  60. $this->properties->setText('CSV using LOAD DATA');
  61. $this->properties->setExtension('ldi');
  62. $leaf = new TextPropertyItem(
  63. "columns",
  64. __('Column names: ')
  65. );
  66. $generalOptions->addProperty($leaf);
  67. $leaf = new BoolPropertyItem(
  68. "ignore",
  69. __('Do not abort on INSERT error')
  70. );
  71. $generalOptions->addProperty($leaf);
  72. $leaf = new BoolPropertyItem(
  73. "local_option",
  74. __('Use LOCAL keyword')
  75. );
  76. $generalOptions->addProperty($leaf);
  77. }
  78. /**
  79. * Handles the whole import logic
  80. *
  81. * @param array &$sql_data 2-element array with sql data
  82. *
  83. * @return void
  84. */
  85. public function doImport(&$sql_data = array())
  86. {
  87. global $finished, $import_file, $charset_conversion, $table;
  88. global $ldi_local_option, $ldi_replace, $ldi_ignore, $ldi_terminated,
  89. $ldi_enclosed, $ldi_escaped, $ldi_new_line, $skip_queries, $ldi_columns;
  90. $compression = $GLOBALS['import_handle']->getCompression();
  91. if ($import_file == 'none'
  92. || $compression != 'none'
  93. || $charset_conversion
  94. ) {
  95. // We handle only some kind of data!
  96. $GLOBALS['message'] = PMA\libraries\Message::error(
  97. __('This plugin does not support compressed imports!')
  98. );
  99. $GLOBALS['error'] = true;
  100. return;
  101. }
  102. $sql = 'LOAD DATA';
  103. if (isset($ldi_local_option)) {
  104. $sql .= ' LOCAL';
  105. }
  106. $sql .= ' INFILE \'' . PMA\libraries\Util::sqlAddSlashes($import_file)
  107. . '\'';
  108. if (isset($ldi_replace)) {
  109. $sql .= ' REPLACE';
  110. } elseif (isset($ldi_ignore)) {
  111. $sql .= ' IGNORE';
  112. }
  113. $sql .= ' INTO TABLE ' . PMA\libraries\Util::backquote($table);
  114. if (strlen($ldi_terminated) > 0) {
  115. $sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\'';
  116. }
  117. if (strlen($ldi_enclosed) > 0) {
  118. $sql .= ' ENCLOSED BY \''
  119. . PMA\libraries\Util::sqlAddSlashes($ldi_enclosed) . '\'';
  120. }
  121. if (strlen($ldi_escaped) > 0) {
  122. $sql .= ' ESCAPED BY \''
  123. . PMA\libraries\Util::sqlAddSlashes($ldi_escaped) . '\'';
  124. }
  125. if (strlen($ldi_new_line) > 0) {
  126. if ($ldi_new_line == 'auto') {
  127. $ldi_new_line
  128. = (PMA\libraries\Util::whichCrlf() == "\n")
  129. ? '\n'
  130. : '\r\n';
  131. }
  132. $sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\'';
  133. }
  134. if ($skip_queries > 0) {
  135. $sql .= ' IGNORE ' . $skip_queries . ' LINES';
  136. $skip_queries = 0;
  137. }
  138. if (strlen($ldi_columns) > 0) {
  139. $sql .= ' (';
  140. $tmp = preg_split('/,( ?)/', $ldi_columns);
  141. $cnt_tmp = count($tmp);
  142. for ($i = 0; $i < $cnt_tmp; $i++) {
  143. if ($i > 0) {
  144. $sql .= ', ';
  145. }
  146. /* Trim also `, if user already included backquoted fields */
  147. $sql .= PMA\libraries\Util::backquote(
  148. trim($tmp[$i], " \t\r\n\0\x0B`")
  149. );
  150. } // end for
  151. $sql .= ')';
  152. }
  153. PMA_importRunQuery($sql, $sql, $sql_data);
  154. PMA_importRunQuery('', '', $sql_data);
  155. $finished = true;
  156. }
  157. }