ImportLdi.php 5.2 KB

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