ParseOgcFilter.php 7.3 KB

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