ParseOgcFilter.php 6.5 KB

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