summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Search/QueryBuilder.php
blob: 34b2a23802833eb76368065674c8929b30002313 (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
<?php

namespace SMW\MediaWiki\Search;

use SMW\MediaWiki\Search\Form\FormsBuilder;
use SMW\Query\Language\Conjunction;
use SMW\Query\Language\Disjunction;
use SMW\Query\Language\NamespaceDescription;
use SMW\Query\Parser\TermParser;
use SMW\Store;
use SMWQuery as Query;
use SMWQueryProcessor as QueryProcessor;
use Title;
use WebRequest;
use WikiPage;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class QueryBuilder {

	/**
	 * @var WebRequest
	 */
	private $request;

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

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

	/**
	 * @since 3.0
	 *
	 * @param WebRequest|null $request
	 * @param array|null $data
	 */
	public function __construct( WebRequest $request = null, array $data = [] ) {
		$this->request = $request;
		$this->data = $data;

		if ( $this->request === null ) {
			$this->request = $GLOBALS['wgRequest'];
		}
	}

	/**
	 * @since 3.0
	 *
	 * @param string $term
	 *
	 * @return Query|null
	 */
	public function getQuery( $term ) {

		if ( !is_string( $term ) || trim( $term ) === '' ) {
			return null;
		}

		if ( !array_key_exists( $term, $this->queryCache ) ) {

			$params = QueryProcessor::getProcessedParams( [] );
			$query = QueryProcessor::createQuery( $term, $params );

			$description = $query->getDescription();

			if ( $description === null || is_a( $description, 'SMWThingDescription' ) ) {
				$query = null;
			}

			$this->queryCache[$term] = $query;
		}

		return $this->queryCache[$term];
	}

	/**
	 * @since 3.0
	 *
	 * @param Query $query
	 * @param array $searchableNamespaces
	 */
	public function addNamespaceCondition( Query $query = null, $searchableNamespaces = [] ) {

		if ( $query === null ) {
			return;
		}

		$namespaces = [];

		foreach ( $searchableNamespaces as $ns => $name ) {
			if ( $this->request->getCheck( 'ns' . $ns ) ) {
				$namespaces[] = $ns;
			}
		}

		$namespacesDisjunction = new Disjunction(
			array_map( function ( $ns ) {
				return new NamespaceDescription( $ns );
			}, $namespaces )
		);

		$description = new Conjunction( [ $query->getDescription(), $namespacesDisjunction ] );
		$query->setDescription( $description );
	}

	/**
	 * @since 3.0
	 *
	 * @param Query $query
	 */
	public function addSort( Query $query = null ) {

		if ( $query === null ) {
			return;
		}

		// @see SortForm
		$sort = $this->request->getVal( 'sort' );

		if ( $sort === 'recent' ) {
			$query->setSortKeys( [ '_MDAT' => 'desc' ] );
		} elseif ( $sort === 'title' ) {
			$query->setSortKeys( [ '' => 'asc' ] );
		} else {
			// Sort by score/relevance if it is supported otherwise the default
			// by title sort will be used instead.
			$query->setOption( Query::SCORE_SORT, 'desc' );
		}
	}

	/**
	 * @since 3.0
	 *
	 * @return []
	 */
	public function getQueryString( Store $store, $term ) {

		// Special invisible char which is set by the JS component to allow to
		// push a forms submit through the SearchEngine without an actual "search
		// term" to avoid being blocked on an empty request which only contains
		// structured searches.
		$term = rtrim( $term, " " );
		$prefix_map = [];

		if ( $this->data === [] ) {
			$data = SearchProfileForm::getFormDefinitions( $store );
		} else {
			$data = $this->data;
		}

		if ( isset( $data['term_parser']['prefix'] ) && $data['term_parser']['prefix'] ) {
			$prefix_map = (array)$data['term_parser']['prefix'];
		}

		$termParser = new TermParser( $prefix_map );
		$term = $termParser->parse( $term );

		$form = $this->request->getVal( 'smw-form' );

		if ( ( $data = $this->fetchFieldValues( $form, $data ) ) === [] && trim( $term ) ) {
			return $term;
		}

		$queryString = '';
		$lastOr = '';

		foreach ( $data as $key => $values ) {

			if ( !is_array( $values ) ) {
				continue;
			}

			foreach ( $values as $k => $value ) {

				if ( !isset( $value[0] ) || $value[0] === '' ) {
					continue;
				}

				$val = $value[0];
				$op = strtolower( $value[1] ) === 'or' ? ' OR ' : '';

				$queryString .= "[[$key::$val]]$op";
			}
		}

		// Remove last OR to ensure <q></q> has no open OR expression
		if ( substr( $queryString, -3 ) === 'OR ' ) {
			$lastOr = $term !== '' ? 'OR' : '';
			$queryString = substr( $queryString, 0, -3 );
		}

		if ( $queryString === '' ) {
			return $term;
		}

		return "<q>$queryString</q> $lastOr $term";
	}

	/**
	 * @since 3.0
	 *
	 * @param string $form
	 * @param array $data
	 *
	 * @return []
	 */
	public function fetchFieldValues( $form, array $data ) {

		$fieldValues = [];

		if ( !isset( $data['forms'] ) ) {
			return [];
		}

		if ( $form === 'open' ) {
			$properties = $this->request->getArray( 'property' );
			$pvalues = $this->request->getArray( 'pvalue' );
			$op = $this->request->getArray( 'op' );

			foreach ( $properties as $i => $property ) {

				if ( !isset( $fieldValues[$property] ) ) {
					$fieldValues[$property] = [];
				}

				$fieldValues[$property][] = [ $pvalues[$i], $op[$i] ];
			}

			return $fieldValues;
		}

		$fieldsCounter = [];

		foreach ( $data['forms'] as $key => $value ) {

			// @see FormsBuilder
			$k = FormsBuilder::toLowerCase( $key );

			foreach ( $value as $property ) {

				if ( is_array( $property ) ) {
					foreach ( $property as $p => $options ) {
						$property = $p;
					}
				}

				$name = FormsBuilder::toLowerCase( $property );

				if ( !isset( $fieldsCounter[$name] ) ) {
					$fieldsCounter[$name] = 0;
				} else {
					$fieldsCounter[$name]++;
				}

				if ( $form !== $k ) {
					continue;
				}

				$vals = $this->request->getArray(
					FormsBuilder::toLowerCase( $property )
				);

				if ( !isset( $vals[$fieldsCounter[$name]] ) ) {
					continue;
				}

				$val = $vals[$fieldsCounter[$name]];

				// Conditions from custom forms are conjunctive
				$fieldValues[$property][] = [ $val, 'and' ];
			}
		}

		return $fieldValues;
	}



}