ImportLdi.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 PhpMyAdmin\Plugins\Import;
  10. use PhpMyAdmin\Import;
  11. use PhpMyAdmin\Message;
  12. use PhpMyAdmin\Plugins\Import\AbstractImportCsv;
  13. use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
  14. use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
  15. use PhpMyAdmin\Util;
  16. if (!defined('PHPMYADMIN')) {
  17. exit;
  18. }
  19. // We need relations enabled and we work only on database
  20. if ($GLOBALS['plugin_param'] !== 'table') {
  21. $GLOBALS['skip_import'] = true;
  22. return;
  23. }
  24. /**
  25. * Handles the import for the CSV format using load data
  26. *
  27. * @package PhpMyAdmin-Import
  28. * @subpackage LDI
  29. */
  30. class ImportLdi extends AbstractImportCsv
  31. {
  32. /**
  33. * Constructor
  34. */
  35. public function __construct()
  36. {
  37. $this->setProperties();
  38. }
  39. /**
  40. * Sets the import plugin properties.
  41. * Called in the constructor.
  42. *
  43. * @return void
  44. */
  45. protected function setProperties()
  46. {
  47. if ($GLOBALS['cfg']['Import']['ldi_local_option'] == 'auto') {
  48. $GLOBALS['cfg']['Import']['ldi_local_option'] = false;
  49. $result = $GLOBALS['dbi']->tryQuery(
  50. 'SELECT @@local_infile;'
  51. );
  52. if ($result != false && $GLOBALS['dbi']->numRows($result) > 0) {
  53. $tmp = $GLOBALS['dbi']->fetchRow($result);
  54. if ($tmp[0] == 'ON') {
  55. $GLOBALS['cfg']['Import']['ldi_local_option'] = true;
  56. }
  57. }
  58. $GLOBALS['dbi']->freeResult($result);
  59. unset($result);
  60. }
  61. $generalOptions = parent::setProperties();
  62. $this->properties->setText('CSV using LOAD DATA');
  63. $this->properties->setExtension('ldi');
  64. $leaf = new TextPropertyItem(
  65. "columns",
  66. __('Column names: ')
  67. );
  68. $generalOptions->addProperty($leaf);
  69. $leaf = new BoolPropertyItem(
  70. "ignore",
  71. __('Do not abort on INSERT error')
  72. );
  73. $generalOptions->addProperty($leaf);
  74. $leaf = new BoolPropertyItem(
  75. "local_option",
  76. __('Use LOCAL keyword')
  77. );
  78. $generalOptions->addProperty($leaf);
  79. }
  80. /**
  81. * Handles the whole import logic
  82. *
  83. * @param array &$sql_data 2-element array with sql data
  84. *
  85. * @return void
  86. */
  87. public function doImport(array &$sql_data = array())
  88. {
  89. global $finished, $import_file, $charset_conversion, $table;
  90. global $ldi_local_option, $ldi_replace, $ldi_ignore, $ldi_terminated,
  91. $ldi_enclosed, $ldi_escaped, $ldi_new_line, $skip_queries, $ldi_columns;
  92. $compression = $GLOBALS['import_handle']->getCompression();
  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 \'' . $GLOBALS['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($ldi_terminated) > 0) {
  117. $sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\'';
  118. }
  119. if (strlen($ldi_enclosed) > 0) {
  120. $sql .= ' ENCLOSED BY \''
  121. . $GLOBALS['dbi']->escapeString($ldi_enclosed) . '\'';
  122. }
  123. if (strlen($ldi_escaped) > 0) {
  124. $sql .= ' ESCAPED BY \''
  125. . $GLOBALS['dbi']->escapeString($ldi_escaped) . '\'';
  126. }
  127. if (strlen($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($ldi_columns) > 0) {
  141. $sql .= ' (';
  142. $tmp = preg_split('/,( ?)/', $ldi_columns);
  143. $cnt_tmp = count($tmp);
  144. for ($i = 0; $i < $cnt_tmp; $i++) {
  145. if ($i > 0) {
  146. $sql .= ', ';
  147. }
  148. /* Trim also `, if user already included backquoted fields */
  149. $sql .= Util::backquote(
  150. trim($tmp[$i], " \t\r\n\0\x0B`")
  151. );
  152. } // end for
  153. $sql .= ')';
  154. }
  155. Import::runQuery($sql, $sql, $sql_data);
  156. Import::runQuery('', '', $sql_data);
  157. $finished = true;
  158. }
  159. }