Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using long deprecated ParamProcessor stuff #4172

Merged
merged 1 commit into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt particular uneasy about this alteration knowing the limit setting has caused a regression in the past therefore I devised an integration test #4187 that would fail if the limit where causing some unexpected changes due to use of ParamDefinition::setDefault.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As expected from the Hero Of The Tests ;)

'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