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

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Tue Dec 18 00:06:02 PST 2012


Author: armin11
Date: 2012-12-18 00:06:01 -0800 (Tue, 18 Dec 2012)
New Revision: 8530

Modified:
   trunk/mapbender/http/classes/class_wmc.php
   trunk/mapbender/http/php/mod_wmc2ol.php
Log:
Add possibility to update wmc documents if the layer names are changed in the updateWMS process.

Modified: trunk/mapbender/http/classes/class_wmc.php
===================================================================
--- trunk/mapbender/http/classes/class_wmc.php	2012-12-12 10:57:21 UTC (rev 8529)
+++ trunk/mapbender/http/classes/class_wmc.php	2012-12-18 08:06:01 UTC (rev 8530)
@@ -503,105 +503,102 @@
 		return $resultObj;
 	}
 
-	public function updateUrlsFromDb () {
-		$query_mbWMSId = "/wmc:ViewContext/wmc:LayerList/wmc:Layer/wmc:Extension/mapbender:wms_id";
+	/*
+	* function to update the information about wms in a mapbender wmc object and stores it in the database
+	* @return WMC as XML or false.
+	*/
+	public function updateUrlsFromDb() {
+		$startTime = microtime();
+		//declare xpath to pull all layer with given ids from stored wmc docs
+		$query_mbLayerId = "/wmc:ViewContext/wmc:LayerList/wmc:Layer/wmc:Extension/mapbender:layer_id";
+		//parse xml with php simple xml and handle errors
+		libxml_use_internal_errors(true);
 		try {
-			$WMCDoc = DOMDocument::loadXML($this->toXml());
+			$WMCDoc = simplexml_load_string($this->toXml());
+			if ($WMCDoc === false) {
+				foreach(libxml_get_errors() as $error) {
+        				$err = new mb_exception("class_wmc:".$error->message);
+    				}
+				throw new Exception("class_wmc:".'Cannot parse WMC XML!');
+				return false;
+			}
 		}
-		catch (Exception $E) {
-			new mb_exception("WMC XML is broken.");
+		catch (Exception $e) {
+    			$err = new mb_exception("class_wmc:".$e->getMessage());
+			return false;
 		}
-
-		$xpath = new DOMXPath($WMCDoc);
-		$xpath->registerNamespace("wmc","http://www.opengis.net/context");
-		$xpath->registerNamespace("mapbender","http://www.mapbender.org/context");
-		$xpath->registerNamespace("xlink","http://www.w3.org/1999/xlink");
-
-		$WMSIdList = $xpath->query($query_mbWMSId);
-		foreach($WMSIdList as $WMSId) {
-			$id =  $WMSId->nodeValue;
-			$sql = "SELECT wms_timestamp,wms_getmap,wms_getlegendurl, wms_owsproxy " .
-				"FROM wms WHERE wms_id = $1";// AND " .
-				//"wms_owsproxy <> NULL AND wms_owsproxy <> ''";
-			$v = array($id);
-			$t = array("t");
-
-			$res = db_prep_query($sql,$v,$t);
-			if (db_error()) {
-				true;
-			} //FIMXE: PROPER ERROR MESSAGE
-
-			if($row = db_fetch_array($res)) {
-				//check if ows_proxy is set
+		//register relevant namespaces
+		$WMCDoc->registerXPathNamespace("wmc","http://www.opengis.net/context");
+		$WMCDoc->registerXPathNamespace("mapbender","http://www.mapbender.org/context");
+		$WMCDoc->registerXPathNamespace("xlink","http://www.w3.org/1999/xlink");
+		//pull out List of layer objects
+		$layerIdList = $WMCDoc->xpath($query_mbLayerId);
+		$e = new mb_notice(count($layerIdList));
+		//Select current layer and wms information with one SQL select query!
+		$v = array();
+		$t = array();
+		$layerIds = array();
+		$sql = "SELECT layer_id, layer_name, fkey_wms_id, wms_timestamp, wms_getmap, wms_getlegendurl, wms_owsproxy FROM layer, wms WHERE layer_id in ( ";
+		$i = 0;
+		foreach($layerIdList as $layerIdObject) {
+			//use only integer layer ids
+			if (is_int((integer)$layerIdObject) && $layerIdObject != null && $layerIdObject != "") {
+				if($i > 0){$sql .= ",";}
+				$sql .= "$".($i + 1);
+				array_push($v,$layerIdObject);
+				array_push($t,'i');
+			}
+			$i++;
+		}
+		$i = 0;
+		$sql .= ") and layer.fkey_wms_id = wms.wms_id";
+		$res = db_prep_query($sql,$v,$t);
+		$e = new mb_notice("class_wmc: sql to pull current wms and layer information from database: ".$sql);
+		//for each found layer
+		while($row = db_fetch_array($res)){
+			$wmsId = $row["fkey_wms_id"];
+			$layerId = $row["layer_id"];
+			$layerName = $row["layer_name"];
+			//xpath to pull a special wmc layer object from simple xml object
+			$queryPath = "/wmc:ViewContext/wmc:LayerList/wmc:Layer[wmc:Extension/mapbender:layer_id='".(integer)$layerId."']";
+			//Some help for get and set attributes: http://stackoverflow.com/questions/2956601/change-xml-node-element-value-in-php-and-save-file
+			//Condition for secured and unsecured mapbender wms
+			if (isset($row["wms_owsproxy"]) && $row["wms_owsproxy"] != '') {
+				//set relevant wms urls to owsproxy urls
+				$wmsowsproxy = $row["wms_owsproxy"];
+				$owsproxyurl = OWSPROXY."/".session_id()."/".$wmsowsproxy."?";
+				$wmsGetMapUrl = $owsproxyurl;
+				$wmsGetLegendUrl = $owsproxyurl;
+			} else { 
+				//service is not secured - exchange urls with the latest ones from database
+				$wmsGetMapUrl = $row["wms_getmap"];
+				$wmsGetLegendUrl = $row["wms_getlegendurl"];
+				//Exchange the given styles for each layer
+				//TODO: Exchange the style urls on a right way!
+			}
+			//Loop over found layerObject - normally only one single layer is there. Alter the information in the simple xml object
+			foreach($WMCDoc->xpath($queryPath) as $layer ) {
+				$e = new mb_notice("class_wmc: exchange old layer name : ".$layer->Name." with new layer name: ".$layerName);
+  				$layer->Name = $layerName;
+				$e = new mb_notice("class_wmc: exchange old getmap url : ".$layer->Server->OnlineResource->attributes('xlink', true)->href." with new getmap url: ".$wmsGetMapUrl);
+ 				$layer->Server->OnlineResource->attributes('xlink', true)->href = $wmsGetMapUrl;
+				//Help for problem with xlink:href attributes: http://php.net/manual/de/class.simplexmlelement.php!!!!!	
+				//exchange legend urls
 				if (isset($row["wms_owsproxy"]) && $row["wms_owsproxy"] != '') {
-					//set relevant wms urls to owsproxy urls
-					$wmsowsproxy = $row["wms_owsproxy"];
-					$owsproxyurl = OWSPROXY."/".session_id()."/".$wmsowsproxy."?";
-					$wmsGetMapUrl = $owsproxyurl;
-					$wmsGetLegendUrl = $owsproxyurl;
-					//in the case of owsproxy the urls should be exchanged every time!
-					$MapResources = $xpath->query("../../wmc:Server/wmc:OnlineResource",$WMSId);
-					foreach($MapResources as $MapResource) {
-						$MapResource->setAttribute("xlink:href",$wmsGetMapUrl);
-					}
-
-					$LegendResources = $xpath->query("../../wmc:StyleList/wmc:Style/wmc:LegendURL/wmc:OnlineResource",$WMSId);
-					foreach ($LegendResources as $LegendResource) {
-							$e = new mb_notice("class_wmc.php: old getlegendurl (WMC): ".$LegendResource->getAttribute("xlink:href"));
-							$e = new mb_notice("class_wmc.php: new getlegendurl (OWSPROXY): ".$wmsGetLegendUrl);
-							//get param part of getlegend url from xml
-							$arURL = parse_url($LegendResource->getAttribute("xlink:href"));
-							$query = $arURL["query"];
-							$url = $wmsGetLegendUrl . $query;
-							$LegendResource->setAttribute("xlink:href",$url);
-					}
-				} else { //service is not secured - exchange urls only when they may have changed
-					$wmsGetMapUrl = $row["wms_getmap"];
-					$wmsGetLegendUrl = $row["wms_getlegendurl"];
-					
-					//in cases when no owsproxy is defined exchange only if timestamp are not in sync
-					$wms_timestamp = $row["wms_timestamp"];
-					//if ($this->timestamp < $wms_timestamp) {//TODO: check if such a distiction is really needful - the timestamp of a wmc changes if some metadata changes - therefor it is needed - exchange all urls by default! 
-					if (false) {
-						// wmc is fresh, life is good
-						$e = new mb_notice("class_wmc.php: the wms metadata has not been changed - no url will be exchanged!");
-					}
-					else {
-						$MapResources = $xpath->query("../../wmc:Server/wmc:OnlineResource",$WMSId);
-						$e = new mb_notice("class_wmc.php: the wms is newer than the wmc -urls will be exchanged!");
-						foreach($MapResources as $MapResource) {
-							$MapResource->setAttribute("xlink:href",$wmsGetMapUrl);
-						}
-						//Exchange the given styles for each layer
-						$query_mbLayerId = "/wmc:ViewContext/wmc:LayerList/wmc:Layer/wmc:Extension/mapbender:layer_id";
-						$LayerIdList = $xpath->query($query_mbLayerId);
-						//TODO: exchange the style urls on a right way!
-						/*foreach($LayerIdList as $LayerId) {
-							
-							$e = new mb_exception("layer_id: ".print_r($LayerId->nodeValue));
-						}*/
-
-						//$LegendResources = $xpath->query("../../wmc:StyleList/wmc:Style/wmc:LegendURL/wmc:OnlineResource",$WMSId);
-						
-						/*foreach ($LegendResources as $LegendResource) {
-							$e = new mb_exception("class_wmc.php: old getlegendurl (WMC): ".$LegendResource->getAttribute("xlink:href"));
-							$e = new mb_exception("class_wmc.php: new getlegendurl (DB): ".$wmsGetLegendUrl);
-							//get param part of getlegend url from xml
-							$arURL = parse_url($LegendResource->getAttribute("xlink:href"));
-							$query = $arURL["query"];
-							$url = $wmsGetLegendUrl . $query;
-							$LegendResource->setAttribute("xlink:href",$url);
-						}*/
-					}
+					$arURL = parse_url($layer->StyleList->Style->LegendURL->OnlineResource->attributes('xlink', true)->href);
+					$query = $arURL["query"];
+					$url = $wmsGetLegendUrl . $query;
+					$layer->StyleList->Style->LegendURL->OnlineResource->attributes('xlink', true)->href = $url;
 				}
-				
 			}
 		}
 		$updatedWMC = $WMCDoc->saveXML();
-		$this->update_existing($updatedWMC,$wmsId);
+		$e = new mb_notice($updatedWMC);
+		$this->update_existing($updatedWMC, $this->wmc_id);
+		$endTime = microtime();
+		$e = new mb_notice((string)($endTime - $startTime));	
 		return $updatedWMC;
 	}
-
 	/**
 	 * Stores this WMC in the database. The WMC has to be instantiated first, see above.
 	 *
@@ -841,10 +838,9 @@
 		return $result;
 	}
 
-
-    /*
-    * overwrites an exact version of a wmc in the database
-    */
+    	/*
+    	* overwrites an exact version of a wmc in the database
+    	*/
 	public function update_existing($xml,$id) {
 		$sql = "UPDATE mb_user_wmc SET wmc = $1 WHERE wmc_serial_id = $2";
 		$v = array($xml,$id);

Modified: trunk/mapbender/http/php/mod_wmc2ol.php
===================================================================
--- trunk/mapbender/http/php/mod_wmc2ol.php	2012-12-12 10:57:21 UTC (rev 8529)
+++ trunk/mapbender/http/php/mod_wmc2ol.php	2012-12-18 08:06:01 UTC (rev 8530)
@@ -109,8 +109,27 @@
 		return false;
 	}
 }
+
+//Function to pull layer names from database. They may have been updated since the wmc have been saved!
+function getLayerNames($wmsId){
+	$sql = "SELECT layer_id, layer_name FROM layer WHERE fkey_wms_id = $1";
+	$v = array($wmsId);
+	$t = array("i");
+	$res = db_prep_query($sql,$v,$t);
+	while($row = db_fetch_array($res)){
+		$layerNames[$row["layer_id"]] = $row["layer_name"];
+	}
+	if (count($layerNames) > 0) {
+		return $layerNames;
+	} else {
+		return false;
+	}
+}
+
 //end of functions which may be included from class_administration in next versions
 //**************************************************************************
+
+
 //Function to create an OpenLayers Javascript from a mapbender wmc document
 function createOlFromWMC_id($wmc_id, $pointRadius, $fillColor){
 	//$myWmc = new wmc();
@@ -362,6 +381,7 @@
 		$layer_id=dom_import_simplexml($extensions->layer_id)->nodeValue;
 		$layer_name=$xml->LayerList->Layer[$i]->Name;
 		$wms_id=dom_import_simplexml($extensions->wms_id)->nodeValue;
+		$layerNames = getLayerNames($wms_id);
 		$has_permission=$admin->getLayerPermission($wms_id, $layer_name, $userId);
 		if ($has_permission || $layer_id==''){
 			$getMapUrl = $xml->LayerList->Layer[$i]->Server->OnlineResource->attributes('http://www.w3.org/1999/xlink')->href;
@@ -371,7 +391,15 @@
 			}
 			$html.="		\"".$getMapUrl."\",\n";
 			$html.="		{\n";
-			$html.="		layers: \"".$xml->LayerList->Layer[$i]->Name."\",\n";
+			//output for testing layer names
+			//foreach ($layerNames as $key => $value) {
+    			//	$e = new mb_exception("Key: $key; Value: $value");
+			//}
+			if (isset($layerNames[(string)$layer_id]) && $layerNames[(string)$layer_id] != "") {
+				$html.="		layers: \"".$layerNames[(string)$layer_id]."\",\n";
+			} else {
+				$html.="		layers: \"".$xml->LayerList->Layer[$i]->Name."\",\n";
+			}
 			//get FormatList and the current active format -> TODO: make a function for getting actual format for request
 			$format='png';
 			foreach ($xml->LayerList->Layer[$i]->FormatList->Format as $current_format) {
@@ -413,11 +441,11 @@
 	$startLayerId = $firstLayerId+1;
 	for ($i=$startLayerId; $i<count($layer_array); $i++) {
 		$extensions=$xml->LayerList->Layer[$i]->Extension->children('http://www.mapbender.org/context');
-		#$layer_id=$extensions->layer_id;
 		$wms_id=$extensions->wms_id;
 		$layer_id=dom_import_simplexml($extensions->layer_id)->nodeValue;
 		$layer_name=$xml->LayerList->Layer[$i]->Name;
 		$wms_id=dom_import_simplexml($extensions->wms_id)->nodeValue;
+		$layerNames = getLayerNames($wms_id);
 		$has_permission=$admin->getLayerPermission($wms_id, $layer_name, $userId);
 		if (($xml->LayerList->Layer[$i]->attributes()->hidden=='0' && $has_permission && $extensions->layer_parent != '') ||
 			($layer_id=='' && $xml->LayerList->Layer[$i]->attributes()->hidden=='0')){
@@ -429,7 +457,11 @@
 			}
 			$html.="		\"".$getMapUrl."\",\n";
 			$html.="		{\n";
-			$html.="		layers: \"".$xml->LayerList->Layer[$i]->Name."\",\n";
+			if (isset($layerNames[(string)$layer_id]) && $layerNames[(string)$layer_id] != "") {
+				$html.="		layers: \"".$layerNames[(string)$layer_id]."\",\n";
+			} else {
+				$html.="		layers: \"".$xml->LayerList->Layer[$i]->Name."\",\n";
+			}
 			//Get FormatList and the current active format
 			$format='png';
 			foreach ($xml->LayerList->Layer[$i]->FormatList->Format as $current_format) {



More information about the Mapbender_commits mailing list