ParseOgcQuery.php 7.4 KB

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