format parameter not sent!";
echo ($message);
exit;
}
//it could be that instead of posting an XML document they indicate the url of a document
// from where to retrive it.
if (isset($_REQUEST['doc_url'])) {
$doc_url=$_REQUEST['doc_url'];
$xml = file_get_contents($doc_url);
} else {
if (isset($_REQUEST['xml'])) {
$xml=$_REQUEST['xml'];
} else {
header("Content-type: text/xml; charset=UTF-8");
$message="no input set. Please post the xml suing the XML parameter or point to document where to retrieve data using doc_url";
echo ($message);
exit;
}
}
//format of the output response. HTML is the default if not specified.
if (isset($_REQUEST['resultSummaryFormat'])) {
$resultFormat=$_REQUEST['resultSummaryFormat'];
} else {
$resultFormat="html";
}
//-----------
//CACHING
//Check if the same url has been before request and is in the database
$query = $_SERVER["QUERY_STRING"];
$sql="SELECT id, \"summaryReport\" FROM \"datasetsMetadata\" WHERE \"processedUrl\"=MD5('" .$query. "')";
$result=pg_query($dbcon, $sql);
$rs = pg_fetch_assoc($result);
//If there is a dataset already in the database then use it
if ($rs) {
$summaryReport = $rs['summaryReport'];
//sum 1 to the number of times this has been used
$sql="UPDATE \"datasetsMetadata\" SET \"timesUsed\"=\"timesUsed\"+1 WHERE id = ".$rs['id'];
pg_query($dbcon, $sql);
} else {
//We parse and generate the dataset
//NAME OF THE DATASET
//Generate a random number to use as a unique identifier for this temporary dataset
srand(time());
$datasetIdNumeric = (rand()%99999999);
$datasetId = "ds" . $datasetIdNumeric;
//PARSE INPUT
//parse the input depending on the format
if ($format=='simple_xml') {
list ($featureCount,$sql) = simple_xml_parser($xml, $datasetId);
}
if ($format=='gbif_gis') {
list ($featureCount,$sql) = gbif_gis_xml_parser($xml, $datasetId);
}
//if there are no features (featureCount=0) set the datasetId to null
if (!$featureCount) { $datasetId = "";}
//INSERT IN DB
//Execute the SQL!
if ($featureCount>0) {
$result=pg_query($dbcon, $sql);
if (!$result) {
//If the insert did not work we cancel the script
echo "There was a problem processing the data. The dataset has not been created, please contact the sysAdmin";
exit;
}
}
//CREATE SUMMARY
//Generate the summaryReport of the temporary dataset depending on the resultformat
if ($resultFormat=='html') {
$summaryReport = processHTMLReport($datasetId,$featureCount,$geoserverExternalOWSUrl);
}
elseif ($resultFormat=='xml') {
$summaryReport = processXMLReport($datasetId,$featureCount,$geoserverExternalOWSUrl);
}
//REGISTER in METATADA TABLE
//Register the new created dataset in the datasetsMetadata table only if something has been written
if ($featureCount>0) {
$sql= "INSERT INTO \"datasetsMetadata\"(id,\"tableName\",\"processedUrl\",\"daysAlive\",\"summaryReport\", \"timesUsed\") VALUES ($datasetIdNumeric,'$datasetId',MD5('{$_SERVER["QUERY_STRING"]}'),1,'".pg_escape_string($summaryReport)."',1)";
//echo $sql;
$result=pg_query($dbcon, $sql);
if (!$result) {
//If the insert did not work we cancel the script
echo "There was a problem processing the data. The dataset has not been created, please contact the sysAdmin";
exit;
}
//REGISTER DATASET in GEOSERVER (only if there has been something inserted)
//-------------------------------------------------------------
//Register the new data type using geoserver configuration tool
$client = new HttpClient($geoserverInternalHost, $geoserverInternalPort);
$client->setDebug(false);
//Authenticate in Geoserver
$client->post($geoserverPath.'admin/loginSubmit.do', array (
'username' =>$geoserver_user,
'password' =>$geoserver_pwd,
'submit' => 'Submit'
));
$headers = $client->getHeaders();
$foo= explode(";",$headers['set-cookie']);
$foo= explode("=",$foo[0]);
$sessionArray = array('JSESSIONID' => $foo[1]);
$client->setCookies($sessionArray);
//Create a new Feature Type
$client->post($geoserverPath.'config/data/typeNewSubmit.do', array ('selectedNewFeatureType' => 'temp_datasets:::'.$datasetId));
//edit feature details
$client->post($geoserverPath.'config/data/typeEditorSubmit.do', array (
'styleId' => 'poi',
'SRS' => '4326',
'title' => 'temporary dataset:' . $datasetId,
'minX' => '-180',
'minY' => '-90',
'maxX' => '180',
'maxY' => '90',
'keywords' => ' ',
'abstract' => 'temporary dataset:' . $datasetId,
'schemaBase' => '--',
'action' => 'Submit'
));
//Click Apply
$client->post($geoserverPath.'admin/saveToGeoServer.do', array ('submit' => 'Apply'));
//Click Save
$client->post($geoserverPath.'admin/saveToXML.do', array ('submit' => 'Save'));
//-----------------------------------------------
}
}
//SET ENCODING AND OUTPUT
//Set correct encode and return report
header("Content-type: text/$resultFormat; charset=UTF-8");
echo ($summaryReport);
?>