-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecal.php
184 lines (169 loc) · 6.25 KB
/
recal.php
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* XLS parsing uses php-excel-reader from http://code.google.com/p/php-excel-reader/
*/
header('Content-Type: text/plain');
if (isset($argv[1]))
{
$Filepath = $argv[1];
}
elseif (isset($_GET['File']))
{
$Filepath = $_GET['File'];
}
else
{
if (php_sapi_name() == 'cli')
{
echo 'Please specify filename as the first argument'.PHP_EOL;
}
else
{
echo 'Please specify filename as a HTTP GET parameter "File", e.g., "/test.php?File=test.xlsx"';
}
exit;
}
// Excel reader from http://code.google.com/p/php-excel-reader/
require('spreadsheet/php-excel-reader/excel_reader2.php');
require('spreadsheet/SpreadsheetReader.php');
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$servername = "";
$username = "";
$password = "";
$dbname = "";
try {
$mysqli = new mysqli($servername, $username, $password, $dbname);
$mysqli->set_charset("utf8mb4");
} catch(Exception $e) {
error_log($e->getMessage());
exit('Error connecting to database'); //Should be a message a typical user could understand
}
date_default_timezone_set('UTC');
$StartMem = memory_get_usage();
echo '--------------------------------------------'.PHP_EOL;
echo 'Starting memory: '.$StartMem.PHP_EOL;
echo '--------------------------------------------'.PHP_EOL;
try
{
$Spreadsheet = new SpreadsheetReader($Filepath);
$BaseMem = memory_get_usage();
$Sheets = $Spreadsheet -> Sheets();
echo '--------------------------------------------'.PHP_EOL;
echo 'Spreadsheets:'.PHP_EOL;
print_r($Sheets);
echo '--------------------------------------------'.PHP_EOL;
echo '--------------------------------------------'.PHP_EOL;
$numnum = 0;
$TY = 0;
$TTY = 0;
$oldrow = "";
foreach ($Sheets as $Index => $Name)
{
$Time = microtime(true);
$Spreadsheet -> ChangeSheet($Index);
foreach ($Spreadsheet as $Key => $Row)
{
if ($Row)
{
$numnum++;
$vote = $Row[3];
$column = substr($Filepath, 0, strpos($Filepath, "."));
$column = str_replace("votes/","",$column);
$const = str_replace(",", '', $Row[2]);
if ($numnum < 11){
continue;
}
$stmt = $mysqli->prepare("SELECT * FROM mastervotes WHERE Constituency = ?");
$stmt->bind_param("s", $const);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows === 0){
echo "----------------------------------------".PHP_EOL;
echo"Couldn't Find ";
echo "$Row[0] who voted $Row[3] ".PHP_EOL;
echo "Dumping Data: ".PHP_EOL;
echo "----------------------------------------".PHP_EOL;
}
while($row = $result->fetch_assoc()) {
$i = $row['MPid'];
$FistName = $row['FirstName'];
$Lastname = $row['Lastname'];
$party = $row['Party'];
$Constituency = $row['Constituency'];
$name = "$FistName $Lastname";
$vote1 = $row[$column];
if ($result->num_rows > 1){
if ($name != $Row[0]){
continue;//Same constituency different MP
}
}
if ($party === "Sinn Féin" || $party === "Speaker" || $name==="Eleanor Laing"||$name==="Lindsay Hoyle"||$name==="Rosie Winterton"){
$votem = "NA";
echo "adding Speaker/Sinn Fein vote";
$stmt = $mysqli->prepare("UPDATE mastervotes SET $column = ? WHERE MPid = ?");
$stmt->bind_param("si", $votem, $i);
$stmt->execute();
}
if ($vote1 != $Row[3]){
if ($Row[3] === "No Vote Recorded" && $vote1 === "ABS" || $Row[3] === "No Vote Recorded" && $vote1 === "NA"){
continue;
}elseif ($Row[3] === "Teller - Ayes" && $vote1 === "TelY"){
$TY++;
continue;
}elseif ($Row[3] === "Teller - Noes" && $vote1 === "TelN"){
$TY++;
continue;
}elseif (($Row[3] === "Aye" && $vote1 === "ABP") ||($Row[3] === "No" && $vote1 === "ABP") ){
continue;
}elseif ($Row[3] === "Teller - Ayes"){
$votem = "TelY";
echo "Adding Teller Vote for " . $name.PHP_EOL;
$stmt = $mysqli->prepare("UPDATE mastervotes SET $column = ? WHERE MPid = ?");
$stmt->bind_param("si", $votem, $i);
$stmt->execute();
}elseif ($Row[3] === "Teller - Noes"){
$votem = "TelN";
$stmt = $mysqli->prepare("UPDATE mastervotes SET $column = ? WHERE MPid = ?");
$stmt->bind_param("si", $votem, $i);
$stmt->execute();
}else{
echo $Row[0] . " has wrong info".PHP_EOL;
echo "Vote registered as : " . $vote1.PHP_EOL;
echo "When actual vote is : " . $Row[3].PHP_EOL;
if ($Row[2] === $oldrow){
$votem = "ABP";
echo "ABP!";
}elseif ($Row[3] === "No Vote Recorded"){
$votem = "ABS";
}else{
$votem = $Row[3];
}
$stmt = $mysqli->prepare("UPDATE mastervotes SET $column = ? WHERE MPid = ?");
$stmt->bind_param("si", $votem, $i);
$stmt->execute();
$oldrow = $Row[2];
}
}
}
}
else
{
var_dump($Row);
}
$CurrentMem = memory_get_usage();
}
echo $TY;
echo PHP_EOL.'---------------------------------------------'.PHP_EOL;
echo 'Time: '.(microtime(true) - $Time);
echo PHP_EOL;
echo '---------------------------------------------'.PHP_EOL;
echo 'MasterVotes Updated With '.$Name.' '.PHP_EOL;
echo '---------------------------------------------'.PHP_EOL;
}
}
catch (Exception $E)
{
echo $E -> getMessage();
}
$stmt->close();
?>