Translated using Weblate.
[phpmyadmin.git] / libraries / import / ldi.php
blob47319568f69c0fbdccc33a996be3fbd5ef7e9267
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 /**
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' => __('CSV using LOAD DATA'),
36 'extension' => 'ldi', // This is nonsense, however we want to default to our parser for csv
37 'options' => array(
38 array('type' => 'begin_group', 'name' => 'general_opts'),
39 array('type' => 'bool', 'name' => 'replace', 'text' => __('Replace table data with file')),
40 array('type' => 'bool', 'name' => 'ignore', 'text' => __('Do not abort on INSERT error')),
41 array('type' => 'text', 'name' => 'terminated', 'text' => __('Columns terminated by'), 'size' => 2, 'len' => 2),
42 array('type' => 'text', 'name' => 'enclosed', 'text' => __('Columns enclosed by'), 'size' => 2, 'len' => 2),
43 array('type' => 'text', 'name' => 'escaped', 'text' => __('Columns escaped by'), 'size' => 2, 'len' => 2),
44 array('type' => 'text', 'name' => 'new_line', 'text' => __('Lines terminated by'), 'size' => 2),
45 array('type' => 'text', 'name' => 'columns', 'text' => __('Column names')),
46 array('type' => 'bool', 'name' => 'local_option', 'text' => __('Use LOCAL keyword')),
47 array('type' => 'end_group')
49 'options_text' => __('Options'),
51 /* We do not define function when plugin is just queried for information above */
52 return;
55 if ($import_file == 'none' || $compression != 'none' || $charset_conversion) {
56 // We handle only some kind of data!
57 $message = PMA_Message::error(__('This plugin does not support compressed imports!'));
58 $error = true;
59 return;
62 $sql = 'LOAD DATA';
63 if (isset($ldi_local_option)) {
64 $sql .= ' LOCAL';
66 $sql .= ' INFILE \'' . PMA_sqlAddSlashes($import_file) . '\'';
67 if (isset($ldi_replace)) {
68 $sql .= ' REPLACE';
69 } elseif (isset($ldi_ignore)) {
70 $sql .= ' IGNORE';
72 $sql .= ' INTO TABLE ' . PMA_backquote($table);
74 if (strlen($ldi_terminated) > 0) {
75 $sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\'';
77 if (strlen($ldi_enclosed) > 0) {
78 $sql .= ' ENCLOSED BY \'' . PMA_sqlAddSlashes($ldi_enclosed) . '\'';
80 if (strlen($ldi_escaped) > 0) {
81 $sql .= ' ESCAPED BY \'' . PMA_sqlAddSlashes($ldi_escaped) . '\'';
83 if (strlen($ldi_new_line) > 0) {
84 if ($ldi_new_line == 'auto') {
85 $ldi_new_line = PMA_whichCrlf() == "\n" ? '\n' : '\r\n';
87 $sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\'';
89 if ($skip_queries > 0) {
90 $sql .= ' IGNORE ' . $skip_queries . ' LINES';
91 $skip_queries = 0;
93 if (strlen($ldi_columns) > 0) {
94 $sql .= ' (';
95 $tmp = preg_split('/,( ?)/', $ldi_columns);
96 $cnt_tmp = count($tmp);
97 for ($i = 0; $i < $cnt_tmp; $i++) {
98 if ($i > 0) {
99 $sql .= ', ';
101 /* Trim also `, if user already included backquoted fields */
102 $sql .= PMA_backquote(trim($tmp[$i], " \t\r\n\0\x0B`"));
103 } // end for
104 $sql .= ')';
107 PMA_importRunQuery($sql, $sql);
108 PMA_importRunQuery();
109 $finished = true;