-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCompoundQueryProcessor.php
More file actions
290 lines (238 loc) · 7.51 KB
/
CompoundQueryProcessor.php
File metadata and controls
290 lines (238 loc) · 7.51 KB
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 SCQ;
use SMW\Query\QueryResult;
use SMWQueryProcessor as QueryProcessor;
use SMWQuery as Query;
use Parser;
/**
* Class that holds static functions for handling compound queries.
* This class inherits from Semantic MediaWiki's QueryProcessor.
*
* @license GNU GPL v2+
* @since 1.0
*
* @author Yaron Koren
* @author Peter Grassberger < petertheone@gmail.com >
*/
class CompoundQueryProcessor extends QueryProcessor {
/**
* Comparison helper function, used in sorting results.
*/
public static function compareQueryResults( $a, $b ) {
if ( $a->getSerialization() == $b->getSerialization() ) {
return 0;
}
return ( $a->getSerialization() < $b->getSerialization() ) ? -1 : 1;
}
/**
* Handler for the #compound_query parser function.
*
* @param Parser $parser
*
* @return string
*/
public static function doCompoundQuery( Parser &$parser ) {
global $smwgQEnabled, $smwgIQRunningNumber;
if ( !$smwgQEnabled ) {
return smwfEncodeMessages( [ wfMessage( 'smw_iq_disabled' )->inContentLanguage()->text() ] );
}
$smwgIQRunningNumber++;
$params = func_get_args();
array_shift( $params ); // We already know the $parser.
list( $queryParams, $otherParams ) = self::separateParams( $params );
list( $queryResult, $otherParams ) = self::queryAndMergeResults( $queryParams, $otherParams );
return self::getResultFromQueryResult(
$queryResult,
$otherParams,
SMW_OUTPUT_WIKI
);
}
/**
* Separates $queryParams from $otherParams.
*
* @param $params
* @return array
*/
public static function separateParams( $params ) {
$queryParams = [];
$otherParams = [];
foreach ( $params as $param ) {
// Very primitive heuristic - if the parameter
// includes a square bracket, then it's a
// sub-query; otherwise it's a regular parameter.
if ( strpos( $param, '[' ) !== false ) {
$queryParams[] = $param;
} else {
$parts = explode( '=', $param, 2 );
if ( count( $parts ) >= 2 ) {
$otherParams[strtolower( trim( $parts[0] ) )] = $parts[1]; // don't trim here, some params care for " "
}
}
}
return [ $queryParams, $otherParams ];
}
/**
* Query and merge results of subqueries.
*
* @param $queryParams
* @param $otherParams
* @return array
*/
public static function queryAndMergeResults( $queryParams, $otherParams ) {
$results = [];
$printRequests = [];
foreach ( $queryParams as $param ) {
$subQueryParams = self::getSubParams( $param );
if ( array_key_exists( 'format', $otherParams ) && !array_key_exists( 'format', $subQueryParams ) ) {
$subQueryParams['format'] = $otherParams['format'];
}
$nextResult = self::getQueryResultFromFunctionParams($subQueryParams);
$results = self::mergeSMWQueryResults( $results, $nextResult->getResults() );
$printRequests = self::mergeSMWPrintRequests( $printRequests, $nextResult->getPrintRequests() );
}
// Sort results so that they'll show up by page name
if( !isset($otherParams['unsorted']) || !strcmp( $otherParams['unsorted'], 'on' ) ) {
uasort( $results, [ '\SCQ\CompoundQueryProcessor', 'compareQueryResults' ] );
}
$queryResult = new CompoundQueryResult( $printRequests, new Query(), $results, smwfGetStore() );
if ( version_compare( SMW_VERSION, '1.6.1', '>' ) ) {
self::addThisPrintout( $printRequests, $otherParams );
$otherParams = self::getProcessedParams( $otherParams, $printRequests );
}
return [ $queryResult, $otherParams ];
}
/**
* An alternative to explode() - that function won't work here,
* because we don't want to split the string on all semicolons, just
* the ones that aren't contained within square brackets
*
* @param string $param
*
* @return array
*/
protected static function getSubParams( $param ) {
$sub_params = [];
$sub_param = '';
$uncompleted_square_brackets = 0;
for ( $i = 0; $i < strlen( $param ); $i++ ) {
$c = $param[$i];
if ( ( $c == ';' ) && ( $uncompleted_square_brackets <= 0 ) ) {
$sub_params[] = trim( $sub_param );
$sub_param = '';
} else {
$sub_param .= $c;
if ( $c == '[' ) {
$uncompleted_square_brackets++;
}
elseif ( $c == ']' ) {
$uncompleted_square_brackets--;
}
}
}
$sub_params[] = trim( $sub_param );
return $sub_params;
}
/**
* @param $rawparams
* @param $context
* @param $showmode
*
* @return QueryResult
*/
protected static function getQueryResultFromFunctionParams( $rawParams, $context = QueryProcessor::INLINE_QUERY, $showMode = false ) {
list( $querystring, $params, $printouts ) = self::getComponentsFromFunctionParams( $rawParams, $showMode );
return self::getQueryResultFromQueryString( $querystring, $params, $printouts, $context );
}
/**
* Combine two arrays of SMWWikiPageValue objects into one
*
* @param array $result1
* @param array $result2
*
* @return array
*/
protected static function mergeSMWQueryResults( $result1, $result2 ) {
if ( $result1 == null ) {
return $result2;
}
$existing_page_names = [];
foreach ( $result1 as $r1 ) {
$existing_page_names[] = $r1->getSerialization();
}
foreach ( $result2 as $r2 ) {
$page_name = $r2->getSerialization();
if ( ! in_array( $page_name, $existing_page_names ) ) {
$result1[] = $r2;
}
}
return $result1;
}
protected static function mergeSMWPrintRequests( $printRequests1, $printRequests2 ) {
$existingPrintoutLabels = [];
foreach ( $printRequests1 as $p1 ) {
$existingPrintoutLabels[] = $p1->getLabel();
}
foreach ( $printRequests2 as $p2 ) {
$label = $p2->getLabel();
if ( ! in_array( $label, $existingPrintoutLabels ) ) {
$printRequests1[] = $p2;
}
}
return $printRequests1;
}
/**
* @param $querystring
* @param array $params
* @param $extraPrintouts
* @param $outputMode
* @param $context
*
* @return QueryResult
*/
protected static function getQueryResultFromQueryString( $querystring, array $params, $extraPrintouts, $context = QueryProcessor::INLINE_QUERY ) {
if ( version_compare( SMW_VERSION, '1.6.1', '>' ) ) {
QueryProcessor::addThisPrintout( $extraPrintouts, $params );
$params = self::getProcessedParams( $params, $extraPrintouts, false );
}
$query = self::createQuery( $querystring, $params, $context, null, $extraPrintouts );
$queryResult = smwfGetStore()->getQueryResult( $query );
$parameters = [];
if ( version_compare( SMW_VERSION, '1.7.2', '>' ) ) {
foreach ( $params as $param ) {
$parameters[$param->getName()] = $param->getValue();
}
}
else {
$parameters = $params;
}
foreach ( $queryResult->getResults() as $wikiPage ) {
$wikiPage->display_options = $parameters;
}
return $queryResult;
}
/**
* Matches getResultFromQueryResult() from SMWQueryProcessor,
* except that formats of type 'debug' and 'count' aren't handled.
*
* @param CompoundQueryResult $res
* @param array $params These need to be the result of a list fed to getProcessedParams as of SMW 1.6.2
* @param $outputmode
* @param $context
* @param string $format
*
* @return string
*/
protected static function getResultFromQueryResult( CompoundQueryResult $res, array $params, $outputmode, $context = QueryProcessor::INLINE_QUERY, $format = '' ) {
if ( version_compare( SMW_VERSION, '1.6.1', '>' ) ) {
$format = $params['format'];
if ( version_compare( SMW_VERSION, '1.7.2', '>' ) ) {
$format = $format->getValue();
}
} else {
$format = self::getResultFormat( $params );
}
$printer = self::getResultPrinter( $format, $context );
$result = $printer->getResult( $res, $params, $outputmode );
return $result;
}
}