-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport-van-phone.php
More file actions
executable file
·132 lines (111 loc) · 3.04 KB
/
import-van-phone.php
File metadata and controls
executable file
·132 lines (111 loc) · 3.04 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#! /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} vanfile.csv");
}
$filename = array_shift($argv);
if (!file_exists($filename) || !is_readable($filename)) {
exit("File {$filename} does not exist or cannot be read\n");
}
// Do we want to filter based on an existin voters table?
$voter_filter = array_shift($argv);
$schema['van_info'] = array(
'fields' => array(
'voter_id' => array(
'type' => 'int',
'not null' => TRUE,
),
'preferred_phone' => array(
'type' => 'varchar',
'length' => 14,
'not null' => TRUE,
'default' => '',
),
'preferred_email' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
),
'primary key' => array('voter_id'),
'indexes' => array(),
);
$handle = @fopen($filename, "r");
if (!$handle) {
echo "Faile to open file\n";
exit;
}
if (db_table_exists('van_info')) {
db_drop_table('van_info');
}
db_create_table('van_info', $schema['van_info']);
$van_fields = array_keys($schema['van_info']['fields']);
$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
}
elseif (count(explode("\t", $line)) >= 2) {
$delimiter = "\t"; // tab delimited
}
}
$header_fields = str_getcsv($line, $delimiter);
foreach ($header_fields as $idx => $value) {
$header_fields[$idx] = trim($value);
}
// 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";
var_dump($header_fields);
while ($fields = fgetcsv($handle, 1000, $delimiter)) {
if (count($fields) < 3) {
echo "Invalid line: {$line}\n";
continue;
}
$info = array();
foreach(array('voter_id' => 'AffNo', 'preferred_phone' => 'Preferred Phone', 'preferred_email' => 'PreferredEmail',) as $sql => $key) {
$info[$sql] = $fields[$idx[$key]];
}
try {
db_insert('van_info')
->fields($van_fields)
->values(array_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;
function voter_reformat_date($str) {
$parts = explode('/', $str);
if (count($parts) == 3) {
return "{$parts[2]}-{$parts[0]}-{$parts[1]}";
}
return '';
}