summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Query/QueryComparator.php
blob: f2fb168f63852867ff86a8a295c1cf25e87e21b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php

namespace SMW\Query;

/**
 * @license GNU GPL v2+
 * @since 1.5.3
 *
 * @author mwjames
 * @author Jeroen De Dauw
 */
class QueryComparator {

	/**
	 * @var QueryComparator
	 */
	private static $instance = null;

	/**
	 * @var array
	 */
	private $comparators = null;

	/**
	 * @var array
	 */
	private $reverseCache = [];

	/**
	 * @since 2.3
	 *
	 * @param string $comparatorList
	 * @param boolean $strictComparators
	 */
	public function __construct( $comparatorList, $strictComparators ) {
		$this->comparators = $this->getEnabledComparators( $comparatorList, $strictComparators );
	}

	/**
	 * @since 2.3
	 *
	 * @return self
	 */
	public static function getInstance() {

		if ( self::$instance === null ) {
			self::$instance = new self(
				$GLOBALS['smwgQComparators'],
				$GLOBALS['smwStrictComparators']
			);
		}

		return self::$instance;
	}

	/**
	 * @since 2.3
	 */
	public static function clear() {
		self::$instance = null;
	}

	/**
	 * Gets an array with all supported comparator strings.
	 * The string for SMW_CMP_EQ, which is an empty string, is not in this list.
	 *
	 * @since 1.5.3
	 *
	 * @return array
	 */
	public function getComparatorStrings() {
		return array_keys( $this->comparators );
	}

	/**
	 * Gets the SMW_CMP_ for a string comparator, falling back to the
	 * $defaultComparator when none is found.
	 *
	 * @since 1.5.3
	 *
	 * @param string $string
	 * @param integer $defaultComparator Item of the SMW_CMP_ enum
	 *
	 * @return integer Item of the SMW_CMP_ enum
	 */
	public function getComparatorFromString( $string, $defaultComparator = SMW_CMP_EQ ) {

		if ( $string === '' ) {
			return SMW_CMP_EQ;
		}

		return array_key_exists( $string, $this->comparators ) ? $this->comparators[$string] : $defaultComparator;
	}

	/**
	 * @since 2.5
	 *
	 * @param string $value
	 * @param integer $comparator
	 *
	 * @return boolean
	 */
	public function containsComparator( $value, $comparator = SMW_CMP_EQ ) {
		return $this->extractComparatorFromString( $value ) === $comparator;
	}

	/**
	 * Extract possible comparators from a value and alter it to consist
	 * only of the remaining effective value string (without the comparator).
	 *
	 * @since 2.4
	 *
	 * @param $value
	 *
	 * @return integer
	 */
	public function extractComparatorFromString( &$value ) {

		$comparator = SMW_CMP_EQ;

		foreach ( $this->getComparatorStrings() as $string ) {
			if ( strpos( $value, $string ) === 0 ) {
				$comparator = $this->getComparatorFromString( substr( $value, 0, strlen( $string ) ) );
				$value = substr( $value, strlen( $string ) );
				break;
			}
		}

		return $comparator;
	}

	/**
	 * Gets the comparator string for a comparator.
	 *
	 * @since 1.5.3
	 *
	 * @param $comparator
	 *
	 * @return string
	 */
	public function getStringForComparator( $comparator ) {

		if ( $this->reverseCache === [] ) {
			$this->reverseCache = array_flip( $this->comparators );
		}

		if ( $comparator == SMW_CMP_EQ ) {
			return '';
		} elseif ( array_key_exists( $comparator, $this->reverseCache ) ) {
			return $this->reverseCache[$comparator];
		}

		throw new Exception( "Comparator $comparator does not have a string representatation" );
	}

	private function getEnabledComparators( $comparatorList, $strictComparators ) {

		// Note: Comparators that contain other comparators at the beginning of
		// the string need to be at beginning of the array.
		$comparators = [
			'like:' => SMW_CMP_PRIM_LIKE,
			'nlike:' => SMW_CMP_PRIM_NLKE,
			'in:' => SMW_CMP_IN,
			'not:' => SMW_CMP_NOT,
			'phrase:' => SMW_CMP_PHRASE,
			'!~' => SMW_CMP_NLKE,
			'<<' => SMW_CMP_LESS,
			'>>' => SMW_CMP_GRTR,
			'<' => $strictComparators ? SMW_CMP_LESS : SMW_CMP_LEQ,
			'>' => $strictComparators ? SMW_CMP_GRTR : SMW_CMP_GEQ,
			'≤' => SMW_CMP_LEQ,
			'≥' => SMW_CMP_GEQ,
			'!' => SMW_CMP_NEQ,
			'~' => SMW_CMP_LIKE,
		];

		if ( strpos( $comparatorList, '|' ) === false ) {
			return $comparators;
		}

		$allowedComparators = explode( '|', $comparatorList );

		// Remove the comparators that are not allowed.
		foreach ( $comparators as $string => $comparator ) {
			if ( !in_array( $string, $allowedComparators ) ) {
				unset( $comparators[$string] );
			}
		}

		return $comparators;
	}

}