Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d50b27c97b
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 86 lines (69 sloc) 2.4 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
<?php

// NOTE: This is now the master copy, the file postcode.php here:
// http://cvs.sourceforge.net/viewcvs.py/publicwhip/publicwhip/website/
// is copied from this openaustralia.org version.

include_once INCLUDESPATH."constituencies.inc";


// Whether the form of the postcode is one or not
function is_postcode($postcode)
{
    //return preg_match("/^[A-Z]{1,2}\d[A-Z\d]? ?\d[ABD-HJLNP-UW-Z]{2}$/i", $postcode);
return validate_postcode($postcode); // in utility.php
}

//returns the constituency name, given uh, a postcode.
function postcode_to_constituency($postcode) {
    global $last_postcode;
    global $last_postcode_value;

    #if(!defined("POSTCODE_SEARCH_DOMAIN") || !POSTCODE_SEARCH_DOMAIN) {
    # return fake_postcode($postcode);
    #}

    $postcode = canonicalise_postcode($postcode);

    if ($last_postcode == $postcode) {
        twfy_debug ("TIME", "Postcode $postcode looked up last time, is $last_postcode_value .");
        return $last_postcode_value;
    }

    $start = getmicrotime();
    twfy_debug_timestamp();
    $ret = postcode_to_constituency_internal($postcode);
    $duration = getmicrotime() - $start;
    twfy_debug ("TIME", "Postcode $postcode lookup took $duration seconds, returned $ret.");
    twfy_debug_timestamp();
    $last_postcode = $postcode;
    $last_postcode_value = $ret;
    return $ret;
}

// map a postcode to an MP, random but deterministic
function fake_postcode ($postcode){
$db = new ParlDB;
$fake_cons_id=abs(crc32($postcode)%630);
  $query = "select name from constituency where main_name and cons_id = '".$fake_cons_id."'";
    $q2 = $db->query($query);
    if ($q2->rows <= 0)
        return false;

    return $q2->field(0, "name");
}

function postcode_to_constituency_internal($postcode) {
# Try and match with regexp to exclude non postcodes quickly
if (!is_postcode($postcode))
return '';
  
$db = new ParlDB;

$q = $db->query('select name from postcode_lookup where postcode = "'. mysql_escape_string($postcode).'"');
if ($q->rows == 1) {
$name = $q->field(0, 'name');
return $name;
}
elseif ($q->rows > 1) {
for ($i=0; $i < $q->rows; $i++)
$name[] = $q->field($i, 'name');
return $name;
}
else
return '';
}

function canonicalise_postcode($pc) {
$pc = str_replace(' ', '', $pc);
$pc = trim($pc);
$pc = strtoupper($pc);
$pc = preg_replace('#(\d[A-Z]{2})#', ' $1', $pc);
return $pc;
}

?>
Something went wrong with that request. Please try again.