ImportLdi.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. if (! defined('PHPMYADMIN')) {
  10. exit;
  11. }
  12. /* Get the import interface */
  13. require_once 'libraries/plugins/import/AbstractImportCsv.class.php';
  14. // We need relations enabled and we work only on database
  15. if ($GLOBALS['plugin_param'] !== 'table') {
  16. $GLOBALS['skip_import'] = true;
  17. return;
  18. }
  19. /**
  20. * Handles the import for the CSV format using load data
  21. *
  22. * @package PhpMyAdmin-Import
  23. * @subpackage LDI
  24. */
  25. class ImportLdi extends AbstractImportCsv
  26. {
  27. /**
  28. * Constructor
  29. */
  30. public function __construct()
  31. {
  32. $this->setProperties();
  33. }
  34. /**
  35. * Sets the import plugin properties.
  36. * Called in the constructor.
  37. *
  38. * @return void
  39. */
  40. protected function setProperties()
  41. {
  42. if ($GLOBALS['cfg']['Import']['ldi_local_option'] == 'auto') {
  43. $GLOBALS['cfg']['Import']['ldi_local_option'] = false;
  44. $result = PMA_DBI_try_query('SHOW VARIABLES LIKE \'local\\_infile\';');
  45. if ($result != false && PMA_DBI_num_rows($result) > 0) {
  46. $tmp = PMA_DBI_fetch_row($result);
  47. if ($tmp[1] == 'ON') {
  48. $GLOBALS['cfg']['Import']['ldi_local_option'] = true;
  49. }
  50. }
  51. PMA_DBI_free_result($result);
  52. unset($result);
  53. }
  54. $generalOptions = parent::setProperties();
  55. $this->properties->setText('CSV using LOAD DATA');
  56. $this->properties->setExtension('ldi');
  57. $leaf = new TextPropertyItem();
  58. $leaf->setName("columns");
  59. $leaf->setText(__('Column names: '));
  60. $generalOptions->addProperty($leaf);
  61. $leaf = new BoolPropertyItem();
  62. $leaf->setName("ignore");
  63. $leaf->setText(__('Do not abort on INSERT error'));
  64. $generalOptions->addProperty($leaf);
  65. $leaf = new BoolPropertyItem();
  66. $leaf->setName("local_option");
  67. $leaf->setText(__('Use LOCAL keyword'));
  68. $generalOptions->addProperty($leaf);
  69. }
  70. /**
  71. * This method is called when any PluginManager to which the observer
  72. * is attached calls PluginManager::notify()
  73. *
  74. * @param SplSubject $subject The PluginManager notifying the observer
  75. * of an update.
  76. *
  77. * @return void
  78. */
  79. public function update (SplSubject $subject)
  80. {
  81. }
  82. /**
  83. * Handles the whole import logic
  84. *
  85. * @return void
  86. */
  87. public function doImport()
  88. {
  89. global $finished, $error, $import_file, $compression, $charset_conversion, $table;
  90. global $ldi_local_option, $ldi_replace, $ldi_ignore, $ldi_terminated, $ldi_enclosed,
  91. $ldi_escaped, $ldi_new_line, $skip_queries, $ldi_columns;
  92. if ($import_file == 'none'
  93. || $compression != 'none'
  94. || $charset_conversion
  95. ) {
  96. // We handle only some kind of data!
  97. $message = PMA_Message::error(
  98. __('This plugin does not support compressed imports!')
  99. );
  100. $error = true;
  101. return;
  102. }
  103. $sql = 'LOAD DATA';
  104. if (isset($ldi_local_option)) {
  105. $sql .= ' LOCAL';
  106. }
  107. $sql .= ' INFILE \'' . PMA_Util::sqlAddSlashes($import_file) . '\'';
  108. if (isset($ldi_replace)) {
  109. $sql .= ' REPLACE';
  110. } elseif (isset($ldi_ignore)) {
  111. $sql .= ' IGNORE';
  112. }
  113. $sql .= ' INTO TABLE ' . PMA_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_Util::sqlAddSlashes($ldi_enclosed) . '\'';
  120. }
  121. if (strlen($ldi_escaped) > 0) {
  122. $sql .= ' ESCAPED BY \''
  123. . PMA_Util::sqlAddSlashes($ldi_escaped) . '\'';
  124. }
  125. if (strlen($ldi_new_line) > 0) {
  126. if ($ldi_new_line == 'auto') {
  127. $ldi_new_line
  128. = (PMA_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_Util::backquote(
  148. trim($tmp[$i], " \t\r\n\0\x0B`")
  149. );
  150. } // end for
  151. $sql .= ')';
  152. }
  153. PMA_importRunQuery($sql, $sql);
  154. PMA_importRunQuery();
  155. $finished = true;
  156. }
  157. }