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
8 * @package phpMyAdmin-Import
10 if (! defined('PHPMYADMIN')) {
16 if ($plugin_param !== 'table') {
20 if (isset($plugin_list)) {
21 $plugin_list['csv'] = array(
25 array('type' => 'bool', 'name' => 'replace', 'text' => 'strReplaceTable'),
26 array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreDuplicates'),
27 array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 2),
28 array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy', 'size' => 2, 'len' => 2),
29 array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy', 'size' => 2, 'len' => 2),
30 array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2),
32 'options_text' => 'strOptions',
35 if ($plugin_param !== 'table') {
36 $plugin_list['csv']['options'][] =
37 array('type' => 'bool', 'name' => 'col_names', 'text' => 'strImportColNames');
39 $plugin_list['csv']['options'][] =
40 array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames');
43 /* We do not define function when plugin is just queried for information above */
47 $replacements = array(
52 $csv_terminated = strtr($csv_terminated, $replacements);
53 $csv_enclosed = strtr($csv_enclosed, $replacements);
54 $csv_escaped = strtr($csv_escaped, $replacements);
55 $csv_new_line = strtr($csv_new_line, $replacements);
57 if (strlen($csv_terminated) != 1) {
58 $message = PMA_Message
::error('strInvalidCSVParameter');
59 $message->addParam('strFieldsTerminatedBy', false);
61 // The default dialog of MS Excel when generating a CSV produces a
62 // semi-colon-separated file with no chance of specifying the
63 // enclosing character. Thus, users who want to import this file
64 // tend to remove the enclosing character on the Import dialog.
65 // I could not find a test case where having no enclosing characters
66 // confuses this script.
67 // But the parser won't work correctly with strings so we allow just
69 } elseif (strlen($csv_enclosed) > 1) {
70 $message = PMA_Message
::error('strInvalidCSVParameter');
71 $message->addParam('strFieldsEnclosedBy', false);
73 } elseif (strlen($csv_escaped) != 1) {
74 $message = PMA_Message
::error('strInvalidCSVParameter');
75 $message->addParam('strFieldsEscapedBy', false);
77 } elseif (strlen($csv_new_line) != 1 && $csv_new_line != 'auto') {
78 $message = PMA_Message
::error('strInvalidCSVParameter');
79 $message->addParam('strLinesTerminatedBy', false);
87 if (isset($csv_replace)) {
88 $sql_template = 'REPLACE';
90 $sql_template = 'INSERT';
91 if (isset($csv_ignore)) {
92 $sql_template .= ' IGNORE';
95 $sql_template .= ' INTO ' . PMA_backquote($table);
97 $tmp_fields = PMA_DBI_get_fields($db, $table);
99 if (empty($csv_columns)) {
100 $fields = $tmp_fields;
102 $sql_template .= ' (';
104 $tmp = preg_split('/,( ?)/', $csv_columns);
105 foreach ($tmp as $key => $val) {
106 if (count($fields) > 0) {
107 $sql_template .= ', ';
109 /* Trim also `, if user already included backquoted fields */
110 $val = trim($val, " \t\r\n\0\x0B`");
112 foreach ($tmp_fields as $id => $field) {
113 if ($field['Field'] == $val) {
119 $message = PMA_Message
::error('strInvalidColumn');
120 $message->addParam($val);
125 $sql_template .= PMA_backquote($val);
127 $sql_template .= ') ';
130 $required_fields = count($fields);
132 $sql_template .= ' VALUES (';
135 // Defaults for parser
145 $col_names = array();
151 while (!($finished && $i >= $len) && !$error && !$timeout_passed) {
152 $data = PMA_importGetNextChunk();
153 if ($data === FALSE) {
154 // subtract data we didn't handle yet and stop processing
155 $offset -= strlen($buffer);
157 } elseif ($data === TRUE) {
158 // Handle rest of buffer
160 // Append new data to buffer
163 // Do not parse string when we're not at the end and don't have new line inside
164 if (($csv_new_line == 'auto' && strpos($buffer, "\r") === FALSE && strpos($buffer, "\n") === FALSE)
165 ||
($csv_new_line != 'auto' && strpos($buffer, $csv_new_line) === FALSE)) {
170 // Current length of our buffer
171 $len = strlen($buffer);
172 // Currently parsed char
175 // Deadlock protection
176 if ($lasti == $i && $lastlen == $len) {
177 $message = PMA_Message
::error('strInvalidCSVFormat');
178 $message->addParam($line);
185 // This can happen with auto EOL and \r at the end of buffer
188 if ($ch == $csv_terminated) {
189 if ($i == $len - 1) {
200 if ($ch == $csv_enclosed) {
201 if ($i == $len - 1) {
212 while (($need_end && $ch != $csv_enclosed)
213 ||
(!$need_end && !($ch == $csv_terminated
214 ||
$ch == $csv_new_line ||
($csv_new_line == 'auto'
215 && ($ch == "\r" ||
$ch == "\n"))))) {
216 if ($ch == $csv_escaped) {
217 if ($i == $len - 1) {
225 if ($i == $len - 1) {
235 // unquoted NULL string
236 if (false === $need_end && $value === 'NULL') {
245 // Need to strip trailing enclosing char?
246 if ($need_end && $ch == $csv_enclosed) {
247 if ($finished && $i == $len - 1) {
249 } elseif ($i == $len - 1) {
258 // Are we at the end?
259 if ($ch == $csv_new_line ||
($csv_new_line == 'auto' && ($ch == "\r" ||
$ch == "\n")) ||
($finished && $i == $len - 1)) {
263 if ($ch == $csv_terminated) {
264 if ($i == $len - 1) {
272 // If everything went okay, store value
277 if ($csv_finish ||
$ch == $csv_new_line ||
($csv_new_line == 'auto' && ($ch == "\r" ||
$ch == "\n"))) {
278 if ($csv_new_line == 'auto' && $ch == "\r") { // Handle "\r\n"
279 if ($i >= ($len - 2) && !$finished) {
280 break; // We need more data to decide new line
282 if ($buffer[$i +
1] == "\n") {
286 // We didn't parse value till the end of line, so there was empty one
292 foreach ($values as $ley => $val) {
297 if ($col_count > $max_cols) {
298 $max_cols = $col_count;
305 // Do we have correct count of values?
306 if (count($values) != $required_fields) {
309 if ($values[count($values) - 1] == ';') {
310 unset($values[count($values) - 1]);
312 $message = PMA_Message
::error('strInvalidCSVFieldCount');
313 $message->addParam($line);
320 $sql = $sql_template;
321 foreach ($values as $key => $val) {
328 $sql .= '\'' . addslashes($val) . '\'';
336 * @todo maybe we could add original line to verbose SQL in comment
338 PMA_importRunQuery($sql, $sql);
344 $buffer = substr($buffer, $i +
1);
345 $len = strlen($buffer);
350 } // End of parser loop
351 } // End of import loop
354 /* Fill out all rows */
355 $num_rows = count($rows);
356 for ($i = 0; $i < $num_rows; ++
$i) {
357 for ($j = count($rows[$i]); $j < $max_cols; ++
$j) {
358 $rows[$i][] = 'NULL';
362 if ($_REQUEST['csv_col_names']) {
363 $col_names = array_splice($rows, 0, 1);
364 $col_names = $col_names[0];
367 if ((isset($col_names) && count($col_names) != $max_cols) ||
!isset($col_names)) {
368 // Fill out column names
369 for ($i = 0; $i < $max_cols; ++
$i) {
370 $col_names[] = 'COL '.($i+
1);
375 $result = PMA_DBI_fetch_result('SHOW TABLES');
376 $tbl_name = 'TABLE '.(count($result) +
1);
378 $tbl_name = 'TBL_NAME';
381 $tables[] = array($tbl_name, $col_names, $rows);
383 /* Obtain the best-fit MySQL types for each column */
385 $analyses[] = PMA_analyzeTable($tables[0]);
388 * string $db_name (no backquotes)
390 * array $table = array(table_name, array() column_names, array()() rows)
391 * array $tables = array of "$table"s
393 * array $analysis = array(array() column_types, array() column_sizes)
394 * array $analyses = array of "$analysis"s
396 * array $create = array of SQL strings
398 * array $options = an associative array of options
401 /* Set database name to the currently selected one, if applicable */
404 $options = array('create_db' => false);
410 /* Non-applicable parameters */
413 /* Created and execute necessary SQL statements from data */
414 PMA_buildSQL($db_name, $tables, $analyses, $create, $options);
420 // Commit any possible data in buffers
421 PMA_importRunQuery();
423 if (count($values) != 0 && !$error) {
424 $message = PMA_Message
::error('strInvalidCSVFormat');
425 $message->addParam($line);