summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Query/Parser/DescriptionProcessor.php
blob: 45af6deb16be4677e3aa6458f104b5e2f3fe1718 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php

namespace SMW\Query\Parser;

use SMW\DataValueFactory;
use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\Message;
use SMW\Query\DescriptionFactory;
use SMW\Query\Language\Conjunction;
use SMW\Query\Language\Description;
use SMW\Query\Language\Disjunction;
use SMW\Query\Language\ValueDescription;
use SMW\Site;
use SMWDataValue as DataValue;
use SMW\Query\QueryComparator;

/**
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 * @author Markus Krötzsch
 */
class DescriptionProcessor {

	/**
	 * @var DataValueFactory
	 */
	private $dataValueFactory;

	/**
	 * @var DescriptionFactory
	 */
	private $descriptionFactory;

	/**
	 * @var integer
	 */
	private $queryFeatures;

	/**
	 * @var DIWikiPage|null
	 */
	private $contextPage;

	/**
	 * @var boolean
	 */
	private $selfReference = false;

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

	/**
	 * @since 2.4
	 *
	 * @param integer $queryFeatures
	 */
	public function __construct( $queryFeatures = false ) {
		$this->queryFeatures = $queryFeatures === false ? $GLOBALS['smwgQFeatures'] : $queryFeatures;
		$this->dataValueFactory = DataValueFactory::getInstance();
		$this->descriptionFactory = new DescriptionFactory();
	}

	/**
	 * @since 2.4
	 *
	 * @param DIWikiPage|null $contextPage
	 */
	public function setContextPage( DIWikiPage $contextPage = null ) {
		$this->contextPage = $contextPage;
	}

	/**
	 * @since 2.4
	 */
	public function clear() {
		$this->errors = [];
		$this->selfReference = false;
	}

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

	/**
	 * @since 3.0
	 *
	 * @return boolean
	 */
	public function containsSelfReference() {
		return $this->selfReference;
	}

	/**
	 * @since 2.4
	 *
	 * @param array|string $error
	 */
	public function addError( $error ) {

		if ( !is_array( $error ) ) {
			$error = (array)$error;
		}

		if ( $error !== [] ) {
			$this->errors[] = Message::encode( $error );
		}
	}

	/**
	 * @since 2.4
	 *
	 * @param string $msgKey
	 */
	public function addErrorWithMsgKey( $msgKey /*...*/ ) {
		$this->errors[] = Message::encode( func_get_args() );
	}

	/**
	 * @since 2.4
	 *
	 * @param DIProperty $property
	 * @param string $chunk
	 *
	 * @return Description|null
	 */
	public function newDescriptionForPropertyObjectValue( DIProperty $property, $chunk ) {

		$dataValue = $this->dataValueFactory->newDataValueByProperty( $property );
		$dataValue->setContextPage( $this->contextPage );

		// Indicates whether a value is being used by a query condition or not which
		// can lead to a modified validation of a value.
		$dataValue->setOption( DataValue::OPT_QUERY_CONTEXT, true );
		$dataValue->setOption( 'isCapitalLinks', Site::isCapitalLinks() );

		$description = $dataValue->getQueryDescription( $chunk );
		$this->addError( $dataValue->getErrors() );

		return $description;
	}

	/**
	 * @since 2.4
	 *
	 * @param string $chunk
	 *
	 * @return Description|null
	 */
	public function newDescriptionForWikiPageValueChunk( $chunk ) {

		// Only create a simple WpgValue to initiate the query description target
		// operation. If the chunk contains something like "≤Issue/1220" then the
		// WpgValue would return with an error as it cannot parse ≤ as/ legal
		// character, the chunk itself is processed by
		// DataValue::getQueryDescription hence no need to use it as input for
		// the factory instance
		$dataValue = $this->dataValueFactory->newTypeIDValue( '_wpg', 'QP_WPG_TITLE' );
		$dataValue->setContextPage( $this->contextPage );

		$dataValue->setOption( DataValue::OPT_QUERY_CONTEXT, true );

		// #3587
		// Requesting capital links is influenced by two factors, `wgCapitalLinks`
		// is enabled sitewide and the `WikiPageValue` condition is identified
		// as SMW_CMP_EQ/NEQ (e.g. [[Foo]], [[!Foo]]) with other expressions
		// (e.g. [[~foo*]]) to remain in the form of the user input
		$queryComparator = QueryComparator::getInstance();

		if ( Site::isCapitalLinks() && (
			$queryComparator->containsComparator( $chunk, SMW_CMP_EQ ) ||
			$queryComparator->containsComparator( $chunk, SMW_CMP_NEQ ) ) ) {
			$dataValue->setOption( 'isCapitalLinks', true );
		}

		$description = null;

		$description = $dataValue->getQueryDescription( $chunk );
		$this->addError( $dataValue->getErrors() );

		if ( !$this->selfReference && $this->contextPage !== null && $description instanceof ValueDescription ) {
			$this->selfReference = $description->getDataItem()->equals( $this->contextPage );
		}

		return $description;
	}

	/**
	 * The method was supposed to be named just `or` and `and` but this works
	 * only on PHP 7.1 therefore ...
	 */

	/**
	 * @since 2.4
	 *
	 * @param Description|null $currentDescription
	 * @param Description|null $newDescription
	 *
	 * @return Description|null
	 */
	public function asOr( Description $currentDescription = null, Description $newDescription = null ) {
		return $this->newCompoundDescription( $currentDescription, $newDescription, SMW_DISJUNCTION_QUERY );
	}

	/**
	 * @since 2.4
	 *
	 * @param Description|null $currentDescription
	 * @param Description|null $newDescription
	 *
	 * @return Description|null
	 */
	public function asAnd( Description $currentDescription = null, Description $newDescription = null ) {
		return $this->newCompoundDescription( $currentDescription, $newDescription, SMW_CONJUNCTION_QUERY );
	}

	/**
	 * Extend a given description by a new one, either by adding the new description
	 * (if the old one is a container description) or by creating a new container.
	 * The parameter $conjunction determines whether the combination of both descriptions
	 * should be a disjunction or conjunction.
	 *
	 * In the special case that the current description is NULL, the new one will just
	 * replace the current one.
	 *
	 * The return value is the expected combined description. The object $currentDescription will
	 * also be changed (if it was non-NULL).
	 */
	private function newCompoundDescription( Description $currentDescription = null, Description $newDescription = null, $compoundType = SMW_CONJUNCTION_QUERY ) {

		$notallowedmessage = 'smw_noqueryfeature';

		if ( $newDescription instanceof SomeProperty ) {
			$allowed = $this->queryFeatures & SMW_PROPERTY_QUERY;
		} elseif ( $newDescription instanceof ClassDescription ) {
			$allowed = $this->queryFeatures & SMW_CATEGORY_QUERY;
		} elseif ( $newDescription instanceof ConceptDescription ) {
			$allowed = $this->queryFeatures & SMW_CONCEPT_QUERY;
		} elseif ( $newDescription instanceof Conjunction ) {
			$allowed = $this->queryFeatures & SMW_CONJUNCTION_QUERY;
			$notallowedmessage = 'smw_noconjunctions';
		} elseif ( $newDescription instanceof Disjunction ) {
			$allowed = $this->queryFeatures & SMW_DISJUNCTION_QUERY;
			$notallowedmessage = 'smw_nodisjunctions';
		} else {
			$allowed = true;
		}

		if ( !$allowed ) {
			$this->addErrorWithMsgKey( $notallowedmessage, $newDescription->getQueryString() );
			return $currentDescription;
		}

		if ( $newDescription === null ) {
			return $currentDescription;
		} elseif ( $currentDescription === null ) {
			return $newDescription;
		} else { // we already found descriptions
			return $this->newCompoundDescriptionByType( $compoundType, $currentDescription, $newDescription );
		}
	}

	private function newCompoundDescriptionByType( $compoundType, $currentDescription, $newDescription ) {

		if ( ( ( $compoundType & SMW_CONJUNCTION_QUERY ) != 0 && ( $currentDescription instanceof Conjunction ) ) ||
		     ( ( $compoundType & SMW_DISJUNCTION_QUERY ) != 0 && ( $currentDescription instanceof Disjunction ) ) ) { // use existing container
			$currentDescription->addDescription( $newDescription );
			return $currentDescription;
		} elseif ( ( $compoundType & SMW_CONJUNCTION_QUERY ) != 0 ) { // make new conjunction
			return $this->newConjunction( $currentDescription, $newDescription );
		} elseif ( ( $compoundType & SMW_DISJUNCTION_QUERY ) != 0 ) { // make new disjunction
			return $this->newDisjunction( $currentDescription, $newDescription );
		}
	}

	private function newConjunction( $currentDescription, $newDescription ) {

		if ( $this->queryFeatures & SMW_CONJUNCTION_QUERY ) {
			return $this->descriptionFactory->newConjunction( [ $currentDescription, $newDescription ] );
		}

		$this->addErrorWithMsgKey( 'smw_noconjunctions', $newDescription->getQueryString() );

		return $currentDescription;
	}

	private function newDisjunction( $currentDescription, $newDescription ) {

		if ( $this->queryFeatures & SMW_DISJUNCTION_QUERY ) {
			return $this->descriptionFactory->newDisjunction( [ $currentDescription, $newDescription ] );
		}

		$this->addErrorWithMsgKey( 'smw_nodisjunctions', $newDescription->getQueryString() );

		return $currentDescription;
	}

}