function search_by_usage($search, $house = 0) { $data = array(); $SEARCHENGINE = new SEARCHENGINE($search); $data['pagetitle'] = $SEARCHENGINE->query_description_short(); $SEARCHENGINE = new SEARCHENGINE($search . ' groupby:speech'); $count = $SEARCHENGINE->run_count(0, 5000, 'date'); if ($count <= 0) { $data['error'] = 'No results'; return $data; } $SEARCHENGINE->run_search(0, 5000, 'date'); $gids = $SEARCHENGINE->get_gids(); if (count($gids) <= 0) { $data['error'] = 'No results'; return $data; } if (count($gids) == 5000) { $data['limit_reached'] = true; } # Fetch all the speakers of the results, count them up and get min/max date usage $speaker_count = array(); $gids = join('","', $gids); $db = new ParlDB(); $q = $db->query('SELECT gid,person_id,hdate FROM hansard WHERE gid IN ("' . $gids . '")'); for ($n = 0; $n < $q->rows(); $n++) { $gid = $q->field($n, 'gid'); $person_id = $q->field($n, 'person_id'); $hdate = $q->field($n, 'hdate'); if (!isset($speaker_count[$person_id])) { $speaker_count[$person_id] = 0; $maxdate[$person_id] = '1001-01-01'; $mindate[$person_id] = '9999-12-31'; } $speaker_count[$person_id]++; if ($hdate < $mindate[$person_id]) { $mindate[$person_id] = $hdate; } if ($hdate > $maxdate[$person_id]) { $maxdate[$person_id] = $hdate; } } # Fetch details of all the speakers $speakers = array(); $pids = array(); if (count($speaker_count)) { $person_ids = join(',', array_keys($speaker_count)); $q = $db->query('SELECT member_id, member.person_id, title, given_name, family_name, lordofname, constituency, house, party, moffice_id, dept, position, from_date, to_date, left_house FROM member LEFT JOIN moffice ON member.person_id = moffice.person 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 WHERE member.person_id IN (' . $person_ids . ') ' . ($house ? " AND house={$house}" : '') . ' ORDER BY left_house DESC'); for ($n = 0; $n < $q->rows(); $n++) { $mid = $q->field($n, 'member_id'); if (!isset($pids[$mid])) { $title = $q->field($n, 'title'); $first = $q->field($n, 'given_name'); $last = $q->field($n, 'family_name'); $lordofname = $q->field($n, 'lordofname'); $house = $q->field($n, 'house'); $party = $q->field($n, 'party'); $full_name = ucfirst(member_full_name($house, $title, $first, $last, $lordofname)); $pid = $q->field($n, 'person_id'); $pids[$mid] = $pid; $speakers[$pid]['house'] = $house; $speakers[$pid]['left'] = $q->field($n, 'left_house'); } $dept = $q->field($n, 'dept'); $posn = $q->field($n, 'position'); $moffice_id = $q->field($n, 'moffice_id'); if ($dept && $q->field($n, 'to_date') == '9999-12-31') { $speakers[$pid]['office'][$moffice_id] = prettify_office($posn, $dept); } if (!isset($speakers[$pid]['name'])) { $speakers[$pid]['name'] = $full_name . ($house == 1 ? ' MP' : ''); $speakers[$pid]['party'] = $party; } } } if (isset($speaker_count[0])) { $speakers[0] = array('party' => '', 'name' => 'Headings, procedural text, etc.', 'house' => 0, 'count' => 0); } $party_count = array(); $ok = 0; foreach ($speakers as $pid => &$speaker) { $speaker['count'] = $speaker_count[$pid]; $speaker['pmaxdate'] = $maxdate[$pid]; $speaker['pmindate'] = $mindate[$pid]; $ok = 1; if (!isset($party_count[$speaker['party']])) { $party_count[$speaker['party']] = 0; } $party_count[$speaker['party']] += $count; } function sort_by_count($a, $b) { if ($a['count'] > $b['count']) { return -1; } if ($a['count'] < $b['count']) { return 1; } return 0; } uasort($speakers, 'sort_by_count'); arsort($party_count); if (!$ok) { $data['error'] = 'No results'; return $data; } $data['party_count'] = $party_count; $data['speakers'] = $speakers; return $data; }
print '<table><tr><th>No.</th><th>Date</th></tr>'; foreach ($hdates as $hdate => $count) { print '<tr><td>'; print $count . '</td><td>'; print '<a href="' . WEBPATH . 'hansard/?d=' . $hdate . '">'; print $hdate; print '</a>'; print '</td></tr>'; } print '</table>'; */ } else { $SEARCHENGINE = new SEARCHENGINE($searchstring); $pagetitle = 'Search for ' . $SEARCHENGINE->query_description_short(); $pagenum = get_http_var('p'); if (is_numeric($pagenum) && $pagenum > 1) { $pagetitle .= ", page $pagenum"; } $DATA->set_page_metadata($this_page, 'title', $pagetitle); $DATA->set_page_metadata($this_page, 'rss', 'search/rss/?s=' . urlencode($searchstring)); $PAGE->page_start(); $PAGE->stripe_start(); $PAGE->search_form($searchstring); $o = get_http_var('o'); $args = array ( 's' => $searchstring, 'p' => $pagenum,
} arsort($hdates); print '<table><tr><th>No.</th><th>Date</th></tr>'; foreach ($hdates as $hdate => $count) { print '<tr><td>'; print $count . '</td><td>'; print '<a href="' . WEBPATH . 'hansard/?d=' . $hdate . '">'; print $hdate; print '</a>'; print '</td></tr>'; } print '</table>'; */ } else { $SEARCHENGINE = new SEARCHENGINE($searchstring); $pagetitle = "Search: " . $SEARCHENGINE->query_description_short(); $pagenum = get_http_var('p'); if (is_numeric($pagenum) && $pagenum > 1) { $pagetitle .= " page {$pagenum}"; } $DATA->set_page_metadata($this_page, 'title', $pagetitle); $DATA->set_page_metadata($this_page, 'rss', 'search/rss/?s=' . urlencode($searchstring)); $PAGE->page_start_mobile(); $PAGE->stripe_start(); $PAGE->search_form(); $o = get_http_var('o'); $args = array('s' => $searchstring, 'p' => $pagenum, 'num' => get_http_var('num'), 'pop' => get_http_var('pop'), 'o' => $o == 'd' || $o == 'r' ? $o : 'd'); $LIST = new HANSARDLIST(); if ($args['s']) { $db = $LIST->db; find_members($args);
function search_normal($searchstring) { global $PAGE, $DATA, $this_page, $SEARCHENGINE; $SEARCHENGINE = new SEARCHENGINE($searchstring); $qd = $SEARCHENGINE->valid ? $SEARCHENGINE->query_description_short() : $searchstring; $pagetitle = 'Search for ' . $qd; $pagenum = get_http_var('p'); if (!is_numeric($pagenum)) { $pagenum = 1; } if ($pagenum > 1) { $pagetitle .= ", page {$pagenum}"; } $DATA->set_page_metadata($this_page, 'title', $pagetitle); $DATA->set_page_metadata($this_page, 'rss', 'search/rss/?s=' . urlencode($searchstring)); if ($pagenum == 1) { # Allow indexing of first page of search results $DATA->set_page_metadata($this_page, 'robots', ''); } $PAGE->page_start(); $PAGE->stripe_start(); $PAGE->search_form($searchstring); $o = get_http_var('o'); $args = array('s' => $searchstring, 'p' => $pagenum, 'num' => get_http_var('num'), 'pop' => get_http_var('pop'), 'o' => $o == 'd' || $o == 'r' || $o == 'o' ? $o : 'd'); if ($pagenum == 1 && $args['s'] && !preg_match('#[a-z]+:[a-z0-9]+#', $args['s'])) { find_members($args['s']); find_constituency($args); find_glossary_items($args); } if (!defined('FRONT_END_SEARCH') || !FRONT_END_SEARCH) { print '<p>Apologies, search has been turned off currently for performance reasons.</p>'; } if (!$SEARCHENGINE->valid) { $PAGE->error_message($SEARCHENGINE->error); } else { $LIST = new HANSARDLIST(); $LIST->display('search', $args); } }