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

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Wed Jul 13 08:13:47 PDT 2016


Author: armin11
Date: 2016-07-13 08:13:47 -0700 (Wed, 13 Jul 2016)
New Revision: 9542

Added:
   trunk/mapbender/http/classes/class_cswClient.php
Log:
Draft of a simple csw client class to insert/update/delete metadata records in distributed catalogues, that are registered in the mapbender database

Added: trunk/mapbender/http/classes/class_cswClient.php
===================================================================
--- trunk/mapbender/http/classes/class_cswClient.php	                        (rev 0)
+++ trunk/mapbender/http/classes/class_cswClient.php	2016-07-13 15:13:47 UTC (rev 9542)
@@ -0,0 +1,241 @@
+<?php
+# $Id$
+# http://www.mapbender.org/index.php/class_cswClient
+# 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");
+require_once(dirname(__FILE__)."/class_csw.php");
+require_once(dirname(__FILE__)."/class_iso19139.php");
+/**
+ * CSW client class to make requests to and handle results from catalogues
+ * @author armin11
+ *
+ */
+class cswClient {
+	var $cswId;
+	var $operationName;
+	var $operationResult;
+	//var $operationStatus;
+	var $operationSuccessful;
+	var $operationException;
+	var $operationExceptionText;
+	
+	public function __construct() {
+		$this->cswId = null;
+		$this->operation = null;	
+		$this->result = null;
+		$this->operationSuccessful = false;
+	}
+	
+	public function doRequest($cswId, $operationName, $recordId=false, $record=false) {
+		$this->cswId = $cswId;
+		$csw = new csw();
+		$csw->createCatObjFromDB($this->cswId);
+		$e = new mb_exception($csw->cat_op_values[$operationName]['get']);
+		//check for operation support
+		switch (strtolower($operationName)) {
+			case "getrecords":
+				if (isset($csw->cat_op_values[$operationName]['get']) ||  isset($csw->cat_op_values[$operationName]['post'])) {
+					//all ok
+				} else {
+					$e = new mb_exception("classes/class_cswClient.php: Operation not supported by catalogue!");
+					return false;
+				}
+				break;
+			case "getrecordbyid":
+				if (isset($csw->cat_op_values[$operationName]['get']) ||  isset($csw->cat_op_values[$operationName]['post'])) {
+					//all ok
+				} else {
+					$e = new mb_exception("classes/class_cswClient.php: Operation not supported by catalogue!");
+					return false;
+				}
+				break;
+			case "transactionupdate":
+				if (isset($csw->cat_op_values["transaction"]['get']) ||  isset($csw->cat_op_values["transaction"]['post'])) {
+					//all ok
+				} else {
+					$e = new mb_exception("classes/class_cswClient.php: Operation not supported by catalogue!");
+					return false;
+				}
+				break;
+			case "transactioninsert":
+				if (isset($csw->cat_op_values["transaction"]['get']) ||  isset($csw->cat_op_values["transaction"]['post'])) {
+					//all ok
+				} else {
+					$e = new mb_exception("classes/class_cswClient.php: Operation not supported by catalogue!");
+					return false;
+				}
+				break;
+			
+			default: 
+				break;
+		}
+		/*if () {
+		}*/
+		switch (strtolower($operationName)) {
+			case "getrecordbyid":
+				//define standard xml request
+				//maybe from xsd?
+				$postRequest = '<?xml version="1.0"?>';			
+				$postRequest .= '<csw:GetRecordById xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" service="CSW" version="2.0.2" outputSchema="http://www.isotc211.org/2005/gmd">';
+    				$postRequest .= '<csw:Id>'.$recordId.'</csw:Id>';
+				$postRequest .= '</csw:GetRecordById>';
+				break;
+			case "getrecords":
+				
+				break;
+			case "transactionupdate":
+				$postRequest = '<?xml version="1.0" encoding="UTF-8"?>';
+				$postRequest .= '<csw:Transaction service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:ogc="http://www.opengis.net/ogc" xmlns:apiso="http://www.opengis.net/cat/csw/apiso/1.0">';
+    				$postRequest .= '<csw:Update>';
+				$postRequest .= explode("\n", $record->metadata, 2)[1];
+				$postRequest .= ' </csw:Update>';
+				$postRequest .= '</csw:Transaction>';
+				break;
+			case "transactioninsert":
+				$postRequest = '<?xml version="1.0" encoding="UTF-8"?>';
+				$postRequest .= '<csw:Transaction service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:ogc="http://www.opengis.net/ogc" xmlns:apiso="http://www.opengis.net/cat/csw/apiso/1.0">';
+    				$postRequest .= '<csw:Insert>';
+				$postRequest .= explode("\n", $record->metadata, 2)[1];
+				$postRequest .= ' </csw:Insert>';
+				$postRequest .= '</csw:Transaction>';
+				break;
+			case "transactiondelete":
+				$postRequest = '<?xml version="1.0" encoding="UTF-8"?>';
+				$postRequest .= '<csw:Transaction service="CSW" version="2.0.2" xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:ogc="http://www.opengis.net/ogc" xmlns:apiso="http://www.opengis.net/cat/csw/apiso/1.0">';
+    				$postRequest .= '<csw:Delete>';
+            			$postRequest .= '<csw:Constraint version="1.0.0">';
+                		$postRequest .= '<ogc:Filter>';
+                    		$postRequest .= '<ogc:PropertyIsLike wildCard="%" singleChar="_" escapeChar="/">';
+                       		$postRequest .= ' <ogc:PropertyName>apiso:Identifier</ogc:PropertyName>';
+                        	$postRequest .= '<ogc:Literal>'.$recordId.'</ogc:Literal>';
+                    		$postRequest .= '</ogc:PropertyIsLike>';
+                		$postRequest .= '</ogc:Filter>';
+            			$postRequest .= '</csw:Constraint>';
+        			$postRequest .= '</csw:Delete>';
+    				$postRequest .= '</csw:Transaction>';
+				break;
+			default: 
+				break;
+			
+		}
+		//do request and return result
+		//$e = new mb_exception($csw->cat_op_values[$operationName]['post']);
+		if (strpos($operationName, "transaction") === false) {
+			$this->operationResult = $this->getResult($csw->cat_op_values[$operationName]['post'], $postRequest);
+		} else {
+			$this->operationResult = $this->getResult($csw->cat_op_values["transaction"]['post'], $postRequest);
+		}
+		//parse response
+		libxml_use_internal_errors(true);
+		try {
+			$cswResponseObject = simplexml_load_string($this->operationResult);
+			if ($$cswResponseObject === false) {
+				foreach(libxml_get_errors() as $error) {
+        				$err = new mb_exception("class_cswClient:".$error->message);
+    				}
+				throw new Exception("class_cswClient:".'Cannot parse CSW Response XML!');
+				$this->operationResult = "Cannot parse CSW Response XML!";
+				return false;
+			}
+		}
+		catch (Exception $e) {
+    			$err = new mb_exception("class_cswClient:".$e->getMessage());
+			$this->operationResult = $e->getMessage();
+			return false;
+		}
+		if ($cswResponseObject !== false) {
+			$cswResponseObject->registerXPathNamespace("ows", "http://www.opengis.net/ows");
+			$cswResponseObject->registerXPathNamespace("gml", "http://www.opengis.net/gml");
+			$cswResponseObject->registerXPathNamespace("gmd", "http://www.isotc211.org/2005/gmd");
+			$cswResponseObject->registerXPathNamespace("csw", "http://www.opengis.net/cat/csw/2.0.2");
+			$cswResponseObject->registerXPathNamespace("ogc", "http://www.opengis.net/ogc");
+			$cswResponseObject->registerXPathNamespace("gco", "http://www.isotc211.org/2005/gco");
+			//$iso19139Xml->registerXPathNamespace("ogc", "http://www.opengis.net/ogc");
+			$cswResponseObject->registerXPathNamespace("xlink", "http://www.w3.org/1999/xlink");
+			$cswResponseObject->registerXPathNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
+			//$cswResponseObject->registerXPathNamespace("default", "");
+			$this->operationException = $cswResponseObject->xpath('/ows:ExceptionReport');
+			if (is_array($this->operationException) && count($this->operationException) > 0 || !is_array($this->operationException)) {
+				$err = new mb_exception("class_cswClient first exception that occured: ".$this->operationException[0]->asXML());
+				$this->operationResult = "An ows exception occured!";
+			} else {
+				switch (strtolower($operationName)) {
+					case "getrecordbyid":
+						//try to handle metadata - count the returned records
+						$metadataRecord = $cswResponseObject->xpath('/csw:GetRecordByIdResponse/gmd:MD_Metadata');
+						if (is_array($metadataRecord) && count($metadataRecord) <> 1 || !is_array($metadataRecord)) {
+							//$err = new mb_exception(count($metadataRecord));
+							//$err = new mb_exception($metadataRecord[0]->asXml());
+							$err = new mb_exception("class_cswClient.php: More or less than one metadata returned for a getrecordby id response!");
+							$this->operationResult = "More or less than one metadata returned for a getrecordby id response!";
+							return false;
+						} else {
+							$this->operationSuccessful = true;
+							//return $metadataRecord[0]->asXML();
+							//return "<result>One metadata record with id ".$recordId." found in catalogue</result>";
+							return true;
+						}
+						break;
+				}
+			}
+			//handle expections if occur
+			//count objects in response - if no object available
+		}
+		return $cswResponseObject->asXML();
+	}
+
+	public function pushRecord($isoMetadataObject, $auth=false) {
+		//check if metadata already in catalogue
+		if ($this->doRequest($this->cswId, 'getrecordbyid', $isoMetadataObject->fileIdentifier)) {
+			$err = new mb_exception("class_cswClient.php: do a transaction update");
+			$response = $this->doRequest($this->cswId, 'transactionupdate', false, $isoMetadataObject);
+		} else {
+			$err = new mb_exception("class_cswClient.php: do a transaction insert");
+			$response = $this->doRequest($this->cswId, 'transactioninsert', false, $isoMetadataObject);
+		}
+		return $response;
+	}
+
+	private function getResult($url, $postData, $auth=false) {
+			//$e = new mb_exception("postdata: ".$postData);
+			$cswInterfaceObject = new connector();
+			$cswInterfaceObject->set('httpType','POST');
+			$postData = stripslashes($postData);
+			$dataXMLObject = new SimpleXMLElement($postData);
+			$postData = $dataXMLObject->asXML();
+			//$e = new mb_exception($postData);
+			$cswInterfaceObject->set('curlSendCustomHeaders',true);
+			$cswInterfaceObject->set('httpPostData', $postData);
+			$cswInterfaceObject->set('httpContentType','text/xml');
+			$cswInterfaceObject->load($url);
+			//$file = $cswInterfaceObject->file;
+			return $cswInterfaceObject->file;
+	}
+	//insert or update - depends if record with same fileidentifier already exists in catalog and if timestamp is not newer than the timestamp of the new record
+
+	
+	public function deleteRecord($fileIdentifier, $auth=false) {
+		$response = $this->doRequest($this->cswId, 'transactiondelete', $fileIdentifier, false);
+		return $response;
+	}
+
+	private function getRecordsPage($catalogId, $page, $filter=false) {
+
+	}
+}
+?>



More information about the Mapbender_commits mailing list