-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport-csv.php
More file actions
executable file
·113 lines (94 loc) · 2.54 KB
/
import-csv.php
File metadata and controls
executable file
·113 lines (94 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#! /usr/bin/env php
<?php
/**
* Portions of this code are copyrighted by the contributors to Drupal.
* Additional code copyright 2011-2012 by Peter Wolanin.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 or any later version.
*/
require_once dirname(__FILE__) . '/db-helper.php';
ini_set('auto_detect_line_endings', 1);
$scriptname = array_shift($argv);
if (count($argv) < 1) {
exit("usage: {$scriptname} TABLENAME data.csv\n\n");
}
$table = array_shift($argv);
$filename = array_shift($argv);
if (!file_exists($filename) || !is_readable($filename)) {
exit("File {$filename} does not exist or cannot be read\n");
}
$handle = @fopen($filename, "r");
if (!$handle) {
echo "Faile to open file\n";
exit;
}
$delimiter = NULL;
$rows = 0;
$start = time();
// Get the header row.
if (($line = fgets($handle)) !== FALSE) {
if (!isset($delimiter)) {
$delimiter = ','; // CSV default
if (count(explode('|', $line)) >= 2) {
$delimiter = '|'; // Pipe delimited
}
}
$header_fields = str_getcsv($line, $delimiter);
foreach($header_fields as $idx => $field) {
// Normalize to lower case.
$header_fields[$idx] = strtolower($field);
}
// Allow us to look up desired fields based on name in header.
$idx = array_flip($header_fields);
$expected_num_fields = count($header_fields);
}
echo "Header fields:\n";
print_r($header_fields);
if (!db_table_exists($table)) {
// TODO: create the table.
$table_fields = $header_fields;
echo "TODO - right now you need to pre-create the table\n";
exit(1);
}
else {
$table_fields = array();
$etable = db_escape_table($table);
$r = db_query("DESCRIBE {{$etable}}");
foreach ($r as $row) {
$name = $row->Field;
// If the column name was one of the header fields, we'll use it.
if (isset($idx[$name])) {
$table_fields[] = $name;
}
}
}
echo "Table fields to use:\n";
print_r($table_fields);
while (($fields = fgetcsv($handle, $delimiter)) !== FALSE) {
if (count($fields) < 3) {
echo "Invalid line: {$line}\n";
continue;
}
$info = array();
foreach($table_fields as $key) {
$info[] = $fields[$idx[$key]];
}
try {
db_insert($table)
->fields($table_fields)
->values($info)
->execute();
}
catch (PDOException $e) {
// Dump the bad data for inspection.
print_r($info);
throw $e;
}
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
exit;