<commit_before><?php require_once "common.inc";
# $Id: search.php,v 1.35 2005/03/28 10:53:41 frabcus Exp $

# The Public Whip, Copyright (C) 2003 <NAME> and <NAME>
# This is free software, and you are welcome to redistribute it under
# certain conditions.  However, it comes with ABSOLUTELY NO WARRANTY.
# For details see the file LICENSE.html in the top level of the source.

    include "db.inc";
    $prettyquery = html_scrub(trim($_GET["query"]));
    $query = strtolower(db_scrub(trim($_GET["query"])));
    $title = "Search for '$prettyquery'"; 
    if ($prettyquery == "") {
        $onload = "givefocus('query')";
        $title = "Search";
    }
    include "render.inc";
    include "parliaments.inc";
    include "constituencies.inc";
    include "postcode.inc";

    $db = new DB(); 

    $postcode = is_postcode($query);
    $header = false;

    if ($query <> "")
    {
        $found = false;

        if (!$postcode)
        {
            $header = true;
            include "header.inc";
            
            # Perform query on divisions
            $db->query("$divisions_query_start and (upper(division_name) like '%$query%'
                or upper(motion) like '%$query%')
                order by division_date desc, division_number desc");

            if ($db->rows() > 0)
            {
                $found = true;
                print "<p>Found these " . $db->rows() . " divisions matching '$prettyquery':";
                print "<table class=\"votes\">\n";
                print "<tr
                class=\"headings\"><td>No.</td><td>Date</td><td>Subject</td><td>Rebellions</td><td>Turnout</td></tr>";
                render_divisions_table($db);
                print "</table>\n";
            }
        }

        # Perform query on MPs
        $score_clause = "(";
        $score_clause .= "(upper(concat(first_name, ' ', last_name)) = '$query') * 10";


        $querybits = explode(" ", $query);

        foreach ($querybits as $querybit)
        {
            $querybits = trim($querybits);
            if ($querybits != "")
            {
                $score_clause .= "+ (upper(constituency) = '$querybit') * 10 + 
                (soundex(concat(first_name, ' ', last_name)) = soundex('$querybit')) * 8 + 
                (soundex(constituency) = soundex('$querybit')) * 8 + 
                (soundex(last_name) = soundex('$querybit')) * 6 + 
                (upper(constituency) like '%$querybit%') * 4 + 
                (upper(last_name) like '%$querybit%') * 4 + 
                (soundex(first_name) = soundex('$querybit')) * 2 + 
                (upper(first_name) like '%$querybit%') + 
                (soundex(constituency) like concat('%',soundex('$querybit'),'%'))";
            }
        }
        $score_clause .= ")";

        if ($postcode)
        {
#            print "Postcode searching is not available.  Please search for your MP name. If you don't know their name, you can try searching for your town or district name, or use <a href=\"http://www.faxyourmp.com\">FaxYourMP</a> to find your MP name from postcode.";
            $score_clause = "(1=0)";
            $pccons = postcode_to_constituency($query);
            if (isset($pccons))
            {
                # Overwrite over matches if we have postcode
                $score_clause = "(constituency = '" . db_scrub($pccons) . "')";
            }
        }

        $db->query("$mps_query_start and ($score_clause > 0) 
                    order by $score_clause desc, constituency, entered_house desc, last_name, first_name");

        # Even with postcode, we check the query first, as the search page gives better error message
        if ($postcode and $db->rows() > 0)
        {
            header("Location: mp.php?constituency=" . urlencode($pccons));
            exit;
        }

        if (!$header)
            include "header.inc";

        if ($db->rows() > 0)
        {
            $found = true;
            print "<p>Found these MPs matching ";
            if ($postcode)
                print "postcode ";
            print "'$prettyquery':";
            print "<table class=\"mps\"><tr
                class=\"headings\"><td>Date</td><td>Name</td><td>Constituency</td><td>Party</td></tr>\n";
            $prettyrow = 0;
            while ($row = $db->fetch_row())
            {
                $prettyrow = pretty_row_start($prettyrow);<commit_msg>Fixed up search<commit_after><?php require_once "common.inc";
# $Id: search.php,v 1.36 2005/03/28 11:28:39 frabcus Exp $

# The Public Whip, Copyright (C) 2003 <NAME> and <NAME>
# This is free software, and you are welcome to redistribute it under
# certain conditions.  However, it comes with ABSOLUTELY NO WARRANTY.
# For details see the file LICENSE.html in the top level of the source.

    include "db.inc";
    $prettyquery = html_scrub(trim($_GET["query"]));
    $query = strtolower(db_scrub(trim($_GET["query"])));
    $title = "Search for '$prettyquery'"; 
    if ($prettyquery == "") {
        $onload = "givefocus('query')";
        $title = "Search";
    }
    include "render.inc";
    include "parliaments.inc";
    include "constituencies.inc";
    include "postcode.inc";

    $db = new DB(); 

    $postcode = is_postcode($query);
    $header = false;

    if ($query <> "")
    {
        $found = false;

        if (!$postcode)
        {
            $header = true;
            include "header.inc";
            
            # Perform query on divisions
            $db->query("$divisions_query_start and (lower(division_name) like '%$query%'
                or lower(motion) like '%$query%')
                order by division_date desc, division_number desc");

            if ($db->rows() > 0)
            {
                $found = true;
                print "<p>Found these " . $db->rows() . " divisions matching '$prettyquery':";
                print "<table class=\"votes\">\n";
                print "<tr
                class=\"headings\"><td>No.</td><td>Date</td><td>Subject</td><td>Rebellions</td><td>Turnout</td></tr>";
                render_divisions_table($db);
                print "</table>\n";
            }
        }

        # Perform query on MPs
        $score_clause = "(";
        $score_clause .= "(lower(concat(first_name, ' ', last_name)) = '$query') * 10";


        $querybits = explode(" ", $query);

        foreach ($querybits as $querybit)
        {
            $querybits = trim($querybits);
            if ($querybits != "")
            {
                $score_clause .= "+ (lower(constituency) = '$querybit') * 10 + 
                (soundex(concat(first_name, ' ', last_name)) = soundex('$querybit')) * 8 + 
                (soundex(constituency) = soundex('$querybit')) * 8 + 
                (soundex(last_name) = soundex('$querybit')) * 6 + 
                (lower(constituency) like '%$querybit%') * 4 + 
                (lower(last_name) like '%$querybit%') * 4 + 
                (soundex(first_name) = soundex('$querybit')) * 2 + 
                (lower(first_name) like '%$querybit%') + 
                (soundex(constituency) like concat('%',soundex('$querybit'),'%'))";
            }
        }
        $score_clause .= ")";

        if ($postcode)
        {
#            print "Postcode searching is not available.  Please search for your MP name. If you don't know their name, you can try searching for your town or district name, or use <a href=\"http://www.faxyourmp.com\">FaxYourMP</a> to find your MP name from postcode.";
            $score_clause = "(1=0)";
            $pccons = postcode_to_constituency($query);
            if (isset($pccons))
            {
                # Overwrite over matches if we have postcode
                $score_clause = "(constituency = '" . db_scrub($pccons) . "')";
            }
        }

        $db->query("$mps_query_start and ($score_clause > 0) 
                    order by $score_clause desc, constituency, entered_house desc, last_name, first_name");

        # Even with postcode, we check the query first, as the search page gives better error message
        if ($postcode and $db->rows() > 0)
        {
            header("Location: mp.php?constituency=" . urlencode($pccons));
            exit;
        }

        if (!$header)
            include "header.inc";

        if ($db->rows() > 0)
        {
            $found = true;
            print "<p>Found these MPs whose names sound like ";
            if ($postcode)
                print "postcode ";
            print "'$prettyquery':";
            print "<table class=\"mps\"><tr
                class=\"headings\"><td>Date</td><td>Name</td><td>Constituency</td><td>Party</td></tr>\n";
            $prettyrow = 0;
            while ($row = $db->fetch_row())
            {
                $prettyrow = pretty_row_start($prettyrow);
{'fraction_non_alphanumeric': 0.15236148955495005, 'fraction_numerical': 0.00862851952770209, 'mean_word_length': 1.9130291005291005, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5243326', 'n_tokens_mistral': 2742, 'n_tokens_neox': 2575, 'n_words': 630}