ParseOgcFilter.php 6.9 KB

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