r68123 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r68122‎ | r68123 | r68124 >
Date:14:12, 16 June 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Fixed issue with aliasing resolution
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php
@@ -275,41 +275,52 @@
276276 */
277277 static public function getResultFromQueryString( $querystring, array $params, $extraprintouts, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY ) {
278278 wfProfileIn( 'SMWQueryProcessor::getResultFromQueryString (SMW)' );
 279+
279280 $format = SMWQueryProcessor::getResultFormat( $params );
280281 $query = SMWQueryProcessor::createQuery( $querystring, $params, $context, $format, $extraprintouts );
281282 $result = SMWQueryProcessor::getResultFromQuery( $query, $params, $extraprintouts, $outputmode, $context, $format );
282283 wfProfileOut( 'SMWQueryProcessor::getResultFromQueryString (SMW)' );
 284+
283285 return $result;
284286 }
285287
286288 static public function getResultFromQuery( $query, array $params, $extraprintouts, $outputmode, $context = SMWQueryProcessor::INLINE_QUERY, $format = '' ) {
287289 wfProfileIn( 'SMWQueryProcessor::getResultFromQuery (SMW)' );
 290+
288291 // Query routing allows extensions to provide alternative stores as data sources
289292 // The while feature is experimental and is not properly integrated with most of SMW's architecture. For instance, some query printers just fetch their own store.
290293 ///TODO: case-insensitive
291294 global $smwgQuerySources;
 295+
292296 if ( array_key_exists( "source", $params ) && array_key_exists( $params["source"], $smwgQuerySources ) ) {
293297 $store = new $smwgQuerySources[$params["source"]]();
294298 $query->params = $params; // this is a hack
295299 } else {
296300 $store = smwfGetStore(); // default store
297301 }
 302+
298303 $res = $store->getQueryResult( $query );
299304
300305 if ( ( $query->querymode == SMWQuery::MODE_INSTANCES ) || ( $query->querymode == SMWQuery::MODE_NONE ) ) {
301306 wfProfileIn( 'SMWQueryProcessor::getResultFromQuery-printout (SMW)' );
 307+
302308 if ( $format == '' ) {
303309 $format = SMWQueryProcessor::getResultFormat( $params );
304310 }
 311+
305312 $printer = SMWQueryProcessor::getResultPrinter( $format, $context, $res );
306313 $result = $printer->getResult( $res, $params, $outputmode );
 314+
307315 wfProfileOut( 'SMWQueryProcessor::getResultFromQuery-printout (SMW)' );
308316 wfProfileOut( 'SMWQueryProcessor::getResultFromQuery (SMW)' );
 317+
309318 return $result;
310319 } else { // result for counting or debugging is just a string
311320 if ( array_key_exists( 'intro', $params ) ) $res = str_replace( '_', ' ', $params['intro'] ) . $res;
312321 if ( array_key_exists( 'outro', $params ) ) $res .= str_replace( '_', ' ', $params['outro'] );
 322+
313323 wfProfileOut( 'SMWQueryProcessor::getResultFromQuery (SMW)' );
 324+
314325 return $res . smwfEncodeMessages( $query->getErrors() );
315326 }
316327 }
@@ -319,24 +330,37 @@
320331 */
321332 static protected function getResultFormat( array $params ) {
322333 $format = 'auto';
 334+
323335 if ( array_key_exists( 'format', $params ) ) {
 336+ global $smwgResultFormats;
 337+
324338 $format = strtolower( trim( $params['format'] ) );
325 - global $smwgResultAliases, $smwgResultFormats;
 339+
326340 if ( !array_key_exists( $format, $smwgResultFormats ) ) {
327 - $is_alias = false;
328 - foreach ( $smwgResultAliases as $main_format => $aliases ) {
329 - if ( in_array( $format, $aliases ) ) {
330 - $format = $main_format;
331 - $is_alias = true;
332 - break;
333 - }
334 - }
335 - if ( !$is_alias ) $format = 'auto'; // If it is an unknown format, defaults to list/table again
 341+ $isAlias = self::resolveFormatAliases( $format );
 342+ if ( !$isAlias ) $format = 'auto'; // If it is an unknown format, defaults to list/table again
336343 }
337344 }
 345+
338346 return $format;
339347 }
 348+
 349+ static protected function resolveFormatAliases( &$format ) {
 350+ global $smwgResultAliases;
340351
 352+ $isAlias = false;
 353+
 354+ foreach ( $smwgResultAliases as $mainFormat => $aliases ) {
 355+ if ( in_array( $format, $aliases ) ) {
 356+ $format = $mainFormat;
 357+ $isAlias = true;
 358+ break;
 359+ }
 360+ }
 361+
 362+ return $isAlias;
 363+ }
 364+
341365 /**
342366 * Find suitable SMWResultPrinter for the given format. The context in which the query is to be
343367 * used determines some basic settings of the returned printer object. Possible contexts are
@@ -344,8 +368,13 @@
345369 */
346370 static public function getResultPrinter( $format, $context = SMWQueryProcessor::SPECIAL_PAGE ) {
347371 global $smwgResultFormats;
348 - $formatclass = ( array_key_exists( $format, $smwgResultFormats ) ) ? $smwgResultFormats[$format]:'SMWAutoResultPrinter';
349 - return new $formatclass( $format, ( $context != SMWQueryProcessor::SPECIAL_PAGE ) );
 372+
 373+ self::resolveFormatAliases( $format );
 374+
 375+ // TODO: this seems to contain the same logic as found in getResultFormat - a single function for this might be better.
 376+ $formatClass = array_key_exists( $format, $smwgResultFormats ) ? $smwgResultFormats[$format] : 'SMWAutoResultPrinter';
 377+
 378+ return new $formatClass( $format, ( $context != SMWQueryProcessor::SPECIAL_PAGE ) );
350379 }
351380
352381 }
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r68125Follow up to r68123 - documented functionsjeroendedauw15:14, 16 June 2010

Status & tagging log