new
[phpmyadmin/crack.git] / libraries / import / ldi.php
blob5931da4f393f5ae19b272e97cf36329595412aa6
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * CSV import plugin for phpMyAdmin
6 * @version $Id$
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 if ($plugin_param !== 'table') {
16 return;
19 if (isset($plugin_list)) {
20 if ($GLOBALS['cfg']['Import']['ldi_local_option'] == 'auto') {
21 $GLOBALS['cfg']['Import']['ldi_local_option'] = FALSE;
23 $result = PMA_DBI_try_query('SHOW VARIABLES LIKE \'local\\_infile\';');
24 if ($result != FALSE && PMA_DBI_num_rows($result) > 0) {
25 $tmp = PMA_DBI_fetch_row($result);
26 if ($tmp[1] == 'ON') {
27 $GLOBALS['cfg']['Import']['ldi_local_option'] = TRUE;
30 PMA_DBI_free_result($result);
31 unset($result);
33 $plugin_list['ldi'] = array(
34 'text' => 'strLDI',
35 'extension' => 'ldi', // This is nonsense, however we want to default to our parser for csv
36 'options' => array(
37 array('type' => 'bool', 'name' => 'replace', 'text' => 'strReplaceTable'),
38 array('type' => 'bool', 'name' => 'ignore', 'text' => 'strIgnoreDuplicates'),
39 array('type' => 'text', 'name' => 'terminated', 'text' => 'strFieldsTerminatedBy', 'size' => 2, 'len' => 2),
40 array('type' => 'text', 'name' => 'enclosed', 'text' => 'strFieldsEnclosedBy', 'size' => 2, 'len' => 2),
41 array('type' => 'text', 'name' => 'escaped', 'text' => 'strFieldsEscapedBy', 'size' => 2, 'len' => 2),
42 array('type' => 'text', 'name' => 'new_line', 'text' => 'strLinesTerminatedBy', 'size' => 2),
43 array('type' => 'text', 'name' => 'columns', 'text' => 'strColumnNames'),
44 array('type' => 'bool', 'name' => 'local_option', 'text' => 'strLDILocal'),
46 'options_text' => 'strOptions',
48 /* We do not define function when plugin is just queried for information above */
49 return;
52 if ($import_file == 'none' || $compression != 'none' || $charset_conversion) {
53 // We handle only some kind of data!
54 $message = PMA_Message::error('strInvalidLDIImport');
55 $error = TRUE;
56 return;
59 $sql = 'LOAD DATA';
60 if (isset($ldi_local_option)) {
61 $sql .= ' LOCAL';
63 $sql .= ' INFILE \'' . PMA_sqlAddslashes($import_file) . '\'';
64 if (isset($ldi_replace)) {
65 $sql .= ' REPLACE';
66 } elseif (isset($ldi_ignore)) {
67 $sql .= ' IGNORE';
69 $sql .= ' INTO TABLE ' . PMA_backquote($table);
71 if (strlen($ldi_terminated) > 0) {
72 $sql .= ' FIELDS TERMINATED BY \'' . $ldi_terminated . '\'';
74 if (strlen($ldi_enclosed) > 0) {
75 $sql .= ' ENCLOSED BY \'' . PMA_sqlAddslashes($ldi_enclosed) . '\'';
77 if (strlen($ldi_escaped) > 0) {
78 $sql .= ' ESCAPED BY \'' . PMA_sqlAddslashes($ldi_escaped) . '\'';
80 if (strlen($ldi_new_line) > 0){
81 if ($ldi_new_line == 'auto') {
82 $ldi_new_line = PMA_whichCrlf() == "\n" ? '\n' : '\r\n';
84 $sql .= ' LINES TERMINATED BY \'' . $ldi_new_line . '\'';
86 if ($skip_queries > 0) {
87 $sql .= ' IGNORE ' . $skip_queries . ' LINES';
88 $skip_queries = 0;
90 if (strlen($ldi_columns) > 0) {
91 $sql .= ' (';
92 $tmp = split(',( ?)', $ldi_columns);
93 $cnt_tmp = count($tmp);
94 for ($i = 0; $i < $cnt_tmp; $i++) {
95 if ($i > 0) {
96 $sql .= ', ';
98 $sql .= PMA_backquote(trim($tmp[$i]));
99 } // end for
100 $sql .= ')';
103 PMA_importRunQuery($sql, $sql);
104 PMA_importRunQuery();
105 $finished = TRUE;