WfsBiAudit.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_WfsBiAudit extends RouteBase {
  4. const maxResolveDepth = 3;
  5. private $dom, $path = [], $relations = [], $tablesUsed = [];
  6. private static function output($output) {
  7. header("Content-Type: application/xml");
  8. header("Content-Transfer-Encoding: binary");
  9. header("Content-Length: " . strlen($output));
  10. echo $output;
  11. }
  12. private static function throwServiceException($message) {
  13. $xml = <<<EOT
  14. <?xml version="1.0" encoding="UTF-8"?>
  15. <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">
  16. <ServiceException>{$message}</ServiceException>
  17. </ServiceExceptionReport>
  18. EOT;
  19. self::output($xml);
  20. die();
  21. }
  22. public function objectStructureAction($namespace) { // objectStructAction
  23. Lib::loadClass('SchemaFactory');
  24. Lib::ACL('SchemaFactory');
  25. try {
  26. error_log('objectStructureAction for '.$namespace);
  27. if (empty($namespace)) throw new Exception("Missing param namespace");
  28. $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
  29. $return=$this->objectStructView($item);
  30. return $return;
  31. } catch (Exception $e) {
  32. //error_log('danger', "Error #" . $e->getCode() . "|" . $e->getLine() . ": " . $e->getMessage());
  33. DBG::log($e);
  34. }
  35. }
  36. //input param $item['namespace']='default_db/BI_audit_CEIDG_powiazania/BI_audit_CEIDG_powiazania'
  37. public function objectStructView($item) {
  38. $namespace = $item['namespace'];
  39. $thisGetLink = [ $this, 'getLink' ];
  40. $getBackRefList=ACL::getBackRefList($item['namespace']);
  41. foreach($getBackRefList as $ind=>$backref) {
  42. $exploded_ns = explode('/', $backref['namespace']);
  43. $return['backrefs'][]=$exploded_ns[1].'__x3A__'.$exploded_ns[2].':'.$exploded_ns[3];
  44. }
  45. foreach($item['field'] as $ind=>$refs) {
  46. $return['refs'][]=$refs['fieldNamespace'];
  47. $return['refs__rootTableName'][]=$refs['_rootTableName'];
  48. $return['refs__objectNamespace'][]=$refs['objectNamespace'];
  49. }
  50. $return['table']=$item['name'];
  51. $return['primaryKey']=$item['primaryKey'];
  52. //print_r($this->BaseStruct);
  53. return $return;
  54. }
  55. private function addChild($node, $name, $value = null) {
  56. $child = $this->dom->createElement($name, $value);
  57. $node->appendChild($child);
  58. return $child;
  59. }
  60. private function addAttribute($node, $name, $value) {
  61. $attr = $this->dom->createAttribute($name);
  62. $attr->value = $value;
  63. $node->appendChild($attr);
  64. }
  65. private function relationName($ID) {
  66. if (!isset($this->relations[$ID])) {
  67. $query = "select `RELATION` from `BI_audit_ALL_ref_RELATIONS` where `ID` & {$ID} order by `ID`";
  68. $result = DB::getPDO()->fetchAll($query);
  69. $rels = array_map('reset', $result);
  70. $this->relations[$ID] = implode(", ", $rels);
  71. }
  72. return $this->relations[$ID];
  73. }
  74. private function findRelations_base($node, $ID, $resolveDepth, $relation = null, $BaseStruct,$table) {
  75. if (in_array($ID, $this->path)) return;
  76. $this->path[] = $ID;
  77. foreach($BaseStruct['objectNamespace'] as $ind=>$refs){
  78. $getRefTable=ACL::getRefTable('default_db/'.$table.'/'.$table,$refs);
  79. $query = "select `REMOTE_PRIMARY_KEY` from `".$getRefTable."` where `PRIMARY_KEY` = {$ID}";
  80. if (!($row = DB::getPDO()->fetchFirst($query))) {
  81. } else {
  82. /*{
  83. $this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
  84. self::throwServiceException("Błąd danych #102 findRelations_base");
  85. }*/
  86. if (!in_array($BaseStruct[$ind]['_rootTableName'], $this->tablesUsed)) $this->tablesUsed[] = $BaseStruct[$ind]['_rootTableName'];
  87. }
  88. if ($resolveDepth) {
  89. $feature = $this->addChild($node, $BaseStruct[$ind]['refs']);
  90. $this->addAttribute($feature, 'fid', $BaseStruct[$ind]['_rootTableName'].$row['REMOTE_PRIMARY_KEY']);
  91. $this->addChild($feature, "default_db__x3A__".$BaseStruct[$ind]['_rootTableName'].":ID", $row['REMOTE_PRIMARY_KEY']);
  92. if ($relation) $this->addChild($feature, "relation_from", $this->relationName($relation));
  93. //$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
  94. //$where = $relation ? "and ({$relation} & `RELATION_ID`) != {$relation}" : "";
  95. $RefBaseStruct=$this->objectStructureAction($BaseStruct[$ind]['objectNamespace']);
  96. $query = "select `".$RefBaseStruct['primaryKey']."` from `".$RefBaseStruct['table']."` where `".$RefBaseStruct['primaryKey']."` = ".$row['REMOTE_PRIMARY_KEY'] ;
  97. $result = DB::getPDO()->fetchAll($query);
  98. foreach ($result as $row) $this->findRelations($feature, $row['REMOTE_PRIMARY_KEY'], $resolveDepth - 1, null,$BaseStruct['table'],$RefBaseStruct['table']);
  99. } else {
  100. $xlink = $this->addChild($node, $BaseStruct[$ind]['fieldNamespace']);
  101. $this->addAttribute($xlink, 'xlink:href', "https://biuro.biall-net.pl/wfs/default_db/{$RefBaseStruct['table']}?BI_audit_ALL_ref_RELATIONS={$relation}#{$RefBaseStruct['table']}.{$row['REMOTE_PRIMARY_KEY']}");
  102. }
  103. array_pop($this->path);
  104. }
  105. }
  106. private function findRelations($node, $ID, $resolveDepth, $relation = null,$BaseTableFrom,$BaseTableTo) {
  107. if (in_array($ID, $this->path)) return;
  108. $this->path[] = $ID;
  109. if(isset($BaseTableFrom)) {
  110. $query = "select `REMOTE_TABLE`, `REMOTE_ID` from `BI_audit_ALL` where `ID` = {$ID} and `REMOTE_TABLE`='".$BaseTableTo."'"; //todo nie wiem czy nie na odwrot $BaseTableTo
  111. } else {
  112. $query = "select `REMOTE_TABLE`, `REMOTE_ID` from `BI_audit_ALL` where `ID` = {$ID}";
  113. }
  114. if (!($row = DB::getPDO()->fetchFirst($query)))
  115. {
  116. //$this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
  117. // self::throwServiceException("Błąd danych findRelations #130");
  118. $this->BaseStruct=$this->objectStructureAction("default_db/".$BaseTableTo."/".$BaseTableTo );
  119. $query = "select `".$this->BaseStruct['primaryKey']."` from `".$BaseTableTo."` where ".$this->BaseStruct['primaryKey']." = {$ID}";
  120. if (!($ID = DB::getPDO()->fetchValue($query))) {
  121. self::throwServiceException("Błąd danych z BaseStruct/findRelations #162 ");
  122. }
  123. if (!in_array($BaseTableTo, $this->tablesUsed)) $this->tablesUsed[] = $BaseTableTo;
  124. if ($resolveDepth) {
  125. $feature = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  126. $this->addAttribute($feature, 'fid', "{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
  127. $this->addChild($feature, "default_db__x3A__{$row['REMOTE_TABLE']}:ID", $row['REMOTE_ID']);
  128. if ($relation) $this->addChild($feature, "relation_from", $this->relationName($relation));
  129. //$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
  130. $where = $relation ? "and ({$relation} & `RELATION_ID`) != {$relation}" : "";
  131. $query = "select `ID2`, `RELATION_ID` from `BI_audit_ALL_ref` where `ID1` = {$ID} {$where}";
  132. $result = DB::getPDO()->fetchAll($query);
  133. foreach ($result as $row)
  134. // $this->findRelations($feature, $row['ID2'], $resolveDepth - 1, $row['RELATION_ID'],);
  135. findRelations_base($node, $ID, $resolveDepth, $relation = null, $this->BaseStruct,$BaseTableTo) ;
  136. } else {
  137. $xlink = $this->addChild($node, "default_db__x3A__{$BaseTableTo}:{$BaseTableTo}");
  138. $this->addAttribute($xlink, 'xlink:href', "https://biuro.biall-net.pl/wfs/default_db/{$BaseTableTo]}?BI_audit_ALL_ref_RELATIONS={$relation}#{$BaseTableTo}.{$row[$this->BaseStruct['primaryKey']]}");
  139. }
  140. array_pop($this->path);
  141. } else {
  142. if (!in_array($row['REMOTE_TABLE'], $this->tablesUsed)) $this->tablesUsed[] = $row['REMOTE_TABLE'];
  143. if ($resolveDepth) {
  144. $feature = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  145. $this->addAttribute($feature, 'fid', "{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
  146. $this->addChild($feature, "default_db__x3A__{$row['REMOTE_TABLE']}:ID", $row['REMOTE_ID']);
  147. if ($relation) $this->addChild($feature, "relation_from", $this->relationName($relation));
  148. //$query = "select `ID2` from `BI_audit_ALL_ref` where `ID1` = {$ID}";
  149. $where = $relation ? "and ({$relation} & `RELATION_ID`) != {$relation}" : "";
  150. $query = "select `ID2`, `RELATION_ID` from `BI_audit_ALL_ref` where `ID1` = {$ID} {$where}";
  151. $result = DB::getPDO()->fetchAll($query);
  152. foreach ($result as $row) $this->findRelations($feature, $row['ID2'], $resolveDepth - 1, $row['RELATION_ID'],);
  153. } else {
  154. $xlink = $this->addChild($node, "default_db__x3A__{$row['REMOTE_TABLE']}:{$row['REMOTE_TABLE']}");
  155. $this->addAttribute($xlink, 'xlink:href', "https://biuro.biall-net.pl/wfs/default_db/{$row['REMOTE_TABLE']}?BI_audit_ALL_ref_RELATIONS={$relation}#{$row['REMOTE_TABLE']}.{$row['REMOTE_ID']}");
  156. }
  157. array_pop($this->path);
  158. }
  159. }
  160. public function defaultAction() {
  161. $TYPENAME = V::get('TYPENAME', '', $_GET);
  162. $primaryKey = V::get('primaryKey', 0, $_GET, 'int');
  163. $resolveDepth = V::get('resolveDepth', 1, $_GET, 'int');
  164. $TYPENAME_exploded = explode(":", $TYPENAME);
  165. if (!(count($TYPENAME_exploded) == 2 && $primaryKey && $resolveDepth >= 1 && $resolveDepth <= self::maxResolveDepth)) self::throwServiceException("WFS request error");
  166. $table = $TYPENAME_exploded[1];
  167. try {
  168. $this->dom = new DOMDocument('1.0', 'UTF-8');
  169. $this->dom->preserveWhiteSpace = false;
  170. $this->dom->formatOutput = true;
  171. $attrs = [
  172. 'xmlns:wfs' => 'http://www.opengis.net/wfs',
  173. 'xmlns' => 'http://www.opengis.net/wfs',
  174. 'xmlns:gml' => 'http://www.opengis.net/gml',
  175. 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
  176. 'xmlns:xlink' => 'http://www.w3.org/1999/xlink',
  177. ];
  178. $wfs = $this->addChild($this->dom, 'wfs:FeatureCollection');
  179. foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
  180. $gml = $this->addChild($wfs, 'gml:featureMember');
  181. $query = "select `ID` from `BI_audit_ALL` where `REMOTE_TABLE` = " . DB::getPDO()->quote($table) . " and `REMOTE_ID` = {$primaryKey}";
  182. if (!($ID = DB::getPDO()->fetchValue($query))) {
  183. $this->BaseStruct=$this->objectStructureAction("default_db/".$table."/".$table );
  184. $query = "select `".$this->BaseStruct['primaryKey']."` from `".$table."` where ".$this->BaseStruct['primaryKey']." = {$primaryKey}";
  185. if (!($ID = DB::getPDO()->fetchValue($query))) {
  186. self::throwServiceException("Błąd danych z BaseStruct ");
  187. }
  188. $this->findRelations_base($gml, $ID, $resolveDepth,$this->BaseStruct,$table);
  189. foreach ($this->tablesUsed as $table) $this->addAttribute($wfs, "xmlns:default_db__x3A__{$table}", "https://biuro.biall-net.pl/wfs/default_db/{$table}");
  190. $attrs = [
  191. 'numberMatched' => 'unknown',
  192. 'numberReturned' => '1',
  193. ];
  194. foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
  195. $xml = $this->dom->saveXML();
  196. self::output($xml);
  197. } else {
  198. $this->findRelations($gml, $ID, $resolveDepth);
  199. foreach ($this->tablesUsed as $table) $this->addAttribute($wfs, "xmlns:default_db__x3A__{$table}", "https://biuro.biall-net.pl/wfs/default_db/{$table}");
  200. $attrs = [
  201. 'numberMatched' => 'unknown',
  202. 'numberReturned' => '1',
  203. ];
  204. foreach ($attrs as $name => $value) $this->addAttribute($wfs, $name, $value);
  205. $xml = $this->dom->saveXML();
  206. self::output($xml);
  207. }
  208. } catch (Exception $e) {
  209. //$this->objectStructureAction('default_db/BI_audit_CEIDG_pelnomocnicy/BI_audit_CEIDG_pelnomocnicy');
  210. self::throwServiceException($e->getMessage());
  211. }
  212. }
  213. }