OpenSpending csv updates
OpenSpending csv updates

{ {
"dataset":{ "dataset": {
"name":"australian_federal_government_contract_spending", "languages": [
"label":"Australian Federal Government Contract Spending", "en"
"description":"Spending by Australian Federal Government agencies on goods and services from 2007 onwards.", ],
"currency":"AUD", "currency": "AUD",
"unique_keys":[ "name": "australian_federal_government_contract_spending",
"id" "territories": [],
], "default_time": null,
"temporal_granularity":"year" "description": "Spending by Australian Federal Government agencies on goods and services from 2007 onwards.",
}, "schema_version": "2011-12-07",
"mapping":{ "label": "Australian Federal Government Contract Spending"
"category": {  
"fields": [ {  
"column": "category",  
"datatype": "string",  
"name": "label"  
} ],  
   
"type": "classifier",  
"description": "",  
"taxonomy": "unspsc",  
"label": "Contract Goods/Services Category"  
}, },
"from":{ "mapping": {
"fields":[ "category": {
{ "description": "Contract Goods/Services Category",
"column":"agencyABN", "datatype": "string",
"datatype":"string", "label": "category",
"name":"id" "column": "cat1",
}, "type": "attribute",
{ "dimension": "category"
"column":"agencyName", },
"datatype":"string", "sourceurl": {
"name":"label" "description": "Source Document URL",
} "datatype": "string",
], "label": "sourceurl",
"type":"entity", "column": "sourceurl",
"description":"", "type": "attribute",
"label":"Government Agency Australian Business Number" "dimension": "sourceurl"
}, },
"description":{ "from": {
"column":"description", "attributes": {
"datatype":"string", "name": {
"type":"value", "column": "agencyABN",
"description":"Contract as described by agency", "datatype": "id",
"label":"Contract Description" "type": "id",
}, "description": null
"to":{ },
"fields":[ "label": {
{ "column": "agencyName",
"column":"supplierName", "datatype": "string",
"datatype":"string", "type": "attribute",
"name":"label" "description": null
}, }
{ },
"column":"supplierID", "label": "Government Agency",
"datatype":"string", "type": "compound",
"name":"id" "dimension": "from",
} "description": "Government Agency"
], },
"type":"entity", "description": {
"description":"Government Supplier", "description": null,
"label":"Supplier Name" "datatype": "string",
}, "label": "description",
"currency":{ "column": "description",
"default_value":"AUD", "type": "attribute",
"description":"All entries in AUD, foreign transactions are converted at the time of their payment", "dimension": "description"
"column":"", },
"label":"", "publishdate": {
"datatype":"currency", "description": null,
"type":"value" "format": null,
}, "datatype": "date",
"amount":{ "label": "publishDate",
"column":"value", "column": "publishDate",
"datatype":"float", "type": "date",
"type":"value", "dimension": "publishdate"
"description":"The total value of the contract including all variations/amendments/extensions", },
"label":"Contract Value" "to": {
}, "attributes": {
"time":{ "name": {
"column":"contractStart", "column": "supplierid",
"datatype":"date", "datatype": "id",
"type":"value", "type": "id"
"description":"Start of the contract period (goods or services being received)", },
"label":"Contract Start Date" "label": {
}, "column": "supplierName",
"id":{ "datatype": "string",
"column": "CNID", "type": "attribute"
"datatype": "string", }
"name": "label", },
"type": "value", "label": "Supplier Name",
"description": "", "type": "compound",
"label": "Contract Notice ID" "dimension": "to",
} "description": "Government Supplier"
}, },
  "amount": {
"views": [ "description": "The total value of the contract including all variations/amendments/extensions",
  "datatype": "float",
  "label": "value",
  "column": "value",
  "type": "measure",
  "dimension": "amount"
  },
  "cnid": {
  "description": "Contract Notice ID",
  "datatype": "string",
  "label": "CNID",
  "column": "CNID",
  "key": true,
  "type": "attribute",
  "dimension": "cnid"
  },
  "contractend": {
  "description": "End of the contract period (goods or services being received)",
  "format": null,
  "datatype": "date",
  "label": "contractEnd",
  "column": "contractEnd",
  "type": "date",
  "dimension": "contractend"
  },
  "time": {
  "description": "Start of the contract period (goods or services being received)",
  "format": null,
  "datatype": "date",
  "label": "contractStart",
  "column": "contractStart",
  "type": "date",
  "dimension": "time"
  }
  },
  "views": [
{ {
"name": "default", "name": "default",
"entity": "dataset", "label": "Breakdown by Category",
"label": "Default breakdown by Category", "cuts": {},
"dimension": "dataset", "drilldown": "category",
"breakdown": "category", "entity": "dataset",
"filters" : {"name": "australian_federal_government_contract_spending" } "dimension": "dataset"
}, },
{ {
"name":"default", "name": "by_supplier",
"entity": "classifier", "label": "Breakdown by Supplier",
"label":"Breakdown of Spending Areas by Supplier Company", "cuts": {},
"dimension": "category", "drilldown": "to",
"breakdown": "to", "entity": "dataset",
"filters": {"taxonomy": "unspsc"} "dimension": "dataset"
}, },
{ {
"name":"default", "name": "by_agency",
"entity": "classifier", "label": "Breakdown by Agency",
"label":"Breakdown of Spending Areas by Government Agency", "cuts": {},
"dimension": "category", "drilldown": "from",
"breakdown": "from", "entity": "dataset",
"filters": {"taxonomy": "unspsc"} "dimension": "dataset"
} },
] {
  "name": "default",
  "label": "Agency spending breakdown by supplier",
  "cuts": {},
  "drilldown": "to",
  "entity": "dimension",
  "dimension": "from"
  },
  {
  "name": "default",
  "label": "Supplier contracts breakdown by procuring agency",
  "cuts": {},
  "drilldown": "from",
  "entity": "dimension",
  "dimension": "to"
  }
  ]
} }
   
<?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
   
$unspsc = Array(); $unspsc = Array();
$unspscresult = $conn->prepare('select * from "UNSPSCcategories" where "UNSPSC"::text like \'%00000\';'); $unspscresult = $conn->prepare('select * from "UNSPSCcategories" where "UNSPSC"::text like \'%00000\';');
$unspscresult->execute(); $unspscresult->execute();
foreach ($unspscresult->fetchAll() as $row) { foreach ($unspscresult->fetchAll() as $row) {
$unspsc[$row['UNSPSC']] = $row['Title']; $unspsc[$row['UNSPSC']] = $row['Title'];
} }
   
$query = $conn->prepare(' $query = $conn->prepare('
SELECT "CNID",contractnotice."agencyName",agency.abn as "agencyABN", SELECT "CNID",contractnotice."agencyName",agency.abn as "agencyABN",
EXTRACT(EPOCH FROM "publishDate") as "publishDate", EXTRACT(EPOCH FROM "publishDate") as "publishDate",
EXTRACT(EPOCH FROM "contractStart") as "contractStart", EXTRACT(EPOCH FROM "contractStart") as "contractStart",
EXTRACT(EPOCH FROM "contractEnd") as "contractEnd", EXTRACT(EPOCH FROM "contractEnd") as "contractEnd",
value,description,"procurementMethod",category as cat3, value,description,category,
(substr( "categoryUNSPSC"::text, 0, 3 ) || \'000000\'::text) as "cat1", "supplierName",(case when "supplierABN" != 0 THEN "supplierABN"::text ELSE "supplierName" END) as supplierID,
(substr( "categoryUNSPSC"::text, 0, 5 ) || \'0000\'::text) as "cat2",  
"supplierABN","supplierName",  
(  
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" FROM contractnotice join agency on contractnotice."agencyName"=agency."agencyName"
where "childCN" is null' where "childCN" is null'
, array(PDO::ATTR_CURSOR => PDO::FETCH_ORI_NEXT)); , array(PDO::ATTR_CURSOR => PDO::FETCH_ORI_NEXT));
// "supplierCity","supplierPostcode","supplierCountry","contactPostcode",  
$query->execute(); $query->execute();
$errors = $conn->errorInfo(); $errors = $conn->errorInfo();
if ($errors[2] != "") { if ($errors[2] != "") {
die("Export terminated, db error" . print_r($errors, true)); die("Export terminated, db error" . print_r($errors, true));
} }
   
$num_fields = $query->columnCount(); $num_fields = $query->columnCount();
$headers = Array(); $headers = Array();
for ($i = 0; $i < $num_fields; $i++) { // for each column in query, make a CSV header for ($i = 0; $i < $num_fields; $i++) { // for each column in query, make a CSV header
$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);
while ($row = $query->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) { while ($row = $query->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
foreach ($row as $key => &$colvalue) { foreach ($row as $key => &$colvalue) {
   
$colvalue = preg_replace('/[^[:print:]]/', '', utf8_encode($colvalue)); $colvalue = preg_replace('/[^[:print:]]/', '', utf8_encode($colvalue));
if ($headers[$key] == "publishDate" || $headers[$key] == "contractStart" if ($headers[$key] == "publishDate" || $headers[$key] == "contractStart"
|| $headers[$key] == "contractEnd") { || $headers[$key] == "contractEnd") {
$colvalue = date("Y-m-d", $colvalue); $colvalue = date("Y-m-d", $colvalue);
} }
if ($headers[$key] == "CNID") { /* if ($headers[$key] == "CNID") {
$colvalue = str_replace("A","", $colvalue); $colvalue = str_replace("A","", $colvalue);
} }*/
if ($headers[$key] == "cat1" || $headers[$key] == "cat2" if ($headers[$key] == "cat1" || $headers[$key] == "cat2"
|| $headers[$key] == "cat3") { || $headers[$key] == "cat3") {
$colvalue = $unspsc[$colvalue]; $colvalue = $unspsc[$colvalue];
} }
} }
fputcsv($fp, array_values($row)); fputcsv($fp, array_values($row));
} }
die; die;
} }
?> ?>