-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-phone-contacts.php
More file actions
executable file
·76 lines (63 loc) · 2.23 KB
/
update-phone-contacts.php
File metadata and controls
executable file
·76 lines (63 loc) · 2.23 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
#! /usr/bin/env php
<?php
/**
* Portions of this code are copyrighted by the contributors to Drupal.
* Additional code copyright 2011 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.
*
* Imports a CSV file with information from voter contacts.
*/
require_once dirname(__FILE__) . '/db-helper.php';
// Optionally include the DB url from a file.
if (file_exists('./settings.php')) {
include './settings.php';
}
if (count($argv) < 2 && isset($db_url)) {
exit("usage: {$argv[0]} filename");
}
elseif (count($argv) < 3 && !isset($db_url)) {
exit("usage: {$argv[0]} filename mysqli_connection_url\n\nA valid connection URL looks like 'mysqli://myname:pass@127.0.0.1:3306/voterdbname'\n");
}
if (isset($argv[2])) {
$db_url = $argv[2];
}
$filename = $argv[1];
if (!file_exists($filename) || !is_readable($filename)) {
exit("File {$filename} does not exist or cannot be read\n");
}
// This setting handles \r or \n line endings
ini_set('auto_detect_line_endings', 1);
if (($handle = fopen($argv[1], "r")) === FALSE) {
exit("Couldn't open file {$filename}\n");
}
$header = fgetcsv($handle, 1000);
if (is_array($header)) {
$idx = array_flip($header);
}
if (!$header || !isset($idx['voter_id']) || !isset($idx['code']) || !isset($idx['note'])) {
exit("Invalid header row in file {$filename}\n");
}
$active_db = db_connect($db_url);
while (($data = fgetcsv($handle, 1000)) !== FALSE) {
if (!isset($data[$idx['voter_id']])) {
echo "Invalid row: " . print_r($data, TRUE);
continue;
}
$id = $data[$idx['voter_id']];
$code = strtoupper($data[$idx['code']]);
$note = $data[$idx['note']];
// Only write real contacts to the DB.
if (strlen($code)) {
if ($code == 'W' || $code == 'X') {
// Bad phone number.
db_query($active_db, "DELETE FROM van_info WHERE voter_id = '%s'", array($id));
}
if ($code != 'X') {
db_query($active_db, "INSERT INTO voter_contact (voter_id, code, note) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE code = VALUES(code), note = CONCAT_WS(',', note, VALUES(note))", array($id, $code, $note));
}
}
}
exit;