ImportLdi.php 5.2 KB

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