\n";
//**************************overview part begin******************************
//generate div tags for the content - the divs are defined in the array
switch ($layout) {
case "accordion":
$html .= '
';//element
//***************************************************************************
//**************************properties part begin******************************
//generate div tags for the content - the divs are defined in the array
switch ($layout) {
case "accordion":
$html .= '
';//element
//***************************************************************************
//**************************contact part begin******************************
//generate div tags for the content - the divs are defined in the array
switch ($layout) {
case "accordion":
$html .= '
';//element
//***************************************************************************
//**************************terms of use part begin******************************
//generate div tags for the content - the divs are defined in the array
switch ($layout) {
case "accordion":
$html .= '
';//element
//***************************************************************************
//**************************quality part begin******************************
//generate div tags for the content - the divs are defined in the array
switch ($layout) {
case "accordion":
$html .= '
';//element
//***************************************************************************
//**************************Interfaces part begin******************************
//generate div tags for the content - the divs are defined in the array
switch ($layout) {
case "accordion":
$html .= '
';
$html .= '';
if (count($serviceInformation->service) > 0) {
//new for coupled services if they exists:
$html .= '';
}
//$html .= '';
$html .= '
';//foreach upper category element
//***************************************************************************
switch ($layout) {
case "accordion":
$html .= '
';
$html .= '
';
break;
case "tabs":
$html .= '
';
$html .= '
';
break;
case "plain":
$html .= '
';
$html .= '
';
break;
default:
break;
}
$html .= '
'; //demo
$html .= '';
$html .= '';
return $html;
}
}
private function getHtmlRow($t_a, $t_b, $t_c, $iso19139Hash, $isoHashIndex){
$stringToReturn = $t_a."".$iso19139Hash[$isoHashIndex]['html'].": ".$t_b."url;
//$this->licenseJson = stripslashes($otherConstraint);
//$e = new mb_exception("class_iso19139.php: licenseSourceNote: ".$this->licenseSourceNote);
} else {
$URL = "No license url found or json not valid!";
}
}
}
$iso19139Hash[$isoHashIndex]['value'] = $URL;
break;
default:
break;
}
}
if (isset($iso19139Hash[$isoHashIndex]['rdfa_content'])){
//override value with fix content from mapping table
$iso19139Hash[$isoHashIndex]['value'] = $iso19139Hash[$isoHashIndex]['rdfa_content'];
}
//$stringToReturn .= ' vocab="http://schema.org/"';
if (isset($iso19139Hash[$isoHashIndex]['schemaorg_property'])) {
$stringToReturn .= ' property="'.$iso19139Hash[$isoHashIndex]['schemaorg_property'].'"';
}
if (isset($iso19139Hash[$isoHashIndex]['schemaorg_typeof'])) {
$stringToReturn .= ' typeof="'.$iso19139Hash[$isoHashIndex]['schemaorg_typeof'].'"';
if ($iso19139Hash[$isoHashIndex]['schemaorg_typeof'] == "URL") {
$stringToReturn .= ' href="'.$iso19139Hash[$isoHashIndex]['value'].'"';
}
}
/*if (isset($iso19139Hash[$isoHashIndex]['content'])) {
$stringToReturn .= ' content="'.$iso19139Hash[$isoHashIndex]['content'].'"';
}
if (isset($iso19139Hash[$isoHashIndex]['datatype'])) {
$stringToReturn .= ' datatype="'.$iso19139Hash[$isoHashIndex]['datatype'].'"';
}*/
$stringToReturn .= ">".$iso19139Hash[$isoHashIndex]['value']."".$t_c;
return $stringToReturn;
}
private function parseExteriorPolygon($gml3Polygon) {
//cause postgis gives back polygons without namspace, we have to add a namespace before parsing the xml again :-(
$gml3Polygon = "".$gml3Polygon;
libxml_use_internal_errors(true);
try {
$iso19139Xml = simplexml_load_string($gml3Polygon);
if ($iso19139Xml === false) {
foreach(libxml_get_errors() as $error) {
$err = new mb_exception("class_Iso19139:".$error->message);
}
throw new Exception("class_Iso19139:".'Cannot parse Metadata XML!');
return false;
}
}
catch (Exception $e) {
$err = new mb_exception("class_Iso19139:".$e->getMessage());
return false;
}
//if parsing was successful
if ($iso19139Xml !== false) {
//add ns as attribute
$iso19139Xml->addAttribute('xmlns:xmlns:gml', 'http://www.opengis.net/gml');
//reloadXML
$iso19139Xml = simplexml_load_string($iso19139Xml->asXML());
$iso19139Xml->registerXPathNamespace("gml", "http://www.opengis.net/gml");
$e = new mb_notice("class_iso19139.php: parsing successfull!");
//
$posListArray = $iso19139Xml->xpath('/gml:Polygon/gml:exterior/gml:LinearRing/gml:posList');
if (count($posListArray) > 0) {
$e = new mb_notice("class_iso19139.php: found pos list!");
//read posList
$exteriorRingPoints = $posListArray;
if (count($exteriorRingPoints) > 0) {
//poslist is only space separated
$exteriorRingPointsArray = explode(' ',$exteriorRingPoints[0]);
for ($i = 0; $i <= count($exteriorRingPointsArray)/2-1; $i++) {
$polygonalExtentExterior[$i]['x'] = $exteriorRingPointsArray[2*$i];
$polygonalExtentExterior[$i]['y'] = $exteriorRingPointsArray[(2*$i)+1];
}
$e = new mb_notice("class_iso19139.php: ". count($polygonalExtentExterior) . " point objects!");
return $polygonalExtentExterior;
}
}
}
return array();
}
public function createFromDBInternalId($metadataId){
$sql = "SELECT * , st_xmin(the_geom) || ',' || st_ymin(the_geom) || ',' || st_xmax(the_geom) || ',' || st_ymax(the_geom) as bbox2d, st_asgml(3,bounding_geom) as bounding_polygon from mb_metadata WHERE metadata_id = $1";
$v = array($metadataId);
$t = array('i');
$res = db_prep_query($sql,$v,$t);
if ($res) {
$row = db_fetch_assoc($res);
//fill object with information from mb_metadata table
//initialize empty iso19139 object
$this->fileIdentifier = $row['uuid'];
$this->title = $row['title'];
$this->abstract = $row['abstract'];
$this->createDate = $row['createdate'];//"1900-01-01";
$this->changeDate = $row['changedate'];//"1900-01-01";
$this->metadata = $row['data'];
//some possibilities:
$this->datasetId = $row['datasetid'];
$this->datasetIdCodeSpace = $row['datasetid_codespace'];
if (isset($row['bbox2d']) && $row['bbox2d'] != '') {
$bbox = $row['bbox2d'];
//$e = new mb_exception("class_iso19139.php: got bbox for metadata: ".$bbox);
$this->wgs84Bbox = explode(',',$bbox);
}
if (isset($row['bounding_polygon']) && $row['bounding_polygon'] != '') {
//extract coordinates from gml
//push them into array
//store them in object
//$bbox = str_replace(' ',',',str_replace(')','',str_replace('BOX(','',$row['bbox2d'])));
//$e = new mb_exception("class_iso19139.php: got bbox for metadata: ".$bbox);
$gml3FromPostgis = "".$row['bounding_polygon'];
libxml_use_internal_errors(true);
try {
$gml3 = simplexml_load_string($gml3FromPostgis);
if ($gml3 === false) {
foreach(libxml_get_errors() as $error) {
$err = new mb_exception("class_Iso19139:".$error->message);
}
throw new Exception("class_Iso19139:".'Cannot parse Metadata XML!');
return false;
}
}
catch (Exception $e) {
$err = new mb_exception("class_Iso19139:".$e->getMessage());
return false;
}
//if parsing was successful
if ($gml3 !== false) {
$gml3->addAttribute('xmlns:xmlns:gml', 'http://www.opengis.net/gml');
$gml3->registerXPathNamespace("gml", "http://www.opengis.net/gml");
$gml3 = simplexml_load_string($gml3->asXML());
if ($gml3->xpath('//gml:MultiSurface')) {
$e = new mb_notice("class_Iso19139: MultiSurface found!");
$this->polygonalExtentExterior = array();
//count surfaceMembers
$numberOfSurfaces = count($gml3->xpath('/gml:MultiSurface/gml:surfaceMember'));
$e = new mb_notice("class_Iso19139: number of polygons: ".$numberOfSurfaces);
for ($k = 0; $k < $numberOfSurfaces; $k++) {
$this->polygonalExtentExterior[] = $this->parsePolygon($gml3, '//gml:MultiSurface/gml:surfaceMember/');
}
} else {
$e = new mb_notice("class_Iso19139: no MultiSurface found!");
if($gml3->xpath('//gml:Polygon')) {
$e = new mb_notice("class_Iso19139: number of polygons: 1");
$this->polygonalExtentExterior = array();
$this->polygonalExtentExterior[0] = $this->parsePolygon($gml3, '/');
}
}
}
}
//fill keywords and categories later cause they are stored in relations!
/*$this->keywords = array();
$this->keywordsThesaurusName = array();
$this->isoCategoryKeys = array();
//following information is specific to mapbender information model - they are identified by id!
$this->isoCategories = array();
$this->inspireCategories = array();
$this->customCategories = array();
//*/
$this->hierarchyLevel = $row['type'];
$this->tmpExtentBegin = $row['tmp_reference_1'];//"1900-01-01";
$this->tmpExtentEnd = $row['tmp_reference_2'];//"1900-01-01";
$this->randomId = $row['randomid'];
$this->owner = $row['fkey_mb_user_id']; //dummy entry for metadata owner - in case of metadataURL entries the owner of the corresponding service
$this->fkey_mb_group_id = $row['fkey_mb_group_id']; //entry for organization for which this metadata should be published - overwrites metadata point of contact - in case of inheritance by metadata proxy!
$this->href = $row['link'];// "";
$this->format = $row['md_format'];//"";
$this->type = $row['linktype'];//"";
$this->origin = $row['origin'];//"";
$this->refSystem = $row['ref_system'];//"";
$this->harvestResult = $row['harvestresult'];//;0;
$this->harvestException = $row['harvestexception'];//"";
$this->lineage = $row['lineage'];//"";
$this->inspireTopConsistence = $row['inspire_top_consistence'];//"f";
$this->inspireInteroperability = $row['inspire_interoperability'];//"f";
$this->searchable = $row['searchable'];//"t";
$this->spatialResType = $row['spatial_res_type'];//"";
$this->spatialResValue = $row['spatial_res_value'];//"";
$this->export2Csw = $row['export2csw'];//"t";
$this->updateFrequency = $row['update_frequency'];//"";
$this->dataFormat = $row['format'];//"";
$this->inspireCharset = $row['inspire_charset'];//"";
$this->inspireWholeArea = $row['inspire_whole_area'];//"";
$this->inspireActualCoverage = $row['inspire_actual_coverage'];//"";
$this->inspireDownload = $row['inspire_download'];//"";
$this->fees = $row['fees'];
$this->accessConstraints = $row['constraints'];
//$test = print_r($row['datalinks'],true);
//$e = new mb_exception((string)$test);
$this->downloadLinks = $this->jsonDecodeDownloadLinks($row['datalinks']);
//$e = new mb_exception($this->downloadLinks[0]);
//$this->linkAlreadyInDB = false;
//$this->fileIdentifierAlreadyInDB = false;
$this->licenseSourceNote = $row['md_license_source_note'];
$this->resourceResponsibleParty = $row['responsible_party_name'];
$this->resourceContactEmail = $row['responsible_party_email'];
$this->previewImage = $row['preview_image'];
$this->fkeyGuiId = $row['fkey_gui_id'];
$this->fkeyWmcSerialId = $row['fkey_wmc_serial_id'];
$this->fkeyMapviewerId = $row['fkey_mapviewer_id'];
//get relations from other tables:
//get categories and keywords
//get isoCategories
$sql = <<isoCategories[] = (string)$row['md_topic_category_id'];
$this->isoCategoriesKeys[] = $row['md_topic_category_code_en'];
}
//get custom categories
$sql = <<customCategories[] = (string)$row['custom_category_id'];
//generate one keyword entry with special thesaurus
$this->keywords[] = $row['fkey_custom_category_id'];
if (defined("METADATA_DEFAULT_CODESPACE") && METADATA_DEFAULT_CODESPACE != '') {
$this->keywordsThesaurusName[] = METADATA_DEFAULT_CODESPACE;
} else {
$this->keywordsThesaurusName[] = "http://www.mapbender.org";
}
}
//get inspire categories
$sql = <<inspireCategories[] = (string)$row['inspire_category_id'];
$this->keywords[] = $row['inspire_category_key'];
$this->keywordsThesaurusName[] = "GEMET - INSPIRE themes, version 1.0";
}
//get other keywords from keyword table - only fill in, if not already in keywords!
$sql = <<keywords)) {
$this->keywords[] = $row['keyword'];
$this->keywordsThesaurusName[] = "";
}
}
//get predefined license if given in mapbender database
$sql = <<termsOfUseRef = $row['fkey_termsofuse_id'];
if ($this->termsOfUseRef == null) {
$e = new mb_notice("no predefined license set");
} else {
$e = new mb_notice("Predefined license found: ".$this->termsOfUseRef);
}
} else {
$e = new mb_exception("Could not get metadata with id ".$metadataId." from DB!");
return false;
}
//$this->qualifyMetadata();
return true;
}
//the following functions are only for the simple metadata editor
public function createMetadataAddonFromDB() {
}
public function updateMetadataAddonInDB() {
}
//following function is e.g. used by class_wms.php and class_wfsToDb.php
public function deleteMetadataRelation($resourceType, $resourceId, $relationType){
//delete all relations which are defined from capabilities - this don't delete the metadata entries themself!
//all other relations stay alive
$sql = "DELETE FROM ows_relation_metadata WHERE fkey_".$resourceType."_id = $1 AND relation_type = '".$relationType."'";
$v = array($resourceId);
$t = array('i');
$res = db_prep_query($sql,$v,$t);
if(!$res){
$e = new mb_exception("class_Iso19139:"."Cannot delete metadata relations for resource ".$resourceType." with id: ".$resourceId);
return false;
} else {
return true;
}
}
/*public function insertMetadataRelation($resourceType, $resourceId, $relationType){
//delete all relations which are defined from capabilities - this don't delete the metadata entries themself!
//all other relations stay alive
$sql = "DELETE FROM ows_relation_metadata WHERE fkey_".$resourceType."_id = $1 AND relation_type = '".$relationType."'";
$v = array($resourceId);
$t = array('i');
$res = db_prep_query($sql,$v,$t);
if(!$res){
$e = new mb_exception("class_Iso19139:"."Cannot delete metadata relations for resource ".$resourceType." with id: ".$resourceId);
return false;
} else {
return true;
}
}*/
public function checkMetadataRelation($resourceType, $resourceId, $metadataId, $origin){
if ($resourceType !== 'metadata') {
//check if one relation already exists - if so no new one should be generated!!!
$sql = "SELECT count(fkey_metadata_id) FROM ows_relation_metadata WHERE fkey_".$resourceType."_id = $1 AND fkey_metadata_id = $2 AND relation_type = $3";
$v = array($resourceId, $metadataId, $origin);
$t = array('i','i','s');
$res = db_prep_query($sql,$v,$t);
while ($row = db_fetch_array($res)){
$numberOfRelations = $row['count'];
}
if ($numberOfRelations > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public function setInternalMetadataLinkage($metadataId,$resourceType, $resourceId){
//check if internal linkage already exists
if ($this->checkMetadataRelation($resourceType, $resourceId, $metadataId, 'internal')) {
$returnObject['success'] = false;
$returnObject['message'] = _mb("Internal link already exists in database - it will not be created twice!");
return $returnObject;
} else {
//if not create it
$returnObject = array();
$sql = "INSERT INTO ows_relation_metadata (fkey_metadata_id, fkey_".$resourceType."_id, internal, relation_type) VALUES ( $1, $2, $3, 'internal')";
$v = array($metadataId,$resourceId, 1);
$t = array('i','i','i');
try {
$res = db_prep_query($sql,$v,$t);
}
catch (Exception $E){
$returnObject['success'] = false;
$returnObject['message'] = _mb("Could not insert internal metadatalink into database!");
return $returnObject;
}
//bequeath categories from coupled resources
$this->bequeathCategoriesToCoupledResource($metadataId,$resourceType,$resourceId);
//$e = new mb_exception("iso19139.php: bequeathed categories to ".$resourceType." : ".$resourceId);
$returnObject['success'] = true;
$returnObject['message'] = _mb("Internal metadata linkage inserted!");
return $returnObject;
}
}
public function deleteInternalMetadataLinkage($resourceType, $resourceId, $metadataId){
$returnObject = array();
$sql = "DELETE FROM ows_relation_metadata WHERE fkey_metadata_id = $1 and fkey_".$resourceType."_id = $2 and relation_type = 'internal'";
$v = array($metadataId, $resourceId);
$t = array('i','i');
try {
$res = db_prep_query($sql,$v,$t);
}
catch (Exception $E){
$returnObject['success'] = false;
$returnObject['message'] = _mb("Could not delete internal metadata linkage from database!");
return $returnObject;
}
//delete categories from coupled resources
$this->deleteCategoriesFromCoupledResource($metadataId,$resourceType,$resourceId);
$returnObject['success'] = true;
$returnObject['message'] = _mb("Internal metadata linkage deleted!");
return $returnObject;
}
public function deleteMetadataAddon($resourceType, $resourceId, $metadataId){
$returnObject = array();
$sql = "SELECT count(*) as count FROM ows_relation_metadata WHERE fkey_metadata_id = $1";
$v = array($metadataId);
$t = array('i');
try {
$res = db_prep_query($sql,$v,$t);
}
catch (Exception $e){
$returnObject['success'] = false;
$returnObject['message'] = _mb("Could not determine a count of metadata relations!");
return $returnObject;
}
while ($row = db_fetch_assoc($res)) {
$countMetadataLinkage = (integer)$row['count']; //integer
}
if ($countMetadataLinkage == 1) {
$e = new mb_exception("Metadata has only one reference and will be deleted from database if it was created by upload or link!");
//delete the metadata itself cause it has no other reference - really - not right for metador files !!!
$sql = <<deleteCategoriesFromCoupledResource($metadataId,$resourceType,$resourceId);
$returnObject['success'] = true;
$returnObject['message'] = _mb("Metadata and/or linkage was deleted from database!");
return $returnObject;
} else {
//delete only linkage
$sql = "DELETE FROM ows_relation_metadata WHERE fkey_metadata_id = $1 and fkey_".$resourceType."_id = $2";
$v = array($metadataId, $resourceId);
$t = array('i','i');
try {
$res = db_prep_query($sql,$v,$t);
}
catch (Exception $E){
$returnObject['success'] = false;
$returnObject['message'] = _mb("Could not delete metadata linkage from database!");
return $returnObject;
}
//delete categories from coupled resources
$this->deleteCategoriesFromCoupledResource($metadataId,$resourceType,$resourceId);
$returnObject['success'] = true;
$returnObject['message'] = _mb("Metadata linkage deleted!");
return $returnObject;
}
}
public function isLinkAlreadyInDB(){
$sql = << '' AND link IS NOT NULL ORDER BY lastchanged DESC
SQL;
$v = array(
$this->href
);
$t = array('s');
$res = db_prep_query($sql,$v,$t);
while ($row = db_fetch_array($res)){
$metadataId[] = $row['metadata_id'];
}
if (count($metadataId) > 0 && count($metadataId) < 2) {
return $metadataId[0];
} else {
return false;
}
}
public function isFileIdentifierAlreadyInDB(){
$sql = << '' AND uuid IS NOT NULL ORDER BY lastchanged DESC LIMIT 1
SQL;
if (!isset($this->fileIdentifier) || $this->fileIdentifier == '') {
$e = new mb_exception("class_Iso19139:"."Empty or no fileIdentifier found in the metadata! No metadataset will be updated");
return false;
}
$v = array(
$this->fileIdentifier
);
$t = array('s');
$res = db_prep_query($sql,$v,$t);
$metadataId = array();
while ($row = db_fetch_array($res)){
$metadataId[] = $row['metadata_id'];
}
if (count($metadataId) > 0 && count($metadataId) < 2) {
return $metadataId[0];
} else {
return false;
}
}
private function createWktBboxFromArray($bboxArray) { //-180 -90 180 90
$postGisBbox = "";
if (count($bboxArray) != 4 || $bboxArray[0] == '') {
//create dummy bbox
$bboxArray = array(-180,-90,180,90);
}
//"SRID=4326;POLYGON((-140 -80,-140 80,170 80,170 -80,-140 -80))"
$postGisBbox = "SRID=4326;POLYGON((".$bboxArray[0]." ".$bboxArray[1].",".$bboxArray[0]." ".$bboxArray[3].",".$bboxArray[2]." ".$bboxArray[3].",".$bboxArray[2]." ".$bboxArray[1].",".$bboxArray[0]." ".$bboxArray[1]."))";
return $postGisBbox;
}
public function createWktPolygonFromPointArray($pointArray) { //-180 -90 180 90
if ($pointArray == null) {
return null;
}
$postGisPolygon = "";
if (count($pointArray) > 1) { //multisurface
$postGisPolygon = "SRID=4326;MULTIPOLYGON(((";
foreach ($pointArray as $polygon) {
foreach($polygon as $point) {
$postGisPolygon .= trim($point['x'])." ".trim($point['y']).",";
}
$postGisPolygon = rtrim($postGisPolygon,',')."),(";
}
$postGisPolygon = rtrim($postGisPolygon,'(,')."))";
} else {
//"SRID=4326;POLYGON((-140 -80,-140 80,170 80,170 -80,-140 -80))"
$postGisPolygon = "SRID=4326;POLYGON((";
if (count($pointArray) == 0) {
return null;
} else {
//only use first polygon entry
$pointArray = $pointArray[0];
foreach ($pointArray as $point) {
$postGisPolygon .= trim($point['x'])." ".trim($point['y']).",";
}
$postGisPolygon = rtrim($postGisPolygon,',');
$postGisPolygon .= "))";
}
}
//$e = new mb_exception("class_Iso19139.php: wkt: ".$postGisPolygon);
return $postGisPolygon;
}
public function inheritContactAndLicenceInformation($metadataId,$resourceType,$resourceId,$inheritContactInfo,$inheritLicenceInfo){
//$e = new mb_exception("class_iso19139.php: inherit function invoked!");
if ($inheritContactInfo == true || $inheritLicenceInfo == true) {
//get info from coupled service
switch ($resourceType) {
case "layer":
$sql = "SELECT fkey_mb_group_id, wms.wms_id as service_id, fkey_termsofuse_id, wms_license_source_note as service_source_note FROM wms LEFT JOIN wms_termsofuse ON wms.wms_id = wms_termsofuse.fkey_wms_id WHERE wms_id = (SELECT fkey_wms_id FROM layer WHERE layer_id = $resourceId)";
break;
case "featuretype":
$sql = "SELECT fkey_mb_group_id, wfs.wfs_id as service_id, fkey_termsofuse_id, wfs_license_source_note as service_source_note FROM wfs LEFT JOIN wfs_termsofuse ON wfs.wfs_id = wfs_termsofuse.fkey_wfs_id WHERE wfs_id = (SELECT fkey_wfs_id FROM wfs_featuretype WHERE featuretype_id = $resourceId)";
break;
}
$res = db_query($sql);
while ($row = db_fetch_array($res)){
$fkey_mb_group_id = $row['fkey_mb_group_id'];
$service_id = $row['service_id'];
$fkey_termsofuse_id = $row['fkey_termsofuse_id'];
$service_source_note = $row['service_source_note'];
}
//regexpr for strings which represents integer
$regExprInt = "/^[0-9]+$/";
}
if ($inheritContactInfo == true && preg_match($regExprInt,$fkey_mb_group_id)) {
$sqlInheritContact = "UPDATE mb_metadata SET fkey_mb_group_id = $fkey_mb_group_id WHERE metadata_id = $metadataId";
$res = db_query($sqlInheritContact);
//$e = new mb_exception("Metadata with id ".$metadataId." inherits contact information from ".$resourceType." with resourceId ".$resourceId);
}
if ($inheritLicenceInfo == true && preg_match($regExprInt,$fkey_termsofuse_id)) {
try {
//delete own termsofuse if exists
$sqlDeleteTouRelation = "DELETE FROM md_termsofuse WHERE fkey_metadata_id = $metadataId";
$res = db_query($sqlDeleteTouRelation);
//insert service termsofuse relation
$sqlInsertTouRelation = "INSERT INTO md_termsofuse (fkey_termsofuse_id, fkey_metadata_id) VALUES ($fkey_termsofuse_id,$metadataId)";
$res = db_query($sqlInsertTouRelation);
//fill in source note from service
$sqlUpdateSourceNote = "UPDATE mb_metadata SET md_license_source_note = $1 WHERE metadata_id = $2";
$t = array('s', 'i');
$v = array($service_source_note, $metadataId);
$res = db_prep_query($sqlUpdateSourceNote,$v,$t);
//$e = new mb_exception("Metadata with id ".$metadataId." inherits licence information from ".$resourceType." with resourceId ".$resourceId);
}
catch (Exception $e) {
$exception = new mb_exception($e->getMessage());
}
}
}
public function insertKeywordsAndCategoriesIntoDB($metadataId,$resourceType,$resourceId){
//first delete old classifications - after that create new ones
//$e = new mb_exception($metadataId." - ".$resourceType." - ".$resourceId);
$this->deleteKeywordsAndCategoriesFromDB($metadataId,$resourceType,$resourceId);
//insert keywords and categories into tables!
//parse keywords and isoCategories
//INSERT INTO films (code, title, did, date_prod, kind) VALUES
//('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),
//('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy');
//use category name instead of id's!
//map category name to id before insert them into db!
//read maptable from db
//iso - code in xml
if ($resourceType == "wms" || $resourceType == "wfs" || $resourceType == "inspire_dls_atom" || $resourceType == "inspire_dls_atom_dataset") {
return false;
}
switch ($resourceType) {
case "featuretype":
$tablePrefix = 'wfs_featuretype';
break;
case "layer":
$tablePrefix = 'layer';
break;
}
//check if categories are arrays or not, if not parse as integer and safe as array with one element
if (!is_array($this->isoCategories)) {
$intCategory = (integer)$this->isoCategories;
$this->isoCategories = array();
$this->isoCategories[0] = $intCategory;
}
if (!is_array($this->inspireCategories)) {
$intCategory = (integer)$this->inspireCategories;
$this->inspireCategories = array();
$this->inspireCategories[0] = $intCategory;
}
if (!is_array($this->customCategories)) {
$intCategory = (integer)$this->customCategories;
$this->customCategories = array();
$this->customCategories[0] = $intCategory;
}
//map keys into relevant ids
$sqlInsert = "";
if (count($this->isoCategories) > 0 && $this->customCategories[0] !== 0) {
if (count($this->isoCategories) == 1) {
$sqlInsert .= "(".(integer)$metadataId.",".(integer)$this->isoCategories[0].")";
} else {
foreach ($this->isoCategories as $isoCategory) {
$sqlInsert .= "(".(integer)$metadataId.",".(integer)$isoCategory."),";
}
}
$sqlInsert = rtrim($sqlInsert,",");
//$e = new mb_exception("class_Iso19139: insert topic categories: ".$sqlInsert);
$sql = "INSERT INTO mb_metadata_md_topic_category (fkey_metadata_id, fkey_md_topic_category_id) VALUES ".$sqlInsert;
$res = db_query($sql);
if (!$res) {
$e = new mb_exception("class_Iso19139:"._mb("Cannot insert iso categories for this metadata!"));
}
}
$sqlInsert = "";
if (count($this->inspireCategories) > 0 && $this->inspireCategories[0] !== 0) {
if (count($this->inspireCategories) == 1) {
$sqlInsert .= "(".(integer)$metadataId.",".(integer)$this->inspireCategories[0].")";
} else {
foreach ($this->inspireCategories as $inspireCategory) {
$sqlInsert .= "(".(integer)$metadataId.",".(integer)$inspireCategory."),";
}
}
$sqlInsert = rtrim($sqlInsert,",");
$sql = "INSERT INTO mb_metadata_inspire_category (fkey_metadata_id, fkey_inspire_category_id) VALUES ".$sqlInsert;
//$e = new mb_exception("class_Iso19139: insert inspire categories: ".$sqlInsert);
$res = db_query($sql);
if (!$res) {
$e = new mb_exception("class_Iso19139:"._mb("Cannot insert inspire categories for this metadata!"));
}
}
$sqlInsert = "";
if (count($this->customCategories) > 0 && $this->customCategories[0] !== 0) {
if (count($this->customCategories) == 1) {
$sqlInsert .= "(".(integer)$metadataId.",".(integer)$this->customCategories[0].")";
} else {
foreach ($this->customCategories as $customCategory) {
$sqlInsert .= "(".(integer)$metadataId.",".(integer)$customCategory."),";
}
}
$sqlInsert = rtrim($sqlInsert,",");
$sql = "INSERT INTO mb_metadata_custom_category (fkey_metadata_id, fkey_custom_category_id) VALUES ".$sqlInsert;
//$e = new mb_exception("class_Iso19139: insert custom categories: ".$sqlInsert);
$res = db_query($sql);
if (!$res) {
$e = new mb_exception("class_Iso19139:"._mb("Cannot insert custom categories for this metadata!"));
}
}
if ($resourceType == 'layer' || $resourceType == 'featuretype') {
$this->bequeathCategoriesToCoupledResource($metadataId,$resourceType,$resourceId);
}
$sqlInsert = "";
//insert keywords into keyword table
//foreach keyword look for an id or create it newly - made for postgres > 8.2 with returning option for insert statement
$keyword = "'";
$keyword .= implode('\',\'',$this->keywords);
$keyword .= "'";
$existingKeywords = array();
$sql = "SELECT keyword, keyword_id from keyword WHERE keyword in ($keyword);";
$res = db_query($sql);
if (!$res) {
$e = new mb_exception("class_Iso19139.php: cannot get keywords from database!");
} else {
$countExistingKeywords = 0;
while ($row = db_fetch_assoc($res)) {
$existingKeywords[$countExistingKeywords]['keyword'] = $row['keyword'];
//$e = new mb_exception("existing keyword: ".$row['keyword']);
$existingKeywords[$countExistingKeywords]['id'] = $row['keyword_id'];
$existingKeywordsArray[$countExistingKeywords] = $row['keyword'];
$countExistingKeywords++;
}
}
/*foreach ($existingKeywords as $test) {
$e = new mb_exception("exists: ".$test['keyword']);
}*/
//for each existing keyword add a new relation into relation table
if ($countExistingKeywords > 0) {
if ($countexistingKeywords == 1) {
$sqlInsert .= "(".(integer)$metadataId.",".(integer)$existingKeywords[0]['id'].")";
} else {
foreach ($existingKeywords as $existingKeyword) {
$sqlInsert .= "(".(integer)$metadataId.",".(integer)$existingKeyword['id']."),";
}
}
$sqlInsert = rtrim($sqlInsert,",");
$sql = "INSERT INTO mb_metadata_keyword (fkey_metadata_id, fkey_keyword_id) VALUES ".$sqlInsert;
//$e = new mb_exception("class_Iso19139.php: sql for keywords: ".$sql);
$res = db_query($sql);
if (!$res) {
$e = new mb_exception("class_Iso19139:"._mb("Cannot insert keyword relations for this metadata!"));
} else {
$e = new mb_notice("class_Iso19139:"._mb("Inserted keyword relations for existing keywords!"));
}
}
//insert those keywords, that are not already in the keyword table
//$this->keywords
/*$array1 = array("a" => "grün", "rot", "blau", "rot");
$array2 = array("b" => "grün", "gelb", "rot");
$result = array_diff($array1, $array2);
Array
(
[1] => blau
)
*/
$sqlInsert = "";
//test if $existingKeywordsArray is array!
if (is_array($existingKeywordsArray)) {
$otherKeywords = array_values(array_diff($this->keywords,$existingKeywordsArray));
} else {
$otherKeywords = $this->keywords;
}
//debug
/*foreach ($otherKeywords as $test) {
$e = new mb_exception("otherKeywords: ".$test);
}
$e = new mb_exception("otherKeywords: ".$otherKeywords);*/
if (count($otherKeywords) > 0) {
if (count($otherKeywords) == 1) {
//$e = new mb_exception("Only one new keyword found: ".$otherKeywords[0]);
//keyword table
$sqlInsert .= "('".$otherKeywords[0]."')";
} else {
foreach ($otherKeywords as $otherKeyword) {
$sqlInsert .= "('".$otherKeyword."'),";
}
}
$sqlInsert = rtrim($sqlInsert,",");
$sql = "INSERT INTO keyword (keyword) VALUES ".$sqlInsert." RETURNING keyword_id" ;
//$e = new mb_exception("class_Iso19139.php: sql for keywords: ".$sql);
$res = db_query($sql);
if (!$res) {
$e = new mb_notice("class_Iso19139:"._mb("Cannot insert new keywords into keyword table - will insert only new relations!"));
} else {
//insert relations for keywords
$sqlInsert = "";
$insertedKeywords = array();
while ($row = db_fetch_assoc($res)) {
$insertedKeywordIds[] = $row['keyword_id'];
}
if (count($insertedKeywordIds) == 1) {
//keyword relation table
$sqlInsert .= "(".(integer)$metadataId.",".$insertedKeywordIds[0].")";
} else {
foreach ($insertedKeywordIds as $insertedKeywordId) {
$sqlInsert .= "(".(integer)$metadataId.",".(integer)$insertedKeywordId."),";
}
}
$sqlInsert = rtrim($sqlInsert,",");
$sql = "INSERT INTO mb_metadata_keyword (fkey_metadata_id, fkey_keyword_id) VALUES ".$sqlInsert;
//$e = new mb_notice("class_Iso19139.php: sql for keyword relation: ".$sql);
$res = db_query($sql);
if (!$res) {
$e = new mb_exception("class_Iso19139:"._mb("Cannot insert metadata keyword relations into db!"));
}
}
}
//handle licenses as relations
//delete relations from database
//recreate relation to license
//relation is set via json id
//check for defined license id via editor or s.th. else
$licenseId = $this->termsOfUseRef;
if ($this->termsOfUseRef == null) {
//search for given json license
if ($this->licenseJson != null) {
$licenseName = json_decode($this->licenseJson)->id;
if ($licenseName != null) {
//search for same license name in database - if not given create it!
$sql = <<licenseJson);
//check if all fields are there
if ($licenseJson->id != null && $licenseJson->id != "" && $licenseJson->name != null && $licenseJson->name != "" && $licenseJson->url != null && $licenseJson->url != "") {
//insert entry into db - license should be open
$sql = <<id,$licenseJson->name,$licenseJson->url);
$t = array('s','s','s');
$res = db_prep_query($sql,$v,$t);
if (!$res) {
$e = new mb_exception("classes/class_iso19139.php: Cannot create termsofuse entry from given json license in metadata!");
$licenseId = null;
} else {
$sql = <<id);
$t = array('s');
$res = db_prep_query($sql,$v,$t);
$row = db_fetch_assoc($res);
$licenseId = $row['termsofuse_id'];
}
}
}
}
}
}
//$e = new mb_exception("iso19139 license id: ".$this->termsOfUseRef);
if ($licenseId !== null) {
$sql = <<{$objectPrefix."Categories"}) > 0 && $this->{$objectPrefix."Categories"}[0] !== 0) {
if (count($this->{$objectPrefix."Categories"}) == 1) {
$sqlInsertCoupledResource .= "(".(integer)$metadataId.",".(integer)$this->{$objectPrefix."Categories"}[0].",".$resourceId.")";
} else {
foreach ($this->{$objectPrefix."Categories"} as ${$objectPrefix."Category"}) {
$sqlInsertCoupledResource .= "(".(integer)$metadataId.",".(integer)${$objectPrefix."Category"}.",".$resourceId."),";
}
}
$sqlInsertCoupledResource = rtrim($sqlInsertCoupledResource,",");
$sqlCoupledResource = "INSERT INTO ".$tablePrefix."_{$cat}_category (fkey_metadata_id, fkey_{$cat}_category_id, fkey_".$resourceType."_id) VALUES ".$sqlInsertCoupledResource;
//$e = new mb_exception("class_Iso19139: sql to bequeath categories: ".$sqlCoupledResource);
$res = db_query($sqlCoupledResource);
if (!$res) {
$e = new mb_exception("class_Iso19139:"._mb("Cannot insert $cat categories to coupled resource!"));
}
}
}
}
public function deleteKeywordsAndCategoriesFromDB($metadataId,$resourceType,$resourceId) {
$sql = "DELETE FROM mb_metadata_md_topic_category where fkey_metadata_id = $1 ";
$v = array($metadataId);
$t = array('i');
$res = db_prep_query($sql,$v,$t);
if (!$res) {
$e = new mb_exception("class_Iso19139:"._mb("Cannot delete topic category relations for metadata with id ".$metadataId));
} else {
$e = new mb_notice("class_Iso19139: topic category relations deleted from database!");
}
$sql = "DELETE FROM mb_metadata_inspire_category where fkey_metadata_id = $1 ";
$v = array($metadataId);
$t = array('i');
$res = db_prep_query($sql,$v,$t);
if (!$res) {
$e = new mb_exception("class_Iso19139:"._mb("Cannot delete inspire category relations for metadata with id ".$metadataId));
} else {
$e = new mb_notice("class_Iso19139: inspire category relations deleted from database!");
}
$sql = "DELETE FROM mb_metadata_custom_category where fkey_metadata_id = $1 ";
$v = array($metadataId);
$t = array('i');
$res = db_prep_query($sql,$v,$t);
if (!$res) {
$e = new mb_exception("class_Iso19139:"._mb("Cannot delete custom category relations for metadata with id ".$metadataId));
} else {
$e = new mb_notice("class_Iso19139: custom category relations deleted from database!");
}
//delete keyword relations - problem, that keywords are referenced from more than one table. We can only delete the relations but there may be orphaned keywords, which have to be deleted by cronjob - maybe - TODO
$sql = "DELETE FROM mb_metadata_keyword where fkey_metadata_id = $1 ";
$v = array($metadataId);
$t = array('i');
$res = db_prep_query($sql,$v,$t);
if (!$res) {
$e = new mb_exception("class_Iso19139:"._mb("Cannot delete keyword relations for metadata with id ".$metadataId));
} else {
$e = new mb_notice("class_Iso19139: keyword relations deleted from database!");
}
//delete license relation from db
$sql = "DELETE FROM md_termsofuse where fkey_metadata_id = $1 ";
$v = array($metadataId);
$t = array('i');
$res = db_prep_query($sql,$v,$t);
if (!$res) {
$e = new mb_exception("class_Iso19139.php:"._mb("Cannot delete termsofuse relation for metadata with id ".$metadataId));
} else {
$e = new mb_notice("class_Iso19139.php: termsofuse relation deleted from database!");
}
$this->deleteCategoriesFromCoupledResource($metadataId,$resourceType,$resourceId);
}
public function insertMetadataIntoDB() {
//insert an instance for iso19139 into mapbenders database
$e = new mb_notice("class_iso19139.php: insert metadata with title: ".$this->title);
$sql = <<href,
$this->format,
$this->metadata,
$this->type,
$this->fileIdentifier,
$this->title,
$this->createDate,
$this->changeDate,
$this->abstract,
$this->keywords[0],
$this->hierarchyLevel,
$this->tmpExtentBegin,
$this->tmpExtentEnd,
$this->export2Csw,
$this->datasetId,
$this->datasetIdCodeSpace,
$this->randomId,
$this->origin,
$this->owner,
$this->harvestResult,
$this->harvestException,
$this->lineage,
$this->inspireTopConsistence,
$this->spatialResType,
$this->spatialResValue,
$this->updateFrequency,
$this->dataFormat,
$this->inspireCharset,
$this->refSystem,
$this->createWktBboxFromArray($this->wgs84Bbox),
$this->jsonEncodeDownloadLinks($this->downloadLinks),
$this->inspireWholeArea,
$this->inspireActualCoverage,
$this->inspireDownload,
$this->createWktPolygonFromPointArray($this->polygonalExtentExterior),
$this->transferSize,
$this->fees,
$this->licenseSourceNote,
$this->accessConstraints,
$this->resourceResponsibleParty,
$this->resourceContactEmail,
$this->previewImage,
$this->fkey_mb_group_id,
$this->mdProxy,
$this->inspireInteroperability,
$this->searchable,
$this->fkeyGuiId,
$this->fkeyWmcSerialId,
$this->fkeyMapviewerId
);
//$e = new mb_exception($this->tmpExtentBegin);
//$e = new mb_exception($this->tmpExtentEnd);
//$e = new mb_exception($this->createDate);
//$e = new mb_exception($this->changeDate);
$t = array('s','s','s','s','s','s','s','s','s','s','s','s','s','b','s','s','s','s','i','i','s','s','b','s','s','s','s','s','s','POLYGON','s','s','s','i','POLYGON','d','s','s','s','s','s','s','i','b','b','b','i','i','i');
$res = db_prep_query($sql,$v,$t);
return $res;
}
private function jsonEncodeDownloadLinks($dlArray) {
$dummy = new stdClass();
//TODO - check array conversion of json_encode under php > 5.4
//$dummy->downloadLinks = (string)$dlArray[0];
//return json_encode($dummy, JSON_FORCE_OBJECT);
$json = '{"downloadLinks":[';
$i = 0;
//TODO solve the php 5.4 json_encode array problem
foreach ($dlArray as $link) {
$json .= '{"'.$i.'":'.json_encode((string)$link).'},';
$i++;
}
$json = rtrim($json,",");
$json .= "]}";
return $json;
}
private function getWfsVersionForFeaturetype($featuretypeId) {
$sql = "SELECT wfs.wfs_version FROM wfs_featuretype INNER JOIN wfs ON wfs_featuretype.fkey_wfs_id = wfs.wfs_id WHERE wfs_featuretype.featuretype_id = $1";
$v = array($featuretypeId);
$t = array('i');
$res = db_prep_query($sql,$v,$t);
if (!$res) {
$e = new mb_exception("No wfs version found for featuretype with id:".$featuretypeId);
return false;
} else {
$row = db_fetch_assoc($res);
return $row['wfs_version'];
}
}
//TODO - maybe we will encode more things than only the url ;-)
private function jsonDecodeDownloadLinks($dlJson) {
$downloadLinks = json_decode($dlJson);
//new for php 5.4
$dlArray = array();
$i = 0;
foreach ($downloadLinks->downloadLinks as $downloadLink) {
$dlArray[] = urldecode($downloadLink->{$i});
$i++;
}
//$dummy = $dummy->downloadLinks;
return $dlArray;
}
public function updateMetadataById($metadataId, $changeAuthorization=true) {
$e = new mb_notice("update metadata id: ".$metadataId);
//check if the timestamp of an existing metadata element is not newer than the timestamp of the current metadata object!!!!
//TODO
//problem: <<jsonEncodeDownloadLinks($this->downloadLinks));
//$e= new mb_exception("class_iso19139.php: downloadLinks[0]".$this->downloadLinks[0]);
$v = array(
$this->href,
$this->format,
$this->metadata,
$this->type,
$this->fileIdentifier, // is the old one! or not?
$this->title,
$this->createDate,
$this->changeDate,
$this->abstract,
$this->keywords[0],
$this->hierarchyLevel,
$this->tmpExtentBegin,
$this->tmpExtentEnd,
$this->export2Csw,
$this->datasetId,
$this->datasetIdCodeSpace,
$this->randomId,
$this->origin,
//$this->owner, //owner is the old one - maybe here we have something TODO!
$metadataId, //The first metadataId which was found will be selected!
$this->harvestResult,
$this->harvestException,
$this->lineage,
$this->inspireTopConsistence,
$this->spatialResType,
$this->spatialResValue,
$this->updateFrequency,
$this->dataFormat,
$this->inspireCharset,
$this->refSystem,
$this->createWktBboxFromArray($this->wgs84Bbox),
$this->jsonEncodeDownloadLinks($this->downloadLinks),
$this->inspireWholeArea,
$this->inspireActualCoverage,
$this->inspireDownload,
$this->createWktPolygonFromPointArray($this->polygonalExtentExterior),
$this->transferSize,
$this->fees,
$this->licenseSourceNote,
$this->accessConstraints,
$this->resourceResponsibleParty,
$this->resourceContactEmail,
$this->previewImage,
$this->fkey_mb_group_id,
$this->mdProxy,
$this->inspireInteroperability,
$this->searchable,
$this->fkeyGuiId,
$this->fkeyWmcSerialId,
$this->fkeyMapviewerId
);
//$e = new mb_exception("class_iso19139: ".$this->createWktBboxFromArray($this->wgs84Bbox));
$t = array('s','s','s','s','s','s','s','s','s','s','s','s','s','b','s','s','s','s','i','i','s','s','b','s','s','s','s','s','s','POLYGON','s','s','s','i','POLYGON','d','s','s','s','s','s','s','i','b','b','b','i','i','i');
$res = db_prep_query($sql,$v,$t);
} else { //do the update without changing owner and fkey_mb_group_id!
$sql = "UPDATE mb_metadata SET link = $1, origin = $18, md_format = $2, data = $3, ";
$sql .= "linktype = $4, uuid = $5, title = $6, createdate = $7, changedate = $8, lastchanged = now(), ";
$sql .= "abstract = $9, searchtext = $10, type = $11, tmp_reference_1 = $12, tmp_reference_2 = $13, export2csw = $14, datasetid = $15, ";
$sql .= "datasetid_codespace = $16, randomid = $17, harvestresult = $20, harvestexception = $21, lineage = $22, inspire_top_consistence = $23, ";
$sql .= "spatial_res_type = $24, spatial_res_value = $25, update_frequency = $26, format = $27, inspire_charset = $28, ref_system = $29, the_geom = $30, datalinks = $31, inspire_whole_area = $32, inspire_actual_coverage = $33, inspire_download = $34, bounding_geom = $35, transfer_size = $36, fees = $37, md_license_source_note = $38, constraints = $39, responsible_party_name = $40, responsible_party_email = $41, preview_image = $42, md_proxy = $43, inspire_interoperability = $44, searchable = $45, fkey_gui_id = $46, fkey_wmc_serial_id = $47, fkey_mapviewer_id = $48 WHERE metadata_id = $19";
//$e= new mb_exception("class_iso19139.php: downloadLinks json".$this->jsonEncodeDownloadLinks($this->downloadLinks));
//$e= new mb_exception("class_iso19139.php: downloadLinks[0]".$this->downloadLinks[0]);
$v = array(
$this->href,
$this->format,
$this->metadata,
$this->type,
$this->fileIdentifier, // is the old one! or not?
$this->title,
$this->createDate,
$this->changeDate,
$this->abstract,
$this->keywords[0],
$this->hierarchyLevel,
$this->tmpExtentBegin,
$this->tmpExtentEnd,
$this->export2Csw,
$this->datasetId,
$this->datasetIdCodeSpace,
$this->randomId,
$this->origin,
//$this->owner, //owner is the old one - maybe here we have something TODO!
$metadataId, //The first metadataId which was found will be selected!
$this->harvestResult,
$this->harvestException,
$this->lineage,
$this->inspireTopConsistence,
$this->spatialResType,
$this->spatialResValue,
$this->updateFrequency,
$this->dataFormat,
$this->inspireCharset,
$this->refSystem,
$this->createWktBboxFromArray($this->wgs84Bbox),
$this->jsonEncodeDownloadLinks($this->downloadLinks),
$this->inspireWholeArea,
$this->inspireActualCoverage,
$this->inspireDownload,
$this->createWktPolygonFromPointArray($this->polygonalExtentExterior),
$this->transferSize,
$this->fees,
$this->licenseSourceNote,
$this->accessConstraints,
$this->resourceResponsibleParty,
$this->resourceContactEmail,
$this->previewImage,
$this->mdProxy,
$this->inspireInteroperability,
$this->searchable,
$this->fkeyGuiId,
$this->fkeyWmcSerialId,
$this->fkeyMapviewerId
);
//$e = new mb_exception("class_iso19139: ".$this->createWktBboxFromArray($this->wgs84Bbox));
$t = array('s','s','s','s','s','s','s','s','s','s','s','s','s','b','s','s','s','s','i','i','s','s','b','s','s','s','s','s','s','POLYGON','s','s','s','i','POLYGON','d','s','s','s','s','s','s','b','b','b','i','i','i');
$res = db_prep_query($sql,$v,$t);
}
return $res;
}
public function insertMetadataUrlToDB($resourceType, $resourceId){
//check if metadata record already exists, if not create a new one else insert relation only!
$metadataId = $this->isLinkAlreadyInDB();
if ($metadataId != false) {
//update the metadataURL entry
$e = new mb_notice("class_Iso19139:"."existing metadata link(s) found: ".$metadataId." - update will be performed");
$sql = "UPDATE mb_metadata SET link = $1, origin = $2, md_format = $3, linktype = $4, changedate = now(), export2csw = $5, randomid = $6, harvestresult = $8, harvestexception = $9 WHERE metadata_id = $7";
$v = array(
$this->href,
$this->origin,
$this->format,
$this->type,
'f',
$this->randomId,
$metadataId,
$this->harvestResult,
$this->harvestException
);
$t = array('s','s','s','s','b','s','i','i','s');
} else {
$sql = "INSERT INTO mb_metadata (link, origin, md_format, linktype, createdate, changedate, export2csw, randomid, fkey_mb_user_id, harvestresult, harvestexception) ";
$sql .= "VALUES($1, $2, $3, $4, now(), now(), $5, $6, $7, $8, $9)";
$v = array(
$this->href,
$this->origin,
$this->format,
$this->type,
'f',
$this->randomId,
$this->owner,
$this->harvestResult,
$this->harvestException
);
$t = array('s','s','s','s','b','s','i','i','s');
}
$res = db_prep_query($sql,$v,$t);
if(!$res){
db_rollback();
$e = new mb_exception("class_Iso19139:"._mb("Cannot insert or update metadataUrl in database!"));
return false;
} else {
//insert relation into db
//get inserted metadata_id
$sql = <<randomId);
$t = array('s');
try {
$res = db_prep_query($sql,$v,$t);
}
catch (Exception $e){
$e = new mb_exception("class_Iso19139:"._mb("Cannot get metadata record with following random id from database: ".$this->randomId));
}
if (!$res) {
//do nothing
$e = new mb_exception("class_Iso19139:"._mb("Cannot find inserted metadata entry to store relation."));
return false;
} else {
//insert relation
$row = db_fetch_assoc($res);
$metadataId = $row['metadata_id'];
if ($resourceType !== 'metadata') {
//insert relation to layer/featuretype
$sql = "INSERT INTO ows_relation_metadata (fkey_".$resourceType."_id, fkey_metadata_id, relation_type) values ($1, $2, $3);";
$v = array($resourceId, $metadataId, $this->origin);
$t = array('i','i', 's');
$res = db_prep_query($sql,$v,$t);
} else {
$res = false;
}
if(!$res){
db_rollback();
$e = new mb_exception("class_Iso19139:"._mb("Cannot insert metadata relation!"));
return false;
} else {
$sql = "UPDATE mb_metadata SET harvestresult = 0, harvestexception = 'Linked metadata could not be interpreted, only linkage is stored to mb_metadata!' where metadata_id = $1";
$v = array($metadataId);
$t = array('i');
$res = db_prep_query($sql,$v,$t);
if(!$res){
db_rollback();
$e = new mb_exception("class_Iso19139:"._mb("Cannot update mb_metadata table!"));
return false;
}
}
}
return true;
}
return true;
}
public function getExtentGraphic($layer_4326_box) {
$area_4326_box = explode(',',EXTENTSERVICEBBOX);
if ($layer_4326_box[0] <= $area_4326_box[0] || $layer_4326_box[2] >= $area_4326_box[2] || $layer_4326_box[1] <= $area_4326_box[1] || $layer_4326_box[3] >= $area_4326_box[3]) {
if ($layer_4326_box[0] < $area_4326_box[0]) {
$area_4326_box[0] = $layer_4326_box[0];
}
if ($layer_4326_box[2] > $area_4326_box[2]) {
$area_4326_box[2] = $layer_4326_box[2];
}
if ($layer_4326_box[1] < $area_4326_box[1]) {
$area_4326_box[1] = $layer_4326_box[1];
}
if ($layer_4326_box[3] > $area_4326_box[3]) {
$area_4326_box[3] = $layer_4326_box[3];
}
$d_x = $area_4326_box[2] - $area_4326_box[0];
$d_y = $area_4326_box[3] - $area_4326_box[1];
$new_minx = $area_4326_box[0] - 0.05*($d_x);
$new_maxx = $area_4326_box[2] + 0.05*($d_x);
$new_miny = $area_4326_box[1] - 0.05*($d_y);
$new_maxy = $area_4326_box[3] + 0.05*($d_y);
if ($new_minx < -180) $area_4326_box[0] = -180; else $area_4326_box[0] = $new_minx;
if ($new_maxx > 180) $area_4326_box[2] = 180; else $area_4326_box[2] = $new_maxx;
if ($new_miny < -90) $area_4326_box[1] = -90; else $area_4326_box[1] = $new_miny;
if ($new_maxy > 90) $area_4326_box[3] = 90; else $area_4326_box[3] = $new_maxy;
}
$getMapUrl = EXTENTSERVICEURL."VERSION=1.1.1&REQUEST=GetMap&SERVICE=WMS&LAYERS=".EXTENTSERVICELAYER."&STYLES=&SRS=EPSG:4326&BBOX=".$area_4326_box[0].",".$area_4326_box[1].",".$area_4326_box[2].",".$area_4326_box[3]."&WIDTH=120&HEIGHT=120&FORMAT=image/png&BGCOLOR=0xffffff&TRANSPARENT=TRUE&EXCEPTIONS=application/vnd.ogc.se_inimage&minx=".$layer_4326_box[0]."&miny=".$layer_4326_box[1]."&maxx=".$layer_4326_box[2]."&maxy=".$layer_4326_box[3]."&metadata_uuid=".$this->fileIdentifier;
return $getMapUrl;
}
public function insertToDB($resourceType, $resourceId, $inheritContactInfo = false, $inheritLicenceInfo = false){
$result = array(); //value/message
switch ($this->origin) {
case "capabilities":
//check if href is given and resource can be parsed
//following information must be given:
//randomId, href, format, type, origin, owner
//TODO: that empty identifier not identified - see comparing linkage
//check wfs version - if 2.0.0 is format and type attributes
$wfs20 = false;
if ($resourceType == 'featuretype') {
if ($this->getWfsVersionForFeaturetype($resourceId) == '2.0.0') {
$wfs20 = true;
//$e = new mb_exception("wfs 2.0.0 found!");
}
}
if ((($this->type == 'ISO19115:2003' || $this->type == 'ISO 19115:2003') && $this->format =='text/xml') || ($this->type == 'TC211' && $this->format =='text/xml') || (($this->type == 'ISO19115:2003' || $this->type == 'ISO 19115:2003') && ($this->format =='application/vnd.iso.19139+xml' || $this->format =='application/xml')) || $wfs20 == true) {
$e = new mb_notice("class_Iso19139:"."try to parse: ".$this->href);
$metadata = $this->createFromUrl($this->href); //will alter object itself
$e = new mb_notice("class_Iso19139:"."Metadata found: ".$this->metadata);
if ($metadata == false) {
//try to insert only MetadataURL elements
if (!$this->insertMetadataUrlToDB($resourceType, $resourceId)) {
$e = new mb_exception("class_Iso19139:"."Problem while storing MetadataURL entry from wms capabilities to mb_metadata table!");
$result['value'] = false;
$result['message'] = "Problem while storing MetadataURL entry from wms capabilities to mb_metadata table!";
return $result;
} else {
$e = new mb_exception("class_Iso19139:"."Storing only MetadataURL ".$metadata->href." from capabilities to mb database cause the target could not be accessed or parsed!");
$result['value'] = true;
$result['message'] = "Storing only MetadataURL ".$metadata->href." from capabilities to mb database cause the target could not be accessed or parsed!";
return $result;
}
}
$this->harvestResult = 1;
} else {
$e = new mb_exception("class_Iso19139:"."MetadataURL format or type of following url is not supported: Format: ".$this->format." | type: ".$this->type);
$result['value'] = false;
$result['message'] = "Metadata attributes are not set properly. Please check this configuration in the service!";
return $result;
}
break;
case "external":
//don't check format and type -cause they are not given. Otherwise the same as in case of capabilities
$metadata = $this->createFromUrl($this->href); //will alter object itself
if ($metadata == false) {
//try to insert only MetadataURL elements
if (!$this->insertMetadataUrlToDB($resourceType, $resourceId)) {
$e = new mb_exception("class_Iso19139:"."Problem while storing metadata source to mb_metadata table!");
$result['value'] = false;
$result['message'] = "Problem while storing metadata source to mb_metadata table!";
return $result;
} else {
$e = new mb_exception("class_Iso19139:"."Storing only MetadataURL ".$metadata->href." from external source to mb database cause the target could not be accessed or parsed!");
$result['value'] = true;
$result['message'] = "Storing only MetadataURL ".$metadata->href." from external source to mb database cause the target could not be accessed or parsed!";
return $result;
}
}
$this->harvestResult = 1;
break;
case "metador":
//nothing to do at all?
break;
case "upload":
//nothing to do at all?
$e = new mb_notice("class_Iso19139:"."upload found");
//$e = new mb_exception($this->metadata);
$metadata = $this->createMapbenderMetadataFromXML($this->metadata);
if ($metadata == false) {
//xml could not be parsed
$result['value'] = false;
$result['message'] = "Metadata object could not be created!";
return $result;
}
//else save it into database
$this->harvestResult = 1;
break;
case "internal":
//only set relation to existing record - return true;
//is actually handled thru plugins/mb_metadata_server*
break;
default:
$e = new mb_exception("class_Iso19139:"."Metadata origin is not known - please set it before storing values to DB!");
$result['value'] = false;
$result['message'] = "Metadata origin is not known - please set it before storing values to DB!";
return $result;
}
//check if metadata record already exists, if not create a new one, else insert relation only and update the metadata itself!
$metadataId = $this->isFileIdentifierAlreadyInDB();
//$e = new mb_exception("found metadata_id: ".$metadataId);
//check if some things should be inherited from service metadata
if ($inheritContactInfo == true || $inheritLicenceInfo == true) {
$this->mdProxy = "t";
}
if ($metadataId != false) {
//update the metadata - new random id set therefor there is no problem when setting the relation afterwards
$e = new mb_notice("existing metadata fileIdentifier found at metadata with id: ".$metadataId." - update will be performed");
//TODO: Check if the timestamp of an existing metadata element is not newer than the timestamp of the current metadata object!!!!
//for uploaded metadata the harvesting was ok otherwise the function returned before
$res = $this->updateMetadataById($metadataId);
$this->insertKeywordsAndCategoriesIntoDB($metadataId,$resourceType,$resourceId);
$this->inheritContactAndLicenceInformation($metadataId,$resourceType,$resourceId,$inheritContactInfo,$inheritLicenceInfo);
} else {
//check if href already exists
$metadataId = $this->isLinkAlreadyInDB();
//$e = new mb_exception("found metadata_id by link : ".$metadataId);
//if so, the metadataset will be the same - (same url same metadataset) - update this as before
if ($metadataId != false) {
//the link to an existing metadataset already exists - don't store it again or insert anything
$e = new mb_notice("existing metadata linkage found at metadata with id: ".$metadataId." - update will be performed");
//check if the timestamp of an existing metadata element is not newer than the timestamp of the current metadata object!!!!
$res = $this->updateMetadataById($metadataId);
$this->insertKeywordsAndCategoriesIntoDB($metadataId,$resourceType,$resourceId);
$this->inheritContactAndLicenceInformation($metadataId,$resourceType,$resourceId,$inheritContactInfo,$inheritLicenceInfo);
} else {
//insert new record
$e = new mb_notice("class_Iso19139:"."No existing metadata fileIdentifier found in mapbender metadata table. New record will be inserted with uuid: ".$this->fileIdentifier);
$res = $this->insertMetadataIntoDB();
}
}
//after insert/update metadata do the relational things
if(!$res){
db_rollback();
$e = new mb_exception("class_Iso19139:"._mb("Cannot insert or update metadata record into mapbenders mb_metadata table!"));
$result['value'] = false;
$result['message'] = "Cannot insert or update metadata record into mapbenders mb_metadata table!";
return $result;
} else {
//insert relation into db
//get inserted or updated metadata_id by use the randomid
$sql = <<randomId);
$t = array('s');
try {
$res = db_prep_query($sql,$v,$t);
}
catch (Exception $e){
$e = new mb_exception("class_Iso19139:"._mb("Cannot get metadata record with following uuid from database: ".$this->randomId));
}
if (!$res) {
//do nothing
$e = new mb_exception("class_Iso19139:"._mb("Cannot get metadata record with following uuid from database: ".$this->randomId));
$result['value'] = false;
$result['message'] = "Cannot get metadata record with following uuid from database: ".$uuid;
return $result;
} else {
//update metadata record in connected csw if configured
//Propagate information for each new layer to csw if configured*******************
$propagation = new propagateMetadata();
$uuidArray[0] = $this->fileIdentifier;
$resultPropagation = $propagation->doPropagation("metadata", false, "push", $uuidArray);
//********************************************************************************
//insert relations
$row = db_fetch_assoc($res);
$metadataId = $row['metadata_id'];
if ($resourceType !== 'metadata') {
/*************************************/
//check if current relation already exists in case of upload
if ($resourceType != 'application') {
if ($this->checkMetadataRelation($resourceType, $resourceId, $metadataId,$this->origin)) {
$e = new mb_notice("class_Iso19139:"._mb("Relation already exists - it will not be generated twice!"));
$result['value'] = true;
$result['message'] = "Relation already exists - it will not be generated twice!";
return $result;
}
//insert relation to layer/featuretype
$sql = "INSERT INTO ows_relation_metadata (fkey_".$resourceType."_id, fkey_metadata_id, relation_type) values ($1, $2, $3);";
$v = array($resourceId, $metadataId, $this->origin);
$t = array('i','i','s');
$res = db_prep_query($sql,$v,$t);
}
} else {
$res = true;
}
if(!$res){
db_rollback();
$e = new mb_exception("class_Iso19139:"._mb("Cannot insert metadata relation!"));
$result['value'] = false;
$result['message'] = "Cannot insert metadata relation!";
return $result;
} else {
//update related view and download service metadata to resolve the coupling and references - operatesOn attribute and so on ...
//get ids and uuids of searchable layers to pull all service metadata records
$sql = "SELECT layer_id, layer.uuid from layer INNER JOIN ows_relation_metadata ON ows_relation_metadata.fkey_layer_id = layer.layer_id WHERE fkey_metadata_id = $1 AND layer.layer_searchable = 1";
$v = array($metadataId);
$t = array('i');
$res = db_prep_query($sql,$v,$t);
//
$sql = "UPDATE mb_metadata SET harvestresult = 1 where metadata_id = $1";
$v = array($metadataId);
$t = array('i');
$res = db_prep_query($sql,$v,$t);
if(!$res){
db_rollback();
$e = new mb_exception("class_Iso19139:"._mb("Cannot update mb_metadata table to fill in harvest result!"));
$result['value'] = false;
$result['message'] = "Cannot update mb_metadata table to fill in harvest result!";
return $result;
} else {
$this->insertKeywordsAndCategoriesIntoDB($metadataId,$resourceType,$resourceId);
//inherit licence info and contact info from coupled service if this is wished so - that will override and/or extent the original metadata!!!
$this->inheritContactAndLicenceInformation($metadataId,$resourceType,$resourceId,$inheritContactInfo,$inheritLicenceInfo);
//TODO: if this was ok, let the resource (layer/featuretype) inherit the classification from the coupled metadata to support better catalogue search
}
}
}
$result['value'] = true;
$result['message'] = "Insert metadata successfully into database!";
return $result;
}
}
}
?>