Aude has uploaded a new change for review. https://gerrit.wikimedia.org/r/147416
Change subject: Move parser function code for site link lookup out of static method ...................................................................... Move parser function code for site link lookup out of static method Change-Id: I910b3206631ffd8e4d154ce10d930da5333031d6 --- M client/includes/DataAccess/PropertyParserFunction/Runner.php M client/tests/phpunit/includes/DataAccess/PropertyParserFunction/RunnerTest.php 2 files changed, 75 insertions(+), 22 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/16/147416/1 diff --git a/client/includes/DataAccess/PropertyParserFunction/Runner.php b/client/includes/DataAccess/PropertyParserFunction/Runner.php index 1b99362..560c464 100644 --- a/client/includes/DataAccess/PropertyParserFunction/Runner.php +++ b/client/includes/DataAccess/PropertyParserFunction/Runner.php @@ -14,6 +14,7 @@ use Wikibase\Lib\PropertyLabelNotResolvedException; use Wikibase\Lib\SnakFormatter; use Wikibase\Lib\Store\EntityLookup; +use Wikibase\Lib\Store\SiteLinkLookup; use Wikibase\PropertyLabelResolver; /** @@ -40,15 +41,31 @@ private $propertyLabelResolver; /** + * @var SiteLinkLookup + */ + private $siteLinkLookup; + + /** + * @var string + */ + private $siteId; + + /** * @param EntityLookup $entityLookup * @param PropertyLabelResolver $propertyLabelResolver + * @param SiteLinkLookup $siteLinkLookup + * @param string $siteId */ public function __construct( EntityLookup $entityLookup, - PropertyLabelResolver $propertyLabelResolver + PropertyLabelResolver $propertyLabelResolver, + SiteLinkLookup $siteLinkLookup, + $siteId ) { $this->entityLookup = $entityLookup; $this->propertyLabelResolver = $propertyLabelResolver; + $this->siteLinkLookup = $siteLinkLookup; + $this->siteId = $siteId; } /** @@ -185,14 +202,34 @@ /** * @param Parser $parser - * @param EntityId $entityId * @param string $propertyLabel property label or ID (pXXX) * * @return string Wikitext */ - public function runPropertyParserFunction( Parser $parser, EntityId $entityId, $propertyLabel ) { + public function runPropertyParserFunction( Parser $parser, $propertyLabel ) { wfProfileIn( __METHOD__ ); + // @todo use id provided as argument, if arbitrary access allowed + $itemId = $this->getItemIdForConnectedPage( $parser ); + + // @todo handle when site link is not there, such as site link / entity has been deleted... + if ( $itemId === null ) { + wfProfileOut( __METHOD__ ); + return ''; + } + + wfProfileOut( __METHOD__ ); + return $this->renderForEntityId( $parser, $itemId, $propertyLabel ); + } + + /** + * @param Parser $parser + * @param EntityId $entityId + * @param string $propertyLabel + * + * @return string + */ + private function renderForEntityId( Parser $parser, EntityId $entityId, $propertyLabel ) { $targetLanguage = $parser->getTargetLanguage(); if ( $this->isParserUsingVariants( $parser ) && $parser->getConverterLanguage()->hasVariants() ) { @@ -202,13 +239,23 @@ $parser->getConverterLanguage()->getVariants() ); - $text = $this->processRenderedArray( $renderedVariantsArray ); + return $this->processRenderedArray( $renderedVariantsArray ); } else { - $text = $this->renderInLanguage( $entityId, $propertyLabel, $targetLanguage ); + return $this->renderInLanguage( $entityId, $propertyLabel, $targetLanguage ); } + } - wfProfileOut( __METHOD__ ); - return $text; + /** + * @param Parser $parser + * + * @return ItemId + */ + private function getItemIdForConnectedPage( Parser $parser ) { + $title = $parser->getTitle(); + $siteLink = new SiteLink( $this->siteId, $title->getFullText() ); + $itemId = $this->siteLinkLookup->getEntityIdForSiteLink( $siteLink ); + + return $itemId; } /** @@ -223,26 +270,16 @@ wfProfileIn( __METHOD__ ); $wikibaseClient = WikibaseClient::getDefaultInstance(); - $siteId = $wikibaseClient->getSettings()->getSetting( 'siteGlobalID' ); - - $siteLinkLookup = $wikibaseClient->getStore()->getSiteLinkTable(); - $entityId = $siteLinkLookup->getEntityIdForSiteLink( //FIXME: method not in the interface - new SiteLink( $siteId, $parser->getTitle()->getFullText() ) - ); - - // @todo handle when site link is not there, such as site link / entity has been deleted... - if ( $entityId === null ) { - wfProfileOut( __METHOD__ ); - return ''; - } $entityLookup = $wikibaseClient->getStore()->getEntityLookup(); $propertyLabelResolver = $wikibaseClient->getStore()->getPropertyLabelResolver(); + $siteLinkLookup = $wikibaseClient->getStore()->getSiteLinkTable(); + $siteId = $wikibaseClient->getSettings()->getSetting( 'siteGlobalID' ); - $instance = new self( $entityLookup, $propertyLabelResolver ); + $instance = new self( $entityLookup, $propertyLabelResolver, $siteLinkLookup, $siteId ); $result = array( - $instance->runPropertyParserFunction( $parser, $entityId, $propertyLabel ), + $instance->runPropertyParserFunction( $parser, $propertyLabel ), 'noparse' => false, // parse wikitext 'nowiki' => false, // formatters take care of escaping as needed ); diff --git a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/RunnerTest.php b/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/RunnerTest.php index 8289f4f..307f455 100644 --- a/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/RunnerTest.php +++ b/client/tests/phpunit/includes/DataAccess/PropertyParserFunction/RunnerTest.php @@ -45,7 +45,12 @@ $entityLookup ); - return new Runner( $entityLookup, $propertyLabelResolver ); + return new Runner( + $entityLookup, + $propertyLabelResolver, + $this->getSiteLinkLookup(), + 'enwiki' + ); } /** @@ -151,4 +156,15 @@ ); } + private function getSiteLinkLookup() { + $siteLinkLookup = $this->getMockBuilder( 'Wikibase\Lib\Store\SiteLinkLookup' ) + ->getMock(); + + $siteLinkLookup->expects( $this->any() ) + ->method( 'getEntityIdForSiteLink' ) + ->will( $this->returnValue( new ItemId( 'Q3' ) ) ); + + return $siteLinkLookup; + } + } -- To view, visit https://gerrit.wikimedia.org/r/147416 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I910b3206631ffd8e4d154ce10d930da5333031d6 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Aude <aude.w...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits