2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * CSV import plugin for phpMyAdmin
6 * @todo add an option for handling NULL values
7 * @package phpMyAdmin-Import
9 if (! defined('PHPMYADMIN')) {
15 if ($plugin_param !== 'table') {
19 if (isset($plugin_list)) {
20 $plugin_list['csv'] = array(
24 array('type' => 'begin_group', 'name' => 'general_opts'),
25 array('type' => 'bool', 'name' => 'replace', 'text' => __('Replace table data with file')),
26 array('type' => 'bool', 'name' => 'ignore', 'text' => __('Do not abort on INSERT error')),
27 array('type' => 'text', 'name' => 'terminated', 'text' => __('Columns separated with:'), 'size' => 2, 'len' => 2),
28 array('type' => 'text', 'name' => 'enclosed', 'text' => __('Columns enclosed with:'), 'size' => 2, 'len' => 2),
29 array('type' => 'text', 'name' => 'escaped', 'text' => __('Columns escaped with:'), 'size' => 2, 'len' => 2),
30 array('type' => 'text', 'name' => 'new_line', 'text' => __('Lines terminated with:'), 'size' => 2),
32 'options_text' => __('Options'),
35 if ($plugin_param !== 'table') {
36 $plugin_list['csv']['options'][] =
37 array('type' => 'bool', 'name' => 'col_names', 'text' => __('The first line of the file contains the table column names <i>(if this is unchecked, the first line will become part of the data)</i>'));
39 $hint = new PMA_Message(__('If the data in each row of the file is not in the same order as in the database, list the corresponding column names here. Column names must be separated by commas and not enclosed in quotations.'));
40 $plugin_list['csv']['options'][] =
41 array('type' => 'text', 'name' => 'columns', 'text' => __('Column names: ' . PMA_showHint($hint)));
43 $plugin_list['csv']['options'][] = array('type' => 'end_group');
45 /* We do not define function when plugin is just queried for information above */
49 $replacements = array(
54 $csv_terminated = strtr($csv_terminated, $replacements);
55 $csv_enclosed = strtr($csv_enclosed, $replacements);
56 $csv_escaped = strtr($csv_escaped, $replacements);
57 $csv_new_line = strtr($csv_new_line, $replacements);
60 if (strlen($csv_terminated) != 1) {
61 $message = PMA_Message
::error(__('Invalid parameter for CSV import: %s'));
62 $message->addParam(__('Columns terminated by'), false);
65 // The default dialog of MS Excel when generating a CSV produces a
66 // semi-colon-separated file with no chance of specifying the
67 // enclosing character. Thus, users who want to import this file
68 // tend to remove the enclosing character on the Import dialog.
69 // I could not find a test case where having no enclosing characters
70 // confuses this script.
71 // But the parser won't work correctly with strings so we allow just
73 } elseif (strlen($csv_enclosed) > 1) {
74 $message = PMA_Message
::error(__('Invalid parameter for CSV import: %s'));
75 $message->addParam(__('Columns enclosed by'), false);
78 } elseif (strlen($csv_escaped) != 1) {
79 $message = PMA_Message
::error(__('Invalid parameter for CSV import: %s'));
80 $message->addParam(__('Columns escaped by'), false);
83 } elseif (strlen($csv_new_line) != 1 && $csv_new_line != 'auto') {
84 $message = PMA_Message
::error(__('Invalid parameter for CSV import: %s'));
85 $message->addParam(__('Lines terminated by'), false);
90 // If there is an error in the parameters entered, indicate that immediately.
92 PMA_mysqlDie($message->getMessage(), '', '', $err_url);
99 if (isset($csv_replace)) {
100 $sql_template = 'REPLACE';
102 $sql_template = 'INSERT';
103 if (isset($csv_ignore)) {
104 $sql_template .= ' IGNORE';
107 $sql_template .= ' INTO ' . PMA_backquote($table);
109 $tmp_fields = PMA_DBI_get_fields($db, $table);
111 if (empty($csv_columns)) {
112 $fields = $tmp_fields;
114 $sql_template .= ' (';
116 $tmp = preg_split('/,( ?)/', $csv_columns);
117 foreach ($tmp as $key => $val) {
118 if (count($fields) > 0) {
119 $sql_template .= ', ';
121 /* Trim also `, if user already included backquoted fields */
122 $val = trim($val, " \t\r\n\0\x0B`");
124 foreach ($tmp_fields as $id => $field) {
125 if ($field['Field'] == $val) {
131 $message = PMA_Message
::error(__('Invalid column (%s) specified! Ensure that columns names are spelled correctly, separated by commas, and not enclosed in quotes.' ));
132 $message->addParam($val);
137 $sql_template .= PMA_backquote($val);
139 $sql_template .= ') ';
142 $required_fields = count($fields);
144 $sql_template .= ' VALUES (';
147 // Defaults for parser
157 $col_names = array();
163 while (!($finished && $i >= $len) && !$error && !$timeout_passed) {
164 $data = PMA_importGetNextChunk();
165 if ($data === FALSE) {
166 // subtract data we didn't handle yet and stop processing
167 $offset -= strlen($buffer);
169 } elseif ($data === TRUE) {
170 // Handle rest of buffer
172 // Append new data to buffer
175 // Do not parse string when we're not at the end and don't have new line inside
176 if (($csv_new_line == 'auto' && strpos($buffer, "\r") === FALSE && strpos($buffer, "\n") === FALSE)
177 ||
($csv_new_line != 'auto' && strpos($buffer, $csv_new_line) === FALSE)) {
182 // Current length of our buffer
183 $len = strlen($buffer);
184 // Currently parsed char
187 // Deadlock protection
188 if ($lasti == $i && $lastlen == $len) {
189 $message = PMA_Message
::error(__('Invalid format of CSV input on line %d.'));
190 $message->addParam($line);
197 // This can happen with auto EOL and \r at the end of buffer
200 if ($ch == $csv_terminated) {
201 if ($i == $len - 1) {
212 if ($ch == $csv_enclosed) {
213 if ($i == $len - 1) {
224 while (($need_end && $ch != $csv_enclosed)
225 ||
(!$need_end && !($ch == $csv_terminated
226 ||
$ch == $csv_new_line ||
($csv_new_line == 'auto'
227 && ($ch == "\r" ||
$ch == "\n"))))) {
228 if ($ch == $csv_escaped) {
229 if ($i == $len - 1) {
237 if ($i == $len - 1) {
247 // unquoted NULL string
248 if (false === $need_end && $value === 'NULL') {
257 // Need to strip trailing enclosing char?
258 if ($need_end && $ch == $csv_enclosed) {
259 if ($finished && $i == $len - 1) {
261 } elseif ($i == $len - 1) {
270 // Are we at the end?
271 if ($ch == $csv_new_line ||
($csv_new_line == 'auto' && ($ch == "\r" ||
$ch == "\n")) ||
($finished && $i == $len - 1)) {
275 if ($ch == $csv_terminated) {
276 if ($i == $len - 1) {
284 // If everything went okay, store value
289 if ($csv_finish ||
$ch == $csv_new_line ||
($csv_new_line == 'auto' && ($ch == "\r" ||
$ch == "\n"))) {
290 if ($csv_new_line == 'auto' && $ch == "\r") { // Handle "\r\n"
291 if ($i >= ($len - 2) && !$finished) {
292 break; // We need more data to decide new line
294 if ($buffer[$i +
1] == "\n") {
298 // We didn't parse value till the end of line, so there was empty one
304 foreach ($values as $ley => $val) {
309 if ($col_count > $max_cols) {
310 $max_cols = $col_count;
317 // Do we have correct count of values?
318 if (count($values) != $required_fields) {
321 if ($values[count($values) - 1] == ';') {
322 unset($values[count($values) - 1]);
324 $message = PMA_Message
::error(__('Invalid column count in CSV input on line %d.'));
325 $message->addParam($line);
332 $sql = $sql_template;
333 foreach ($values as $key => $val) {
340 $sql .= '\'' . addslashes($val) . '\'';
348 * @todo maybe we could add original line to verbose SQL in comment
350 PMA_importRunQuery($sql, $sql);
356 $buffer = substr($buffer, $i +
1);
357 $len = strlen($buffer);
362 } // End of parser loop
363 } // End of import loop
366 /* Fill out all rows */
367 $num_rows = count($rows);
368 for ($i = 0; $i < $num_rows; ++
$i) {
369 for ($j = count($rows[$i]); $j < $max_cols; ++
$j) {
370 $rows[$i][] = 'NULL';
374 if (isset($_REQUEST['csv_col_names'])) {
375 $col_names = array_splice($rows, 0, 1);
376 $col_names = $col_names[0];
379 if ((isset($col_names) && count($col_names) != $max_cols) ||
!isset($col_names)) {
380 // Fill out column names
381 for ($i = 0; $i < $max_cols; ++
$i) {
382 $col_names[] = 'COL '.($i+
1);
387 $result = PMA_DBI_fetch_result('SHOW TABLES');
388 $tbl_name = 'TABLE '.(count($result) +
1);
390 $tbl_name = 'TBL_NAME';
393 $tables[] = array($tbl_name, $col_names, $rows);
395 /* Obtain the best-fit MySQL types for each column */
397 $analyses[] = PMA_analyzeTable($tables[0]);
400 * string $db_name (no backquotes)
402 * array $table = array(table_name, array() column_names, array()() rows)
403 * array $tables = array of "$table"s
405 * array $analysis = array(array() column_types, array() column_sizes)
406 * array $analyses = array of "$analysis"s
408 * array $create = array of SQL strings
410 * array $options = an associative array of options
413 /* Set database name to the currently selected one, if applicable */
416 $options = array('create_db' => false);
422 /* Non-applicable parameters */
425 /* Created and execute necessary SQL statements from data */
426 PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
432 // Commit any possible data in buffers
433 PMA_importRunQuery();
435 if (count($values) != 0 && !$error) {
436 $message = PMA_Message
::error(__('Invalid format of CSV input on line %d.'));
437 $message->addParam($line);