jenkins-bot has submitted this change and it was merged. Change subject: Introduce EntityIdSiteLinkFormatter for wikitext formatting on client ......................................................................
Introduce EntityIdSiteLinkFormatter for wikitext formatting on client Bug: T148626 Change-Id: I785b573b4591e92d7c9bffb4214f6a51f1c99bcf --- M client/includes/WikibaseClient.php A lib/includes/Formatters/EntityIdSiteLinkFormatter.php M lib/includes/Formatters/WikibaseValueFormatterBuilders.php A lib/tests/phpunit/Formatters/EntityIdSiteLinkFormatterTest.php 4 files changed, 220 insertions(+), 3 deletions(-) Approvals: Hoo man: Looks good to me, approved jenkins-bot: Verified diff --git a/client/includes/WikibaseClient.php b/client/includes/WikibaseClient.php index 135d726..cd95980 100644 --- a/client/includes/WikibaseClient.php +++ b/client/includes/WikibaseClient.php @@ -261,7 +261,10 @@ $this->contentLanguage, new FormatterLabelDescriptionLookupFactory( $this->getTermLookup() ), new LanguageNameLookup( $wgLang->getCode() ), - $this->getRepoItemUriParser() + $this->getRepoItemUriParser(), + null, + $this->getStore()->getSiteLinkLookup(), + $this->getSettings()->getSetting( 'siteGlobalID' ) ); } diff --git a/lib/includes/Formatters/EntityIdSiteLinkFormatter.php b/lib/includes/Formatters/EntityIdSiteLinkFormatter.php new file mode 100644 index 0000000..569abc3 --- /dev/null +++ b/lib/includes/Formatters/EntityIdSiteLinkFormatter.php @@ -0,0 +1,104 @@ +<?php + +namespace Wikibase\Lib\Formatters; + +use Wikibase\DataModel\Entity\EntityId; +use Wikibase\DataModel\Entity\ItemId; +use Wikibase\DataModel\Services\EntityId\EntityIdFormatter; +use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup; +use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookupException; +use Wikibase\Lib\Store\SiteLinkLookup; + +/** + * A formatter for exclusive use on client wikis. It expects an entity ID and returns a wikitext + * snippet, containing a link to a local page as specified by the relevant sitelink, labeled with + * the label in the local (or a fallback) language. Both the sitelink and the label are optional. + * + * @since 0.5 + * + * @license GPL-2.0+ + * @author Thiemo Mättig + */ +class EntityIdSiteLinkFormatter implements EntityIdFormatter { + + /** + * @var SiteLinkLookup + */ + private $siteLinkLookup; + + /** + * @var string + */ + private $localSiteId; + + /** + * @var LabelDescriptionLookup + */ + private $labelDescriptionLookup; + + /** + * @param SiteLinkLookup $siteLinkLookup + * @param string $localSiteId + * @param LabelDescriptionLookup $labelDescriptionLookup + */ + public function __construct( + SiteLinkLookup $siteLinkLookup, + $localSiteId, + LabelDescriptionLookup $labelDescriptionLookup + ) { + $this->siteLinkLookup = $siteLinkLookup; + $this->localSiteId = $localSiteId; + $this->labelDescriptionLookup = $labelDescriptionLookup; + } + + /** + * @see EntityIdFormatter::formatEntityId + * + * @param EntityId $entityId + * + * @return string Wikitext + */ + public function formatEntityId( EntityId $entityId ) { + $term = null; + + try { + $term = $this->labelDescriptionLookup->getLabel( $entityId ); + } catch ( LabelDescriptionLookupException $ex ) { + } + + // TODO: Add language fallback indicator + $label = $term ? wfEscapeWikiText( $term->getText() ) : ''; + + if ( $entityId instanceof ItemId ) { + $pageName = $this->getPageName( $entityId ); + + if ( $pageName !== null ) { + $optionalLabel = $label === '' ? '' : '|' . $label; + + return '[[' . $pageName . $optionalLabel . ']]'; + } + } + + return $label === '' ? $entityId->getSerialization() : $label; + } + + /** + * @param ItemId $itemId + * + * @return string|null + */ + private function getPageName( ItemId $itemId ) { + // TODO: Bad, bad interface + $siteLinkData = $this->siteLinkLookup->getLinks( + [ $itemId->getNumericId() ], + [ $this->localSiteId ] + ); + + if ( count( $siteLinkData ) !== 1 ) { + return null; + } + + return $siteLinkData[0][1]; + } + +} diff --git a/lib/includes/Formatters/WikibaseValueFormatterBuilders.php b/lib/includes/Formatters/WikibaseValueFormatterBuilders.php index 47cec8e..47e02ce 100644 --- a/lib/includes/Formatters/WikibaseValueFormatterBuilders.php +++ b/lib/includes/Formatters/WikibaseValueFormatterBuilders.php @@ -16,7 +16,9 @@ use Wikibase\DataModel\Services\EntityId\EntityIdLabelFormatter; use Wikibase\Formatters\MonolingualHtmlFormatter; use Wikibase\Formatters\MonolingualTextFormatter; +use Wikibase\Lib\Formatters\EntityIdSiteLinkFormatter; use Wikibase\Lib\Store\EntityTitleLookup; +use Wikibase\Lib\Store\SiteLinkLookup; /** * Low level factory for ValueFormatters for well known data types. @@ -58,6 +60,16 @@ private $entityTitleLookup; /** + * @var SiteLinkLookup|null + */ + private $siteLinkLookup; + + /** + * @var string|null + */ + private $localSiteId; + + /** * Unit URIs that represent "unitless" or "one". * * @todo: make this configurable @@ -74,20 +86,26 @@ * @param FormatterLabelDescriptionLookupFactory $labelDescriptionLookupFactory * @param LanguageNameLookup $languageNameLookup * @param EntityIdParser $repoItemUriParser - * @param EntityTitleLookup|null $entityTitleLookup + * @param EntityTitleLookup|null $entityTitleLookup Only when used on a repo. + * @param SiteLinkLookup|null $siteLinkLookup Only when used on a client. + * @param string|null $localSiteId Only when used on a client. */ public function __construct( Language $defaultLanguage, FormatterLabelDescriptionLookupFactory $labelDescriptionLookupFactory, LanguageNameLookup $languageNameLookup, EntityIdParser $repoItemUriParser, - EntityTitleLookup $entityTitleLookup = null + EntityTitleLookup $entityTitleLookup = null, + SiteLinkLookup $siteLinkLookup = null, + $localSiteId = null ) { $this->defaultLanguage = $defaultLanguage; $this->labelDescriptionLookupFactory = $labelDescriptionLookupFactory; $this->languageNameLookup = $languageNameLookup; $this->repoItemUriParser = $repoItemUriParser; $this->entityTitleLookup = $entityTitleLookup; + $this->siteLinkLookup = $siteLinkLookup; + $this->localSiteId = $localSiteId; } private function newPlainEntityIdFormatter( FormatterOptions $options ) { @@ -163,6 +181,14 @@ $this->languageNameLookup ) ); + } elseif ( $format === SnakFormatter::FORMAT_WIKI && $this->siteLinkLookup && $this->localSiteId ) { + return new EntityIdValueFormatter( + new EntityIdSiteLinkFormatter( + $this->siteLinkLookup, + $this->localSiteId, + $this->labelDescriptionLookupFactory->getLabelDescriptionLookup( $options ) + ) + ); } $plainFormatter = $this->newPlainEntityIdFormatter( $options ); diff --git a/lib/tests/phpunit/Formatters/EntityIdSiteLinkFormatterTest.php b/lib/tests/phpunit/Formatters/EntityIdSiteLinkFormatterTest.php new file mode 100644 index 0000000..dd041fa --- /dev/null +++ b/lib/tests/phpunit/Formatters/EntityIdSiteLinkFormatterTest.php @@ -0,0 +1,84 @@ +<?php + +namespace Wikibase\Lib\Tests\Formatters; + +use PHPUnit_Framework_TestCase; +use Wikibase\DataModel\Entity\ItemId; +use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup; +use Wikibase\DataModel\SiteLink; +use Wikibase\DataModel\Term\Term; +use Wikibase\Lib\Formatters\EntityIdSiteLinkFormatter; +use Wikibase\Lib\Store\SiteLinkLookup; + +/** + * @covers Wikibase\Lib\Formatters\EntityIdSiteLinkFormatter + * + * @group Wikibase + * @group WikibaseLib + * + * @license GPL-2.0+ + * @author Thiemo Mättig + */ +class EntityIdSiteLinkFormatterTest extends PHPUnit_Framework_TestCase { + + public function formatEntityIdProvider() { + return [ + [ + new SiteLink( 'enwiki', '<PAGE>' ), + new Term( 'en', '<LABEL>' ), + '[[<PAGE>|<LABEL>]]' + ], + [ + new SiteLink( 'enwiki', '<PAGE>' ), + new Term( 'en', '' ), + '[[<PAGE>]]' + ], + [ + new SiteLink( 'enwiki', '<PAGE>' ), + null, + '[[<PAGE>]]' + ], + [ + null, + new Term( 'en', '<LABEL>' ), + '<LABEL>' + ], + [ + null, + null, + 'Q1' + ], + ]; + } + + /** + * @dataProvider formatEntityIdProvider + */ + public function testFormatEntityId( SiteLink $siteLink = null, Term $label = null, $expected ) { + $id = new ItemId( 'Q1' ); + + $siteLinkLookup = $this->getMock( SiteLinkLookup::class ); + $siteLinkLookup->expects( $this->any() ) + ->method( 'getLinks' ) + ->with( [ $id->getNumericId() ], [ 'enwiki' ], [] ) + ->will( $this->returnValue( $siteLink + ? [ [ null, $siteLink->getPageName() ] ] + : null + ) ); + + $labelDescriptionLookup = $this->getMock( LabelDescriptionLookup::class ); + $labelDescriptionLookup->expects( $this->any() ) + ->method( 'getLabel' ) + ->with( $id ) + ->will( $this->returnValue( $label ) ); + + $formatter = new EntityIdSiteLinkFormatter( + $siteLinkLookup, + 'enwiki', + $labelDescriptionLookup + ); + + $this->assertSame( $expected, $formatter->formatEntityId( $id ) ); + } + +} -- To view, visit https://gerrit.wikimedia.org/r/316960 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I785b573b4591e92d7c9bffb4214f6a51f1c99bcf Gerrit-PatchSet: 5 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits