Skip to content

Commit

Permalink
Check for redirected PIDs in person search.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Apr 6, 2020
1 parent eaa4b4f commit af10fb2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 deletions.
7 changes: 1 addition & 6 deletions classes/Search/Normal.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,7 @@ private function find_members() {
$searchstring = trim(preg_replace('#-?[a-z]+:[a-z0-9]+#', '', $this->searchstring));
if (!$searchstring) return array();

$members = array();

$q = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup($searchstring);
foreach ($q as $row) {
$members[] = $row['person_id'];
}
$members = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup($searchstring);

$db = new \ParlDB;
$q = $db->query("SELECT person FROM moffice WHERE position LIKE :pos ORDER BY from_date DESC, moffice_id",
Expand Down
7 changes: 1 addition & 6 deletions classes/Search/ParseArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,7 @@ private function parse_person_params() {

# Searching from MP pages
if ($searchspeaker = trim(get_http_var('person'))) {
$q = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup($searchspeaker);
$pids = array();
foreach ($q as $row) {
$pids[$row['person_id']] = true;
}
$pids = array_keys($pids);
$pids = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup($searchspeaker);
if (count($pids) > 0) {
$searchstring .= ' speaker:' . join(' speaker:', $pids);
}
Expand Down
30 changes: 19 additions & 11 deletions classes/Utility/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static function searchByUsage($search, $house = 0) {
*/

public static function searchMemberDbLookup($searchstring, $current_only=false) {
if (!$searchstring) return false;
if (!$searchstring) return array();
$searchwords = explode(' ', $searchstring, 3);
$params = array();
if (count($searchwords) == 1) {
Expand Down Expand Up @@ -180,22 +180,30 @@ public static function searchMemberDbLookup($searchstring, $current_only=false)

$db = new \ParlDB;
$q = $db->query("SELECT person_id FROM person_names WHERE type='name' AND ($where)", $params);
return $q;

# Check for redirects
$pids = array();
foreach ($q as $row) {
$pid = $row['person_id'];
$redirect = $db->query("SELECT gid_to FROM gidredirect WHERE gid_from = :gid_from",
array(':gid_from' => "uk.org.publicwhip/person/$pid")
)->first();
if ($redirect) {
$pid = str_replace('uk.org.publicwhip/person/', '', $redirect['gid_to']);
}
$pids[] = $pid;
}

return array_unique($pids);
}

public static function searchMemberDbLookupWithNames($searchstring, $current_only=false) {
$q = self::searchMemberDbLookup($searchstring, $current_only);
$pids = self::searchMemberDbLookup($searchstring, $current_only);

if (!$q->rows()) {
return $q;
if (!count($pids)) {
return $pids;
}

$person_ids = array();
foreach ($q as $row) {
$pid = $row['person_id'];
$person_ids[$pid] = 1;
}
$pids = array_keys($person_ids);
$pids_str = join(',', $pids);

$where = '';
Expand Down
20 changes: 10 additions & 10 deletions tests/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public function testSearchMemberDbLookupSingleName()
// Test a single (first) name.
$results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Joseph');

$this->assertEquals(1, $results->rows());
$this->assertEquals(2, $results->first()['person_id']);
$this->assertEquals(1, count($results));
$this->assertEquals(2, $results[0]);

// Test a single (last) name.
$results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Bloggs');

$this->assertEquals(1, $results->rows());
$this->assertEquals(2, $results->first()['person_id']);
$this->assertEquals(1, count($results));
$this->assertEquals(2, $results[0]);

}

Expand All @@ -65,20 +65,20 @@ public function testSearchMemberDbLookupFullName()
// Test a full name.
$results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Mary Smith');

$this->assertEquals(1, $results->rows());
$this->assertEquals(3, $results->first()['person_id']);
$this->assertEquals(1, count($results));
$this->assertEquals(3, $results[0]);

// Test an inverse full name.
$results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Smith Mary');

$this->assertEquals(1, $results->rows());
$this->assertEquals(3, $results->first()['person_id']);
$this->assertEquals(1, count($results));
$this->assertEquals(3, $results[0]);

// Test a name with title.
$results = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookup('Mrs Smith');

$this->assertEquals(1, $results->rows());
$this->assertEquals(3, $results->first()['person_id']);
$this->assertEquals(1, count($results));
$this->assertEquals(3, $results[0]);

}

Expand Down
4 changes: 2 additions & 2 deletions www/includes/easyparliament/templates/html/alert/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@

<?php
if(
(isset($members) && $members->rows() > 0) ||
(isset($members) && count($members)) ||
(isset($constituencies) && count($constituencies) > 0) ||
($alertsearch)
) {
Expand All @@ -139,7 +139,7 @@
<div class="alert-section alert-section--disambiguation">
<div class="alert-section__primary">

<?php if (isset($members) && $members->rows() > 0) {
<?php if (isset($members) && count($members)) {
$member_options = true; ?>
<h3>Sign up for alerts when people matching <i><?= _htmlspecialchars($alertsearch) ?></i> speaks</h3>
<ul>
Expand Down

0 comments on commit af10fb2

Please sign in to comment.