Redid direct agency info import
[disclosr.git] / import.php
blob:a/import.php -> blob:b/import.php
<?php <?php
require_once 'sag/src/Sag.php'; require_once 'common.inc.php';
$sag = new Sag();  
$sag->setDatabase("disclosr-agencies", true); $sag->setDatabase("disclosr-agencies", true);
$file = "agencies.csv"; //createAgencyDesignDoc();
$handle = fopen($file, "r"); $conn = new PDO("pgsql:dbname=contractDashboard;user=postgres;password=snmc;host=localhost");
$fieldNames = Array(); $namesQ = 'select agency.abn, string_agg("agencyName",\',\') as names from agency inner join agency_nametoabn on agency.abn::text = agency_nametoabn.abn group by agency.abn;';
$line = 0; $abntonames = Array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { foreach ($conn->query($namesQ) as $row) {
if ($line == 0) { $abntonames[$row['abn']] = explode(",",$row['names']);
$fieldNames = $data; }
} $result = $conn->query("select * from agency");
else { while ($agency = $result->fetch(PDO::FETCH_ASSOC)) {
$agency = Array();  
$valueCount = 0; $agency['otherNames'] = $abntonames[$agency['abn']];
foreach ($data as $value) { if (sizeof($abntonames[$agency['abn']]) == 1) $agency['name'] = $abntonames[$agency['abn']][0];
if ($value != "") $agency[$fieldNames[$valueCount]] = $value;  
$valueCount++;  
}  
$agency["lastScraped"] = "1/1/1970"; $agency["lastScraped"] = "1/1/1970";
$agency["scrapeDepth"] = 1; $agency["scrapeDepth"] = 1;
var_dump($agency);  
$sag->post($agency); $sag->post($agency);
} }
$line++;  
if ($line % 10000 == 0) echo "$line records... \n";  
}  
fclose($handle);  
echo "Found a total of $line records in $file.\n";  
?> ?>