-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCQ_QueryProcessor.php
More file actions
263 lines (217 loc) · 7.08 KB
/
SCQ_QueryProcessor.php
File metadata and controls
263 lines (217 loc) · 7.08 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
<?php
/**
* Class that holds static functions for handling compound queries.
* This class inherits from Semantic MediaWiki's SMWQueryProcessor.
*
* @ingroup SemanticCompoundQueries
*
* @author Yaron Koren
*/
class SCQQueryProcessor extends SMWQueryProcessor {
/**
* 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( array( wfMsgForContent( 'smw_iq_disabled' ) ) );
}
$smwgIQRunningNumber++;
$params = func_get_args();
array_shift( $params ); // We already know the $parser.
$other_params = array();
$results = array();
$printRequests = array();
$queryParams = array();
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 ) {
$other_params[strtolower( trim( $parts[0] ) )] = $parts[1]; // don't trim here, some params care for " "
}
}
}
foreach ( $queryParams as $param ) {
$subQueryParams = self::getSubParams( $param );
if ( array_key_exists( 'format', $other_params ) && !array_key_exists( 'format', $subQueryParams ) ) {
$subQueryParams['format'] = $other_params['format'];
}
$next_result = self::getQueryResultFromFunctionParams(
$subQueryParams,
SMW_OUTPUT_WIKI
);
$results = self::mergeSMWQueryResults( $results, $next_result->getResults() );
$printRequests = self::mergeSMWPrintRequests( $printRequests, $next_result->getPrintRequests() );
}
// Sort results so that they'll show up by page name
uasort( $results, array( 'SCQQueryProcessor', 'compareQueryResults' ) );
$query_result = new SCQQueryResult( $printRequests, new SMWQuery(), $results, smwfGetStore() );
if ( version_compare( SMW_VERSION, '1.6.1', '>' ) ) {
SMWQueryProcessor::addThisPrintout( $printRequests, $other_params );
$other_params = parent::getProcessedParams( $other_params, $printRequests );
}
return self::getResultFromQueryResult(
$query_result,
$other_params,
SMW_OUTPUT_WIKI
);
}
/**
* 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 = array();
$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 $outputmode
* @param $context
* @param $showmode
*
* @return SMWQueryResult
*/
protected static function getQueryResultFromFunctionParams( $rawparams, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY, $showmode = false ) {
$printouts = null;
self::processFunctionParams( $rawparams, $querystring, $params, $printouts, $showmode );
return self::getQueryResultFromQueryString( $querystring, $params, $printouts, SMW_OUTPUT_WIKI, $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 = array();
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 = array();
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 SMWQueryResult
*/
protected static function getQueryResultFromQueryString( $querystring, array $params, $extraPrintouts, $outputMode, $context = SMWQueryProcessor::INLINE_QUERY ) {
wfProfileIn( 'SCQQueryProcessor::getQueryResultFromQueryString' );
if ( version_compare( SMW_VERSION, '1.6.1', '>' ) ) {
SMWQueryProcessor::addThisPrintout( $extraPrintouts, $params );
$params = self::getProcessedParams( $params, $extraPrintouts, false );
}
$query = self::createQuery( $querystring, $params, $context, null, $extraPrintouts );
$query_result = smwfGetStore()->getQueryResult( $query );
$parameters = array();
if ( version_compare( SMW_VERSION, '1.7.2', '>' ) ) {
foreach ( $params as $param ) {
$parameters[$param->getName()] = $param->getValue();
}
}
else {
$parameters = $params;
}
foreach ( $query_result->getResults() as $wiki_page ) {
$wiki_page->display_options = $parameters;
}
wfProfileOut( 'SCQQueryProcessor::getQueryResultFromQueryString' );
return $query_result;
}
/**
* Matches getResultFromQueryResult() from SMWQueryProcessor,
* except that formats of type 'debug' and 'count' aren't handled.
*
* @param SCQQueryResult $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( SCQQueryResult $res, array $params, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY, $format = '' ) {
wfProfileIn( 'SCQQueryProcessor::getResultFromQueryResult' );
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, $res );
$result = $printer->getResult( $res, $params, $outputmode );
wfProfileOut( 'SCQQueryProcessor::getResultFromQueryResult' );
return $result;
}
}