summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/QueryEngine/QuerySegmentListBuilder.php
blob: dbe02754f2a55e669ed5f317dc6ba7ea50fc75a1 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php

namespace SMW\SQLStore\QueryEngine;

use InvalidArgumentException;
use OutOfBoundsException;
use SMW\Message;
use SMW\Query\Language\Conjuncton;
use SMW\Query\Language\Description;
use SMW\Store;
use SMW\Utils\CircularReferenceGuard;

/**
 * @license GNU GPL v2+
 * @since 2.2
 *
 * @author Markus Krötzsch
 * @author Jeroen De Dauw
 * @author mwjames
 */
class QuerySegmentListBuilder {

	/**
	 * @var Store
	 */
	private $store;

	/**
	 * @var DispatchingDescriptionInterpreter
	 */
	private $dispatchingDescriptionInterpreter = null;

	/**
	 * @var boolean
	 */
	private $isFilterDuplicates = true;

	/**
	 * Array of generated QueryContainer query descriptions (index => object).
	 *
	 * @var QuerySegment[]
	 */
	private $querySegments = [];

	/**
	 * Array of sorting requests ("Property_name" => "ASC"/"DESC"). Used during query
	 * processing (where these property names are searched while compiling the query
	 * conditions).
	 *
	 * @var string[]
	 */
	private $sortKeys = [];

	/**
	 * @var string[]
	 */
	private $errors = [];

	/**
	 * @var integer
	 */
	private $lastQuerySegmentId = -1;

	/**
	 * @since 2.2
	 *
	 * @param Store $store
	 * @param DescriptionInterpreterFactory $descriptionInterpreterFactory
	 */
	public function __construct( Store $store, DescriptionInterpreterFactory $descriptionInterpreterFactory ) {
		$this->store = $store;
		$this->dispatchingDescriptionInterpreter = $descriptionInterpreterFactory->newDispatchingDescriptionInterpreter( $this );
		$this->circularReferenceGuard = new CircularReferenceGuard( 'sql-query' );
		$this->circularReferenceGuard->setMaxRecursionDepth( 2 );

		QuerySegment::$qnum = 0;
	}

	/**
	 * Filter dulicate segments that represent the same query and to be identified
	 * by the same hash.
	 *
	 * @since 2.5
	 *
	 * @param boolean $isFilterDuplicates
	 */
	public function isFilterDuplicates( $isFilterDuplicates ) {
		$this->isFilterDuplicates = (bool)$isFilterDuplicates;
	}

	/**
	 * @since 2.2
	 *
	 * @return Store
	 */
	public function getStore() {
		return $this->store;
	}

	/**
	 * @since 2.2
	 *
	 * @param array $sortKeys
	 *
	 * @return $this
	 */
	public function setSortKeys( $sortKeys ) {
		$this->sortKeys = $sortKeys;
		return $this;
	}

	/**
	 * @since 2.2
	 *
	 * @return string[]
	 */
	public function getSortKeys() {
		return $this->sortKeys;
	}

	/**
	 * @since 2.2
	 *
	 * @return CircularReferenceGuard
	 */
	public function getCircularReferenceGuard() {
		return $this->circularReferenceGuard;
	}

	/**
	 * @since 2.2
	 *
	 * @param int $id
	 *
	 * @return QuerySegment
	 * @throws InvalidArgumentException
	 * @throws OutOfBoundsException
	 */
	public function findQuerySegment( $id ) {

		if ( !is_int( $id ) ) {
			throw new InvalidArgumentException( '$id needs to be an integer' );
		}

		if ( !array_key_exists( $id, $this->querySegments ) ) {
			throw new OutOfBoundsException( 'There is no query segment with id ' . $id );
		}

		return $this->querySegments[$id];
	}

	/**
	 * @since 2.2
	 *
	 * @return QuerySegment[]
	 */
	public function getQuerySegmentList() {
		return $this->querySegments;
	}

	/**
	 * @since 2.2
	 *
	 * @param QuerySegment $query
	 */
	public function addQuerySegment( QuerySegment $query ) {
		$this->querySegments[$query->queryNumber] = $query;
	}

	/**
	 * @since 2.2
	 *
	 * @return integer
	 */
	public function getLastQuerySegmentId() {
		return $this->lastQuerySegmentId;
	}

	/**
	 * @since 2.2
	 *
	 * @return array
	 */
	public function getErrors() {
		return $this->errors;
	}

	/**
	 * @since 2.2
	 *
	 * @param string $error
	 */
	public function addError( $error, $type = Message::TEXT ) {
		$this->errors[Message::getHash( $error, $type )] = Message::encode( $error, $type );
	}

	/**
	 * Create a new QueryContainer object that can be used to obtain results
	 * for the given description. The result is stored in $this->queries
	 * using a numeric key that is returned as a result of the function.
	 * Returns -1 if no query was created.
	 *
	 * @param Description $description
	 *
	 * @return integer
	 */
	public function getQuerySegmentFrom( Description $description ) {

		$fingerprint = $description->getFingerprint();

		// Get membership of descriptions that are resolved recursively
		if ( $description->getMembership() !== '' ) {
			$fingerprint = $fingerprint . $description->getMembership();
		}

		if ( ( $querySegment = $this->findDuplicates( $fingerprint ) ) ) {
			return $querySegment;
		}

		$querySegment = $this->dispatchingDescriptionInterpreter->interpretDescription(
			$description
		);

		$querySegment->fingerprint = $fingerprint;
		//$querySegment->membership = $description->getMembership();
		//$querySegment->queryString = $description->getQueryString();

		$this->lastQuerySegmentId = $this->registerQuerySegment(
			$querySegment
		);

		return $this->lastQuerySegmentId;
	}

	/**
	 * Register a query object to the internal query list, if the query is
	 * valid. Also make sure that sortkey information is propagated down
	 * from subqueries of this query.
	 *
	 * @param QuerySegment $query
	 */
	private function registerQuerySegment( QuerySegment $query ) {
		if ( $query->type === QuerySegment::Q_NOQUERY ) {
			return -1;
		}

		$this->addQuerySegment( $query );

		// Propagate sortkeys from subqueries:
		if ( $query->type !== QuerySegment::Q_DISJUNCTION ) {
			// Sortkeys are killed by disjunctions (not all parts may have them),
			// NOTE: preprocessing might try to push disjunctions downwards to safe sortkey, but this seems to be minor
			foreach ( $query->components as $cid => $field ) {
				$query->sortfields = array_merge( $this->findQuerySegment( $cid )->sortfields, $query->sortfields );
			}
		}

		return $query->queryNumber;
	}

	private function findDuplicates( $fingerprint ) {

		if ( $this->errors !== [] || $this->isFilterDuplicates === false ) {
			return false;
		}

		foreach ( $this->querySegments as $querySegment ) {
			if ( $querySegment->fingerprint === $fingerprint ) {
				return $querySegment->queryNumber;
			};
		}

		return false;
	}

}