ParseOgcFilter.php 7.0 KB

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