r109807 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109806‎ | r109807 | r109808 >
Date:05:18, 23 January 2012
Author:yaron
Status:deferred
Tags:
Comment:
Fixed handling of different print requests, via new method, mergeSMWPrintRequests(); also improved some tabbing
Modified paths:
  • /trunk/extensions/SemanticCompoundQueries/SCQ_QueryProcessor.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticCompoundQueries/SCQ_QueryProcessor.php
@@ -29,7 +29,7 @@
3030 */
3131 public static function doCompoundQuery( Parser &$parser ) {
3232 global $smwgQEnabled, $smwgIQRunningNumber;
33 -
 33+
3434 if ( !$smwgQEnabled ) {
3535 return smwfEncodeMessages( array( wfMsgForContent( 'smw_iq_disabled' ) ) );
3636 }
@@ -41,6 +41,7 @@
4242
4343 $other_params = array();
4444 $results = array();
 45+ $printRequests = array();
4546 $queryParams = array();
4647
4748 foreach ( $params as $param ) {
@@ -57,40 +58,40 @@
5859 }
5960 }
6061 }
61 -
 62+
6263 foreach ( $queryParams as $param ) {
6364 $subQueryParams = self::getSubParams( $param );
64 -
 65+
6566 if ( array_key_exists( 'format', $other_params ) && !array_key_exists( 'format', $subQueryParams ) ) {
6667 $subQueryParams['format'] = $other_params['format'];
6768 }
68 -
 69+
6970 $next_result = self::getQueryResultFromFunctionParams(
7071 $subQueryParams,
7172 SMW_OUTPUT_WIKI
7273 );
7374
7475 $results = self::mergeSMWQueryResults( $results, $next_result->getResults() );
 76+ $printRequests = self::mergeSMWPrintRequests( $printRequests, $next_result->getPrintRequests() );
7577 }
7678
7779 // Sort results so that they'll show up by page name
7880 uasort( $results, array( 'SCQQueryProcessor', 'compareQueryResults' ) );
7981
80 - $query_result = new SCQQueryResult( $next_result->getPrintRequests(), new SMWQuery(), $results, smwfGetStore() );
81 -
 82+ $query_result = new SCQQueryResult( $printRequests, new SMWQuery(), $results, smwfGetStore() );
 83+
8284 if ( version_compare( SMW_VERSION, '1.6.1', '>' ) ) {
83 - $printouts = $next_result->getPrintRequests();
84 - SMWQueryProcessor::addThisPrintout( $printouts, $other_params );
85 - $other_params = parent::getProcessedParams( $other_params, $printouts );
 85+ SMWQueryProcessor::addThisPrintout( $printRequests, $other_params );
 86+ $other_params = parent::getProcessedParams( $other_params, $printRequests );
8687 }
87 -
 88+
8889 return self::getResultFromQueryResult(
8990 $query_result,
9091 $other_params,
9192 SMW_OUTPUT_WIKI
9293 );
93 - }
94 -
 94+ }
 95+
9596 /**
9697 * An alternative to explode() - that function won't work here,
9798 * because we don't want to split the string on all semicolons, just
@@ -104,28 +105,28 @@
105106 $sub_params = array();
106107 $sub_param = '';
107108 $uncompleted_square_brackets = 0;
108 -
 109+
109110 for ( $i = 0; $i < strlen( $param ); $i++ ) {
110111 $c = $param[$i];
111 -
 112+
112113 if ( ( $c == ';' ) && ( $uncompleted_square_brackets <= 0 ) ) {
113114 $sub_params[] = trim( $sub_param );
114115 $sub_param = '';
115116 } else {
116117 $sub_param .= $c;
117 -
 118+
118119 if ( $c == '[' ) {
119120 $uncompleted_square_brackets++;
120121 }
121 -
 122+
122123 elseif ( $c == ']' ) {
123124 $uncompleted_square_brackets--;
124125 }
125126 }
126127 }
127 -
 128+
128129 $sub_params[] = trim( $sub_param );
129 -
 130+
130131 return $sub_params;
131132 }
132133
@@ -138,6 +139,7 @@
139140 * @return SMWQueryResult
140141 */
141142 protected static function getQueryResultFromFunctionParams( $rawparams, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY, $showmode = false ) {
 143+ $printouts = null;
142144 self::processFunctionParams( $rawparams, $querystring, $params, $printouts, $showmode );
143145 return self::getQueryResultFromQueryString( $querystring, $params, $printouts, SMW_OUTPUT_WIKI, $context );
144146 }
@@ -154,9 +156,8 @@
155157 if ( $result1 == null ) {
156158 return $result2;
157159 }
158 -
159160 $existing_page_names = array();
160 -
 161+
161162 foreach ( $result1 as $r1 ) {
162163 // SMW 1.6+
163164 if ( $r1 instanceof SMWDIWikiPage ) {
@@ -165,22 +166,37 @@
166167 $existing_page_names[] = $r1->getWikiValue();
167168 }
168169 }
169 -
 170+
170171 foreach ( $result2 as $r2 ) {
171 - if ( $r1 instanceof SMWDIWikiPage ) {
 172+ if ( $r2 instanceof SMWDIWikiPage ) {
172173 $page_name = $r2->getDBkey();
173174 } else {
174175 $page_name = $r2->getWikiValue();
175176 }
176 -
 177+
177178 if ( ! in_array( $page_name, $existing_page_names ) ) {
178179 $result1[] = $r2;
179180 }
180181 }
181 -
 182+
182183 return $result1;
183184 }
184185
 186+ protected static function mergeSMWPrintRequests( $printRequests1, $printRequests2 ) {
 187+ $existingPrintoutLabels = array();
 188+ foreach ( $printRequests1 as $p1 ) {
 189+ $existingPrintoutLabels[] = $p1->getLabel();
 190+ }
 191+
 192+ foreach ( $printRequests2 as $p2 ) {
 193+ $label = $p2->getLabel();
 194+ if ( ! in_array( $label, $existingPrintoutLabels ) ) {
 195+ $printRequests1[] = $p2;
 196+ }
 197+ }
 198+ return $printRequests1;
 199+ }
 200+
185201 /**
186202 * @param $querystring
187203 * @param array $params
@@ -192,24 +208,24 @@
193209 */
194210 protected static function getQueryResultFromQueryString( $querystring, array $params, $extraprintouts, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY ) {
195211 wfProfileIn( 'SCQQueryProcessor::getQueryResultFromQueryString' );
196 -
 212+
197213 if ( version_compare( SMW_VERSION, '1.6.1', '>' ) ) {
198214 SMWQueryProcessor::addThisPrintout( $extraprintouts, $params );
199215 $params = self::getProcessedParams( $params, $extraprintouts, false );
200216 }
201 -
 217+
202218 $query = self::createQuery( $querystring, $params, $context, null, $extraprintouts );
203219 $query_result = smwfGetStore()->getQueryResult( $query );
204 -
 220+
205221 foreach ( $query_result->getResults() as $wiki_page ) {
206222 $wiki_page->display_options = $params;
207223 }
208224
209225 wfProfileOut( 'SCQQueryProcessor::getQueryResultFromQueryString' );
210 -
 226+
211227 return $query_result;
212228 }
213 -
 229+
214230 /**
215231 * Matches getResultFromQueryResult() from SMWQueryProcessor,
216232 * except that formats of type 'debug' and 'count' aren't handled.
@@ -231,13 +247,13 @@
232248 else {
233249 $format = self::getResultFormat( $params );
234250 }
235 -
 251+
236252 $printer = self::getResultPrinter( $format, $context, $res );
237253 $result = $printer->getResult( $res, $params, $outputmode );
238 -
 254+
239255 wfProfileOut( 'SCQQueryProcessor::getResultFromQueryResult' );
240 -
 256+
241257 return $result;
242258 }
243 -
 259+
244260 }