3.3.6-rc1
[phpmyadmin/dkf.git] / libraries / import / ldi.php
blobf939a2d56690343d1cb7a50a8d2737986bd4911d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * CSV import plugin for phpMyAdmin
6 * @version $Id$
7 * @package phpMyAdmin-Import
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 if ($plugin_param !== 'table') {
17 return;
20 if (isset($plugin_list)) {
21 if ($GLOBALS['cfg']['Import']['ldi_local_option'] == 'auto') {
22 $GLOBALS['cfg']['Import']['ldi_local_option'] = FALSE;
24 $result = PMA_DBI_try_query('SHOW VARIABLES LIKE \'local\\_infile\';');
25 if ($result != FALSE && PMA_DBI_num_rows($result) > 0) {
26 $tmp = PMA_DBI_fetch_row($result);
27 if ($tmp[1] == 'ON') {
28 $GLOBALS['cfg']['Import']['ldi_local_option'] = TRUE;
31 PMA_DBI_free_result($result);
32 unset($result);
34 $plugin_list['ldi'] = array(
35 'text' => 'strLDI',
36 'extension' => 'ldi', // This is nonsense, however we want to default to our parser for csv
37 'options' => array(
38 array('type' => 'bool', 'name' => 'replace', 'text' => 'strReplaceTable'),
39 array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreDuplicates'),
40 array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 2),
41 array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy', 'size' => 2, 'len' => 2),
42 array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy', 'size' => 2, 'len' => 2),
43 array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2),
44 array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames'),
45 array('type' => 'bool', 'name' => 'local_option', 'text' => 'strLDILocal'),
47 'options_text' => 'strOptions',
49 /* We do not define function when plugin is just queried for information above */
50 return;
53 if ($import_file == 'none' || $compression != 'none' || $charset_conversion) {
54 // We handle only some kind of data!
55 $message = PMA_Message::error('strInvalidLDIImport');
56 $error = TRUE;
57 return;
60 $sql = 'LOAD DATA';
61 if (isset($ldi_local_option)) {
62 $sql .= ' LOCAL';
64 $sql .= ' INFILE \'' . PMA_sqlAddslashes($import_file) . '\'';
65 if (isset($ldi_replace)) {
66 $sql .= ' REPLACE';
67 } elseif (isset($ldi_ignore)) {
68 $sql .= ' IGNORE';
70 $sql .= ' INTO TABLE ' . PMA_backquote($table);
72 if (strlen($ldi_terminated) > 0) {
73 $sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\'';
75 if (strlen($ldi_enclosed) > 0) {
76 $sql .= ' ENCLOSED BY \'' . PMA_sqlAddslashes($ldi_enclosed) . '\'';
78 if (strlen($ldi_escaped) > 0) {
79 $sql .= ' ESCAPED BY \'' . PMA_sqlAddslashes($ldi_escaped) . '\'';
81 if (strlen($ldi_new_line) > 0){
82 if ($ldi_new_line == 'auto') {
83 $ldi_new_line = PMA_whichCrlf() == "\n" ? '\n' : '\r\n';
85 $sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\'';
87 if ($skip_queries > 0) {
88 $sql .= ' IGNORE ' . $skip_queries . ' LINES';
89 $skip_queries = 0;
91 if (strlen($ldi_columns) > 0) {
92 $sql .= ' (';
93 $tmp = preg_split('/,( ?)/', $ldi_columns);
94 $cnt_tmp = count($tmp);
95 for ($i = 0; $i < $cnt_tmp; $i++) {
96 if ($i > 0) {
97 $sql .= ', ';
99 /* Trim also `, if user already included backquoted fields */
100 $sql .= PMA_backquote(trim($tmp[$i], " \t\r\n\0\x0B`"));
101 } // end for
102 $sql .= ')';
105 PMA_importRunQuery($sql, $sql);
106 PMA_importRunQuery();
107 $finished = TRUE;