Skip to content

Commit

Permalink
Merge pull request #4172 from SemanticMediaWiki/ProcessedParam
Browse files Browse the repository at this point in the history
Stop using long deprecated ParamProcessor stuff
  • Loading branch information
JeroenDeDauw authored Jul 31, 2019
2 parents 05ef91b + 44f0b97 commit fc94526
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 32 deletions.
76 changes: 47 additions & 29 deletions includes/query/SMW_QueryProcessor.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

use ParamProcessor\Options;
use ParamProcessor\Param;
use ParamProcessor\ParamDefinition;
use ParamProcessor\ProcessedParam;
use ParamProcessor\Processor;
use SMW\ApplicationFactory;
use SMW\Message;
use SMW\Parser\RecursiveTextProcessor;
use SMW\Query\Deferred;
use SMW\Query\PrintRequest;
Expand All @@ -14,6 +13,8 @@
use SMW\Query\QueryContext;
use SMW\Query\Exception\ResultFormatNotFoundException;
use SMW\Query\ResultFormat;
use SMW\Query\ResultPrinter;
use SMW\Query\ResultPrinters\NullResultPrinter;

/**
* This file contains a static class for accessing functions to generate and execute
Expand Down Expand Up @@ -52,26 +53,35 @@ public static function setRecursiveTextProcessor( RecursiveTextProcessor $recurs
* param name (string) => param value (mixed)
*
* @since 1.6.2
* The return value changed in SMW 1.8 from an array with result values
* to an array with Param objects.
* The return value changed in SMW 3.1 from Param[] to ProcessedParam[]
*
* @param array $params
* @param array $printRequests
* @param PrintRequest[] $printRequests
* @param boolean $unknownInvalid
*
* @return Param[]
* @return ProcessedParam[]
*/
public static function getProcessedParams( array $params, array $printRequests = [], $unknownInvalid = true, $context = null, $showMode = false ) {
$validator = self::getValidatorForParams( $params, $printRequests, $unknownInvalid, $context, $showMode );
$validator->processParameters();
$parameters = $validator->getParameters();

// Negates some weird behaviour of ParamDefinition::setDefault used in
// an individual printer.
// Applying $smwgQMaxLimit, $smwgQMaxInlineLimit will happen at a later
// stage.
$validator = self::getProcessorForParams( $params, $printRequests, $unknownInvalid, $context, $showMode );
$processingResult = $validator->processParameters();

$parameters = [];

foreach ( $processingResult->getParameters() as $parameter ) {
$parameters[$parameter->getName()] = $parameter;
}

// Set limit parameter to its original value.
// This is to allow for special handling of invalid values rather than just setting the parameter to its default.
// $smwgQMaxLimit and $smwgQMaxInlineLimit will be applied at a later point.
if ( isset( $params['limit'] ) && isset( $parameters['limit'] ) ) {
$parameters['limit']->setValue( (int)$params['limit'] );
$parameters['limit'] = new ProcessedParam(
'limit',
(int)$params['limit'],
false,
'limit',
(int)$params['limit']
);
}

return $parameters;
Expand Down Expand Up @@ -228,7 +238,9 @@ static public function processFunctionParams( array $rawParams, &$querystring, &
* @return array( string, array( string => string ), array( SMWPrintRequest ) )
*/
static public function getComponentsFromFunctionParams( array $rawParams, $showMode ) {

/**
* @var ParamListProcessor $paramListProcessor
*/
$paramListProcessor = ApplicationFactory::getInstance()->singleton( 'ParamListProcessor' );

return $paramListProcessor->format(
Expand All @@ -252,7 +264,7 @@ static public function getComponentsFromFunctionParams( array $rawParams, $showM
* @param integer $outputMode SMW_OUTPUT_WIKI, SMW_OUTPUT_HTML, ...
* @param integer $context INLINE_QUERY, SPECIAL_PAGE, CONCEPT_DESC
* @param boolean $showMode process like #show parser function?
* @return array( SMWQuery, array of IParam )
* @return array( SMWQuery, ProcessedParam[] )
*/
static public function getQueryAndParamsFromFunctionParams( array $rawParams, $outputMode, $context, $showMode, $contextPage = null ) {
list( $queryString, $params, $printouts ) = self::getComponentsFromFunctionParams( $rawParams, $showMode );
Expand Down Expand Up @@ -436,7 +448,7 @@ public static function getResultFromQuery( SMWQuery $query, array $params, $outp
* @param $context
*
* @return SMWResultPrinter
* @throws MissingResultFormatException
* @throws ResultFormatNotFoundException
*/
static public function getResultPrinter( $format, $context = self::SPECIAL_PAGE ) {
global $smwgResultFormats;
Expand All @@ -449,6 +461,9 @@ static public function getResultPrinter( $format, $context = self::SPECIAL_PAGE

$formatClass = $smwgResultFormats[$format];

/**
* @var SMWResultPrinter $printer
*/
$printer = new $formatClass( $format, ( $context != self::SPECIAL_PAGE ) );

if ( self::$recursiveTextProcessor === null ) {
Expand All @@ -471,7 +486,7 @@ static public function getResultPrinter( $format, $context = self::SPECIAL_PAGE
* @param integer|null $context
* @param ResultPrinter|null $resultPrinter
*
* @return IParamDefinition[]
* @return ParamDefinition[]
*/
public static function getParameters( $context = null, $resultPrinter = null ) {
return DefaultParamDefinition::getParamDefinitions( $context, $resultPrinter );
Expand All @@ -495,7 +510,7 @@ public static function getFormatParameters( $format ) {

$resultPrinter = self::getResultPrinter( $format );

if ( $resultPrinter instanceof \SMW\Query\ResultPrinters\NullResultPrinter ) {
if ( $resultPrinter instanceof NullResultPrinter ) {
return [];
}

Expand All @@ -509,28 +524,31 @@ public static function getFormatParameters( $format ) {
* and sets them on a new Validator object,
* which is returned and ready to process the parameters.
*
* @since 1.8
*
* @param array $params
* @param array $printRequests
* @param PrintRequest[] $printRequests
* @param boolean $unknownInvalid
*
* @return Processor
*/
private static function getValidatorForParams( array $params, array $printRequests = [], $unknownInvalid = true, $context = null, $showMode = false ) {
private static function getProcessorForParams( array $params, array $printRequests = [], $unknownInvalid = true, $context = null, $showMode = false ) {
$paramDefinitions = self::getParameters( $context );

$paramDefinitions['format']->setPrintRequests( $printRequests );
$paramDefinitions['format']->setShowMode( $showMode );
/**
* @var ResultFormat $formatParameter
*/
$formatParameter = $paramDefinitions['format'];

$formatParameter->setPrintRequests( $printRequests );
$formatParameter->setShowMode( $showMode );

$processorOptions = new Options();
$processorOptions->setUnknownInvalid( $unknownInvalid );

$validator = Processor::newFromOptions( $processorOptions );
$processor = Processor::newFromOptions( $processorOptions );

$validator->setParameters( $params, $paramDefinitions, false );
$processor->setParameters( $params, $paramDefinitions );

return $validator;
return $processor;
}

}
4 changes: 2 additions & 2 deletions src/Query/Processor/DefaultParamDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DefaultParamDefinition {
* @param integer|null $context
* @param ResultPrinter|null $resultPrinter
*
* @return IParamDefinition[]
* @return ParamDefinition[]
*/
public static function getParamDefinitions( $context = null, ResultPrinter $resultPrinter = null ) {
return self::buildParamDefinitions( $GLOBALS, $context, $resultPrinter );
Expand All @@ -47,7 +47,7 @@ public static function getParamDefinitions( $context = null, ResultPrinter $resu
* @param integer|null $context
* @param ResultPrinter|null $resultPrinter
*
* @return IParamDefinition[]
* @return ParamDefinition[]
*/
public static function buildParamDefinitions( $vars, $context = null, ResultPrinter $resultPrinter = null ) {
$params = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Query/ResultFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use ParamProcessor\Definition\StringParam;
use ParamProcessor\IParam;
use SMW\Query\PrintRequest;
use ParamProcessor\IParamDefinition;
use SMWQueryProcessor as QueryProcessor;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Query/ResultPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SMW\Query;

use ParamProcessor\ParamDefinition;
use SMWQueryResult as QueryResult;

/**
Expand Down

0 comments on commit fc94526

Please sign in to comment.