/phpMyAdmin/libraries/plugins/import/ImportLdi.class.php

https://bitbucket.org/subhan_12/mwi-panel · PHP · 187 lines · 123 code · 17 blank · 47 comment · 27 complexity · 60d745aef50f6ff36346ca6aac2b1f98 MD5 · raw file

  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/ImportPlugin.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 ImportPlugin
  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. $props = 'libraries/properties/';
  55. include_once "$props/plugins/ImportPluginProperties.class.php";
  56. include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
  57. include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
  58. include_once "$props/options/items/BoolPropertyItem.class.php";
  59. include_once "$props/options/items/TextPropertyItem.class.php";
  60. $importPluginProperties = new ImportPluginProperties();
  61. $importPluginProperties->setText('CSV using LOAD DATA');
  62. $importPluginProperties->setExtension('ldi');
  63. $importPluginProperties->setOptionsText(__('Options'));
  64. // create the root group that will be the options field for
  65. // $importPluginProperties
  66. // this will be shown as "Format specific options"
  67. $importSpecificOptions = new OptionsPropertyRootGroup();
  68. $importSpecificOptions->setName("Format Specific Options");
  69. // general options main group
  70. $generalOptions = new OptionsPropertyMainGroup();
  71. $generalOptions->setName("general_opts");
  72. // create primary items and add them to the group
  73. $leaf = new BoolPropertyItem();
  74. $leaf->setName("replace");
  75. $leaf->setText(__('Replace table data with file'));
  76. $generalOptions->addProperty($leaf);
  77. // add the main group to the root group
  78. $importSpecificOptions->addProperty($generalOptions);
  79. // set the options for the import plugin property item
  80. $importPluginProperties->setOptions($importSpecificOptions);
  81. $this->properties = $importPluginProperties;
  82. }
  83. /**
  84. * This method is called when any PluginManager to which the observer
  85. * is attached calls PluginManager::notify()
  86. *
  87. * @param SplSubject $subject The PluginManager notifying the observer
  88. * of an update.
  89. *
  90. * @return void
  91. */
  92. public function update (SplSubject $subject)
  93. {
  94. }
  95. /**
  96. * Handles the whole import logic
  97. *
  98. * @return void
  99. */
  100. public function doImport()
  101. {
  102. global $finished, $error, $import_file, $compression, $charset_conversion;
  103. global $ldi_local_option, $ldi_replace, $ldi_terminated, $ldi_enclosed,
  104. $ldi_escaped, $ldi_new_line, $skip_queries, $ldi_columns;
  105. if ($import_file == 'none'
  106. || $compression != 'none'
  107. || $charset_conversion
  108. ) {
  109. // We handle only some kind of data!
  110. $message = PMA_Message::error(
  111. __('This plugin does not support compressed imports!')
  112. );
  113. $error = true;
  114. return;
  115. }
  116. $sql = 'LOAD DATA';
  117. if (isset($ldi_local_option)) {
  118. $sql .= ' LOCAL';
  119. }
  120. $sql .= ' INFILE \'' . PMA_Util::sqlAddSlashes($import_file) . '\'';
  121. if (isset($ldi_replace)) {
  122. $sql .= ' REPLACE';
  123. } elseif (isset($ldi_ignore)) {
  124. $sql .= ' IGNORE';
  125. }
  126. $sql .= ' INTO TABLE ' . PMA_Util::backquote($table);
  127. if (strlen($ldi_terminated) > 0) {
  128. $sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\'';
  129. }
  130. if (strlen($ldi_enclosed) > 0) {
  131. $sql .= ' ENCLOSED BY \''
  132. . PMA_Util::sqlAddSlashes($ldi_enclosed) . '\'';
  133. }
  134. if (strlen($ldi_escaped) > 0) {
  135. $sql .= ' ESCAPED BY \''
  136. . PMA_Util::sqlAddSlashes($ldi_escaped) . '\'';
  137. }
  138. if (strlen($ldi_new_line) > 0) {
  139. if ($ldi_new_line == 'auto') {
  140. $ldi_new_line
  141. = (PMA_Util::whichCrlf() == "\n")
  142. ? '\n'
  143. : '\r\n';
  144. }
  145. $sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\'';
  146. }
  147. if ($skip_queries > 0) {
  148. $sql .= ' IGNORE ' . $skip_queries . ' LINES';
  149. $skip_queries = 0;
  150. }
  151. if (strlen($ldi_columns) > 0) {
  152. $sql .= ' (';
  153. $tmp = preg_split('/,( ?)/', $ldi_columns);
  154. $cnt_tmp = count($tmp);
  155. for ($i = 0; $i < $cnt_tmp; $i++) {
  156. if ($i > 0) {
  157. $sql .= ', ';
  158. }
  159. /* Trim also `, if user already included backquoted fields */
  160. $sql .= PMA_Util::backquote(
  161. trim($tmp[$i], " \t\r\n\0\x0B`")
  162. );
  163. } // end for
  164. $sql .= ')';
  165. }
  166. PMA_importRunQuery($sql, $sql);
  167. PMA_importRunQuery();
  168. $finished = true;
  169. }
  170. }