function candidatesArePeopleToo()
{
    $s = mysql_query("SELECT * FROM candidates");
    while ($r = mysql_fetch_array($s)) {
        $name = $r['name_cleaned'];
        $sid = $r['id'];
        if (strpos($r['url'], "http://") === 0) {
            $url = $r['url'];
        } else {
            $url = 'http://www.alegeri-2008.ro' . $r['url'];
        }
        $idString = '<a href=' . $url . '>alegeri2008</a>';
        $persons = getPersonsByName($name, $idString);
        // If I reached this point, I know for sure I either have one
        // or zero matches, there are no ambiguities.
        if (count($persons) == 0) {
            $person = addPersonToDatabase($name, $r['name_cleaned']);
        } else {
            $person = $persons[0];
            info("Found      {" . $name . "}");
        }
        // First and foremost, attempt to add this to the person's history.
        addPersonHistory($person->id, "alegeri/2008", $url);
        // Locate these people and update them in several tables now.
        // We're mainly interested in updating the ID to the new ID that
        // will be in the People database, from the previous ID that was
        // the senator's ID in the senators table.
        mysql_query("UPDATE candidates " . "SET idperson={$person->id} " . "WHERE id=" . $r['id']);
    }
}
function candidatesArePeopleToo()
{
    global $FLAG_CAN_CHANGE_DB;
    $s = mysql_query("\n    SELECT c.id, c.name, c.idm\n    FROM cdep_2008_deputies AS c\n    where idperson=0");
    while ($r = mysql_fetch_array($s)) {
        $name = $r['name'];
        // Get the party
        $ps = mysql_query("\n        SELECT c.iddep, p.id, p.name\n        FROM cdep_2008_belong as c\n        LEFT JOIN parties AS p ON p.id = c.idparty\n        WHERE iddep = {$r['idm']}\n        ORDER BY c.time DESC");
        $rs = mysql_fetch_array($ps);
        $idString = $rs['name'];
        $persons = getPersonsByName($name, $idString, infoFunction);
        // If I reached this point, I know for sure I either have one
        // or zero matches, there are no ambiguities.
        if (count($persons) == 0) {
            $person = addPersonToDatabase($name, $r['name']);
        } else {
            $person = $persons[0];
        }
        // First and foremost, attempt to add this to the person's history.
        addPersonHistory($person->id, "cdep/2008", "http://www.cdep.ro/pls/parlam/structura.mp?idm={$r['idm']}&cam=2&leg=2008", 1230768000);
        // Locate these people and update them in several tables now.
        // We're mainly interested in updating the ID to the new ID that
        // will be in the People database, from the previous ID that was
        // the senator's ID in the senators table.
        if ($FLAG_CAN_CHANGE_DB) {
            mysql_query("UPDATE cdep_2008_deputies " . "SET idperson={$person->id} " . "WHERE id=" . $r['id']);
        }
    }
    printJsCommitCookieScript();
}
function candidatesArePeopleToo()
{
    global $FLAG_CAN_CHANGE_DB;
    $s = mysql_query("\n    SELECT q.id, q.name, count(*) as num\n    FROM news_qualifiers AS q\n    WHERE idperson=0\n    GROUP BY name\n    ORDER BY num DESC");
    while ($r = mysql_fetch_array($s)) {
        $name = $r['name'];
        // Get the party
        $idString = 'none now';
        $persons = getPersonsByName($name, $idString, infoFunction);
        // If I reached this point, I know for sure I either have one
        // or zero matches, there are no ambiguities.
        if (count($persons) == 0) {
            $person = addPersonToDatabase($name, $r['name']);
        } else {
            $person = $persons[0];
            mysql_query("UPDATE news_qualifiers " . "SET idperson={$person->id} " . "WHERE name='" . $r['name'] . "'");
        }
        info('<a href="/politica/api/add_person_update_qualifier.php?name=' . $r['name'] . '" target="_blank">add</a>');
        // Locate these people and update them in several tables now.
        // We're mainly interested in updating the ID to the new ID that
        // will be in the People database, from the previous ID that was
        // the senator's ID in the senators table.
        if ($FLAG_CAN_CHANGE_DB) {
            mysql_query("UPDATE news_qualifiers " . "SET idperson={$person->id} " . "WHERE name='" . $r['name'] . "'");
        }
    }
    printJsCommitCookieScript();
}
function candidatesArePeopleToo()
{
    global $FLAG_CAN_CHANGE_DB;
    $s = mysql_query("SELECT id, name, t, url, party " . "FROM catavencu_2008 " . "WHERE idperson=0");
    while ($r = mysql_fetch_array($s)) {
        $name = trim($r['name']);
        $idString = '' . $r['url'] . ', ' . $r['party'];
        $persons = getPersonsByName($name, $idString, infoFunction);
        // If I reached this point, I know for sure I either have one
        // or zero matches, there are no ambiguities.
        if (count($persons) == 0) {
            $person = addPersonToDatabase($name, $r['name']);
        } else {
            $person = $persons[0];
        }
        // First and foremost, attempt to add this to the person's history.
        addPersonHistory($person->id, "catavencu/2008", $r['url']);
        // Locate these people and update them in several tables now.
        // We're mainly interested in updating the ID to the new ID that
        // will be in the People database, from the previous ID that was
        // the senator's ID in the senators table.
        if ($FLAG_CAN_CHANGE_DB) {
            mysql_query("UPDATE catavencu_2008 " . "SET idperson={$person->id} " . "WHERE id=" . $r['id']);
        }
    }
    printJsCommitCookieScript();
}
function candidatesArePeopleToo()
{
    global $FLAG_CAN_CHANGE_DB;
    $s = mysql_query("SELECT e.id, e.name, e.idparty, p.name as pname \n    FROM euro_2009_candidates AS e\n    LEFT JOIN parties AS p ON p.id = e.idparty \n    where idperson=0\n    ");
    while ($r = mysql_fetch_array($s)) {
        $name = $r['name'];
        $idString = '' . $r['pname'] . '';
        $persons = getPersonsByName($name, $idString, infoFunction);
        // If I reached this point, I know for sure I either have one
        // or zero matches, there are no ambiguities.
        if (count($persons) == 0) {
            $person = addPersonToDatabase($name, $r['name']);
        } else {
            $person = $persons[0];
        }
        // First and foremost, attempt to add this to the person's history.
        addPersonHistory($person->id, "euro/2009", "http://ro.wikipedia.org/wiki/Alegeri_pentru_Parlamentul_European_%C3%AEn_Rom%C3%A2nia,_2009", 1244349442);
        // Locate these people and update them in several tables now.
        // We're mainly interested in updating the ID to the new ID that
        // will be in the People database, from the previous ID that was
        // the senator's ID in the senators table.
        if ($FLAG_CAN_CHANGE_DB) {
            mysql_query("UPDATE euro_2009_candidates " . "SET idperson={$person->id} " . "WHERE id=" . $r['id']);
        }
    }
    printJsCommitCookieScript();
}
function candidatesArePeopleToo()
{
    global $FLAG_CAN_CHANGE_DB;
    $s = mysql_query("\n    SELECT c.id, c.name\n    FROM govro_people AS c\n    where idperson=0");
    while ($r = mysql_fetch_array($s)) {
        $name = $r['name'];
        // Get the party
        $idString = 'none now';
        $persons = getPersonsByName($name, $idString, infoFunction);
        // If I reached this point, I know for sure I either have one
        // or zero matches, there are no ambiguities.
        if (count($persons) == 0) {
            $person = addPersonToDatabase($name, $r['name']);
        } else {
            $person = $persons[0];
        }
        // First and foremost, attempt to add this to the person's history.
        addPersonHistory($person->id, "gov/ro", "www.gov.ro", 1258848000);
        // Locate these people and update them in several tables now.
        // We're mainly interested in updating the ID to the new ID that
        // will be in the People database, from the previous ID that was
        // the senator's ID in the senators table.
        if ($FLAG_CAN_CHANGE_DB) {
            mysql_query("UPDATE govro_people " . "SET idperson={$person->id} " . "WHERE id=" . $r['id']);
        }
    }
    printJsCommitCookieScript();
}
function senatorsArePeopleToo()
{
    $s = mysql_query("SELECT * FROM senat_2004_senators");
    while ($r = mysql_fetch_array($s)) {
        $name = $r['name'];
        $sid = $r['id'];
        $persons = getPersonsByName($name);
        // If I reached this point, I know for sure I either have one
        // or zero matches, there are no ambiguities.
        if (count($persons) == 0) {
            $person = addPersonToDatabase($name, $r['name_diacritics']);
        } else {
            $person = $persons[0];
        }
        // First and foremost, attempt to add this to the person's history.
        addPersonHistory($person->id, "senat/2004", "http://www.cdep.ro/pls/" . "parlam/structura.mp?idm=" . $r['idm'] . "&cam=1&leg=2004");
        // Locate these people and update them in several tables now.
        // We're mainly interested in updating the ID to the new ID that
        // will be in the People database, from the previous ID that was
        // the senator's ID in the senators table.
        // senators() table.
        mysql_query("UPDATE senators SET idperson={$person->id} WHERE id={$sid}");
        mysql_query("UPDATE senators_belongs " . "SET idperson={$person->id} " . "WHERE idsen={$sid}");
        mysql_query("UPDATE senators_belongs_agg " . "SET idperson={$person->id} " . "WHERE idsen={$sid}");
        mysql_query("UPDATE votes_senators " . "SET idperson={$person->id} " . "WHERE idsen={$sid}");
        mysql_query("UPDATE votes_senators_agg " . "SET idperson={$person->id} " . "WHERE idsen={$sid}");
        mysql_query("UPDATE away_times " . "SET idperson={$person->id} " . "WHERE iddepsen={$sid} AND chamber=1");
    }
}
function addPerson($name)
{
    $persons = getPersonsByName($name);
    // If I reached this point, I know for sure I either have one
    // or zero matches, there are no ambiguities.
    if (count($persons) == 0) {
        $person = addPersonToDatabase($name, $name);
        return $person->id;
    }
    return -1;
}
function addContentToPerson($name, $context, $content, $source)
{
    $results = getPersonsByName($name, $context, infoFunction);
    $person = $results[0];
    info(">> add content to person: " . $person->id);
    info(" ");
    $sql = "INSERT INTO people_facts(idperson, attribute, value) " . "VALUES({$person->id}, 'romaniacurata/2011', '{$content}')";
    mysql_query($sql);
    $sql = "INSERT INTO people_history(idperson, what, url, time) " . "VALUES({$person->id}, 'romaniacurata/2011', '{$source}', 0)";
    mysql_query($sql);
}
function importContactDetails()
{
    global $detail_keys;
    // First, delete everything that was there already.
    mysql_query("DELETE FROM  `people_facts`\n               WHERE  `attribute` LIKE  'contact/%'");
    $agenda_url = "http://agenda.grep.ro/download?format=json";
    $agenda = file_get_contents($agenda_url, 0, null, null);
    $agenda_output = json_decode($agenda);
    foreach ($agenda_output->persons as $personData) {
        // Find this person in our database. Seems like the IDs did not translate
        // well so we have to look at the names.
        if (shouldSkipPerson($personData)) {
            continue;
        }
        $results = getPersonsByName($personData->name, '', infoFunction);
        $person = $results[0];
        foreach ($detail_keys as $dkey) {
            if (!array_key_exists($dkey, $personData)) {
                continue;
            }
            foreach ($personData->{$dkey} as $dval) {
                info(" + {$dkey}: {$dval}");
                if ($dkey == 'phone') {
                    $dval = getCanonicalPhoneNumber($dval);
                    // Incorrect phone number
                    if (!$dval) {
                        continue;
                    }
                }
                if ($dkey == 'website') {
                    $dval = getCanonicalWebsite($dval);
                    // Incorrect phone number
                    if (!$dval) {
                        continue;
                    }
                }
                info(" + {$dkey}: {$dval}");
                $sql = "INSERT INTO people_facts(idperson, attribute, value) " . "VALUES({$person->id}, 'contact/{$dkey}', '{$dval}')";
                mysql_query($sql);
            }
        }
    }
}
function candidatesArePeopleToo()
{
    global $FLAG_CAN_CHANGE_DB;
    $s = mysql_query("SELECT e.id, e.name, e.text \n    FROM euro_parliament_2007_qvorum AS e\n    WHERE idperson=0\n    ");
    while ($r = mysql_fetch_array($s)) {
        $name = $r['name'];
        $persons = getPersonsByName($name, $name, infoFunction);
        // If I reached this point, I know for sure I either have one
        // or zero matches, there are no ambiguities.
        if (count($persons) == 0) {
            $person = addPersonToDatabase($name, $r['name']);
        } else {
            $person = $persons[0];
        }
        // First and foremost, attempt to add this to the person's history.
        addPersonHistory($person->id, "qvorum/2009", "http://qvorum.ro/ViewAnalyse.aspx?analyseID=13", 1242508980);
        if (strpos($r['text'], "o contribuţie slabă") > 0) {
            $score = 0;
        } else {
            if (strpos($r['text'], "o contribuţie medie") > 0) {
                $score = 1;
            } else {
                if (strpos($r['text'], "o contribuţie bună") > 0) {
                    $score = 2;
                } else {
                    if (strpos($r['text'], "o contribuţie deosebită") > 0) {
                        $score = 3;
                    }
                }
            }
        }
        mysql_query("UPDATE euro_parliament_2007_qvorum SET score={$score} \n                 WHERE id={$r['id']}");
        // Locate these people and update them in several tables now.
        // We're mainly interested in updating the ID to the new ID that
        // will be in the People database, from the previous ID that was
        // the senator's ID in the senators table.
        if ($FLAG_CAN_CHANGE_DB) {
            mysql_query("UPDATE euro_parliament_2007_qvorum " . "SET idperson={$person->id} " . "WHERE id=" . $r['id']);
        }
    }
    printJsCommitCookieScript();
}
function candidatesArePeopleToo()
{
    global $FLAG_CAN_CHANGE_DB;
    $s = mysql_query("SELECT * FROM results_2008_candidates WHERE idperson=0");
    while ($r = mysql_fetch_array($s)) {
        $name = $r['nume'];
        $sid = $r['id'];
        list($cod, $judet) = split(" ", $r['college']);
        $idString = "{$judet}-{$cod}";
        if ($judet == 'Strainatate') {
            $url = "http://www.thinkopolis.eu/colegiu/Diaspora-{$cod}";
        } else {
            $jsel = mysql_query("SELECT id FROM counties WHERE name='{$judet}'");
            if ($rsel = mysql_fetch_array($jsel)) {
                $url = "http://alegeri-2008.ro/candidati/x-{$rsel['id']}/";
            }
        }
        // Let's attempt to create an id string. Best guess we have for these
        // people is the info-alegeri site. We would like to avoid linking to the
        // BEC pdf's, even though those would be the best ones.
        $persons = getPersonsByName($name, $idString, getExistingPersonInfo);
        // If I reached this point, I know for sure I either have one
        // or zero matches, there are no ambiguities.
        if (count($persons) == 0) {
            $person = addPersonToDatabase($name, $name);
        } else {
            $person = $persons[0];
        }
        // First and foremost, attempt to add this to the person's history.
        addPersonHistory($person->id, "results/2008", $url);
        // Locate these people and update them in several tables now.
        // We're mainly interested in updating the ID to the new ID that
        // will be in the People database, from the previous ID that was
        // the senator's ID in the senators table.
        if ($FLAG_CAN_CHANGE_DB == true) {
            mysql_query("UPDATE results_2008_candidates " . "SET idperson={$person->id} " . "WHERE id={$sid}");
        }
    }
    printJsCommitCookieScript();
}
function addCandidateToCollege($college, $candidate, $party, $source)
{
    if ($candidate == "") {
        return;
    }
    $context = "+ {{$college}} ({$party}) {$candidate}";
    info($context);
    $results = getPersonsByName($candidate, $context, infoFunction);
    if (count($results) == 0) {
        $person = addPersonToDatabase($candidate, $candidate);
    } else {
        $person = $results[0];
        info("  - person {{$candidate}} id : [" . $person->id . "]");
        // Make sure this matches!
        $s = mysql_query("\n        SELECT colegiu, partid\n        FROM results_2012_old\n        WHERE idperson = {$person->id}");
        $r = mysql_fetch_array($s);
        if ($r) {
            $current_college = ucwords(strtolower(str_replace("-", " ", $r["colegiu"])));
            $exclude_ids = array(1134, 77, 777, 4646, 1676, 3957, 3961, 4512, 2285, 2213, 277, 2220, 2228, 4411, 4407, 4401, 4403, 4750, 4744, 2344, 4409, 1208, 4405, 4405, 2341, 3145, 4473, 4464, 125, 3655, 3658, 3793, 4074, 4415, 4609, 2134, 176, 2207, 4398, 2244, 3888, 4541, 3897, 3901, 3905);
            if ($current_college != "" && $current_college != $college && !in_array($person->id, $exclude_ids)) {
                info("We might have a problem. [{$current_college}] != [{$college}]");
                die;
            }
            $party = $r["partid"];
        }
    }
    $partyId = getPartyId($party);
    if ($partyId <= 0) {
        die("party id gone wrong {$party} {$partyId}");
    }
    // Now that I have the person, let's populate the database with it.
    // We need two things: One an entry in results_2012, and an entry in
    // people_history so we can display the mod on their own page.
    mysql_query("\n      INSERT INTO results_2012(nume, idperson, partid, idpartid, colegiu)\n      values('{$candidate}', {$person->id}, '{$party}', {$partyId}, '{$college}')\n  ");
    mysql_query("\n      INSERT INTO people_history(idperson, what, url, time)\n      values({$person->id}, 'results/2012', '{$source}', 1355032800)\n  ");
}
function addCandidateToCollege($college, $candidate, $party)
{
    if ($candidate == "") {
        return;
    }
    $context = "+ {{$college}} ({$party}) {$candidate}";
    info($context);
    $results = getPersonsByName($candidate, $context, infoFunction);
    if (count($results) == 0) {
        $person = addPersonToDatabase($candidate, $candidate);
    } else {
        $person = $results[0];
        info("  - person {{$candidate}} id : [" . $person->id . "]");
    }
    $partyId = getPartyId($party);
    if ($partyId <= 0) {
        die('party id gone wrong');
    }
    // Now that I have the person, let's populate the database with it.
    // We need two things: One an entry in results_2012, and an entry in
    // people_history so we can display the mod on their own page.
    mysql_query("\n      INSERT INTO results_2012(nume, idperson, partid, idpartid, colegiu)\n      values('{$candidate}', {$person->id}, '{$party}', {$partyId}, '{$college}')\n  ");
    mysql_query("\n      INSERT INTO people_history(idperson, what, url, time)\n      values({$person->id}, 'results/2012', 'http://goo.gl/64gsf', 1355032800)\n  ");
}