ImportLdi.php 5.9 KB

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