summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SPARQLStore/QueryEngine/QueryEngine.php
blob: 046f4ce8c10a688339d9af32e6b398102f6550b3 (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
<?php

namespace SMW\SPARQLStore\QueryEngine;

use RuntimeException;
use SMW\Exporter\Element;
use SMW\Query\DebugFormatter;
use SMW\Query\Language\ThingDescription;
use SMW\QueryEngine as QueryEngineInterface;
use SMW\SPARQLStore\QueryEngine\Condition\Condition;
use SMW\SPARQLStore\QueryEngine\Condition\FalseCondition;
use SMW\SPARQLStore\QueryEngine\Condition\SingletonCondition;
use SMW\SPARQLStore\RepositoryConnection;
use SMWQuery as Query;
use SMWQueryResult as QueryResult;

/**
 * Class mapping SMWQuery objects to SPARQL, and for controlling the execution
 * of these queries to obtain suitable QueryResult objects.
 *
 * @license GNU GPL v2+
 * @since 2.0
 *
 * @author Markus Krötzsch
 * @author mwjames
 */
class QueryEngine implements QueryEngineInterface {

	/**
	 * The name of the SPARQL variable that represents the query result.
	 */
	const RESULT_VARIABLE = 'result';

	/**
	 * @var RepositoryConnection
	 */
	private $connection;

	/**
	 * @var ConditionBuilder
	 */
	private $conditionBuilder;

	/**
	 * @var QueryResultFactory
	 */
	private $queryResultFactory;

	/**
	 * @var EngineOptions
	 */
	private $engineOptions;

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

	/**
	 * @since  2.0
	 *
	 * @param RepositoryConnection $connection
	 * @param ConditionBuilder $conditionBuilder
	 * @param QueryResultFactory $queryResultFactory
	 * @param EngineOptions|null $EngineOptions
	 */
	// @codingStandardsIgnoreStart phpcs, ignore --sniffs=Generic.Files.LineLength
	public function __construct( RepositoryConnection $connection, ConditionBuilder $conditionBuilder, QueryResultFactory $queryResultFactory, EngineOptions $engineOptions = null ) {
	// @codingStandardsIgnoreEnd
		$this->connection = $connection;
		$this->conditionBuilder = $conditionBuilder;
		$this->queryResultFactory = $queryResultFactory;
		$this->engineOptions = $engineOptions;

		if ( $this->engineOptions === null ) {
			$this->engineOptions = new EngineOptions();
		}

		$this->conditionBuilder->setResultVariable( self::RESULT_VARIABLE );
	}

	/**
	 * @since  2.0
	 * @param  Query $query
	 *
	 * @return QueryResult|string
	 */
	public function getQueryResult( Query $query ) {

		if ( ( !$this->engineOptions->get( 'smwgIgnoreQueryErrors' ) || $query->getDescription() instanceof ThingDescription ) &&
		     $query->querymode != Query::MODE_DEBUG &&
		     count( $query->getErrors() ) > 0 ) {
			return $this->queryResultFactory->newEmptyQueryResult( $query, false );
		}

		// don't query, but return something to the printer
		if ( $query->querymode == Query::MODE_NONE || $query->getLimit() < 1 ) {
			return $this->queryResultFactory->newEmptyQueryResult( $query, true );
		}

		$this->sortKeys = $query->sortkeys;
		$this->conditionBuilder->setSortKeys( $this->sortKeys );

		$compoundCondition = $this->conditionBuilder->getConditionFrom(
			$query->getDescription()
		);

		$query->addErrors(
			$this->conditionBuilder->getErrors()
		);

		if ( $query->querymode == Query::MODE_DEBUG ) {
			return $this->getDebugQueryResult( $query, $compoundCondition );
		} elseif ( $query->querymode == Query::MODE_COUNT ) {
			return $this->getCountQueryResult( $query, $compoundCondition );
		}

		return $this->getInstanceQueryResult( $query, $compoundCondition );
	}

	private function getCountQueryResult( Query $query, Condition $compoundCondition ) {

		if ( $this->isSingletonConditionWithElementMatch( $compoundCondition ) ) {
			if ( $compoundCondition->condition === '' ) { // all URIs exist, no querying
				return 1;
			} else {
				$condition = $this->conditionBuilder->convertConditionToString( $compoundCondition );
				$namespaces = $compoundCondition->namespaces;
				$askQueryResult = $this->connection->ask( $condition, $namespaces );

				return $askQueryResult->isBooleanTrue() ? 1 : 0;
			}
		} elseif ( $compoundCondition instanceof FalseCondition ) {
			return 0;
		}

		$condition = $this->conditionBuilder->convertConditionToString( $compoundCondition );
		$namespaces = $compoundCondition->namespaces;
		$this->sortKeys = $this->conditionBuilder->getSortKeys();

		$options = $this->getOptions( $query, $compoundCondition );
		$options['DISTINCT'] = true;

		$repositoryResult = $this->connection->selectCount(
			'?' . self::RESULT_VARIABLE,
			$condition,
			$options,
			$namespaces
		);

		return $this->queryResultFactory->newQueryResult( $repositoryResult, $query );
	}

	private function getInstanceQueryResult( Query $query, Condition $compoundCondition ) {

		if ( $this->isSingletonConditionWithElementMatch( $compoundCondition ) ) {
			$matchElement = $compoundCondition->matchElement;

			if ( $compoundCondition->condition === '' ) { // all URIs exist, no querying
				$results = [  [ $matchElement ] ];
			} else {
				$condition = $this->conditionBuilder->convertConditionToString( $compoundCondition );
				$namespaces = $compoundCondition->namespaces;
				$askQueryResult = $this->connection->ask( $condition, $namespaces );
				$results = $askQueryResult->isBooleanTrue() ? [  [ $matchElement ] ] : [];
			}

			$repositoryResult = new RepositoryResult( [ self::RESULT_VARIABLE => 0 ], $results );

		} elseif ( $compoundCondition instanceof FalseCondition ) {
			$repositoryResult = new RepositoryResult( [ self::RESULT_VARIABLE => 0 ], [] );
		} else {
			$condition = $this->conditionBuilder->convertConditionToString( $compoundCondition );
			$namespaces = $compoundCondition->namespaces;
			$this->sortKeys = $this->conditionBuilder->getSortKeys();

			$options = $this->getOptions( $query, $compoundCondition );
			$options['DISTINCT'] = true;

			$repositoryResult = $this->connection->select(
				'?' . self::RESULT_VARIABLE,
				$condition,
				$options,
				$namespaces
			);
		}

		return $this->queryResultFactory->newQueryResult( $repositoryResult, $query );
	}

	private function getDebugQueryResult( Query $query, Condition $compoundCondition ) {

		$entries = [];

		if ( $this->isSingletonConditionWithElementMatch( $compoundCondition ) ) {
			if ( $compoundCondition->condition === '' ) { // all URIs exist, no querying
				$sparql = 'None (no conditions).';
			} else {
				$condition = $this->conditionBuilder->convertConditionToString( $compoundCondition );
				$namespaces = $compoundCondition->namespaces;
				$sparql = $this->connection->getSparqlForAsk( $condition, $namespaces );
			}
		} elseif ( $compoundCondition instanceof FalseCondition ) {
			$sparql = 'None (conditions can not be satisfied by anything).';
		} else {
			$condition = $this->conditionBuilder->convertConditionToString( $compoundCondition );
			$namespaces = $compoundCondition->namespaces;
			$this->sortKeys = $this->conditionBuilder->getSortKeys();

			$options = $this->getOptions( $query, $compoundCondition );
			$options['DISTINCT'] = true;

			$sparql = $this->connection->getSparqlForSelect(
				'?' . self::RESULT_VARIABLE,
				$condition,
				$options,
				$namespaces
			);
		}

		$entries['SPARQL Query'] = DebugFormatter::prettifySparql( $sparql );

		return DebugFormatter::getStringFrom( 'SPARQLStore', $entries, $query );
	}

	private function isSingletonConditionWithElementMatch( $condition ) {
		return $condition instanceof SingletonCondition && $condition->matchElement instanceof Element;
	}

	/**
	 * Get a SPARQL option array for the given query.
	 *
	 * @param Query $query
	 * @param Condition $compoundCondition (storing order by variable names)
	 *
	 * @return array
	 */
	protected function getOptions( Query $query, Condition $compoundCondition ) {

		$options = [
			'LIMIT' => $query->getLimit() + 1,
			'OFFSET' => $query->getOffset()
		];

		// Build ORDER BY options using discovered sorting fields.
		if ( !$this->engineOptions->isFlagSet( 'smwgQSortFeatures', SMW_QSORT ) || !is_array( $this->sortKeys ) ) {
			return $options;
		}

		$orderByString = '';

		foreach ( $this->sortKeys as $propkey => $order ) {

			if ( !is_string( $propkey ) ) {
				throw new RuntimeException( "Expected a string value as sortkey" );
			}

			if ( ( $order != 'RANDOM' ) && array_key_exists( $propkey, $compoundCondition->orderVariables ) ) {
				$orderByString .= "$order(?" . $compoundCondition->orderVariables[$propkey] . ") ";
			} elseif ( ( $order == 'RANDOM' ) && $this->engineOptions->isFlagSet( 'smwgQSortFeatures', SMW_QSORT_RANDOM ) ) {
				// not supported in SPARQL; might be possible via function calls in some stores
			}
		}

		if ( $orderByString !== '' ) {
			$options['ORDER BY'] = $orderByString;
		}

		return $options;
	}

}