ParseOgcFilter.php 7.8 KB

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