[Mapbender-commits] r9653 - trunk/mapbender/http/classes

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Thu Jan 12 11:51:15 PST 2017


Author: armin11
Date: 2017-01-12 11:51:15 -0800 (Thu, 12 Jan 2017)
New Revision: 9653

Modified:
   trunk/mapbender/http/classes/class_syncCkan.php
Log:
Fix to be downward compatible with php < 5.5 - array_column() is not available in php5.4!

Modified: trunk/mapbender/http/classes/class_syncCkan.php
===================================================================
--- trunk/mapbender/http/classes/class_syncCkan.php	2017-01-12 19:50:30 UTC (rev 9652)
+++ trunk/mapbender/http/classes/class_syncCkan.php	2017-01-12 19:51:15 UTC (rev 9653)
@@ -16,7 +16,7 @@
 */
 require_once(dirname(__FILE__) . '/../../core/globalSettings.php');
 require_once(dirname(__FILE__).'/../classes/class_connector.php');
-require_once(dirname(__FILE__).'/../classes/class_ckanApi25.php');
+require_once(dirname(__FILE__).'/../classes/class_ckanApi.php');
 require_once(dirname(__FILE__).'/../classes/class_group.php');
 require_once(dirname(__FILE__) . '/../php/mod_getDownloadOptions.php');
 require_once(dirname(__FILE__).'/../../conf/ckan.conf');
@@ -42,18 +42,48 @@
 	} else {
 	    $this->ckanApiUrl = CKAN_SERVER_IP;
         }
-        //$this->ckanApiKey;
 	if (defined("CKAN_API_VERSION") && CKAN_API_VERSION !== '') {
 	    $this->ckanApiVersion = CKAN_API_VERSION;
 	} else {
 	    $this->ckanApiVersion = 3;
         }
+	if (defined("CKAN_API_PROTOCOL") && CKAN_API_PROTOCOL !== '') {
+    	    $this->ckanApiProtocol = CKAN_API_PROTOCOL;
+	} else {
+    	    $this->ckanApiProtocol = 'https';
+	}
 	$this->mapbenderUserId = 0;
 	$this->syncOrgaId = 0;
 	$this->topicCkanCategoryMap = $topicCkanCategoryMap; //from ckan.conf
 	$this->compareTimestamps = false; //default to update each dataset, because the ckan index for metadata_modified may not be up to date !!!
     }
 
+   //TODO: Following function is only needed til php 5.5 - after upgrade to debian 8 it is obsolet - see also class_iso19139.php!
+   public function array_column(array $input, $columnKey, $indexKey = null) {
+        $array = array();
+        foreach ($input as $value) {
+            if ( !array_key_exists($columnKey, $value)) {
+                trigger_error("Key \"$columnKey\" does not exist in array");
+                return false;
+            }
+            if (is_null($indexKey)) {
+                $array[] = $value[$columnKey];
+            }
+            else {
+                if ( !array_key_exists($indexKey, $value)) {
+                    trigger_error("Key \"$indexKey\" does not exist in array");
+                    return false;
+                }
+                if ( ! is_scalar($value[$indexKey])) {
+                    trigger_error("Key \"$indexKey\" does not contain scalar value");
+                    return false;
+                }
+                $array[$value[$indexKey]] = $value[$columnKey];
+            }
+        }
+        return $array;
+    }
+
     public function getMapbenderOrganizations() {
         if (isset($this->mapbenderUserId) && (integer)$this->mapbenderUserId > 0) {
             if (isset($this->syncOrgaId) && (integer)$this->syncOrgaId > 0) {
@@ -100,24 +130,28 @@
     }
 
     public function getSyncListJson($departmentsArray, $listAllMetadataInJson = false) {
-        $syncList = new stdClass();
+        $syncList = new stdClass(); //should handle the returned json object
+	$syncListResult = new stdClass();
+	$syncList->help = "helptext";
+	$syncList->success = false;
+        $syncList->function = "getSyncListJson";
         $numberGeoportalOrga = 0;
         foreach ($departmentsArray as $organization) {
-            $syncList->geoportal_organization[$numberGeoportalOrga]->name = $organization["name"];
-            $syncList->geoportal_organization[$numberGeoportalOrga]->id = $organization["id"];
-            $syncList->geoportal_organization[$numberGeoportalOrga]->title = $organization["title"];
-            $syncList->geoportal_organization[$numberGeoportalOrga]->email = $organization["email"];
-            $syncList->geoportal_organization[$numberGeoportalOrga]->ckan_orga_ident = false;
+            $syncListResult->geoportal_organization[$numberGeoportalOrga]->name = $organization["name"];
+            $syncListResult->geoportal_organization[$numberGeoportalOrga]->id = $organization["id"];
+            $syncListResult->geoportal_organization[$numberGeoportalOrga]->title = $organization["title"];
+            $syncListResult->geoportal_organization[$numberGeoportalOrga]->email = $organization["email"];
+            $syncListResult->geoportal_organization[$numberGeoportalOrga]->ckan_orga_ident = false;
             if (isset($organization["ckan_uuid"]) && isset($organization["ckan_api_key"])) {
                 //Test if organization with the given external uuid exists in the coupled ckan
                 //show organizations of the authorized user (from geoportal group table) via action api
                 $ckan = new ckanApi($organization["ckan_api_key"], CKAN_SERVER_IP);
-                $ckan->base_url='http://'.$this->ckanApiUrl.'/api/3/';
+                $ckan->base_url = $this->ckanApiProtocol.'://'.$this->ckanApiUrl.'/api/3/';
                 $ckan->api_version = $this->ckanApiVersion;
                 $result = $ckan->action_organization_list_for_user();
                 foreach($result->result as $orga) {
                     if ($orga->id == $organization["ckan_uuid"]) {
-                        $syncList->geoportal_organization[$numberGeoportalOrga]->ckan_orga_ident = $organization["ckan_uuid"];
+                        $syncListResult->geoportal_organization[$numberGeoportalOrga]->ckan_orga_ident = $organization["ckan_uuid"];
                         //echo "Corresponding ckan organization ".$orga->display_name." found for geoportal group ".$organization["name"]." with id ".$organization["id"]."!<br>";
                         //get list of ids for existing spatial datasets - category spatial should be defined!!!!!!
                         //http://localhost:5000/api/3/action/package_search?fq=transparency_category_de_rp:spatial_data
@@ -131,7 +165,7 @@
                         //echo "List of datasets in ckan instance:"."<br>";
                         $ckanPackageNames = array();
                         if ($listOfSpatialData->success == true) {
-                            $syncList->geoportal_organization[$numberGeoportalOrga]->count_ckan_packages = $listOfSpatialData->result->count;
+                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->count_ckan_packages = $listOfSpatialData->result->count;
                             //echo json_encode($listOfSpatialData)."<br>";
                             //$e = new mb_exception("Number of results: ".$listOfSpatialData->result->count);
                             foreach ($listOfSpatialData->result->results as $dataset) {
@@ -139,8 +173,8 @@
                                 $ckanMetadataArray[$countCkanMetadataArray]['name'] = $dataset->name;
                                 $ckanMetadataArray[$countCkanMetadataArray]['changedate'] = $dataset->metadata_modified;
                                 if ($listAllMetadataInJson == true) {
-                                    $syncList->geoportal_organization[$numberGeoportalOrga]->ckan_packages[$countCkanMetadataArray]->id = $dataset->name;
-                                    $syncList->geoportal_organization[$numberGeoportalOrga]->ckan_packages[$countCkanMetadataArray]->date_time = $dataset->metadata_modified;
+                                    $syncListResult->geoportal_organization[$numberGeoportalOrga]->ckan_packages[$countCkanMetadataArray]->id = $dataset->name;
+                                    $syncListResult->geoportal_organization[$numberGeoportalOrga]->ckan_packages[$countCkanMetadataArray]->date_time = $dataset->metadata_modified;
                                 }
                                 //echo $dataset->title." - ".$dataset->name." - ".$dataset->metadata_modified."<br>";
                                 $ckanPackageNames[] = $dataset->name;
@@ -199,16 +233,16 @@
                                     //use only those that have resources!
                                     if ($geoportalMetadata['hasResource']) {
                                         if ($listAllMetadataInJson == true) {
-                                            $syncList->geoportal_organization[$numberGeoportalOrga]->geoportal_metadata[$numberGeoportalMetadata]->id = $geoportalMetadata['uuid'];
-                                            $syncList->geoportal_organization[$numberGeoportalOrga]->geoportal_metadata[$numberGeoportalMetadata]->date_time = $geoportalMetadata['changedate'];
-                                            $syncList->geoportal_organization[$numberGeoportalOrga]->geoportal_metadata[$numberGeoportalMetadata]->resources = json_decode($geoportalMetadata['resources']);
+                                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->geoportal_metadata[$numberGeoportalMetadata]->id = $geoportalMetadata['uuid'];
+                                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->geoportal_metadata[$numberGeoportalMetadata]->date_time = $geoportalMetadata['changedate'];
+                                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->geoportal_metadata[$numberGeoportalMetadata]->resources = json_decode($geoportalMetadata['resources']);
                                         }
                                         $geoportalUuids[] = $geoportalMetadata['uuid'];
                                         $numberGeoportalMetadata++;
                                     }
                                 }
                             }
-                            $syncList->geoportal_organization[$numberGeoportalOrga]->count_geoportal_packages = $numberGeoportalMetadata;
+                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->count_geoportal_packages = $numberGeoportalMetadata;
                             //build diffs for ckan and geoportal
                             //Arrays: ckanPackageNames / geoportalUuids
                             //Those which are only in ckan: delete them
@@ -220,9 +254,9 @@
                             //if the timestamps should be compared before
                             if ($this->compareTimestamps == true) { 
                                 foreach ($inBoth as $uuid) {
-                                    $e = new mb_notice("classes/class_syncCkan.php: search for uuid: ".$uuid."ckan time : ".$ckanMetadataArray[array_search($uuid, array_column($ckanMetadataArray,'name'))]['changedate']. " - geoportal time: ".$metadataArray[array_search($uuid, array_column($metadataArray,'uuid'))]['changedate']);
-                                    $dateTimeCkan = new DateTime($ckanMetadataArray[array_search($uuid, array_column($ckanMetadataArray,'name'))]['changedate']);
-                                    $dateTimeGeoportal = new DateTime($metadataArray[array_search($uuid, array_column($metadataArray,'uuid'))]['changedate']);
+                                    $e = new mb_notice("classes/class_syncCkan.php: search for uuid: ".$uuid."ckan time : ".$ckanMetadataArray[array_search($uuid, $this->array_column($ckanMetadataArray,'name'))]['changedate']. " - geoportal time: ".$metadataArray[array_search($uuid, $this->array_column($metadataArray,'uuid'))]['changedate']);
+                                    $dateTimeCkan = new DateTime($ckanMetadataArray[array_search($uuid, $this->array_column($ckanMetadataArray,'name'))]['changedate']);
+                                    $dateTimeGeoportal = new DateTime($metadataArray[array_search($uuid, $this->array_column($metadataArray,'uuid'))]['changedate']);
                                     if ($dateTimeCkan > $dateTimeGeoportal) {
                                         //delete from $inBoth!
                                         $e = new mb_notice("classes/class_syncCkan.php: Ckans package newer than geoportals metadata!");
@@ -233,9 +267,9 @@
                                 }
                             }
                             //$e = new mb_exception("Number of packages which are only in ckan: ".count($onlyInCkan)." - number of packages which are only in geoportal: ".count($onlyInGeoportal)." - number of packages which are in both catalogues: ".count($inBoth));
-                            $syncList->geoportal_organization[$numberGeoportalOrga]->update = $inBoth;
-                            $syncList->geoportal_organization[$numberGeoportalOrga]->delete = $onlyInCkan;
-                            $syncList->geoportal_organization[$numberGeoportalOrga]->create = $onlyInGeoportal;
+                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->update = $inBoth;
+                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->delete = $onlyInCkan;
+                            $syncListResult->geoportal_organization[$numberGeoportalOrga]->create = $onlyInGeoportal;
                         }
                         //then pull the ressources which are owned by the user but have no group defined - only if the primary group of the user is the corresponding group!
                         //maybe the catalogue itself is the best way to pull all relevant data, cause the coupling is resolved already!
@@ -250,12 +284,21 @@
             }
         $numberGeoportalOrga++;
         }
+	if (count($syncListResult->geoportal_organization) >= 1) {
+	    $syncList->result = $syncListResult;
+            $syncList->success = true;
+	}
         return json_encode($syncList);    
     }
 	
     public function syncSingleOrga($syncListJson) {
+        $resultObject = new stdClass();
+        $resultObject->help = "Syncing mapbender with ckan for organization with id: ".$this->syncOrgaId;
 	$syncList = json_decode($syncListJson);
         if ($syncList->id == $this->syncOrgaId) {
+            $numberOfDeletedPackages = 0;
+            $numberOfCreatedPackages = 0;
+            $numberOfUpdatedPackages = 0;
 	    //read ckan api-key from database again, because we wont transfer it via json thru the web ;-)
 	    $sql = "SELECT mb_group_ckan_api_key FROM mb_group WHERE mb_group_id = $1"; 
             $v = array($this->syncOrgaId);
@@ -265,17 +308,17 @@
 	        $row = db_fetch_assoc($res);
 		$ckanApiKey = $row['mb_group_ckan_api_key'];
 		$ckan = new ckanApi($ckanApiKey, CKAN_SERVER_IP);
-                $ckan->base_url='http://'.$this->ckanApiUrl.'/api/3/';
+                $ckan->base_url = $this->ckanApiProtocol.'://'.$this->ckanApiUrl.'/api/3/';
                 $ckan->api_version = $this->ckanApiVersion;
 	    } else {
 	        $e = new mb_exception("classes/class_syncCkan.php: No api-key found for mapbender group: ".$this->syncOrgaId);
 	        return false;
 	    }
-            //echo "Synchronize metadata catalogues:"."<br>";
             foreach($syncList->delete as $ckanNameToDelete) {
                 $result = $ckan->action_package_delete("{\"id\":\"".$ckanNameToDelete."\"}");
                 if ($result->success == true) {
                     $e = new mb_notice("classes/class_syncCkan.php: Ckan package with name ".$ckanNameToDelete." successfully deleted!");
+                    $numberOfDeletedPackages++;
                 } else {
                     $e = new mb_exception("classes/class_syncCkan.php: A problem occured while trying to delete ckan package with name ".$ckanNameToDelete);
                 }
@@ -309,6 +352,7 @@
                                     //echo "Creation of package successful!"."<br>";
                                     //recreate views
                                     $viewsUpdateProtocol = $this->recreateResourceViews($ckan, $result, $resultCkanRepresentation);
+                                    $numberOfUpdatedPackages++;
                                     //$e = new mb_exception($viewsUpdateProtocol);
                                 } else {
                                     //echo "A problem occured while trying to create the ckan package"."<br>";
@@ -326,6 +370,7 @@
                                     //echo "Creation of package successful!"."<br>";
                                     //recreate views
                                     $viewsUpdateProtocol = $this->recreateResourceViews($ckan, $result, $resultCkanRepresentation);
+                                    $numberOfCreatedPackages++;
                                     //$e = new mb_exception($viewsUpdateProtocol);
                                 } else {
                                     $e = new mb_exception("classes/class_syncCkan.php: A problem occured while trying to create the ckan package");
@@ -339,9 +384,17 @@
             }
         } else {
             $e = new mb_exception("classes/class_syncCkan.php (syncSingleOrga): Id from json is not identical to id from class! Sync will not be started!");
-            return false;
-        }
-        return true;
+            $resultObject->success = false;
+            $resultObject->error->message = "Organization id for the invoked sync process could not be obtained - please check the id!";
+            //$resultObject->help = "Sync of metadata for orga ";
+            return json_encode($resultObject);
+        }            
+        $resultObject->success = true;
+        $resultObject->result->orga_id = $this->syncOrgaId;
+        $resultObject->result->numberOfDeletedPackages = $numberOfDeletedPackages;
+        $resultObject->result->numberOfUpdatedPackages = $numberOfUpdatedPackages;
+        $resultObject->result->numberOfCreatedPackages = $numberOfCreatedPackages;
+        return json_encode($resultObject);
     }
 
     private function getCkanRepresentation($uuid, $layerArray, $featuretypeArray, $orgaId, $orgaTitle, $orgaEmail, $topicCkanCategoryMap) {



More information about the Mapbender_commits mailing list