[Mapbender-commits] r9448 - in trunk/mapbender/http: classes php

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Mon May 9 06:12:55 PDT 2016


Author: armin11
Date: 2016-05-09 06:12:55 -0700 (Mon, 09 May 2016)
New Revision: 9448

Added:
   trunk/mapbender/http/classes/class_metadata_monitor.php
Modified:
   trunk/mapbender/http/classes/class_layer_monitor.php
   trunk/mapbender/http/classes/class_metadata_new.php
   trunk/mapbender/http/classes/class_wmc.php
   trunk/mapbender/http/php/mod_inspireDownloadFeed.php
Log:
New possibility to log the usage of wms layer, inspire downloadservives and their corresponding metadata!

Modified: trunk/mapbender/http/classes/class_layer_monitor.php
===================================================================
--- trunk/mapbender/http/classes/class_layer_monitor.php	2016-05-09 13:11:13 UTC (rev 9447)
+++ trunk/mapbender/http/classes/class_layer_monitor.php	2016-05-09 13:12:55 UTC (rev 9448)
@@ -18,6 +18,7 @@
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 require_once(dirname(__FILE__)."/../../core/globalSettings.php");
+require_once(dirname(__FILE__)."/class_metadata_monitor.php");
 
 class Layer_load_count {
 
@@ -66,5 +67,68 @@
 			$res = db_prep_query($sql, $v, $t);
 		}
 	}
+	//function to increment more than one layer with one sql statement
+	function incrementMultiLayers($layerIdArray) {
+		if (!is_array($layerIdArray)) {
+			return false;
+		}
+		$layerIdString = implode(",",$layerIdArray);
+		//check for existing entry in load count table, if not exist - insert zero value
+		$sql = "SELECT fkey_layer_id FROM layer_load_count WHERE fkey_layer_id IN (".$layerIdString.")";
+		$res = db_query($sql);
+		$existingLayerIds = array();
+		while($row = db_fetch_array($res)) {
+			array_push($existingLayerIds, $row["fkey_layer_id"]);
+		}
+		//check for existing layers in layer table
+		$sql = "SELECT layer_id FROM layer WHERE layer_id IN (".$layerIdString.")";
+		$res = db_query($sql);
+		$existingLayerIdsInLayer = array();
+		while($row = db_fetch_array($res)) {
+			array_push($existingLayerIdsInLayer, $row["layer_id"]);
+		}
+		//use only those who exists in layer table
+		$layerIdArray = array_intersect($layerIdArray,$existingLayerIdsInLayer);
+		//filter out those layers which are defined in mapbender.conf not to be counted
+		if (DEFINED("LAYERS_EXCLUDED_FROM_COUNTING")) {
+			$arraysToExclude = explode(',',LAYERS_EXCLUDED_FROM_COUNTING);
+			//delete this layers from array
+			$layerIdArray = array_diff($layerIdArray, $arraysToExclude);
+		}
+		//check for existing layers in layer table - delete those
+		$notAlreadyExists = array_diff($layerIdArray, $existingLayerIds);
+		//Insert those into table (initialize)
+		foreach( $notAlreadyExists as $newLayerId ) {
+    			$insertLayer[] = '('.$newLayerId.',0)';
+		}
+		if (count($insertLayer) > 0) {
+			$sql = "INSERT INTO layer_load_count (fkey_layer_id,load_count) VALUES ".implode(',',$insertLayer).";";
+			$res = db_query($sql);
+		}
+		//increment load counts
+		if (count($layerIdArray) > 0) {
+			$sql = "UPDATE layer_load_count SET load_count = load_count+1 WHERE fkey_layer_id in (".implode(',',$layerIdArray).")";
+			$res = db_query($sql);
+			if (!$res) {
+				$e = new mb_exception("class_layer_monitor.php: Could not increment layer load_count of layers");
+				return false;
+			} else {
+				//update load_count for coupled metadata
+				$sql = "SELECT fkey_metadata_id FROM ows_relation_metadata WHERE fkey_layer_id IN (".implode(',',$layerIdArray).")";
+				$metadataMonitor = new Metadata_load_count();
+				$res = db_query($sql);
+				$coupledMetadata = array();
+				while($row = db_fetch_array($res)) {
+					array_push($coupledMetadata, $row["fkey_metadata_id"]);
+				}
+				//update load_count for them
+				$metadataMonitor->incrementMultiMetadata($coupledMetadata);
+				return true;
+			}
+		} else {
+			$e = new mb_exception("class_layer_monitor.php: No layers found to increment load_count");
+			return false;
+		}
+	}
 }
 ?>

Added: trunk/mapbender/http/classes/class_metadata_monitor.php
===================================================================
--- trunk/mapbender/http/classes/class_metadata_monitor.php	                        (rev 0)
+++ trunk/mapbender/http/classes/class_metadata_monitor.php	2016-05-09 13:12:55 UTC (rev 9448)
@@ -0,0 +1,119 @@
+<?php
+# $Id: class_layer_monitor.php 7020 2010-10-04 14:23:22Z christoph $
+# http://www.mapbender.org/index.php/
+# Copyright (C) 2002 CCGIS 
+#
+# 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");
+
+class Metadata_load_count {
+
+	function __construct () {
+	}
+	
+	/**
+	 * increments the load count in table "layer_load_count" for
+	 * each layer in the WMC document by one. 
+	 */
+	function increment($metadata_id) {
+		if (!is_numeric($metadata_id)) {
+			return false;
+		}
+
+		//check if an entry exists for the current metadata id
+		$sql = "SELECT COUNT(metadata_id) AS i FROM mb_metadata WHERE metadata_id = $1";
+		$v = array($metadata_id);
+		$t = array('i');
+		$res = db_prep_query($sql, $v, $t);
+		$row = db_fetch_array($res);
+		if (intval($row["i"]) === 0) {
+			return false;
+		}
+
+		//check if an entry exists for the current metadata id
+		$sql = "SELECT load_count FROM metadata_load_count WHERE fkey_metadata_id = $1";
+		$v = array($metadata_id);
+		$t = array('i');
+		$res = db_prep_query($sql, $v, $t);
+		$row = db_fetch_array($res);
+
+		//if yes, increment the load counter
+		if ($row) {
+			$currentCount = $row["load_count"];
+			$sql = "UPDATE metadata_load_count SET load_count = $1 WHERE fkey_metadata_id = $2";
+			$v = array(intval($currentCount + 1), $metadata_id);
+			$t = array('i', 'i');
+			$res = db_prep_query($sql, $v, $t);
+		}
+		//if no, insert a new row with current metadata id and load_count = 1
+		else {
+			$sql = "INSERT INTO metadata_load_count (fkey_metadata_id, load_count) VALUES ($1, 1)";
+			$v = array($metadata_id);
+			$t = array('i');
+			$res = db_prep_query($sql, $v, $t);
+		}
+	}
+	//function to increment more than one layer with one sql statement
+	function incrementMultiMetadata($metadataIdArray) {
+		if (!is_array($metadataIdArray) || count($metadataIdArray) == 0) {
+			return false;
+		}
+		$metadataIdString = implode(",",$metadataIdArray);
+		//check for existing entry in load count table, if not exist - insert zero value
+		$sql = "SELECT fkey_metadata_id FROM metadata_load_count WHERE fkey_metadata_id IN (".$metadataIdString.")";
+		$res = db_query($sql);
+		$existingMetadataIds = array();
+		while($row = db_fetch_array($res)) {
+			array_push($existingMetadataIds, $row["fkey_metadata_id"]);
+		}
+		//check for existing metadata in mb_metadata table
+		$sql = "SELECT metadata_id FROM mb_metadata WHERE metadata_id IN (".$metadataIdString.")";
+		$res = db_query($sql);
+		$existingMetadataIdsInMetadata = array();
+		while($row = db_fetch_array($res)) {
+			array_push($existingMetadataIdsInMetadata, $row["metadata_id"]);
+		}
+		//use only those who exists in mb_metadata table
+		$metadataIdArray = array_intersect($metadataIdArray,$existingMetadataIdsInMetadata);
+		//check for existing layers in layer table
+		$notAlreadyExists = array_diff($metadataIdArray, $existingMetadataIds);
+		//Insert those into table (initialize)
+		foreach( $notAlreadyExists as $newMetadataId ) {
+    			$insertMetadata[] = '('.$newMetadataId.',0)';
+		}
+		if (count($insertMetadata) > 0) {
+			$sql = "INSERT INTO metadata_load_count (fkey_metadata_id,load_count) VALUES ".implode(',',$insertMetadata).";";
+			$res = db_query($sql);
+		}
+		//increment load counts
+		if (count($metadataIdArray) > 0) {
+			$sql = "UPDATE metadata_load_count SET load_count = load_count+1 WHERE fkey_metadata_id in (".implode(',',$metadataIdArray).")";
+			$res = db_query($sql);
+			if (!$res) {
+				$e = new mb_exception("class_metadata_monitor.php: Could not increment metadata load_count of metadata");
+				return false;
+			} else {
+				$e = new mb_notice("class_metadata_monitor.php: Updated load_count of metadata");
+				return true;
+			}
+		} else {
+			$e = new mb_exception("class_metadata_monitor.php: No metadata found to increment load_count");
+			return false;
+		}
+
+	}
+}
+?>

Modified: trunk/mapbender/http/classes/class_metadata_new.php
===================================================================
--- trunk/mapbender/http/classes/class_metadata_new.php	2016-05-09 13:11:13 UTC (rev 9447)
+++ trunk/mapbender/http/classes/class_metadata_new.php	2016-05-09 13:12:55 UTC (rev 9448)
@@ -325,17 +325,17 @@
 				$this->searchView = 'dataset_search_table';
 				$this->whereStrCatExtension = " AND custom_category.custom_category_hidden = 0";
 				switch ($this->orderBy) {
-					/*case "rank":
+					case "rank":
 						$this->orderBy = " ORDER BY load_count DESC";
-						break;*/
+						break;
 					case "id":
 						$this->orderBy = " ORDER BY metadata_id ASC";
 						break;
 					case "title":
-						$this->orderBy = " ORDER BY title ";
+						$this->orderBy = " ORDER BY title DESC";
 						break;
 					case "date":
-						$this->orderBy = " ORDER BY last_changed DESC ";
+						$this->orderBy = " ORDER BY dataset_timestamp DESC ";
 						break;
 					default:
 						//$this->orderBy = " ORDER BY load_count DESC";
@@ -601,7 +601,7 @@
 			$this->datasetJSON->dataset->srv[$i]->title = $datasetMatrix[$i]['title'];
 			$this->datasetJSON->dataset->srv[$i]->uuid = $datasetMatrix[$i]['fileidentifier'];
 			$this->datasetJSON->dataset->srv[$i]->abstract = $datasetMatrix[$i]['dataset_abstract'];
-			$this->datasetJSON->dataset->srv[$i]->date = date("d.m.Y",$datasetMatrix[$i]['dataset_timestamp']);
+			$this->datasetJSON->dataset->srv[$i]->date = date("Y-m-d",strtotime($datasetMatrix[$i]['dataset_timestamp']));
 			$this->datasetJSON->dataset->srv[$i]->respOrg = $datasetMatrix[$i]['mb_group_name'];
 			$this->datasetJSON->dataset->srv[$i]->logoUrl = $datasetMatrix[$i]['mb_group_logo_path'];
 			list($hasConstraints, $symbolLink) = $this->hasConstraints("dataset", $datasetMatrix[$i]['dataset_id']);
@@ -652,7 +652,7 @@
 				$coupledLayers = new self($this->userId, 'dummysearch', '*', null, null, null, null, null, null, null, count(array_unique($allCoupledLayers)), null, null, null, $this->languageCode, null, 'wms', 1, 'json', 'internal', null, null, $this->hostName, 'rank',implode(',',array_unique($allCoupledLayers)) ,false , null);
 				$srvCount = 0;				
 				foreach (json_decode($coupledLayers->internalResult)->wms->srv as $server) {			
-					$e = new mb_exception("server id ->". $server->id);
+					//$e = new mb_exception("server id ->". $server->id);
 					foreach($server->layer as $layer) {
 						//$layerSearchArray[$layer->id] = $server->id;
 						$layerSearchArray[$layer->id] = $srvCount;
@@ -693,7 +693,7 @@
 				}
 				$featuretypeCount = 0;
 				foreach ($this->datasetJSON->dataset->srv[$i]->coupledResources->featuretype as $ft) {
-					$e = new mb_exception($ft->id ." in service:  ". $featuretypeSearchArray[$ft->id]);
+					//$e = new mb_exception($ft->id ." in service:  ". $featuretypeSearchArray[$ft->id]);
 					$this->datasetJSON->dataset->srv[$i]->coupledResources->featuretype[$featuretypeCount]->srv = json_decode($coupledFeaturetypes->internalResult)->wfs->srv[$featuretypeSearchArray[$featuretype->id]];
 					$featuretypeCount++;
 				}
@@ -1014,7 +1014,7 @@
 				$filename = $this->tempFolder."/".$this->searchId."_filter.json";
 				$fileExists = $admin->getFromStorage($filename,TMP_SEARCH_RESULT_STORAGE);
 				if ($fileExists == false) {
-					$e = new mb_exception("class_metadata_new.php: No filter json exists!");
+					//$e = new mb_exception("class_metadata_new.php: No filter json exists!");
 				} else {
 					$filterJSON = $fileExists;
 					$filterJSON = $this->json->decode($filterJSON);
@@ -1072,7 +1072,7 @@
 				$filename = $this->tempFolder."/".$this->searchId."_filter.json";
 				$fileExists = $admin->getFromStorage($filename,TMP_SEARCH_RESULT_STORAGE);
 				if ($fileExists == false) {
-					$e = new mb_exception("class_metadata_new.php: No filter json exists!");
+					//$e = new mb_exception("class_metadata_new.php: No filter json exists!");
 				} else {
 					$filterJSON = $fileExists;
 					$filterJSON = $this->json->decode($filterJSON);
@@ -1132,7 +1132,7 @@
 				$filename = $this->tempFolder."/".$this->searchId."_filter.json";
 				$fileExists = $admin->getFromStorage($filename,TMP_SEARCH_RESULT_STORAGE);
 				if ($fileExists == false) {
-					$e = new mb_exception("class_metadata_new.php: No filter json exists!");
+					//$e = new mb_exception("class_metadata_new.php: No filter json exists!");
 				} else {
 					$filterJSON = $fileExists;
 					$filterJSON = $this->json->decode($filterJSON);
@@ -1405,7 +1405,7 @@
 		//defining range for paging
 		$sql .= " LIMIT ".$limit." OFFSET ".$offset."";
 		//Print out search SQL term
-		$e = new mb_exception("class_metadata.php: Search => SQL-Request of ".$this->searchResources." service metadata: ".$sql."");
+		$e = new mb_notice("class_metadata.php: Search => SQL-Request of ".$this->searchResources." service metadata: ".$sql."");
 		//parameter: searchId -> can be used global, searchResources -> is only one type per instance!!-> global,which categories -> can be defined global! $whereStr
 		$n = $this->writeCategories($whereStr, $v, $t); 
 		//write counts to filesystem to avoid to many database connections

Modified: trunk/mapbender/http/classes/class_wmc.php
===================================================================
--- trunk/mapbender/http/classes/class_wmc.php	2016-05-09 13:11:13 UTC (rev 9447)
+++ trunk/mapbender/http/classes/class_wmc.php	2016-05-09 13:12:55 UTC (rev 9448)
@@ -1255,7 +1255,7 @@
 	}
 
 	private function incrementLoadCount ($wms) {
-	// counts how often a layer has been loaded
+		// counts how often a layer has been loaded
 		$monitor = new Layer_load_count();
 		foreach ($wms->objLayer as $l) {
 			$monitor->increment($l->layer_uid);
@@ -1263,7 +1263,7 @@
 	}
 
 	private function incrementLayerLoadCount ($layerIdArray) {
-		$layerIdString = implode(",",$layerIdArray);
+		/*$layerIdString = implode(",",$layerIdArray);
 		$sql = "UPDATE layer_load_count SET load_count = load_count+1 WHERE fkey_layer_id in (".$layerIdString.")";
 		$res = db_query($sql);
 		if (!$res) {
@@ -1272,7 +1272,9 @@
 		} else {
 			$e = new mb_notice("class_wmc.php: Updated load_count of layers in wmc!");
 			return true;
-		}
+		}*/
+		$monitor = new Layer_load_count();
+		$monitor->incrementMultiLayers($layerIdArray);
 	}
 
 	//http://stackoverflow.com/questions/3361036/php-simplexml-insert-node-at-certain-position

Modified: trunk/mapbender/http/php/mod_inspireDownloadFeed.php
===================================================================
--- trunk/mapbender/http/php/mod_inspireDownloadFeed.php	2016-05-09 13:11:13 UTC (rev 9447)
+++ trunk/mapbender/http/php/mod_inspireDownloadFeed.php	2016-05-09 13:12:55 UTC (rev 9448)
@@ -30,6 +30,7 @@
 require_once(dirname(__FILE__) . "/../../conf/mimetype.conf");
 require_once(dirname(__FILE__) . "/../classes/class_cache.php");
 require_once(dirname(__FILE__) . "/../classes/class_iso19139.php");
+require_once(dirname(__FILE__) . "/../classes/class_metadata_monitor.php");
 
 //check_epsg_wms_13($tmp_epsg)
 //http://www.weichand.de/inspire/dls/verwaltungsgrenzen.xml
@@ -686,6 +687,7 @@
 	if (!isset($mapbenderMetadata[$m]->metadata_id) || $mapbenderMetadata[$m]->metadata_id == '') {
 		return "<error>The metadataset with id ".$mapbenderMetadata[$m]->metadata_id." has no coupled ".$generateFrom." ressource ".$m."</error>";
 	}
+
 	$crs = $mapbenderMetadata[$m]->metadata_ref_system;
 	if (!isset($mapbenderMetadata[$m]->metadata_ref_system) || $mapbenderMetadata[$m]->metadata_ref_system == '') {
 		return "<error>For the metadataset with id ".$mapbenderMetadata[$m]->metadata_id." is no reference system defined!</error>";
@@ -802,6 +804,13 @@
 	$e = new mb_exception("mod_inspireDownloadFeed.php: md_timestamp: ".date("Y-m-d H:i:s",strtotime($mapbenderMetadata[$m]->md_timestamp)));
 	$e = new mb_exception("mod_inspireDownloadFeed.php: group timestamp: ".date("Y-m-d H:i:s",strtotime($departmentMetadata['timestamp'])));
 	$e = new mb_exception("mod_inspireDownloadFeed.php: user timestamp: ".date("Y-m-d H:i:s",strtotime($userMetadata['timestamp'])));*/
+	if ($type == 'DATASET') {
+		if (isset($mapbenderMetadata[$m]->metadata_id)) {
+			//log download for resource
+			$metadataMonitor = new Metadata_load_count();
+			$metadataMonitor->increment($mapbenderMetadata[$m]->metadata_id);
+		}
+	}
 	$timestamps = array(
 		date("Y-m-d H:i:s",$mapbenderMetadata[$m]->wms_timestamp),
 		date("Y-m-d H:i:s",$mapbenderMetadata[$m]->wfs_timestamp),
@@ -1089,25 +1098,13 @@
 				//TODO: check if epsg, and bbox are filled correctly!
 				$sqlExtent = "SELECT X(transform(GeometryFromText('POINT(".$mapbenderMetadata[$m]->minx." ".$mapbenderMetadata[$m]->miny.")',4326),".$epsgId.")) as minx, Y(transform(GeometryFromText('POINT(".$mapbenderMetadata[$m]->minx." ".$mapbenderMetadata[$m]->miny.")',4326),".$epsgId.")) as miny, X(transform(GeometryFromText('POINT(".$mapbenderMetadata[$m]->maxx." ".$mapbenderMetadata[$m]->maxy.")',4326),".$epsgId.")) as maxx, Y(transform(GeometryFromText('POINT(".$mapbenderMetadata[$m]->maxx." ".$mapbenderMetadata[$m]->maxy.")',4326),".$epsgId.")) as maxy";
 				$resExtent =  db_query($sqlExtent);
-				//TODO calculate length on earth if coordinates are given in deegree
 				$minx = floatval(db_result($resExtent,0,"minx"));
 				$miny = floatval(db_result($resExtent,0,"miny"));
 				$maxx = floatval(db_result($resExtent,0,"maxx"));
 				$maxy = floatval(db_result($resExtent,0,"maxy"));
-				$lengthSql = "SELECT ST_Distance("; 
-				$lengthSql .= "ST_Transform(GeomFromText('POINT(".$minx." ".$miny.")',4326),"."25832"."),";
-				$lengthSql .= "ST_Transform(GeomFromText('POINT(".$minx." ".$maxy.")',4326),"."25832".")) as lengthX,";
-				$lengthSql .= "ST_Distance("; 
-				$lengthSql .= "ST_Transform(GeomFromText('POINT(".$maxx." ".$miny.")',4326),"."25832"."),";
-				$lengthSql .= "ST_Transform(GeomFromText('POINT(".$maxx." ".$maxy.")',4326),"."25832".")) as lengthY";
-				$resLength =  db_query($lengthSql);
-				
-				$diffX = floatval(db_result($resLength,0,"lengthX"));
-				$diffY = floatval(db_result($resLength,0,"lengthY"));
-				$e = new mb_exception($diffX."-".$diffX);
-				//TODO: calculate in deegree again or use other origin?
-				//$diffX = $maxx - $minx; //in m
-				//$diffY = $maxy - $miny;	//in m
+	
+				$diffX = $maxx - $minx; //in m
+				$diffY = $maxy - $miny;	//in m
 				//$e = new mb_exception($diffX);
 				//echo $diffX .":". $diffY;
 				//calculate target number of pixels for x and y



More information about the Mapbender_commits mailing list