jenkins-bot has submitted this change and it was merged. Change subject: Remove $entityId and $parser from PropertyParserFunction constructor params ......................................................................
Remove $entityId and $parser from PropertyParserFunction constructor params Change-Id: Ibeaa953c73f95376c6f5c46f1998c0bb9c95726b --- M client/includes/parserhooks/PropertyParserFunction.php M client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php 2 files changed, 44 insertions(+), 50 deletions(-) Approvals: Hoo man: Looks good to me, approved WikidataJenkins: Verified jenkins-bot: Verified diff --git a/client/includes/parserhooks/PropertyParserFunction.php b/client/includes/parserhooks/PropertyParserFunction.php index 23c15c4..93fa2ba 100644 --- a/client/includes/parserhooks/PropertyParserFunction.php +++ b/client/includes/parserhooks/PropertyParserFunction.php @@ -27,16 +27,6 @@ class PropertyParserFunction { /** - * @var Parser - */ - private $parser; - - /** - * @var EntityId - */ - private $entityId; - - /** * @var EntityLookup */ private $entityLookup; @@ -47,16 +37,13 @@ private $propertyLabelResolver; /** - * @param Parser $parser - * @param EntityId $entityId * @param EntityLookup $entityLookup * @param PropertyLabelResolver $propertyLabelResolver */ - public function __construct( Parser $parser, EntityId $entityId, - EntityLookup $entityLookup, PropertyLabelResolver $propertyLabelResolver + public function __construct( + EntityLookup $entityLookup, + PropertyLabelResolver $propertyLabelResolver ) { - $this->parser = $parser; - $this->entityId = $entityId; $this->entityLookup = $entityLookup; $this->propertyLabelResolver = $propertyLabelResolver; } @@ -64,11 +51,13 @@ /** * Check whether variants are used in this parser run. * + * @param Parser $parser + * * @return bool */ - public function isParserUsingVariants() { - $parserOptions = $this->parser->getOptions(); - return $this->parser->OutputType() === Parser::OT_HTML && !$parserOptions->getInterfaceMessage() + public function isParserUsingVariants( Parser $parser ) { + $parserOptions = $parser->getOptions(); + return $parser->OutputType() === Parser::OT_HTML && !$parserOptions->getInterfaceMessage() && !$parserOptions->getDisableContentConversion(); } @@ -125,16 +114,17 @@ } /** + * @param EntityId $entityId * @param string $propertyLabel property label or ID (pXXX) * @param Language $language * * @return string */ - public function renderInLanguage( $propertyLabel, Language $language ) { + public function renderInLanguage( EntityId $entityId, $propertyLabel, Language $language ) { $renderer = $this->getRenderer( $language ); try { - $status = $renderer->renderForEntityId( $this->entityId, $propertyLabel ); + $status = $renderer->renderForEntityId( $entityId, $propertyLabel ); } catch ( PropertyLabelNotResolvedException $ex ) { $status = $this->getStatusForException( $propertyLabel, $ex->getMessage() ); } catch ( InvalidArgumentException $ex ) { @@ -164,17 +154,18 @@ } /** + * @param EntityId $entityId * @param string $propertyLabel property label or ID (pXXX) * @param string[] $variants Variant codes * * @return string[], key by variant codes */ - public function renderInVariants( $propertyLabel, array $variants ) { + public function renderInVariants( EntityId $entityId, $propertyLabel, array $variants ) { $textArray = array(); foreach ( $variants as $variantCode ) { $variantLanguage = Language::factory( $variantCode ); - $variantText = $this->renderInLanguage( $propertyLabel, $variantLanguage ); + $variantText = $this->renderInLanguage( $entityId, $propertyLabel, $variantLanguage ); // LanguageConverter doesn't handle empty strings correctly, and it's more difficult // to fix the issue there, as it's using empty string as a special value. // Also keeping the ability to check a missing property with {{#if: }} is another reason. @@ -187,21 +178,27 @@ } /** + * @param Parser $parser + * @param EntityId $entityId * @param string $propertyLabel property label or ID (pXXX) * * @return string Wikitext */ - public function doRender( $propertyLabel ) { + public function runPropertyParserFunction( Parser $parser, EntityId $entityId, $propertyLabel ) { wfProfileIn( __METHOD__ ); - $targetLanguage = $this->parser->getTargetLanguage(); + $targetLanguage = $parser->getTargetLanguage(); - if ( $this->isParserUsingVariants() && $this->parser->getConverterLanguage()->hasVariants() ) { - $text = $this->processRenderedArray( $this->renderInVariants( - $propertyLabel, $this->parser->getConverterLanguage()->getVariants() - ) ); + if ( $this->isParserUsingVariants( $parser ) && $parser->getConverterLanguage()->hasVariants() ) { + $renderedVariantsArray = $this->renderInVariants( + $entityId, + $propertyLabel, + $parser->getConverterLanguage()->getVariants() + ); + + $text = $this->processRenderedArray( $renderedVariantsArray ); } else { - $text = $this->renderInLanguage( $propertyLabel, $targetLanguage ); + $text = $this->renderInLanguage( $entityId, $propertyLabel, $targetLanguage ); } wfProfileOut( __METHOD__ ); @@ -211,7 +208,7 @@ /** * @since 0.4 * - * @param Parser &$parser + * @param Parser $parser * @param string $propertyLabel property label or ID (pXXX) * * @return array @@ -236,10 +233,10 @@ $entityLookup = $wikibaseClient->getStore()->getEntityLookup(); $propertyLabelResolver = $wikibaseClient->getStore()->getPropertyLabelResolver(); - $instance = new self( $parser, $entityId, $entityLookup, $propertyLabelResolver ); + $instance = new self( $entityLookup, $propertyLabelResolver ); $result = array( - $instance->doRender( $propertyLabel ), + $instance->runPropertyParserFunction( $parser, $entityId, $propertyLabel ), 'noparse' => false, // parse wikitext 'nowiki' => false, // formatters take care of escaping as needed ); diff --git a/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php b/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php index becdd8b..f0ee7a7 100644 --- a/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php +++ b/client/tests/phpunit/includes/parserhooks/PropertyParserFunctionTest.php @@ -26,16 +26,11 @@ /** * @param Parser $parser - * @param EntityId $entityId * @param Entity|null $entity * * @return PropertyParserFunction */ - private function getPropertyParserFunction( - Parser $parser, - EntityId $entityId, - Entity $entity = null - ) { + private function getPropertyParserFunction( Parser $parser, Entity $entity = null ) { $entityLookup = new MockRepository(); if ( $entity !== null ) { @@ -47,8 +42,7 @@ $entityLookup ); - return new PropertyParserFunction( $parser, $entityId, $entityLookup, - $propertyLabelResolver ); + return new PropertyParserFunction( $entityLookup, $propertyLabelResolver ); } /** @@ -58,8 +52,8 @@ $parser = new Parser(); $parserOptions = new ParserOptions(); $parser->startExternalParse( null, $parserOptions, $outputType ); - $instance = $this->getPropertyParserFunction( $parser, new ItemId( 'q42' ) ); - $renderer = $instance->getRenderer( Language::factory( $languageCode ) ); + $functionRunner = $this->getPropertyParserFunction( $parser ); + $renderer = $functionRunner->getRenderer( Language::factory( $languageCode ) ); $this->assertInstanceOf( 'Wikibase\PropertyParserFunctionRenderer', $renderer ); } @@ -80,14 +74,17 @@ $disableTitleConversion, $expected ) { - $parser = new Parser(); $parserOptions = new ParserOptions(); $parserOptions->setInterfaceMessage( $interfaceMessage ); $parserOptions->disableContentConversion( $disableContentConversion ); $parserOptions->disableTitleConversion( $disableTitleConversion ); + + $parser = new Parser(); $parser->startExternalParse( null, $parserOptions, $outputType ); - $instance = $this->getPropertyParserFunction( $parser, new ItemId( 'q42' ) ); - $this->assertEquals( $expected, $instance->isParserUsingVariants() ); + + $instance = $this->getPropertyParserFunction( $parser ); + + $this->assertEquals( $expected, $instance->isParserUsingVariants( $parser ) ); } public function isParserUsingVariantsProvider() { @@ -109,8 +106,8 @@ $parser = new Parser(); $parserOptions = new ParserOptions(); $parser->startExternalParse( null, $parserOptions, $outputType ); - $instance = $this->getPropertyParserFunction( $parser, new ItemId( 'q42' ) ); - $this->assertEquals( $expected, $instance->processRenderedArray( $textArray ) ); + $functionRunner = $this->getPropertyParserFunction( $parser ); + $this->assertEquals( $expected, $functionRunner->processRenderedArray( $textArray ) ); } public function processRenderedArrayProvider() { @@ -133,10 +130,10 @@ $item = Item::newEmpty(); $item->setId( new ItemId( 'Q42' ) ); - $instance = $this->getPropertyParserFunction( $parser, $item->getId(), $item ); + $functionRunner = $this->getPropertyParserFunction( $parser, $item ); $lang = Language::factory( 'qqx' ); - $result = $instance->renderInLanguage( 'invalidLabel', $lang ); + $result = $functionRunner->renderInLanguage( $item->getId(), 'invalidLabel', $lang ); // Test against the regexp of the {{#iferror parser function, as that should be able // to detect errors from PropertyParserFunction. See ExtParserFunctions::iferror -- To view, visit https://gerrit.wikimedia.org/r/147116 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ibeaa953c73f95376c6f5c46f1998c0bb9c95726b Gerrit-PatchSet: 4 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: WikidataJenkins <wikidata-servi...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits