Преглед изворни кода

Api WFS do danych z BiAudit

Mariusz Muszyński пре 8 година
родитељ
комит
afb731e2e7
1 измењених фајлова са 101 додато и 0 уклоњено
  1. 101 0
      SE/se-lib/Route/WfsBiAudit.php

+ 101 - 0
SE/se-lib/Route/WfsBiAudit.php

@@ -0,0 +1,101 @@
+<?php
+
+Lib::loadClass('RouteBase');
+
+class Route_WfsBiAudit extends RouteBase {
+
+	const maxResolveDepth = 3;
+	private $dom, $tablesUsed = [];
+
+	private static function output($output) {
+		header("Content-Type: application/xml");
+		header("Content-Transfer-Encoding: binary");
+		header("Content-Length: " . strlen($output));
+		echo $output;
+	}
+
+	private static function throwServiceException($message) {
+		$xml = <<<EOT
+<?xml version="1.0" encoding="UTF-8"?>
+<ServiceExceptionReport xmlns="http://www.opengis.net/ogc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/ogc http://schemas.opengis.net/wfs/1.0.0/OGC-exception.xsd" version="1.2.0">
+  <ServiceException>{$message}</ServiceException>
+</ServiceExceptionReport>
+EOT;
+
+		self::output($xml);
+		die();
+	}
+
+	private function addChild($node, $name, $value = null) {
+		$child = $this->dom->createElement($name, $value);
+		$node->appendChild($child);
+		return $child;
+	}
+
+	private function addAttribute($node, $name, $value) {
+		$attr = $this->dom->createAttribute($name);
+		$attr->value = $value;
+		$node->appendChild($attr);
+	}
+
+	private function findRelations($node, $ID, $resolveDepth) {
+		$query = "select `REMOTE_TABLE`, `REMOTE_ID` from `BI_audit_ALL` where `ID` = {$ID}";
+		if (!($row = DB::getPDO()->fetchFirst($query))) self::throwServiceException("Błąd danych");
+		if (!in_array($row['REMOTE_TABLE'], $this->tablesUsed)) $this->tablesUsed[] = $row['REMOTE_TABLE'];
+		if ($resolveDepth) {
+			$feature = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
+			$this->addAttribute($feature, 'fid', "{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
+			$this->addChild($feature, "default_db__x3A__{$row['REMOTE_TABLE']}:ID", $row['REMOTE_ID']);
+			$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
+			$result = DB::getPDO()->fetchAll($query);
+			foreach ($result as $row) $this->findRelations($feature, $row['ID2'], $resolveDepth - 1);
+		} else {
+			$xlink = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
+			$this->addAttribute($xlink, 'xlink:href', "https://biuro.biall-net.pl/wfs/default_db/{$row['REMOTE_TABLE']}#{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
+		}
+	}
+
+	public function defaultAction() {
+		$TYPENAME = V::get('TYPENAME', '', $_GET);
+		$primaryKey = V::get('primaryKey', 0, $_GET, 'int');
+		$resolveDepth = V::get('resolveDepth', 1, $_GET, 'int');
+		$TYPENAME_exploded = explode(":", $TYPENAME);
+		if (!(count($TYPENAME_exploded) == 2 && $primaryKey && $resolveDepth >= 1 && $resolveDepth <= self::maxResolveDepth)) self::throwServiceException("WFS request error");
+		$table = $TYPENAME_exploded[1];
+
+		try {
+			$this->dom = new DOMDocument('1.0', 'UTF-8');
+			$this->dom->preserveWhiteSpace = false;
+			$this->dom->formatOutput = true;
+
+			$attrs = [
+				'xmlns:wfs' => 'http://www.opengis.net/wfs',
+				'xmlns' => 'http://www.opengis.net/wfs',
+				'xmlns:gml' => 'http://www.opengis.net/gml',
+				'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
+				'xmlns:xlink' => 'http://www.w3.org/1999/xlink',
+			];
+
+			$wfs = $this->addChild($this->dom, 'wfs:FeatureCollection');
+			foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
+			$gml = $this->addChild($wfs, 'gml:featureMember');
+
+			$query = "select `ID` from `BI_audit_ALL` where `REMOTE_TABLE` = " . DB::getPDO()->quote($table) . " and `REMOTE_ID` = {$primaryKey}";
+			if (!($ID = DB::getPDO()->fetchValue($query))) self::throwServiceException("Błąd danych");
+			$this->findRelations($gml, $ID, $resolveDepth);
+
+			foreach ($this->tablesUsed as $table) $this->addAttribute($wfs, "xmlns:default_db__x3A__{$table}", "https://biuro.biall-net.pl/wfs/default_db/{$table}");
+			$attrs = [
+				'numberMatched' => 'unknown',
+				'numberReturned' => '1',
+			];
+			foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
+
+			$xml = $this->dom->saveXML();
+			self::output($xml);
+		} catch (Exception $e) {
+			self::throwServiceException($e->getMessage());
+		}
+	}
+
+}