[Mapbender-commits] r9634 - in trunk/mapbender/http: javascripts php

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Tue Jan 3 05:44:51 PST 2017


Author: armin11
Date: 2017-01-03 05:44:51 -0800 (Tue, 03 Jan 2017)
New Revision: 9634

Added:
   trunk/mapbender/http/javascripts/mod_syncCkan_client.php
   trunk/mapbender/http/php/mod_syncCkan_server.php
Modified:
   trunk/mapbender/http/php/mod_syncCkan.php
Log:
Prototyp of new synchronization tool for ckan data portals > 2.5.3.

Added: trunk/mapbender/http/javascripts/mod_syncCkan_client.php
===================================================================
--- trunk/mapbender/http/javascripts/mod_syncCkan_client.php	                        (rev 0)
+++ trunk/mapbender/http/javascripts/mod_syncCkan_client.php	2017-01-03 13:44:51 UTC (rev 9634)
@@ -0,0 +1,154 @@
+<?php
+/**
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation; either version 2, or (at your option)
+* any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+*/
+require_once(dirname(__FILE__) . '/../../core/globalSettings.php');
+require_once(dirname(__FILE__).'/../../conf/ckan.conf');
+
+if (defined("CKAN_SERVER_PORT") && CKAN_SERVER_PORT !== '') {
+    $ckanApiUrl = CKAN_SERVER_IP.":".CKAN_SERVER_PORT;
+} else {
+    $ckanApiUrl = CKAN_SERVER_IP;
+}
+if (defined("CKAN_API_VERSION") && CKAN_API_VERSION !== '') {
+    $ckanApiVersion = CKAN_API_VERSION;
+} else {
+    $ckanApiVersion = 3;
+}
+if (defined("CKAN_API_PROTOCOL") && CKAN_API_PROTOCOL !== '') {
+    $ckanApiProtocol = CKAN_API_PROTOCOL;
+} else {
+    $ckanApiProtocol = 'https';
+}
+$ckanBaseUrl = $ckanApiProtocol.'://'.$ckanApiUrl.'/api/'.$ckanApiVersion.'/';
+$ckanShowOrgaUrl = $ckanBaseUrl.'action/organization_show?id=';
+//TODO - do authorization later!
+//require_once(dirname(__FILE__)."/../php/mb_validatePermission.php");
+$compareTimestamps = false;
+//Parse parameters
+if (isset($_REQUEST["compareTimestamps"]) & $_REQUEST["compareTimestamps"] != "") {
+	$testMatch = $_REQUEST["compareTimestamps"];	
+ 	if (!($testMatch == 'false' or $testMatch == 'true')){ 
+		echo 'Parameter <b>compareTimestamps</b> is not valid (false, true).<br/>'; 
+		die(); 		
+ 	}
+	switch ($testMatch) {
+		case "false":
+			$compareTimestamps = false;
+			break;
+		case "true":
+			$compareTimestamps = true;
+			break;
+	}
+	$testMatch = NULL;
+}
+?>
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+<head>
+<?php
+echo '<meta http-equiv="Content-Type" content="text/html; charset='.CHARSET.'">';
+$title = _mb('Ckan sync module');	
+?>
+<title><?php echo $title;?></title>
+<script src="../extensions/jquery-1.12.0.min.js"></script>
+<script>
+function callServer(compareTimestamps, syncDepartment) {
+	if (syncDepartment == 0) {
+		$("#get_orga_list_info").css("display","block");
+	} else {
+		$("#sync_single_orga").css("display","block");
+	}
+	$.ajax({
+  		url: '../php/mod_syncCkan_server.php',
+  		type: "post",
+		async: true,
+		data: {compareTimestamps: compareTimestamps, syncDepartment: syncDepartment},
+       		dataType: "json",
+  		success: function(result) {
+			if (result.success == true) {
+				if (syncDepartment == 0) {
+					//re-initialize table
+					$("#get_orga_list_info").css("display","none");
+					for (orga of result.result.geoportal_organization) {
+						//read length of delete, update, create into variables
+						if ("undefined" !== typeof(orga.delete)) {var p_delete = (orga.delete).length;} else {var p_delete = 0;}
+						if ("undefined" !== typeof(orga.update)) {var p_update = (orga.update).length;} else {var p_update = 0;}
+						if ("undefined" !== typeof(orga.create)) {var p_create = (orga.create).length;} else {var p_create = 0;}
+						$('#sync_status_table tr:last').after('<tr id=\'r_id_'+orga.id+'\'>'+'<td id=\'mborgatitle_'+orga.id+'\'>'+'<a target=\'_blank\' href=\'../php/mod_showOrganizationInfo.php?id='+orga.id+'\'>'+orga.title+' ('+orga.count_geoportal_packages+')'+'</a>'+'</td>'+'<td id=\'ckanorgauuid_'+orga.id+'\'><a target=\'_blank\' href=\'<?php echo $ckanShowOrgaUrl;?>'+orga.ckan_orga_ident+'\'>'+orga.ckan_orga_ident+' ('+orga.count_ckan_packages+')'+'</a></td>'+'<td id=\'to_delete_'+orga.id+'\'>'+p_delete+'</td>'+'<td id=\'to_update_'+orga.id+'\'>'+p_update+'</td>'+'<td id=\'to_create_'+orga.id+'\'>'+p_create+'</td>'+'</tr>');
+						//add button for start syncing if something may be done ;-)
+						if ((p_delete+p_update+p_create) > 0) {
+							//$form .= "<button class=\"btn btn-primary\" type=\"button\" id=\"maintenance_button\" onclick=\"callServer('".$resourceType."','".$maintenanceFunction."',$('#resource_id_list').val(),"."1".");\">";
+							$('#r_id_'+orga.id+' td:last').after('<td id=\'button_column_'+orga.id+'\'><button class=\"btn btn-primary\" type=\"button\" id=\"sync_button_'+orga.id+'\" onclick=\"callServer('+compareTimestamps+','+orga.id+');\">Start sync</button></td>');
+						}
+						//
+					}
+				} else {
+					//update row for specific organization
+					if (result.success == true) {
+						//update values
+						$('#to_delete_'+result.result.orga_id).text(parseInt($('#to_delete_'+result.result.orga_id).text()) - result.result.numberOfDeletedPackages);
+						$('#to_update_'+result.result.orga_id).text(parseInt($('#to_update_'+result.result.orga_id).text()) - result.result.numberOfUpdatedPackages);
+						$('#to_create_'+result.result.orga_id).text(parseInt($('#to_create_'+result.result.orga_id).text()) - result.result.numberOfCreatedPackages);
+						//check if something more is to be done
+						if ((parseInt($('#to_delete_'+result.result.orga_id).text()) + parseInt($('#to_update_'+result.result.orga_id).text()) + parseInt($('#to_create_'+result.result.orga_id).text())) == 0) {
+							//alert('drop button');
+							$('#button_column_'+result.result.orga_id).remove();
+						} else {
+							//alert((parseInt($('#to_delete_'+result.result.orga_id).text()) + parseInt($('#to_update_'+result.result.orga_id).text()) + parseInt($('#to_create_'+result.result.orga_id).text())));
+						}
+					}
+					$("#sync_single_orga").css("display","none");
+						
+				}
+				//alert(JSON.stringify(result.result));
+			} else {
+				alert(result.error.message);
+			}
+ 		}
+	});
+	return false;
+}
+</script>
+<style type="text/css">
+.loading_symbol {
+    -webkit-animation:spin 4s linear infinite;
+    -moz-animation:spin 4s linear infinite;
+    animation:spin 4s linear infinite;
+}
+ at -moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
+ at -webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
+ at keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }
+</style>
+<?php include '../include/dyn_css.php'; ?>
+</head>
+<body onload="callServer(<?php if ($compareTimestamps == false) {echo "false";} else {echo "true";} ?>,<?php if ($syncDepartment == false) {echo 0;} else {echo $syncDepartment;} ?>)">
+<div id="title"><?php echo $title; ?></div>
+<div id="get_orga_list_info" style="display: none;"><p><img class="loading_symbol" src="../img/loader_lightblue.gif" style="margin-left: auto; margin-right: auto;"/><?php echo _mb("Getting organization info ..."); ?></p></div>
+<div id="sync_single_orga" style="display: none;"><p><img class="loading_symbol" src="../img/loader_lightblue.gif" style="margin-left: auto; margin-right: auto;"/><?php echo _mb("Syncing metadata ..."); ?></p></div>
+<form id="sync_status_form">
+    <table id ="sync_status_table">
+        <tr id="sync_status_table_row_header">
+            <th><?php echo _mb('Mapbender group'); ?></th>
+            <th><?php echo _mb('Ckan organization'); ?></th>
+            <th><?php echo _mb('# delete'); ?></th>
+            <th><?php echo _mb('# update'); ?></th>
+	    <th><?php echo _mb('# create'); ?></th>
+	    <th><?php echo _mb('Action'); ?></th>
+        </tr>
+    </table>
+</form>
+</body>
+</html>

Modified: trunk/mapbender/http/php/mod_syncCkan.php
===================================================================
--- trunk/mapbender/http/php/mod_syncCkan.php	2017-01-03 07:42:34 UTC (rev 9633)
+++ trunk/mapbender/http/php/mod_syncCkan.php	2017-01-03 13:44:51 UTC (rev 9634)
@@ -58,7 +58,7 @@
 if (defined("CKAN_EXPORT_URL") && CKAN_EXPORT_URL != "") {
 	$mapbenderCkanUrl = CKAN_EXPORT_URL;
 } else {
-	$mapbenderCkanUrl = "http://localhost/mapbender_trunk/php/mod_exportMapbenderLayer2CkanObjects.php";
+	$mapbenderCkanUrl = "http://localhost/mb_trunk/php/mod_exportMapbenderLayer2CkanObjects.php";
 }
 //read ckan objects from source into variable
 $ckanObjectConnector = new connector($mapbenderCkanUrl);

Added: trunk/mapbender/http/php/mod_syncCkan_server.php
===================================================================
--- trunk/mapbender/http/php/mod_syncCkan_server.php	                        (rev 0)
+++ trunk/mapbender/http/php/mod_syncCkan_server.php	2017-01-03 13:44:51 UTC (rev 9634)
@@ -0,0 +1,157 @@
+<?php
+// Display errors for demo
+//for later development php 5.6+
+//https://github.com/GSA/ckan-php-client
+//@ini_set('error_reporting', E_ALL);
+//@ini_set('display_errors', 'stdout');	
+// Include class_ckanApi.php
+require_once(dirname(__FILE__).'/../classes/class_connector.php');
+require_once(dirname(__FILE__).'/../classes/class_group.php');
+require_once(dirname(__FILE__).'/../classes/class_syncCkan.php');
+require_once(dirname(__FILE__) . '/../php/mod_getDownloadOptions.php');
+require_once(dirname(__FILE__).'/../../conf/ckan.conf');
+
+//http://localhost/mb_trunk/php/mod_syncCkan_new.php?userId=1&compareTimestamps=true&syncDepartment=24
+//http://localhost/mb_trunk/php/mod_syncCkan_new.php?userId=1&compareTimestamps=false
+//TODO: Problem - datestamp for ckan_package - wrong????? Bug in 2.5.3??? Will the index not be updated???
+$registratingDepartments = false;
+$userId = false;
+$outputFormat = "json";
+$compareTimestamps = false;
+$listAllMetadataInJson = true;
+//initiate resultObject to give back as json
+$resultObject->success = false;
+
+//parse request parameter
+if (isset($_REQUEST["registratingDepartments"]) & $_REQUEST["registratingDepartments"] != "") {
+	//validate to csv integer list
+	$testMatch = $_REQUEST["registratingDepartments"];
+	$pattern = '/^[\d,]*$/';		
+ 	if (!preg_match($pattern,$testMatch)){ 
+		//echo 'registratingDepartments: <b>'.$testMatch.'</b> is not valid.<br/>';
+		$resultObject->error->message = 'Parameter registratingDepartments is not valid (integer or cs integer list).';
+		echo json_encode($resultObject);	
+		die();
+ 	}
+	$registratingDepartments = $testMatch;
+	$testMatch = NULL;
+}
+
+if (isset($_REQUEST["syncDepartment"]) && $_REQUEST["syncDepartment"] !== "" && $_REQUEST["syncDepartment"] !== null) {
+        $testMatch = $_REQUEST["syncDepartment"];
+        $pattern = '/^[0-9]*$/';  
+        if (!preg_match($pattern,$testMatch)){
+                $resultObject->error->message = 'Parameter syncDepartment is not valid (integer).';
+                echo json_encode($resultObject);
+		die();	
+        }
+        $syncDepartment = (integer)$testMatch;
+        $testMatch = NULL;
+}
+
+if (isset($_REQUEST["userId"]) & $_REQUEST["userId"] != "") {
+        $testMatch = $_REQUEST["userId"];
+        $pattern = '/^[0-9]*$/';  
+        if (!preg_match($pattern,$testMatch)){
+                $resultObject->error->message = 'Parameter userId is not valid (integer).';
+                echo json_encode($resultObject);
+		die();
+        }
+	if ($testMatch !== Mapbender::session()->get("mb_user_id")) {
+		$resultObject->error->message = 'Parameter userId is not equal to the userId from session information - maybe there is no current session!';
+		echo json_encode($resultObject);
+		die();
+	}
+        $userId = $testMatch;
+        $testMatch = NULL;
+} else { 
+	$userId = Mapbender::session()->get("mb_user_id");
+  	if ($userId == false) {
+	  	$userId = PUBLIC_USER;
+    	}
+}
+
+if (isset($_REQUEST["compareTimestamps"]) & $_REQUEST["compareTimestamps"] != "") {
+	$testMatch = $_REQUEST["compareTimestamps"];	
+ 	if (!($testMatch == 'false' or $testMatch == 'true')){ 
+	 	$resultObject->error->message = 'Parameter compareTimestamps is not valid (false, true).'; 
+		echo json_encode($resultObject);	
+		die();	
+ 	}
+	switch ($testMatch) {
+		case "false":
+			$compareTimestamps = false;
+			break;
+		case "true":
+			$compareTimestamps = true;
+			break;
+	}
+	$testMatch = NULL;
+}
+
+if (isset($_REQUEST["outputFormat"]) & $_REQUEST["outputFormat"] != "") {
+	$testMatch = $_REQUEST["outputFormat"];	
+ 	if (!($testMatch == 'json' or $testMatch == 'debug')){ 
+		$resultObject->error->message = 'Parameter outputFormat is not valid (json, debug).'; 
+		echo json_encode($resultObject);
+		die();	
+ 	}
+	$outputFormat = $testMatch;
+	$testMatch = NULL;
+}
+
+/*
+Serverside function of the ckan sync tool for metadata / service providers
+return values should work similar to ckans api functions
+e.g. 
+{
+    "help": "Creates a package",
+    "success": false,
+    "error": {
+        "message": "Access denied",
+        "__type": "Authorization Error"
+        }
+}
+{
+    "help": "Creates a package",
+    "success": true,
+    "result": {
+        "resultObj": "s.th."
+        }
+}
+
+
+
+*/
+
+$syncCkanClass = new SyncCkan();
+$syncCkanClass->mapbenderUserId = $userId;
+$syncCkanClass->compareTimestamps = $compareTimestamps;
+
+if (isset($syncDepartment)) {
+        //write requested orga id to class to prohibit invocation of other orga comparism if one orga should be synced
+	$syncCkanClass->syncOrgaId = $syncDepartment;
+}
+
+$departmentsArray = $syncCkanClass->getMapbenderOrganizations();
+
+//second parameter is listAllMetadataInJson ( = true) - it is needed if we want to sync afterwards. The syncList includes all necessary information about one organization
+
+$syncListJson = $syncCkanClass->getSyncListJson($departmentsArray, true);
+$syncList = json_decode($syncListJson);
+if ($syncList->success = true) {
+    foreach ($syncList->result->geoportal_organization as $orga) {
+        //try to sync single orga - the class has already set the syncOrgaId if wished!
+	if ($syncDepartment == $orga->id) {
+            //overwrite result with result from sync process
+            $syncList = json_decode($syncCkanClass->syncSingleOrga(json_encode($orga)));
+	}
+    }
+}
+//create new syncListJson
+$syncListJson = json_encode($syncList);
+header('Content-type:application/json;charset=utf-8');
+echo $syncListJson;
+
+
+?>



More information about the Mapbender_commits mailing list