summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/QueryEngine/DescriptionInterpreters/ComparatorMapper.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/SemanticMediaWiki/src/SQLStore/QueryEngine/DescriptionInterpreters/ComparatorMapper.php')
-rw-r--r--www/wiki/extensions/SemanticMediaWiki/src/SQLStore/QueryEngine/DescriptionInterpreters/ComparatorMapper.php64
1 files changed, 64 insertions, 0 deletions
diff --git a/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/QueryEngine/DescriptionInterpreters/ComparatorMapper.php b/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/QueryEngine/DescriptionInterpreters/ComparatorMapper.php
new file mode 100644
index 00000000..e32ac8c2
--- /dev/null
+++ b/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/QueryEngine/DescriptionInterpreters/ComparatorMapper.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace SMW\SQLStore\QueryEngine\DescriptionInterpreters;
+
+use RuntimeException;
+use SMW\Query\Language\ValueDescription;
+use SMWDIUri as DIUri;
+
+/**
+ * @license GNU GPL v2+
+ * @since 2.2
+ *
+ * @author mwjames
+ */
+class ComparatorMapper {
+
+ /**
+ * @since 2.2
+ *
+ * @param ValueDescription $description
+ * @param string &$value
+ *
+ * @return string
+ * @throws RuntimeException
+ */
+ public function mapComparator( ValueDescription $description, &$value ) {
+
+ $comparatorMap = [
+ SMW_CMP_EQ => '=',
+ SMW_CMP_LESS => '<',
+ SMW_CMP_GRTR => '>',
+ SMW_CMP_LEQ => '<=',
+ SMW_CMP_GEQ => '>=',
+ SMW_CMP_NEQ => '!=',
+ SMW_CMP_LIKE => ' LIKE ',
+ SMW_CMP_PRIM_LIKE => ' LIKE ',
+ SMW_CMP_NLKE => ' NOT LIKE ',
+ SMW_CMP_PRIM_NLKE => ' NOT LIKE '
+ ];
+
+ $comparator = $description->getComparator();
+
+ if ( !isset( $comparatorMap[$comparator] ) ) {
+ throw new RuntimeException( "Unsupported comparator $comparator in value description." );
+ }
+
+ if ( $comparator === SMW_CMP_LIKE || $comparator === SMW_CMP_NLKE || $comparator === SMW_CMP_PRIM_LIKE || $comparator === SMW_CMP_PRIM_NLKE ) {
+
+ if ( $description->getDataItem() instanceof DIUri ) {
+ $value = str_replace( [ 'http://', 'https://', '%2A' ], [ '*', '*', '*' ], $value );
+ }
+
+ // Escape to prepare string matching:
+ $value = str_replace(
+ [ '\\', '%', '_', '*', '?' ],
+ [ '\\\\', '\%', '\_', '%', '_' ],
+ $value
+ );
+ }
+
+ return $comparatorMap[$comparator];
+ }
+
+}