Issues (404)

scripts/alertmpchanged.php (4 issues)

1
<?php
2
/*
3
 * Name: alertmpchanged.php
4
 * Description: Mailer for those whose MP has changed at the election
5
 */
6
7
function mlog($message) {
8
    print $message;
9
}
10
11
include_once dirname(__FILE__) . '/../www/includes/easyparliament/init.php';
12
ini_set('memory_limit', -1);
13
include_once INCLUDESPATH . 'easyparliament/member.php';
14
15
$global_start = getmicrotime();
16
$db = new ParlDB();
17
18
$nomail = false;
19
$onlyemail = '';
20
$fromemail = '';
21
$fromflag = false;
22
$toemail = '';
23
$template = 'alert_new_mp';
24
for ($k = 1; $k < $argc; $k++) {
25
    if ($argv[$k] == '--nomail') {
26
        $nomail = true;
27
    }
28
    if (preg_match('#^--only=(.*)$#', $argv[$k], $m)) {
29
        $onlyemail = $m[1];
30
    }
31
    if (preg_match('#^--from=(.*)$#', $argv[$k], $m)) {
32
        $fromemail = $m[1];
33
    }
34
    if (preg_match('#^--to=(.*)$#', $argv[$k], $m)) {
35
        $toemail = $m[1];
36
    }
37
}
38
39
if (DEVSITE) {
40
    $nomail = true;
41
}
42
43
# Change this to the end date
44
$END_DATE = '2017-05-03';
45
46
if ($nomail) {
47
    mlog("NOT SENDING EMAIL\n");
48
}
49
if (($fromemail && $onlyemail) || ($toemail && $onlyemail)) {
50
    mlog("Can't have both from/to and only!\n");
51
    exit;
52
}
53
54
$active = 0;
55
$queries = 0;
56
$unregistered = 0;
57
$registered = 0;
58
$sentemails = 0;
59
60
$LIVEALERTS = new ALERT();
61
62
$current = ['email' => '', 'token' => ''];
63
$email_text = [];
64
$change_text = [];
65
$globalsuccess = 1;
66
67
# Fetch all confirmed, non-deleted alerts
68
$confirmed = 1;
69
$deleted = 0;
70
$alertdata = $LIVEALERTS->fetch($confirmed, $deleted);
71
$alertdata = $alertdata['data'];
72
73
$outof = count($alertdata);
74
$members = [];
75
$start_time = time();
76
foreach ($alertdata as $alertitem) {
77
    $active++;
78
    $email = $alertitem['email'];
79
    if ($onlyemail && $email != $onlyemail) {
80
        continue;
81
    }
82
    if ($fromemail && strtolower($email) == $fromemail) {
83
        $fromflag = true;
84
    }
85
    if ($fromemail && !$fromflag) {
86
        continue;
87
    }
88
    if ($toemail && strtolower($email) >= $toemail) {
89
        continue;
90
    }
91
    $criteria_raw = $alertitem['criteria'];
92
93
    // we only care about alerts for people speaking
94
    if (!strstr($criteria_raw, 'speaker:')) {
95
        continue;
96
    }
97
98
    preg_match('#speaker:(\d+)#', $criteria_raw, $m);
99
    $person_id = $m[1];
100
    if (!isset($members[$person_id])) {
101
        $queries++;
102
        $members[$person_id] = new MEMBER(['person_id' => $person_id]);
103
    }
104
    $member = $members[$person_id];
105
106
    // if they're still elected then don't send the email
107
    if ($member->current_member_anywhere()) {
108
        continue;
109
    }
110
111
    // skip if they didn't lose their westminster seat in the most recent election
112
    if ($member->left_house(1)['date'] != $END_DATE) {
113
        continue;
114
    }
115
116
    if (!isset($cons[$member->constituency])) {
117
        $cons_member = new MEMBER(['constituency' => $member->constituency, 'house' => 1]);
118
        if (!$cons_member) {
119
            continue;
120
        }
121
        $cons[$member->constituency] = $cons_member;
122
    } else {
123
        $cons_member = $cons[$member->constituency];
124
    }
125
126
    // these should never happen but let's just be sure
127
    if ($cons_member->person_id == $member->person_id) {
128
        continue;
129
    }
130
    if (!$cons_member->current_member_anywhere()) {
131
        continue;
132
    }
133
134
    if ($email != $current['email']) {
135
        if ($email_text && $change_text) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $email_text of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug Best Practice introduced by
The expression $change_text of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
136
            write_and_send_email($current, $email_text, $change_text, $template);
0 ignored issues
show
The call to write_and_send_email() has too few arguments starting with html_banner. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
            /** @scrutinizer ignore-call */ 
137
            write_and_send_email($current, $email_text, $change_text, $template);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
137
        }
138
        $current['email'] = $email;
139
        $current['token'] = $alertitem['alert_id'] . '-' . $alertitem['registrationtoken'];
140
        $email_text = [];
141
        $change_text = [];
142
        $q = $db->query('SELECT user_id FROM users WHERE email = :email', [
143
            ':email' => $email])->first();
144
        if ($q) {
145
            $user_id = $q['user_id'];
146
            $registered++;
147
        } else {
148
            $user_id = 0;
149
            $unregistered++;
150
        }
151
        mlog("\nEMAIL: $email, uid $user_id; memory usage : " . memory_get_usage() . "\n");
152
    }
153
154
    $lh = $member->left_house();
155
    $lh = array_shift($lh);
156
    $text = '* ' . $member->full_name();
157
    if (!in_array($text, $email_text)) {
158
        $email_text[] = $text;
159
160
        $change = '* ' . $cons_member->full_name() . ': https://' . DOMAIN . '/C/' . $alertitem['registrationtoken'];
161
        $change_text[] = $change;
162
    }
163
}
164
if ($email_text && $change_text) {
0 ignored issues
show
$email_text is of type array|string[], thus it always evaluated to false.
Loading history...
165
    write_and_send_email($current, $email_text, $change_text, $template);
166
}
167
168
mlog("\n");
169
170
$sss = "Active alerts: $active\nEmail lookups: $registered registered, $unregistered unregistered\nQuery lookups: $queries\nSent emails: $sentemails\n";
171
if ($globalsuccess) {
172
    $sss .= 'Everything went swimmingly, in ';
173
} else {
174
    $sss .= 'Something went wrong! Total time: ';
175
}
176
$sss .= (getmicrotime() - $global_start) . "\n\n";
177
mlog($sss);
178
mlog(date('r') . "\n");
179
180
function write_and_send_email($current, $data, $change, $template) {
181
    global $globalsuccess, $sentemails, $nomail, $start_time;
182
183
    $sentemails++;
184
    mlog("SEND $sentemails : Sending email to $current[email] ... ");
185
    $d = ['to' => $current['email'], 'template' => $template];
186
    $m = [
187
        'DATA' => join("\n", $data),
188
        'CHANGE' => join("\n", $change),
189
        'ALERT_IS' => count($data) == 1 ? 'alert is' : 'alerts are',
190
        'MPS' => count($data) == 1 ? 'This MP' : 'These MPs',
191
        'MPS2' => count($data) == 1 ? 'MP' : 'MPs',
192
        'ALERTS' => count($data) == 1 ? 'an alert' : 'some alerts',
193
        'ALERTS2' => count($data) == 1 ? 'alert' : 'alerts',
194
        'LINKS' => count($data) == 1 ? 'link' : 'links',
195
    ];
196
    if (!$nomail) {
197
        $success = send_template_email($d, $m, true);
198
        mlog("sent ... ");
199
        # sleep if time between sending mails is less than a certain number of seconds on average
200
        if (((time() - $start_time) / $sentemails) < 0.5) {
201
            # number of seconds per mail not to be quicker than
202
            mlog("pausing ... ");
203
            sleep(1);
204
        }
205
    } else {
206
        mlog(join('', $data));
207
        $success = 1;
208
    }
209
    mlog("done\n");
210
211
    if (!$success) {
212
        $globalsuccess = 0;
213
    }
214
}
215