ParseOgcFilter.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. Lib::loadClass('SqlQueryWhereBuilder');
  3. Lib::loadClass('Api_WfsException');
  4. /**
  5. * @see wfs 1.0 wfs:GetFeature schema: http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd
  6. * @see docs wfs 1.1 http://docs.opengeospatial.org/is/04-094r1/04-094r1.html
  7. * wfs:GetFeature
  8. * wfs:Query [ maxOccurs => "unbounded", typeName [ use => "required" ] ]
  9. * ogc:PropertyName [ maxOccurs => "unbounded" ]
  10. * ogc:Filter [ maxOccurs => "1" ]
  11. * ogc:SortBy [ maxOccurs => "1" ]
  12. */
  13. /* TODO: fetch with recurse by global wfs:GetFeature attributes
  14. * @see "Example 11" in http://docs.opengeospatial.org/is/04-094r1/04-094r1.html
  15. <wfs:GetFeature traverseXlinkDepth="1" traverseXlinkExpiry="1"> ...
  16. */
  17. /* TODO: fetch with recurse by local wfs:XlinkPropertyName
  18. * @see "Example 12" in http://docs.opengeospatial.org/is/04-094r1/04-094r1.html
  19. <wfs:GetFeature>
  20. <wfs:Query typeName="Town">
  21. <wfs:PropertyName>gml:name</wfs:PropertyName>
  22. <wfs:XlinkPropertyName traverseXlinkDepth="2" traverseXlinkExpiry="2">gml:directedNode</wfs:XlinkPropertyName>
  23. */
  24. /* TODO: parse xlink query:
  25. <ogc:PropertyIsEqualTo>
  26. <ogc:PropertyName>File/@xlink:href</ogc:PropertyName>
  27. <ogc:Literal>https://biuro.biall-net.pl/wfs/objects#File.45</ogc:Literal>
  28. </ogc:PropertyIsEqualTo>
  29. * TODO: parse bbox query:
  30. <ogc:Filter xmlns:app="http://www.deegree.org/app" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc">
  31. <ogc:BBOX>
  32. <ogc:PropertyName>app:geometry</ogc:PropertyName>
  33. <gml:Envelope>
  34. <gml:coord>
  35. <gml:X>343720</gml:X>
  36. <gml:Y>4374533</gml:Y>
  37. </gml:coord>
  38. <gml:coord>
  39. <gml:X>444136</gml:X>
  40. <gml:Y>4433308</gml:Y>
  41. </gml:coord>
  42. </gml:Envelope>
  43. </ogc:BBOX>
  44. </ogc:Filter>
  45. * TODO: in SqlQueryWhereBuilder split query for main database and join-ed db (right join in ref's)
  46. *
  47. * @usage:
  48. $parser = new ParseOgcFilter();
  49. $parser->loadOgcFilter($ogcFilter);
  50. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  51. $query = $queryWhereBuilder->getQueryWhere('t');
  52. *
  53. */
  54. class ParseOgcFilter {
  55. public $_ogcFilter = '';
  56. public function __construct() {
  57. }
  58. public function loadOgcFilter($ogcFilter) {
  59. // validate?
  60. $this->_ogcFilter = $ogcFilter;
  61. }
  62. public function convertToSqlQueryWhereBuilder() {
  63. $ogcFilterXmlString = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  64. $ogcFilterXmlString .= <<<OGC_FILTER_XML_FILE
  65. <filter xmlns="https://biuro.biall-net.pl/SE/version-git/schema/filter.xsd"
  66. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  67. xmlns:ogc="http://www.opengis.net/ogc">
  68. {$this->_ogcFilter}
  69. </filter>
  70. OGC_FILTER_XML_FILE;
  71. $convertedGuiXml = $this->_convertOgcFilterToCmdList($ogcFilterXmlString);
  72. DBG::_('DBG_DS_OGC', '>1', "convertedGuiXml(".strlen($convertedGuiXml).")", $convertedGuiXml, __CLASS__, __FUNCTION__, __LINE__);
  73. $tags = array();
  74. $parserXml = xml_parser_create();
  75. xml_parser_set_option($parserXml, XML_OPTION_CASE_FOLDING, 0);
  76. xml_parser_set_option($parserXml, XML_OPTION_SKIP_WHITE, 1);
  77. if (0 == xml_parse_into_struct($parserXml, $convertedGuiXml, $tags)) {
  78. throw new Exception("Parse Request xml error #" . __LINE__ . ": parse converted transaction failed");
  79. }
  80. xml_parser_free($parserXml);
  81. if (empty($tags)) {
  82. throw new Exception("Parse Request xml error #" . __LINE__ . ": parse converted transaction returns empty structure");
  83. }
  84. $queryWhereBuilder = new SqlQueryWhereBuilder();
  85. foreach ($tags as $tag) {
  86. switch ($tag['tag']) {
  87. case 'filter': {// root tag
  88. }
  89. break;
  90. case 'sql_filter_comparisonFieldToValue': {
  91. $fieldName = V::get('fieldName', '', $tag['attributes']);
  92. $fieldFunction = V::get('fieldFunction', null, $tag['attributes']);
  93. $comparisonSign = V::get('comparisonSign', '', $tag['attributes']);
  94. $value = V::get('value', '', $tag['attributes']);
  95. DBG::_('DBG_DS_OGC', '>3', "sql_filter_comparisonFieldToValue comparisonSign", $comparisonSign, __CLASS__, __FUNCTION__, __LINE__);
  96. if ('like' == $comparisonSign) {
  97. $wildCard = V::get('wildCard', '', $tag['attributes']);
  98. $singleChar = V::get('singleChar', '', $tag['attributes']);
  99. $escapeChar = V::get('escapeChar', '', $tag['attributes']);
  100. // wildCard="*" singleChar="#" escapeChar="!" => sql: % _ \
  101. // '*ORMA!*' => '%ORMA\*'
  102. // TODO: first replace every escapeChar
  103. $valLength = strlen($value);
  104. $valCharsAllowReplace = array(); for ($i = 0; $i < $valLength; $i++) $valCharsAllowReplace[] = true;
  105. {// escapeChar
  106. $lastOffset = 0;
  107. while (false !== ($pos = strpos($value, $escapeChar, $lastOffset))) {
  108. DBG::_('DBG_DS_OGC', '>3', "sql_filter_comparisonFieldToValue like value({$value}) escapeChar({$escapeChar}) pos({$pos}) lastOffset({$lastOffset})", null, __CLASS__, __FUNCTION__, __LINE__);
  109. $value[$pos] = '\\';
  110. $valCharsAllowReplace[$pos] = false;
  111. if ($pos + 1 < $valLength) $valCharsAllowReplace[$pos + 1] = false;
  112. $lastOffset = $pos + 1;
  113. }
  114. }
  115. {// singleChar
  116. $lastOffset = 0;
  117. while (false !== ($pos = strpos($value, $singleChar, $lastOffset))) {
  118. DBG::_('DBG_DS_OGC', '>3', "sql_filter_comparisonFieldToValue like value({$value}) singleChar({$singleChar}) pos({$pos}) lastOffset({$lastOffset})", null, __CLASS__, __FUNCTION__, __LINE__);
  119. if ($valCharsAllowReplace[$pos]) {
  120. $value[$pos] = '_';
  121. $valCharsAllowReplace[$pos] = false;
  122. }
  123. $lastOffset = $pos + 1;
  124. }
  125. }
  126. {// wildCard
  127. $lastOffset = 0;
  128. while (false !== ($pos = strpos($value, $wildCard, $lastOffset))) {
  129. DBG::_('DBG_DS_OGC', '>3', "sql_filter_comparisonFieldToValue like value({$value}) wildCard({$wildCard}) pos({$pos}) lastOffset({$lastOffset})", null, __CLASS__, __FUNCTION__, __LINE__);
  130. if ($valCharsAllowReplace[$pos]) {
  131. $value[$pos] = '%';
  132. $valCharsAllowReplace[$pos] = false;
  133. }
  134. $lastOffset = $pos + 1;
  135. }
  136. }
  137. }
  138. DBG::_('DBG_DS_OGC', '>3', "sql_filter_comparisonFieldToValue value({$value})", null, __CLASS__, __FUNCTION__, __LINE__);
  139. if ($fieldFunction) {
  140. $queryWhereBuilder->addComparisonFieldFunToValue($fieldFunction, $fieldName, $comparisonSign, $value);
  141. } else {
  142. $queryWhereBuilder->addComparisonFieldToValue($fieldName, $comparisonSign, $value);
  143. }
  144. }
  145. break;
  146. case 'sql_filter_comparisonFieldIsNull': {
  147. $fieldName = V::get('fieldName', '', $tag['attributes']);
  148. $queryWhereBuilder->sql_filter_comparisonFieldIsNull($fieldName);
  149. }
  150. break;
  151. case 'sql_filter_openBlock': {
  152. $blockType = V::get('type', '', $tag['attributes']);
  153. DBG::_('DBG_DS_OGC', '>2', "sql_filter_openBlock block Type {$blockType} attrs", json_encode($tag), __CLASS__, __FUNCTION__, __LINE__);
  154. $queryWhereBuilder->openBlock($blockType);
  155. }
  156. break;
  157. case 'sql_filter_closeBlock': {
  158. $blockType = V::get('type', '', $tag['attributes']);
  159. $queryWhereBuilder->closeBlock($blockType);
  160. }
  161. break;
  162. case 'not_implemented_tag': {
  163. $tagName = V::get('name', '', $tag['attributes']);
  164. DBG::_('DBG_DS_OGC', '>1', "Not Implemented tag '{$tagName}'", $queryWhereBuilder, __CLASS__, __FUNCTION__, __LINE__);
  165. throw new Api_WfsException("Not Implemented tag '{$tagName}'", 501, null, 'NotImplementedOgcTag', 'request');
  166. }
  167. break;
  168. default: {
  169. DBG::_('DBG_DS_OGC', '>1', "TODO: tag {$tag['tag']}", $queryWhereBuilder, __CLASS__, __FUNCTION__, __LINE__);
  170. throw new HttpException("TODO: tag {$tag['tag']}", 501);
  171. }
  172. }
  173. }
  174. $queryWhereBuilder->parseQueryWhere();
  175. DBG::_('DBG_DS_OGC', '>1', "queryWhereBuilder", $queryWhereBuilder, __CLASS__, __FUNCTION__, __LINE__);
  176. return $queryWhereBuilder;
  177. }
  178. public function _convertOgcFilterToCmdList($ogcFilterXmlString) {
  179. $convertOgcFilterXslString = @file_get_contents(APP_PATH_SCHEMA . DS . 'wfs' . DS . 'convertOgcFilterToXmlTaskList.xsl');
  180. if (false === $convertOgcFilterXslString) throw new HttpException("Cannot find file 'convertOgcFilter...'", 404);
  181. if (empty($convertOgcFilterXslString)) throw new HttpException("Empty file 'convertOgcFilter...'", 404);
  182. $requestXml = new DOMDocument();
  183. $isFileCorrect = @$requestXml->loadXml($ogcFilterXmlString);
  184. if (false === $isFileCorrect) throw new HttpException("Parse error for ogc filter", 400);
  185. $convertOgcFilterXsl = new DOMDocument();
  186. $isFileCorrect = @$convertOgcFilterXsl->loadXml($convertOgcFilterXslString);
  187. if (false === $isFileCorrect) throw new HttpException("Parse error for file 'convertOgcFilter...'", 500);
  188. $proc = new XSLTProcessor();
  189. $isImported = $proc->importStylesheet($convertOgcFilterXsl);
  190. if (false === $isImported) throw new HttpException("XSLT Parse error for import 'convertOgcFilter...'", 500);
  191. return $proc->transformToXML($requestXml);
  192. }
  193. }