2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * CSV import plugin for phpMyAdmin using LOAD DATA
6 * @package PhpMyAdmin-Import
9 if (! defined('PHPMYADMIN')) {
13 /* Get the import interface */
14 require_once 'libraries/plugins/import/AbstractImportCsv.class.php';
16 // We need relations enabled and we work only on database
17 if ($GLOBALS['plugin_param'] !== 'table') {
18 $GLOBALS['skip_import'] = true;
23 * Handles the import for the CSV format using load data
25 * @package PhpMyAdmin-Import
28 class ImportLdi
extends AbstractImportCsv
33 public function __construct()
35 $this->setProperties();
39 * Sets the import plugin properties.
40 * Called in the constructor.
44 protected function setProperties()
46 if ($GLOBALS['cfg']['Import']['ldi_local_option'] == 'auto') {
47 $GLOBALS['cfg']['Import']['ldi_local_option'] = false;
49 $result = PMA_DBI_try_query('SHOW VARIABLES LIKE \'local\\_infile\';');
50 if ($result != false && PMA_DBI_num_rows($result) > 0) {
51 $tmp = PMA_DBI_fetch_row($result);
52 if ($tmp[1] == 'ON') {
53 $GLOBALS['cfg']['Import']['ldi_local_option'] = true;
56 PMA_DBI_free_result($result);
60 $generalOptions = parent
::setProperties();
61 $this->properties
->setText('CSV using LOAD DATA');
62 $this->properties
->setExtension('ldi');
64 $leaf = new TextPropertyItem();
65 $leaf->setName("columns");
66 $leaf->setText(__('Column names: '));
67 $generalOptions->addProperty($leaf);
69 $leaf = new BoolPropertyItem();
70 $leaf->setName("ignore");
71 $leaf->setText(__('Do not abort on INSERT error'));
72 $generalOptions->addProperty($leaf);
74 $leaf = new BoolPropertyItem();
75 $leaf->setName("local_option");
76 $leaf->setText(__('Use LOCAL keyword'));
77 $generalOptions->addProperty($leaf);
81 * This method is called when any PluginManager to which the observer
82 * is attached calls PluginManager::notify()
84 * @param SplSubject $subject The PluginManager notifying the observer
89 public function update (SplSubject
$subject)
94 * Handles the whole import logic
98 public function doImport()
100 global $finished, $error, $import_file, $compression, $charset_conversion, $table;
101 global $ldi_local_option, $ldi_replace, $ldi_ignore, $ldi_terminated, $ldi_enclosed,
102 $ldi_escaped, $ldi_new_line, $skip_queries, $ldi_columns;
104 if ($import_file == 'none'
105 ||
$compression != 'none'
106 ||
$charset_conversion
108 // We handle only some kind of data!
109 $message = PMA_Message
::error(
110 __('This plugin does not support compressed imports!')
117 if (isset($ldi_local_option)) {
120 $sql .= ' INFILE \'' . PMA_Util
::sqlAddSlashes($import_file) . '\'';
121 if (isset($ldi_replace)) {
123 } elseif (isset($ldi_ignore)) {
126 $sql .= ' INTO TABLE ' . PMA_Util
::backquote($table);
128 if (strlen($ldi_terminated) > 0) {
129 $sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\'';
131 if (strlen($ldi_enclosed) > 0) {
132 $sql .= ' ENCLOSED BY \''
133 . PMA_Util
::sqlAddSlashes($ldi_enclosed) . '\'';
135 if (strlen($ldi_escaped) > 0) {
136 $sql .= ' ESCAPED BY \''
137 . PMA_Util
::sqlAddSlashes($ldi_escaped) . '\'';
139 if (strlen($ldi_new_line) > 0) {
140 if ($ldi_new_line == 'auto') {
142 = (PMA_Util
::whichCrlf() == "\n")
146 $sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\'';
148 if ($skip_queries > 0) {
149 $sql .= ' IGNORE ' . $skip_queries . ' LINES';
152 if (strlen($ldi_columns) > 0) {
154 $tmp = preg_split('/,( ?)/', $ldi_columns);
155 $cnt_tmp = count($tmp);
156 for ($i = 0; $i < $cnt_tmp; $i++
) {
160 /* Trim also `, if user already included backquoted fields */
161 $sql .= PMA_Util
::backquote(
162 trim($tmp[$i], " \t\r\n\0\x0B`")
168 PMA_importRunQuery($sql, $sql);
169 PMA_importRunQuery();