Data export for alaveteli
Data export for alaveteli


Former-commit-id: 94980807a4ab6705611f602879d6b7dd0e7e62f1

--- /dev/null
+++ b/alaveteli/exportAgencies.csv.php
@@ -1,1 +1,75 @@
+<?php
 
+include_once("../include/common.inc.php");
+setlocale(LC_CTYPE, 'C');
+
+$headers = Array("#id", "name", "request_email", "short_name", "notes", "publication_scheme", "home_page", "tag_string");
+
+$db = $server->get_db('disclosr-agencies');
+
+$tag = Array();
+try {
+    $rows = $db->get_view("app", "byDeptStateName", null, true)->rows;
+    //print_r($rows);
+    foreach ($rows as $row) {
+        $tag[$row->value] = phrase_to_tag(dept_to_portfolio($row->key));
+    }
+} catch (SetteeRestClientException $e) {
+    setteErrorHandler($e);
+}
+
+$fp = fopen('php://output', 'w');
+if ($fp && $db) {
+    header('Content-Type: text/csv');
+    header('Content-Disposition: attachment; filename="export.' . date("c") . '.csv"');
+    header('Pragma: no-cache');
+    header('Expires: 0');
+    fputcsv($fp, $headers);
+    try {
+        $agencies = $db->get_view("app", "byCanonicalName", null, true)->rows;
+        //print_r($rows);
+        foreach ($agencies as $agency) {
+        //    print_r($agency);
+            $row = Array();
+            $row["#id"] = $agency->value->_id;
+            $row["name"] = $agency->value->name;
+            if (isset($agency->value->foi_email)) {
+            $row["request_email"] = $agency->value->foi_email;
+            } else {
+                $row["request_email"] = "foi@".GetDomain($agency->value->website);
+                // 
+            }
+            if (isset($agency->value->short_name)) {
+            $row["short_name"] = $agency->value->short_name;
+            } else {
+                $out = Array();
+                preg_match_all('/[A-Z]/', $agency->value->name, $out);
+                $row["short_name"] = implode("",$out[0]);
+            }
+            $row["notes"] = "";
+            $row["publication_scheme"] = $agency->value->infoPublicationSchemeURL;
+            $row["home_page"] = $agency->value->website;
+            if ($agency->value->orgType == "FMA-DepartmentOfState") {
+                $row["tag_string"] = $tag[$agency->value->_id];
+            } else {
+                $row["tag_string"] = $tag[$agency->value->parentOrg];;
+            }
+            
+            fputcsv($fp, array_values($row));
+            
+            if (isset($agency->value->foiBodies)) {
+                foreach ($agency->value->foiBodies as $foiBody) {
+                    $row['name'] = $foiBody;
+                    $row['short_name'] = "";
+                    fputcsv($fp, array_values($row));
+                }
+            }
+        }
+    } catch (SetteeRestClientException $e) {
+        setteErrorHandler($e);
+    }
+
+    die;
+}
+?>
+

--- /dev/null
+++ b/alaveteli/exportCategories.rb.php
@@ -1,1 +1,23 @@
+<?php
+include_once("../include/common.inc.php");
+setlocale(LC_CTYPE, 'C');
+ header('Content-Type: text/csv');
+    header('Content-Disposition: attachment; filename="public_body_categories_en.rb"');
+    header('Pragma: no-cache');
+    header('Expires: 0');
+echo 'PublicBodyCategories.add(:en, ['.PHP_EOL;
+echo '    "Portfolios",'.PHP_EOL;
+$db = $server->get_db('disclosr-agencies');
 
+try {
+    $rows = $db->get_view("app", "byDeptStateName", null, true)->rows;
+    //print_r($rows);
+    foreach ($rows as $row) {
+        echo '        [ "'.phrase_to_tag(dept_to_portfolio($row->key)).'","'. dept_to_portfolio($row->key).'","'.dept_to_portfolio($row->key).'" ],'.PHP_EOL;
+    }
+} catch (SetteeRestClientException $e) {
+    setteErrorHandler($e);
+}
+echo '])';
+?>
+

file:a/graph.php -> file:b/graph.php
--- a/graph.php
+++ b/graph.php
@@ -15,7 +15,7 @@
     $rows = $db->get_view("app", "byCanonicalName", null, true)->rows;
     //print_r($rows);
     foreach ($rows as $row) {
-        echo "nodes[\"{$row->value}\"] = graph.newNode({label: \"{$row->key}\"});" . PHP_EOL;
+        echo "nodes[\"{$row->value->_id}\"] = graph.newNode({label: \"{$row->key}\"});" . PHP_EOL;
     }
 } catch (SetteeRestClientException $e) {
     setteErrorHandler($e);

--- a/include/common.inc.php
+++ b/include/common.inc.php
@@ -1,4 +1,15 @@
 <?php
+
+date_default_timezone_set("Australia/Sydney");
+
+$basePath = "";
+if (strstr($_SERVER['PHP_SELF'], "alaveteli/")
+        || strstr($_SERVER['PHP_SELF'], "myway/")
+        || strstr($_SERVER['PHP_SELF'], "lib/")
+        || strstr($_SERVER['PHP_SELF'], "geo/")
+        || strstr($_SERVER['PHP_SELF'], "include/")
+        || strstr($_SERVER['PHP_SELF'], "servicealerts/"))
+    $basePath = "../";
 
 include_once ('couchdb.inc.php');
 include_once ('template.inc.php');
@@ -33,6 +44,26 @@
     # Typecast to (object) will automatically convert array -> stdClass
     return (object) $array;
 }
+
+function dept_to_portfolio($deptName) {
+    return trim(str_replace("Department of", "", str_replace("Department of the", "Department of", $deptName)));
+} 
+function phrase_to_tag ($phrase) {
+    return str_replace(" ","_",str_replace(",","",strtolower($phrase)));
+}
+function GetDomain($url)
+{
+$nowww = ereg_replace('www\.','',$url);
+$domain = parse_url($nowww);
+if(!empty($domain["host"]))
+    {
+     return $domain["host"];
+     } else
+     {
+     return $domain["path"];
+     }
+}
+
 ?>
 
 

--- a/include/couchdb.inc.php
+++ b/include/couchdb.inc.php
@@ -1,6 +1,8 @@
 <?php
 
-include "schemas/schemas.inc.php";
+include $basePath."schemas/schemas.inc.php";
+
+require ($basePath.'couchdb/settee/src/settee.php');
 
 function createAgencyDesignDoc() {
     global $db;
@@ -65,21 +67,19 @@
     return $db->save($obj, true);
 }
 
-require ('couchdb/settee/src/settee.php');
 
 if( php_uname('n') == "vanille") {
 
 $server = new SetteeServer('http://192.168.178.21:5984');
 } else 
-    if( php_uname('n') == "kyuubey") {
+    if( php_uname('n') == "KYUUBEY") {
 
-$server = new SetteeServer('http://192.168.1.8:5984');
+$server = new SetteeServer('http://192.168.1.148:5984');
 } else {
     $server = new SetteeServer('http://127.0.0.1:5984');
 }
 function setteErrorHandler($e) {
     echo $e->getMessage() . "<br>" . PHP_EOL;
 }
-
 ?>
 

--- a/schemas/agency.json.php
+++ b/schemas/agency.json.php
@@ -5,7 +5,10 @@
     "type" => "object",
     "properties" => Array(
         "name" => Array("type" => "string", "required" => true, "x-title"=> "Agency Name", "description" => "Agency Name, most recent and broadest"),
+        "short_name" => Array("type" => "string", "required" => false, "x-title"=> "Agency Short Name", "description" => "Agency Name shortened, usually to an acronym"),
         "otherNames" => Array("type" => "array", "required" => true, "x-title" => "Agency Past/Other Names", "description" => "Agency Names",
+            "items" => Array("type" => "string")),
+        "foiBodies" => Array("type" => "array", "required" => true, "x-title" => "FOI Bodies", "description" => "Organisational units within this agency that are subject to FOI Act but are not autonomous",
             "items" => Array("type" => "string")),
         "orgType"=> Array("type" => "string", "required" => true, "x-title"=> "Agency Name", "description" => "Agency Name, most recent and broadest"),
         "parentOrg"=> Array("type" => "string", "required" => true, "x-title"=> "Agency Name", "description" => "Agency Name, most recent and broadest"),