Export for OpenSpending
Export for OpenSpending

<?php <?php
include_once ("../../lib/common.inc.php"); include_once ("../../lib/common.inc.php");
function processFile($fpath, $tablename) function processFile($fpath, $tablename)
{ {
global $conn; global $conn;
echo " ============== $fpath ============== <br>"; echo " ============== $fpath ============== <br>";
flush(); flush();
$row = 1; $row = 1;
$success = 0; $success = 0;
$dupes = 0; $dupes = 0;
$handle = fopen($fpath, "r"); $handle = fopen($fpath, "r");
//"t" mode string translates windows line breaks to unix //"t" mode string translates windows line breaks to unix
$datamapping0507 = array( $datamapping0507 = array(
"Agency" => "agencyName", "Agency" => "agencyName",
"CN ID" => "CNID", "CN ID" => "CNID",
"Publish Date" => "publishDate", "Publish Date" => "publishDate",
"Contract Start Date" => "contractStart", "Contract Start Date" => "contractStart",
"Contract End Date" => "contractEnd", "Contract End Date" => "contractEnd",
"Value (AUD)" => "value", "Value (AUD)" => "value",
"Title" => "description", "Title" => "description",
"Category" => "category", "Category" => "category",
"ATM ID" => "atmID", "ATM ID" => "atmID",
"Supplier Name" => "supplierName", "Supplier Name" => "supplierName",
"LastUpdated" => "amendDate", "LastUpdated" => "amendDate",
"" => "" "" => ""
); );
$headers; $headers;
$contractNoticeFields = array( $contractNoticeFields = array(
"importFile", "importFile",
"CNID", "CNID",
"description", "description",
"agencyName", "agencyName",
"publishDate", "publishDate",
"category", "category",
"contractStart", "contractStart",
"contractEnd", "contractEnd",
"value", "value",
"atmID", "atmID",
"supplierName", "supplierName",
"amendDate" "amendDate"
); );
if ($tablename == "contractnotice") { if ($tablename == "contractnotice") {
$contractNoticeInsertQ = 'INSERT INTO contractnotice ("' . implode('" , "', $contractNoticeFields) . '") VALUES ( '; $contractNoticeInsertQ = 'INSERT INTO contractnotice ("' . implode('" , "', $contractNoticeFields) . '") VALUES ( ';
foreach ($contractNoticeFields as $key => $f) { foreach ($contractNoticeFields as $key => $f) {
$contractNoticeInsertQ.= ($key == 0 ? "" : ", ") . "?"; $contractNoticeInsertQ.= ($key == 0 ? "" : ", ") . "?";
} }
$contractNoticeInsertQ.= ");"; $contractNoticeInsertQ.= ");";
$contractNoticeInsertQ = $conn->prepare($contractNoticeInsertQ); $contractNoticeInsertQ = $conn->prepare($contractNoticeInsertQ);
} }
while (($data = fgetcsv($handle, 1000, "\t")) !== false) { while (($data = fgetcsv($handle, 1000, "\t")) !== false) {
$num = count($data); $num = count($data);
if ($row == 3) { if ($row == 3) {
$headers = $data; $headers = $data;
} }
elseif ($row > 3) { elseif ($row > 3) {
if ($num > count($datamapping0507)) { if ($num > count($datamapping0507)) {
die("<font color=red>Error in data import; data mapping fields out of bounds or changed</font><br>" . $fname . "data:" .$num. print_r($data ,true). "mapping:" . count($datamapping0507). print_r($datamapping0507 ,true)); die("<font color=red>Error in data import; data mapping fields out of bounds or changed</font><br>" . $fname . "data:" .$num. print_r($data ,true). "mapping:" . count($datamapping0507). print_r($datamapping0507 ,true));
} }
$contractNoticeInsert = Array(); $contractNoticeInsert = Array();
$contractNoticeInsert[] = $fpath; $contractNoticeInsert[] = $fpath;
$keys = array_keys($datamapping0507); $keys = array_keys($datamapping0507);
for ($c = 0; $c < $num; $c++) { for ($c = 0; $c < $num; $c++) {
$data[$c] = trim($data[$c], "="); $data[$c] = trim($data[$c], "=");
$data[$c] = trim($data[$c], "\""); $data[$c] = trim($data[$c], "\"");
if ($tablename == "contractnotice") { if ($tablename == "contractnotice") {
if (in_array(($datamapping0507[$headers[$c]]) , $contractNoticeFields)) { if (in_array(($datamapping0507[$headers[$c]]) , $contractNoticeFields)) {
if (($datamapping0507[$headers[$c]]) == "parentCN" || ($datamapping0507[$headers[$c]]) == "CNID") { if (($datamapping0507[$headers[$c]]) == "parentCN" || ($datamapping0507[$headers[$c]]) == "CNID") {
$data[$c] = substr($data[$c], 2); // take off the "CN" prefix $data[$c] = substr($data[$c], 2); // take off the "CN" prefix
$data[$c] = str_replace("-A", "00", $data[$c]); // make amendments really big numbers $data[$c] = str_replace("-A", "00", $data[$c]); // make amendments really big numbers
if (!is_numeric($data[$c]) && $data[$c] != "") die($data[$c] . " is not numeric"); if (!is_numeric($data[$c]) && $data[$c] != "") die($data[$c] . " is not numeric");
if ($data[$c] > 0) { if ($data[$c] > 0) {
$contractNoticeInsert[] = $data[$c]; $contractNoticeInsert[] = $data[$c];
} }
else { else {
$contractNoticeInsert[] = 0; $contractNoticeInsert[] = 0;
} }
} }
elseif (($datamapping0507[$headers[$c]]) == "supplierABN") { elseif (($datamapping0507[$headers[$c]]) == "supplierABN") {
if ($data[$c] > 0) { if ($data[$c] > 0) {
$contractNoticeInsert[] = $data[$c]; $contractNoticeInsert[] = $data[$c];
} }
else { else {
$contractNoticeInsert[] = null; $contractNoticeInsert[] = null;
} }
} }
elseif (($datamapping0507[$headers[$c]]) == "amendDate" || ($datamapping0507[$headers[$c]]) == "publishDate" || ($datamapping0507[$headers[$c]]) == "contractStart" || ($datamapping0507[$headers[$c]]) == "contractEnd") { elseif (($datamapping0507[$headers[$c]]) == "amendDate" || ($datamapping0507[$headers[$c]]) == "publishDate" || ($datamapping0507[$headers[$c]]) == "contractStart" || ($datamapping0507[$headers[$c]]) == "contractEnd") {
$contractNoticeInsert[] = date('Y-m-d H:i:s', strtotime($data[$c])); $contractNoticeInsert[] = date('Y-m-d H:i:s', strtotime($data[$c]));
} }
else { else {
if (strstr("\" =", $data[$c] > 0)) { if (strstr("\" =", $data[$c] > 0)) {
die("Invalid Description field" . $contractNoticeInsert); die("Invalid Description field" . $contractNoticeInsert);
} }
$colvalue = preg_replace( '/[^[:print:]]/', '',utf8_encode( $data[$c])); $colvalue = preg_replace( '/[^[:print:]]/', '',utf8_encode( $data[$c]));
   
$contractNoticeInsert[] = $colvalue; $contractNoticeInsert[] = $colvalue;
} }
} }
} }
} }
flush(); flush();
if ($tablename == "contractnotice") { if ($tablename == "contractnotice") {
$contractNoticeInsertQ->execute($contractNoticeInsert); $contractNoticeInsertQ->execute($contractNoticeInsert);
$errors = $conn->errorInfo(); $errors = $conn->errorInfo();
if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) { if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) {
$dupes++; $dupes++;
} }
elseif ($errors[1] == 0) { elseif ($errors[1] == 0) {
$success++; $success++;
} }
else { else {
foreach ($contractNoticeFields as $key => $cnf) { foreach ($contractNoticeFields as $key => $cnf) {
echo var_dump($contractNoticeInsert[$key]) . $cnf . "<br>"; echo var_dump($contractNoticeInsert[$key]) . $cnf . "<br>";
} }
echo $data[2] . " failed CN insert.<br>" . print_r($errors, true) . "<br> row $row <br><br>\n"; echo $data[2] . " failed CN insert.<br>" . print_r($errors, true) . "<br> row $row <br><br>\n";
} }
} }
flush(); flush();
//echo "<hr>\n"; //echo "<hr>\n";
} }
$row++; $row++;
} }
fclose($handle); fclose($handle);
echo " $dupes duplicate records<br>"; echo " $dupes duplicate records<br>";
echo " $success records successfully created<br>"; echo " $success records successfully created<br>";
flush(); flush();
return $success; return $success;
} }
$path = './'; $path = './';
if ($_REQUEST["fname"] == "") { if ($_REQUEST["fname"] == "") {
echo "Get files from: https://www.tenders.gov.au/?event=public.reports.list<br>"; echo "Get files from: https://www.tenders.gov.au/?event=public.reports.list<br>";
$dhandle = opendir($path); $dhandle = opendir($path);
// define an array to hold the files // define an array to hold the files
$files = array(); $files = array();
if ($dhandle) { if ($dhandle) {
// loop through all of the files // loop through all of the files
while (false !== ($fname = readdir($dhandle))) { while (false !== ($fname = readdir($dhandle))) {
if (($fname != '.') && ($fname != '..')) { if (($fname != '.') && ($fname != '..') && (!isset($_REQUEST["filter"]) || strpos($fname,$_REQUEST["filter"]) != false)) {
echo "<a href=\"import.php?fname=$fname\">$fname</a>&nbsp;" . filesize($path . $fname) . "&nbsp;" . date("c", filemtime($path . $fname)) . "<br/>"; echo "<a href=\"import.php?fname=$fname\">$fname</a>&nbsp;" . filesize($path . $fname) . "&nbsp;" . date("c", filemtime($path . $fname)) . "<br/>";
processFile($path . $fname, "contractnotice"); processFile($path . $fname, "contractnotice");
} }
} }
} }
} }
else { else {
$success = 0; $success = 0;
$fname = $_REQUEST["fname"]; $fname = $_REQUEST["fname"];
$success+= processFile($path . $fname, "contractnotice"); $success+= processFile($path . $fname, "contractnotice");
   
} }
?> ?>
   
  {
  "dataset":{
  "name":"australian_federal_government_contract_spending",
  "label":"Australian Federal Government Contract Spending",
  "description":"Spending by Australian Federal Government agencies on goods and services from 2007 onwards.",
  "currency":"AUD",
  "unique_keys":[
  "id"
  ],
  "temporal_granularity":"year"
  },
  "mapping":{
  "category": {
  "fields": [ {
  "column": "category",
  "datatype": "string",
  "name": "label"
  } ],
 
  "type": "classifier",
  "description": "",
  "taxonomy": "unspsc",
  "label": "Contract Goods/Services Category"
  },
  "from":{
  "fields":[
  {
  "column":"agencyABN",
  "datatype":"string",
  "name":"id"
  },
  {
  "column":"agencyName",
  "datatype":"string",
  "name":"label"
  }
  ],
  "type":"entity",
  "description":"",
  "label":"Government Agency Australian Business Number"
  },
  "description":{
  "column":"description",
  "datatype":"string",
  "type":"value",
  "description":"Contract as described by agency",
  "label":"Contract Description"
  },
  "to":{
  "fields":[
  {
  "column":"supplierName",
  "datatype":"string",
  "name":"label"
  },
  {
  "column":"supplierID",
  "datatype":"string",
  "name":"id"
  }
  ],
  "type":"entity",
  "description":"Government Supplier",
  "label":"Supplier Name"
  },
  "currency":{
  "default_value":"AUD",
  "description":"All entries in AUD, foreign transactions are converted at the time of their payment",
  "column":"",
  "label":"",
  "datatype":"currency",
  "type":"value"
  },
  "amount":{
  "column":"value",
  "datatype":"float",
  "type":"value",
  "description":"The total value of the contract including all variations/amendments/extensions",
  "label":"Contract Value"
  },
  "time":{
  "column":"contractStart",
  "datatype":"date",
  "type":"value",
  "description":"Start of the contract period (goods or services being received)",
  "label":"Contract Start Date"
  },
  "id":{
  "column": "CNID",
  "datatype": "string",
  "name": "label",
  "type": "value",
  "description": "",
  "label": "Contract Notice ID"
  }
  },
 
  "views": [
  {
  "name": "default",
  "entity": "dataset",
  "label": "Default breakdown by Category",
  "dimension": "dataset",
  "breakdown": "category",
  "filters" : {"name": "australian_federal_government_contract_spending" }
  },
  {
  "name":"default",
  "entity": "classifier",
  "label":"Breakdown of Spending Areas by Supplier Company",
  "dimension": "category",
  "breakdown": "to",
  "filters": {"taxonomy": "unspsc"}
  },
  {
  "name":"default",
  "entity": "classifier",
  "label":"Breakdown of Spending Areas by Government Agency",
  "dimension": "category",
  "breakdown": "from",
  "filters": {"taxonomy": "unspsc"}
  }
  ]
  }
 
<?php <?php
   
include_once("./lib/common.inc.php"); include_once("./lib/common.inc.php");
setlocale(LC_CTYPE, 'C'); setlocale(LC_CTYPE, 'C');
// source: http://stackoverflow.com/questions/81934/easy-way-to-export-a-sql-table-without-access-to-the-server-or-phpmyadmin#81951 // source: http://stackoverflow.com/questions/81934/easy-way-to-export-a-sql-table-without-access-to-the-server-or-phpmyadmin#81951
$query = $conn->prepare(' $query = $conn->prepare('
SELECT "CNID",contractnotice."agencyName",agency.abn as "agencyABN",DATE("publishDate"),"contractStart","contractEnd",value,description,"procurementMethod",category,"categoryUNSPSC" SELECT "CNID",contractnotice."agencyName",agency.abn as "agencyABN",EXTRACT(EPOCH FROM "publishDate") as "publishDate",EXTRACT(EPOCH FROM "contractStart") as "contractStart",EXTRACT(EPOCH FROM "contractEnd") as "contractEnd",value,description,"procurementMethod",category,"categoryUNSPSC",
,"supplierABN","supplierName","supplierCity","supplierPostcode","supplierCountry","contactPostcode", "supplierABN","supplierName","supplierCity","supplierPostcode","supplierCountry","contactPostcode",
( (
case when "supplierABN" != 0 THEN "supplierABN"::text ELSE "supplierName" END) as supplierID, case when "supplierABN" != 0 THEN "supplierABN"::text ELSE "supplierName" END) as supplierID,
(\'https://www.tenders.gov.au/?event=public.advancedsearch.keyword&keyword=CN\'::text || "CNID"::text) as sourceURL (\'https://www.tenders.gov.au/?event=public.advancedsearch.keyword&keyword=CN\'::text || "CNID"::text) as sourceURL
FROM contractnotice join agency on contractnotice."agencyName"=agency."agencyName" where "childCN" = 0 limit 5'); FROM contractnotice join agency on contractnotice."agencyName"=agency."agencyName" where "childCN" = 0');
  // (substr( "categoryUNSPSC"::text, 0, 2 ) || \'0000000\'::text) as "categoryUNSPSClv1", "categoryUNSPSC", (substr( "categoryUNSPSC"::text, 0, 3 ) || \'000000\'::text) as "categoryUNSPSClv2" "categoryUNSPSC", (substr( "categoryUNSPSC"::text, 0, 4 ) || \'00000\'::text as "categoryUNSPSClv3")
$query->execute(); $query->execute();
if (!$query) { if (!$query) {
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
die('Couldn\'t fetch records'); die('Couldn\'t fetch records');
} }
   
$num_fields = $query->columnCount(); $num_fields = $query->columnCount();
$headers = array(); $headers = array();
for ($i = 0; $i < $num_fields; $i++) { for ($i = 0; $i < $num_fields; $i++) {
$meta = $query->getColumnMeta($i); $meta = $query->getColumnMeta($i);
$headers[] = $meta['name']; $headers[] = $meta['name'];
} }
$fp = fopen('php://output', 'w'); $fp = fopen('php://output', 'w');
if ($fp && $query) { if ($fp && $query) {
header('Content-Type: text/csv'); header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.'.date("c").'.csv"'); header('Content-Disposition: attachment; filename="export.'.date("c").'.csv"');
header('Pragma: no-cache'); header('Pragma: no-cache');
header('Expires: 0'); header('Expires: 0');
fputcsv($fp, $headers); fputcsv($fp, $headers);
foreach ($query->fetchAll(PDO::FETCH_NUM) as $row) { foreach ($query->fetchAll(PDO::FETCH_NUM) as $row) {
foreach ($row as &$colvalue) { foreach ($row as $key => &$colvalue) {
   
$colvalue = preg_replace( '/[^[:print:]]/', '', $colvalue = preg_replace( '/[^[:print:]]/', '',
utf8_encode($colvalue)); utf8_encode($colvalue));
  if ($headers[$key] == "publishDate" || $headers[$key] == "contractStart"
  || $headers[$key] == "contractEnd") {
  $colvalue = date("Y-m-d",$colvalue);
  }
} }
fputcsv($fp, array_values($row)); fputcsv($fp, array_values($row));
} }
die; die;
} }
?> ?>