Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugins / import / ImportLdi.class.php
blobe38def9c87edd8dfeec057f8d45431ab923bc363
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * CSV import plugin for phpMyAdmin using LOAD DATA
6 * @package PhpMyAdmin-Import
7 * @subpackage LDI
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
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;
19 return;
22 /**
23 * Handles the import for the CSV format using load data
25 * @package PhpMyAdmin-Import
26 * @subpackage LDI
28 class ImportLdi extends AbstractImportCsv
30 /**
31 * Constructor
33 public function __construct()
35 $this->setProperties();
38 /**
39 * Sets the import plugin properties.
40 * Called in the constructor.
42 * @return void
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);
57 unset($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);
80 /**
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
85 * of an update.
87 * @return void
89 public function update (SplSubject $subject)
93 /**
94 * Handles the whole import logic
96 * @return void
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!')
112 $error = true;
113 return;
116 $sql = 'LOAD DATA';
117 if (isset($ldi_local_option)) {
118 $sql .= ' LOCAL';
120 $sql .= ' INFILE \'' . PMA_Util::sqlAddSlashes($import_file) . '\'';
121 if (isset($ldi_replace)) {
122 $sql .= ' REPLACE';
123 } elseif (isset($ldi_ignore)) {
124 $sql .= ' 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') {
141 $ldi_new_line
142 = (PMA_Util::whichCrlf() == "\n")
143 ? '\n'
144 : '\r\n';
146 $sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\'';
148 if ($skip_queries > 0) {
149 $sql .= ' IGNORE ' . $skip_queries . ' LINES';
150 $skip_queries = 0;
152 if (strlen($ldi_columns) > 0) {
153 $sql .= ' (';
154 $tmp = preg_split('/,( ?)/', $ldi_columns);
155 $cnt_tmp = count($tmp);
156 for ($i = 0; $i < $cnt_tmp; $i++) {
157 if ($i > 0) {
158 $sql .= ', ';
160 /* Trim also `, if user already included backquoted fields */
161 $sql .= PMA_Util::backquote(
162 trim($tmp[$i], " \t\r\n\0\x0B`")
164 } // end for
165 $sql .= ')';
168 PMA_importRunQuery($sql, $sql);
169 PMA_importRunQuery();
170 $finished = true;