|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MySociety\TheyWorkForYou\Utility; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Search Utilities |
|
7
|
|
|
* |
|
8
|
|
|
* Utility functions related to search and search strings |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
class Search { |
|
12
|
|
|
public static function searchByUsage($search, $house = 0) { |
|
13
|
|
|
$data = []; |
|
14
|
|
|
$SEARCHENGINE = new \SEARCHENGINE($search); |
|
15
|
|
|
$data['pagetitle'] = $SEARCHENGINE->query_description_short(); |
|
16
|
|
|
$SEARCHENGINE = new \SEARCHENGINE($search . ' groupby:speech'); |
|
17
|
|
|
$count = $SEARCHENGINE->run_count(0, 5000, 'date'); |
|
18
|
|
|
if ($count <= 0) { |
|
19
|
|
|
$data['error'] = 'No results'; |
|
20
|
|
|
|
|
21
|
|
|
return $data; |
|
22
|
|
|
} |
|
23
|
|
|
$SEARCHENGINE->run_search(0, 5000, 'date'); |
|
24
|
|
|
$gids = $SEARCHENGINE->get_gids(); |
|
25
|
|
|
if (count($gids) <= 0) { |
|
26
|
|
|
$data['error'] = 'No results'; |
|
27
|
|
|
|
|
28
|
|
|
return $data; |
|
29
|
|
|
} |
|
30
|
|
|
if (count($gids) == 5000) { |
|
31
|
|
|
$data['limit_reached'] = true; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
# Fetch all the speakers of the results, count them up and get min/max date usage |
|
35
|
|
|
$speaker_count = []; |
|
36
|
|
|
$gids = join('","', $gids); |
|
37
|
|
|
$db = new \ParlDB(); |
|
38
|
|
|
$q = $db->query('SELECT person_id,hdate FROM hansard WHERE gid IN ("' . $gids . '")'); |
|
39
|
|
|
foreach ($q as $row) { |
|
40
|
|
|
$person_id = $row['person_id']; |
|
41
|
|
|
$hdate = $row['hdate']; |
|
42
|
|
|
if (!isset($speaker_count[$person_id])) { |
|
43
|
|
|
$speaker_count[$person_id] = 0; |
|
44
|
|
|
$maxdate[$person_id] = '1001-01-01'; |
|
45
|
|
|
$mindate[$person_id] = '9999-12-31'; |
|
46
|
|
|
} |
|
47
|
|
|
$speaker_count[$person_id]++; |
|
48
|
|
|
if ($hdate < $mindate[$person_id]) { |
|
|
|
|
|
|
49
|
|
|
$mindate[$person_id] = $hdate; |
|
50
|
|
|
} |
|
51
|
|
|
if ($hdate > $maxdate[$person_id]) { |
|
|
|
|
|
|
52
|
|
|
$maxdate[$person_id] = $hdate; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
# Fetch details of all the speakers |
|
57
|
|
|
$speakers = []; |
|
58
|
|
|
$pids = []; |
|
59
|
|
|
if (count($speaker_count)) { |
|
60
|
|
|
$person_ids = join(',', array_keys($speaker_count)); |
|
61
|
|
|
$q = $db->query('SELECT member_id, member.person_id, title, given_name, family_name, lordofname, |
|
62
|
|
|
constituency, house, party, |
|
63
|
|
|
moffice_id, dept, position, from_date, to_date, left_house |
|
64
|
|
|
FROM member LEFT JOIN moffice ON member.person_id = moffice.person |
|
65
|
|
|
JOIN person_names pn ON member.person_id = pn.person_id AND pn.type="name" AND pn.start_date <= left_house AND left_house <= pn.end_date |
|
66
|
|
|
WHERE member.person_id IN (' . $person_ids . ') |
|
67
|
|
|
' . ($house ? " AND house=$house" : '') . ' |
|
68
|
|
|
ORDER BY left_house DESC'); |
|
69
|
|
|
foreach ($q as $row) { |
|
70
|
|
|
$mid = $row['member_id']; |
|
71
|
|
|
if (!isset($pids[$mid])) { |
|
72
|
|
|
$title = $row['title']; |
|
73
|
|
|
$first = $row['given_name']; |
|
74
|
|
|
$last = $row['family_name']; |
|
75
|
|
|
$lordofname = $row['lordofname']; |
|
76
|
|
|
$house = $row['house']; |
|
77
|
|
|
$party = $row['party']; |
|
78
|
|
|
$full_name = ucfirst(member_full_name($house, $title, $first, $last, $lordofname)); |
|
79
|
|
|
$pid = $row['person_id']; |
|
80
|
|
|
$pids[$mid] = $pid; |
|
81
|
|
|
$speakers[$pid]['house'] = $house; |
|
82
|
|
|
$speakers[$pid]['left'] = $row['left_house']; |
|
83
|
|
|
} |
|
84
|
|
|
$dept = $row['dept']; |
|
85
|
|
|
$posn = $row['position']; |
|
86
|
|
|
$moffice_id = $row['moffice_id']; |
|
87
|
|
|
if ($dept && $row['to_date'] == '9999-12-31') { |
|
88
|
|
|
$speakers[$pid]['office'][$moffice_id] = prettify_office($posn, $dept); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
if (!isset($speakers[$pid]['name'])) { |
|
91
|
|
|
$speakers[$pid]['name'] = $full_name . ($house == 1 ? ' MP' : ''); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
if (!isset($speakers[$pid]['party']) && $party) { |
|
|
|
|
|
|
94
|
|
|
$speakers[$pid]['party'] = $party; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
if (isset($speaker_count[0])) { |
|
99
|
|
|
$speakers[0] = ['party' => '', 'name' => 'Headings, procedural text, etc.', 'house' => 0, 'count' => 0]; |
|
100
|
|
|
} |
|
101
|
|
|
$party_count = []; |
|
102
|
|
|
$ok = 0; |
|
103
|
|
|
foreach ($speakers as $pid => &$speaker) { |
|
104
|
|
|
$speaker['count'] = $speaker_count[$pid]; |
|
105
|
|
|
$speaker['pmaxdate'] = $maxdate[$pid]; |
|
106
|
|
|
$speaker['pmindate'] = $mindate[$pid]; |
|
107
|
|
|
$ok = 1; |
|
108
|
|
|
if (isset($speaker['party'])) { |
|
109
|
|
|
if (!isset($party_count[$speaker['party']])) { |
|
110
|
|
|
$party_count[$speaker['party']] = 0; |
|
111
|
|
|
} |
|
112
|
|
|
$party_count[$speaker['party']] += $count; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
uasort($speakers, function ($a, $b) { |
|
117
|
|
|
|
|
118
|
|
|
if ($a['count'] > $b['count']) { |
|
119
|
|
|
return -1; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if ($a['count'] < $b['count']) { |
|
123
|
|
|
return 1; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return 0; |
|
127
|
|
|
|
|
128
|
|
|
}); |
|
129
|
|
|
|
|
130
|
|
|
arsort($party_count); |
|
131
|
|
|
if (!$ok) { |
|
132
|
|
|
$data['error'] = 'No results'; |
|
133
|
|
|
return $data; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$data['party_count'] = $party_count; |
|
137
|
|
|
$data['speakers'] = $speakers; |
|
138
|
|
|
|
|
139
|
|
|
return $data; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Return query result from looking for MPs |
|
144
|
|
|
*/ |
|
145
|
|
|
|
|
146
|
|
|
public static function searchMemberDbLookup($searchstring, $current_only = false) { |
|
|
|
|
|
|
147
|
2 |
|
if (!$searchstring) { |
|
148
|
2 |
|
return []; |
|
149
|
|
|
} |
|
150
|
|
|
$searchwords = explode(' ', $searchstring, 3); |
|
151
|
2 |
|
$params = []; |
|
152
|
2 |
|
if (count($searchwords) == 1) { |
|
153
|
2 |
|
$params[':like_0'] = '%' . $searchwords[0] . '%'; |
|
154
|
1 |
|
$where = "given_name LIKE :like_0 OR family_name LIKE :like_0 OR lordofname LIKE :like_0"; |
|
155
|
1 |
|
} elseif (count($searchwords) == 2) { |
|
156
|
1 |
|
// We don't do anything special if there are more than two search words. |
|
157
|
|
|
// And here we're assuming the user's put the names in the right order. |
|
158
|
|
|
$params[':like_0'] = '%' . $searchwords[0] . '%'; |
|
159
|
1 |
|
$params[':like_1'] = '%' . $searchwords[1] . '%'; |
|
160
|
1 |
|
$params[':like_0_and_1'] = '%' . $searchwords[0] . ' ' . $searchwords[1] . '%'; |
|
161
|
1 |
|
$params[':like_0_and_1_hyphen'] = '%' . $searchwords[0] . '-' . $searchwords[1] . '%'; |
|
162
|
1 |
|
$where = "(given_name LIKE :like_0 AND family_name LIKE :like_1)"; |
|
163
|
1 |
|
$where .= " OR (given_name LIKE :like_1 AND family_name LIKE :like_0)"; |
|
164
|
1 |
|
$where .= " OR (title LIKE :like_0 AND family_name LIKE :like_1)"; |
|
165
|
1 |
|
$where .= " OR given_name LIKE :like_0_and_1 OR given_name LIKE :like_0_and_1_hyphen"; |
|
166
|
1 |
|
$where .= " OR family_name LIKE :like_0_and_1 OR family_name LIKE :like_0_and_1_hyphen"; |
|
167
|
1 |
|
$where .= " OR lordofname LIKE :like_0_and_1"; |
|
168
|
1 |
|
if (strtolower($searchwords[0]) == 'nick') { |
|
169
|
1 |
|
$where .= " OR (given_name LIKE '%nicholas%' AND family_name LIKE :like_1)"; |
|
170
|
1 |
|
} |
|
171
|
|
|
} else { |
|
172
|
|
|
$searchwords[2] = str_replace('of ', '', $searchwords[2]); |
|
173
|
|
|
$params[':like_0'] = '%' . $searchwords[0] . '%'; |
|
174
|
|
|
$params[':like_1'] = '%' . $searchwords[1] . '%'; |
|
175
|
|
|
$params[':like_2'] = '%' . $searchwords[2] . '%'; |
|
176
|
|
|
$params[':like_0_and_1'] = '%' . $searchwords[0] . ' ' . $searchwords[1] . '%'; |
|
177
|
|
|
$params[':like_1_and_2'] = '%' . $searchwords[1] . ' ' . $searchwords[2] . '%'; |
|
178
|
|
|
$params[':like_1_and_2_hyphen'] = '%' . $searchwords[1] . '-' . $searchwords[2] . '%'; |
|
179
|
|
|
$where = "(given_name LIKE :like_0_and_1 AND family_name LIKE :like_2)"; |
|
180
|
|
|
$where .= " OR (given_name LIKE :like_0 AND family_name LIKE :like_1_and_2)"; |
|
181
|
|
|
$where .= " OR (given_name LIKE :like_0 AND family_name LIKE :like_1_and_2_hyphen)"; |
|
182
|
|
|
$where .= " OR (title LIKE :like_0 AND family_name LIKE :like_1_and_2)"; |
|
183
|
|
|
$where .= " OR (title LIKE :like_0 AND family_name LIKE :like_1_and_2_hyphen)"; |
|
184
|
|
|
$where .= " OR (title LIKE :like_0 AND given_name LIKE :like_1 AND family_name LIKE :like_2)"; |
|
185
|
|
|
$where .= " OR (title LIKE :like_0 AND family_name LIKE :like_1 AND lordofname LIKE :like_2)"; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$db = new \ParlDB(); |
|
189
|
2 |
|
$q = $db->query("SELECT person_id FROM person_names WHERE type='name' AND ($where)", $params); |
|
190
|
2 |
|
|
|
191
|
|
|
# Check for redirects |
|
192
|
|
|
$pids = []; |
|
193
|
2 |
|
foreach ($q as $row) { |
|
194
|
2 |
|
$pid = $row['person_id']; |
|
195
|
2 |
|
$redirect = $db->query( |
|
196
|
2 |
|
"SELECT gid_to FROM gidredirect WHERE gid_from = :gid_from", |
|
197
|
2 |
|
[':gid_from' => "uk.org.publicwhip/person/$pid"] |
|
198
|
2 |
|
)->first(); |
|
199
|
2 |
|
if ($redirect) { |
|
200
|
|
|
$pid = str_replace('uk.org.publicwhip/person/', '', $redirect['gid_to']); |
|
201
|
|
|
} |
|
202
|
2 |
|
$pids[] = $pid; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
2 |
|
return array_unique($pids); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public static function searchMemberDbLookupWithNames($searchstring, $current_only = false) { |
|
209
|
|
|
$pids = self::searchMemberDbLookup($searchstring, $current_only); |
|
210
|
|
|
|
|
211
|
|
|
if (!count($pids)) { |
|
212
|
|
|
return $pids; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$pids_str = join(',', $pids); |
|
216
|
|
|
|
|
217
|
|
|
$where = ''; |
|
218
|
|
|
if ($current_only) { |
|
219
|
|
|
$where = "AND left_house='9999-12-31'"; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
# This is not totally accurate (e.g. minimum entered date may be from a |
|
223
|
|
|
# different house, or similar), but should be good enough. |
|
224
|
|
|
$db = new \ParlDB(); |
|
225
|
|
|
$q = $db->query("SELECT member.person_id, |
|
226
|
|
|
title, given_name, family_name, lordofname, |
|
227
|
|
|
constituency, party, |
|
228
|
|
|
(SELECT MIN(entered_house) FROM member m WHERE m.person_id=member.person_id) min_entered_house, |
|
229
|
|
|
left_house, house |
|
230
|
|
|
FROM member, person_names pn |
|
231
|
|
|
WHERE member.person_id IN ($pids_str) $where |
|
232
|
|
|
AND member.person_id = pn.person_id AND pn.type = 'name' |
|
233
|
|
|
AND pn.start_date <= member.left_house AND member.left_house <= pn.end_date |
|
234
|
|
|
AND left_house = (SELECT MAX(left_house) FROM member m WHERE m.person_id=member.person_id) |
|
235
|
|
|
GROUP BY person_id |
|
236
|
|
|
ORDER BY family_name, lordofname, given_name, person_id"); |
|
237
|
|
|
|
|
238
|
|
|
return $q->fetchAll(); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Search Constituencies |
|
243
|
|
|
* |
|
244
|
|
|
* Given a search term, find constituencies by name or postcode. |
|
245
|
|
|
* |
|
246
|
|
|
* @param string $searchterm The term to search for. |
|
247
|
|
|
* @param bool $mp_only if true (default) only return westminster constituency if using a postcode, otherwise return all. |
|
248
|
|
|
* |
|
249
|
|
|
* @return array A list of the array of constituencies, then a boolean |
|
250
|
|
|
* saying whether it was a postcode used. |
|
251
|
|
|
*/ |
|
252
|
1 |
|
|
|
253
|
1 |
|
public static function searchConstituenciesByQuery($searchterm, $mp_only = true) { |
|
254
|
|
|
if (validate_postcode($searchterm)) { |
|
255
|
|
|
// Looks like a postcode - can we find the constituency? |
|
256
|
|
|
if ($mp_only) { |
|
257
|
|
|
$constituency = Postcode::postcodeToConstituency($searchterm); |
|
258
|
|
|
if ($constituency) { |
|
259
|
|
|
return [ [$constituency], true ]; |
|
260
|
|
|
} |
|
261
|
|
|
} else { |
|
262
|
1 |
|
$constituencies = Postcode::postcodeToConstituencies($searchterm); |
|
263
|
1 |
|
if ($constituencies) { |
|
264
|
|
|
return [ $constituencies, true ]; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
1 |
|
} |
|
268
|
1 |
|
|
|
269
|
|
|
// No luck so far - let's see if they're searching for a constituency. |
|
270
|
1 |
|
$try = strtolower($searchterm); |
|
271
|
1 |
|
$query = "select distinct |
|
272
|
1 |
|
(select name from constituency where cons_id = o.cons_id and main_name) as name |
|
273
|
|
|
from constituency AS o where name like :try |
|
274
|
|
|
and from_date <= date(now()) and date(now()) <= to_date"; |
|
275
|
1 |
|
$db = new \ParlDB(); |
|
276
|
|
|
$q = $db->query($query, [':try' => '%' . $try . '%']); |
|
277
|
|
|
|
|
278
|
|
|
$constituencies = []; |
|
279
|
|
|
foreach ($q as $row) { |
|
280
|
|
|
$constituencies[] = $row['name']; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return [ $constituencies, false ]; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
public static function searchMembersByConstituency($searchterm) { |
|
287
|
|
|
$try = strtolower($searchterm); |
|
288
|
|
|
$query = "select distinct person_id from member where left_reason = 'still_in_office' and constituency like :try"; |
|
289
|
|
|
$args = [':try' => '%' . $try . '%']; |
|
290
|
|
|
|
|
291
|
|
|
$db = new \ParlDB(); |
|
292
|
|
|
$q = $db->query($query, $args); |
|
293
|
|
|
|
|
294
|
|
|
$members = []; |
|
295
|
|
|
foreach ($q as $row) { |
|
296
|
|
|
$members[] = $row['person_id']; |
|
297
|
|
|
} |
|
298
|
|
|
return $members; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* get list of names of speaker IDs from search string |
|
303
|
|
|
* |
|
304
|
|
|
* @param string $searchstring The search string with the speaker:NNN text |
|
305
|
|
|
* |
|
306
|
|
|
* @return array Array with the speaker id string as key and speaker name as value |
|
307
|
|
|
*/ |
|
308
|
|
|
|
|
309
|
|
|
public static function speakerNamesForIDs($searchstring) { |
|
310
|
|
|
$criteria = explode(' ', $searchstring); |
|
311
|
|
|
$speakers = []; |
|
312
|
|
|
|
|
313
|
|
|
foreach ($criteria as $c) { |
|
314
|
|
|
if (preg_match('#^speaker:(\d+)#', $c, $m)) { |
|
315
|
|
|
$MEMBER = new \MEMBER(['person_id' => $m[1]]); |
|
316
|
|
|
$speakers[$m[1]] = $MEMBER->full_name(); |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
return $speakers; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* get list of members of speaker IDs from search string |
|
325
|
|
|
* |
|
326
|
|
|
* @param string $searchstring The search string with the speaker:NNN text |
|
327
|
|
|
* |
|
328
|
|
|
* @return array Array with the speaker id string as key and speaker name as value |
|
329
|
|
|
*/ |
|
330
|
|
|
|
|
331
|
|
|
public static function membersForIDs($searchstring) { |
|
332
|
|
|
$criteria = explode(' ', $searchstring); |
|
333
|
|
|
$speakers = []; |
|
334
|
|
|
|
|
335
|
|
|
foreach ($criteria as $c) { |
|
336
|
|
|
if (preg_match('#^speaker:(\d+)#', $c, $m)) { |
|
337
|
|
|
$MEMBER = new \MEMBER(['person_id' => $m[1]]); |
|
338
|
|
|
$speakers[$m[1]] = $MEMBER; |
|
339
|
|
|
} |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
return $speakers; |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* replace speaker:NNNN with speaker:Name in search string |
|
347
|
|
|
* |
|
348
|
|
|
* @param string $searchstring The search string with the speaker:NNN text |
|
349
|
|
|
* |
|
350
|
|
|
* @return string The search string with replaced speaker IDs |
|
351
|
|
|
*/ |
|
352
|
|
|
public static function speakerIDsToNames($searchstring) { |
|
353
|
|
|
$speakers = self::speakerNamesForIDs($searchstring); |
|
354
|
|
|
|
|
355
|
|
|
foreach ($speakers as $id => $name) { |
|
356
|
|
|
$searchstring = str_replace('speaker:' . $id, "speaker:$name", $searchstring); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
return $searchstring; |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
|