Adrian Lang has uploaded a new change for review. https://gerrit.wikimedia.org/r/200848
Change subject: Add language fallback for badge labels ...................................................................... Add language fallback for badge labels Bug: T94552 Change-Id: I158482cb9ff225ae9389c071919edf6a3f9a3aaa --- M lib/includes/store/TermLookup.php A repo/includes/EntityIdLabelFormatterFactory.php M repo/includes/EntityParserOutputGenerator.php M repo/includes/WikibaseRepo.php A repo/tests/phpunit/includes/EntityIdLabelFormatterFactoryTest.php M view/src/EntityViewFactory.php M view/src/SiteLinksView.php M view/tests/phpunit/EntityViewFactoryTest.php M view/tests/phpunit/SiteLinksViewTest.php 9 files changed, 130 insertions(+), 98 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/48/200848/1 diff --git a/lib/includes/store/TermLookup.php b/lib/includes/store/TermLookup.php index 4fb59b6..9af057b 100644 --- a/lib/includes/store/TermLookup.php +++ b/lib/includes/store/TermLookup.php @@ -31,8 +31,7 @@ /** * Gets all labels of an Entity with the specified EntityId. * - * If $languages is given, the result will contain the entries for the - * requested languages, if they exist. + * The result will contain the entries for the requested languages, if they exist. * * @param EntityId $entityId * @param string[] $languageCodes The list of languages to fetch diff --git a/repo/includes/EntityIdLabelFormatterFactory.php b/repo/includes/EntityIdLabelFormatterFactory.php new file mode 100644 index 0000000..f179fdd --- /dev/null +++ b/repo/includes/EntityIdLabelFormatterFactory.php @@ -0,0 +1,38 @@ +<?php + +namespace Wikibase\Repo; + +use Wikibase\Lib\EntityIdLabelFormatter; +use Wikibase\Lib\SnakFormatter; +use Wikibase\Lib\Store\LabelLookup; +use Wikibase\View\EntityIdFormatterFactory; + +/** + * A factory for generating EntityIdHtmlLinkFormatters. + * + * @license GPL 2+ + * @author Daniel Kinzler + */ +class EntityIdLabelFormatterFactory implements EntityIdFormatterFactory { + + /** + * @see EntityIdFormatterFactory::getOutputFormat + * + * @return string SnakFormatter::FORMAT_HTML + */ + public function getOutputFormat() { + return SnakFormatter::FORMAT_PLAIN; + } + + /** + * @see EntityIdFormatterFactory::getEntityIdFormater + * + * @param LabelLookup $labelLookup + * + * @return EntityIdLabelFormatter + */ + public function getEntityIdFormater( LabelLookup $labelLookup ) { + return new EntityIdLabelFormatter( $labelLookup ); + } + +} diff --git a/repo/includes/EntityParserOutputGenerator.php b/repo/includes/EntityParserOutputGenerator.php index 2ca6043..9a42f45 100644 --- a/repo/includes/EntityParserOutputGenerator.php +++ b/repo/includes/EntityParserOutputGenerator.php @@ -150,6 +150,14 @@ $editable = $options->getEditSection(); $usedEntityIds = $this->referencedEntitiesFinder->findSnakLinks( $snaks ); + + // FIXME: Bad + if( $entity instanceof Item ) { + foreach( $entity->getSiteLinkList()->getIterator() as $sitelink ) { + $usedEntityIds += $sitelink->getBadges(); + } + } + $entityInfo = $this->getEntityInfo( $usedEntityIds ); $configVars = $this->configBuilder->build( $entity, $entityInfo ); diff --git a/repo/includes/WikibaseRepo.php b/repo/includes/WikibaseRepo.php index dc20e9a..4308a34 100644 --- a/repo/includes/WikibaseRepo.php +++ b/repo/includes/WikibaseRepo.php @@ -1015,8 +1015,8 @@ $entityViewFactory = new EntityViewFactory( $this->getEntityIdHtmlLinkFormatterFactory(), + new EntityIdLabelFormatterFactory(), $this->getHtmlSnakFormatterFactory(), - $this->getEntityLookup(), $this->getSiteStore(), $this->getDataTypeFactory(), $templateFactory, diff --git a/repo/tests/phpunit/includes/EntityIdLabelFormatterFactoryTest.php b/repo/tests/phpunit/includes/EntityIdLabelFormatterFactoryTest.php new file mode 100644 index 0000000..3562ca3 --- /dev/null +++ b/repo/tests/phpunit/includes/EntityIdLabelFormatterFactoryTest.php @@ -0,0 +1,38 @@ +<?php + +namespace Wikibase\Repo\Test; + +use PHPUnit_Framework_TestCase; +use Wikibase\Lib\SnakFormatter; +use Wikibase\Repo\EntityIdLabelFormatterFactory; + +/** + * @covers Wikibase\Repo\EntityIdHtmlLinkFormatterFactory + * + * @group ValueFormatters + * @group WikibaseLib + * @group Wikibase + * + * @licence GNU GPL v2+ + * @author Daniel Kinzler + */ +class EntityIdLabelFormatterFactoryTest extends PHPUnit_Framework_TestCase { + + private function getFormatterFactory() { + return new EntityIdLabelFormatterFactory(); + } + + public function testGetFormat() { + $factory = $this->getFormatterFactory(); + + $this->assertEquals( SnakFormatter::FORMAT_PLAIN, $factory->getOutputFormat() ); + } + + public function testGetEntityIdFormatter() { + $factory = $this->getFormatterFactory(); + + $formatter = $factory->getEntityIdFormater( $this->getMock( 'Wikibase\Lib\Store\LabelLookup' ) ); + $this->assertInstanceOf( 'Wikibase\Lib\EntityIdFormatter', $formatter ); + } + +} diff --git a/view/src/EntityViewFactory.php b/view/src/EntityViewFactory.php index 254dd42..3d1530e 100644 --- a/view/src/EntityViewFactory.php +++ b/view/src/EntityViewFactory.php @@ -10,7 +10,6 @@ use Wikibase\Lib\EntityIdFormatter; use Wikibase\Lib\LanguageNameLookup; use Wikibase\Lib\SnakFormatter; -use Wikibase\Lib\Store\EntityLookup; use Wikibase\Lib\Store\LabelLookup; use Wikibase\View\Template\TemplateFactory; @@ -30,12 +29,12 @@ /** * @var EntityIdFormatterFactory */ - private $idFormatterFactory; + private $htmlIdFormatterFactory; /** - * @var EntityLookup + * @var EntityIdFormatterFactory */ - private $entityLookup; + private $plaintextIdFormatterFactory; /** * @var SiteStore @@ -73,9 +72,9 @@ private $languageNameLookup; /** - * @param EntityIdFormatterFactory $idFormatterFactory + * @param EntityIdFormatterFactory $htmlIdFormatterFactory + * @param EntityIdFormatterFactory $plaintextIdFormatterFactory * @param HtmlSnakFormatterFactory $htmlSnakFormatterFactory - * @param EntityLookup $entityLookup * @param SiteStore $siteStore * @param DataTypeFactory $dataTypeFactory * @param TemplateFactory $templateFactory @@ -85,9 +84,9 @@ * @param string[] $badgeItems */ public function __construct( - EntityIdFormatterFactory $idFormatterFactory, + EntityIdFormatterFactory $htmlIdFormatterFactory, + EntityIdFormatterFactory $plaintextIdFormatterFactory, HtmlSnakFormatterFactory $htmlSnakFormatterFactory, - EntityLookup $entityLookup, SiteStore $siteStore, DataTypeFactory $dataTypeFactory, TemplateFactory $templateFactory, @@ -96,11 +95,12 @@ array $specialSiteLinkGroups, array $badgeItems ) { - $this->checkOutputFormat( $idFormatterFactory->getOutputFormat() ); + $this->checkOutputFormat( $htmlIdFormatterFactory->getOutputFormat(), 'HTML' ); + $this->checkOutputFormat( $plaintextIdFormatterFactory->getOutputFormat(), 'Plain' ); - $this->idFormatterFactory = $idFormatterFactory; + $this->htmlIdFormatterFactory = $htmlIdFormatterFactory; + $this->plaintextIdFormatterFactory = $plaintextIdFormatterFactory; $this->htmlSnakFormatterFactory = $htmlSnakFormatterFactory; - $this->entityLookup = $entityLookup; $this->siteStore = $siteStore; $this->dataTypeFactory = $dataTypeFactory; $this->siteLinkGroups = $siteLinkGroups; @@ -112,15 +112,17 @@ /** * @param string $format + * @param string $expected 'HTML' or 'Plain' * * @throws InvalidArgumentException */ - private function checkOutputFormat( $format ) { - if ( $format !== SnakFormatter::FORMAT_HTML - && $format !== SnakFormatter::FORMAT_HTML_DIFF - && $format !== SnakFormatter::FORMAT_HTML_WIDGET + private function checkOutputFormat( $format, $expected ) { + if ( ( $expected === 'HTML' && $format !== SnakFormatter::FORMAT_HTML + && $format !== SnakFormatter::FORMAT_HTML_DIFF + && $format !== SnakFormatter::FORMAT_HTML_WIDGET + ) || ($expected === 'Plain' && $format !== SnakFormatter::FORMAT_PLAIN ) ) { - throw new InvalidArgumentException( 'HTML format expected, got ' . $format ); + throw new InvalidArgumentException( $expected . ' format expected, got ' . $format ); } } @@ -162,11 +164,10 @@ $this->templateFactory, $this->siteStore->getSites(), $editSectionGenerator, - $this->entityLookup, + $this->plaintextIdFormatterFactory->getEntityIdFormater( $labelLookup ), $this->languageNameLookup, $this->badgeItems, - $this->specialSiteLinkGroups, - $language->getCode() + $this->specialSiteLinkGroups ); return new ItemView( @@ -204,7 +205,7 @@ LabelLookup $labelLookup, EditSectionGenerator $editSectionGenerator ) { - $propertyIdFormatter = $this->getPropertyIdFormatter( $labelLookup ); + $propertyIdFormatter = $this->htmlIdFormatterFactory->getEntityIdFormater( $labelLookup ); $snakHtmlGenerator = new SnakHtmlGenerator( $this->templateFactory, @@ -238,15 +239,6 @@ $this->languageNameLookup, $languageCode ); - } - - /** - * @param LabelLookup $labelLookup - * - * @return EntityIdFormatter - */ - private function getPropertyIdFormatter( LabelLookup $labelLookup ) { - return $this->idFormatterFactory->getEntityIdFormater( $labelLookup ); } } diff --git a/view/src/SiteLinksView.php b/view/src/SiteLinksView.php index 379f694..3fff9a7 100644 --- a/view/src/SiteLinksView.php +++ b/view/src/SiteLinksView.php @@ -2,15 +2,13 @@ namespace Wikibase\View; -use InvalidArgumentException; use Sanitizer; use Site; use SiteList; use Wikibase\DataModel\Entity\ItemId; use Wikibase\DataModel\SiteLink; -use Wikibase\DataModel\Term\FingerprintProvider; +use Wikibase\Lib\EntityIdFormatter; use Wikibase\Lib\LanguageNameLookup; -use Wikibase\Lib\Store\EntityLookup; use Wikibase\View\Template\TemplateFactory; /** @@ -23,6 +21,11 @@ * @author Bene* < benestar.wikime...@gmail.com > */ class SiteLinksView { + + /** + * @var EntityIdFormatter + */ + private $entityIdFormatter; /** * @var TemplateFactory @@ -40,11 +43,6 @@ private $sectionEditLinkGenerator; /** - * @var EntityLookup - */ - private $entityLookup; - - /** * @var LanguageNameLookup */ private $languageNameLookup; @@ -60,38 +58,30 @@ private $specialSiteLinkGroups; /** - * @var string - */ - private $languageCode; - - /** * @param TemplateFactory $templateFactory * @param SiteList $sites * @param EditSectionGenerator $sectionEditLinkGenerator - * @param EntityLookup $entityLookup + * @param EntityIdFormatter $entityIdFormatter A plaintext producing EntityIdFormatter * @param LanguageNameLookup $languageNameLookup * @param string[] $badgeItems * @param string[] $specialSiteLinkGroups - * @param string $languageCode */ public function __construct( TemplateFactory $templateFactory, SiteList $sites, EditSectionGenerator $sectionEditLinkGenerator, - EntityLookup $entityLookup, + EntityIdFormatter $entityIdFormatter, LanguageNameLookup $languageNameLookup, array $badgeItems, - array $specialSiteLinkGroups, - $languageCode + array $specialSiteLinkGroups ) { $this->sites = $sites; $this->sectionEditLinkGenerator = $sectionEditLinkGenerator; - $this->entityLookup = $entityLookup; $this->badgeItems = $badgeItems; $this->specialSiteLinkGroups = $specialSiteLinkGroups; - $this->languageCode = $languageCode; $this->templateFactory = $templateFactory; $this->languageNameLookup = $languageNameLookup; + $this->entityIdFormatter = $entityIdFormatter; } /** @@ -104,13 +94,8 @@ * @param string[] $groups An array of site group IDs * * @return string - * @throws InvalidArgumentException */ - public function getHtml( array $siteLinks, $itemId, array $groups ) { - if ( $itemId !== null && !( $itemId instanceof ItemId ) ) { - throw new InvalidArgumentException( '$itemId must be an ItemId or null.' ); - } - + public function getHtml( array $siteLinks, ItemId $itemId = null, array $groups ) { $html = ''; if ( count( $groups ) === 0 ) { @@ -135,7 +120,7 @@ * * @return string */ - private function getHtmlForSiteLinkGroup( array $siteLinks, $itemId, $group ) { + private function getHtmlForSiteLinkGroup( array $siteLinks, ItemId $itemId = null, $group ) { return $this->templateFactory->render( 'wikibase-sitelinkgroupview', // TODO: support entity-id as prefix for element IDs. htmlspecialchars( 'sitelinks-' . $group, ENT_QUOTES ), @@ -321,34 +306,12 @@ $html .= $this->templateFactory->render( 'wb-badge', $classes, - $this->getTitleForBadge( $badge ), + $this->entityIdFormatter->formatEntityId( $badge ), $badge ); } return $this->templateFactory->render( 'wikibase-badgeselector', $html ); - } - - /** - * Returns the title for the given badge id. - * @todo use TermLookup when we have one - * - * @param ItemId $badgeId - * - * @return string - */ - private function getTitleForBadge( ItemId $badgeId ) { - $badge = $this->entityLookup->getEntity( $badgeId ); - - if ( $badge instanceof FingerprintProvider ) { - $labels = $badge->getFingerprint()->getLabels(); - - if ( $labels->hasTermForLanguage( $this->languageCode ) ) { - return $labels->getByLanguage( $this->languageCode )->getText(); - } - } - - return $badgeId->getSerialization(); } } diff --git a/view/tests/phpunit/EntityViewFactoryTest.php b/view/tests/phpunit/EntityViewFactoryTest.php index 2358aeb..d122b13 100644 --- a/view/tests/phpunit/EntityViewFactoryTest.php +++ b/view/tests/phpunit/EntityViewFactoryTest.php @@ -81,7 +81,6 @@ return new EntityViewFactory( $this->getEntityIdFormatterFactory(), $this->getSnakFormatterFactory(), - $this->getMock( 'Wikibase\Lib\Store\EntityLookup' ), $this->getSiteStore(), $this->getMock( 'DataTypes\DataTypeFactory' ), new TemplateFactory( TemplateRegistry::getDefaultInstance() ), diff --git a/view/tests/phpunit/SiteLinksViewTest.php b/view/tests/phpunit/SiteLinksViewTest.php index a9b459a..0a692af 100644 --- a/view/tests/phpunit/SiteLinksViewTest.php +++ b/view/tests/phpunit/SiteLinksViewTest.php @@ -165,14 +165,13 @@ new TemplateFactory( TemplateRegistry::getDefaultInstance() ), $this->newSiteList(), $this->getEditSectionGeneratorMock(), - $this->getEntityLookupMock(), + $this->getEntityIdFormatterMock(), new LanguageNameLookup(), array( 'Q42' => 'wb-badge-featuredarticle', 'Q12' => 'wb-badge-goodarticle' ), - array( 'special group' ), - 'en' + array( 'special group' ) ); } @@ -202,26 +201,22 @@ } /** - * @return EntityLookup + * @return EntityIdFormatter */ - private function getEntityLookupMock() { - $entityLookup = $this->getMockBuilder( 'Wikibase\Lib\Store\EntityLookup' ) - ->disableOriginalConstructor() - ->getMock(); + private function getEntityIdFormatterMock() { + $entityIdFormatter = $this->getMock( 'Wikibase\Lib\EntityIdFormatter' ); - $entityLookup->expects( $this->any() ) - ->method( 'getEntity' ) + $entityIdFormatter->expects( $this->any() ) + ->method( 'formatEntityId' ) ->will( $this->returnCallback( function( EntityId $id ) { if ( $id->getSerialization() === 'Q42' ) { - $item = new Item(); - $item->setLabel( 'en', 'Featured article' ); - return $item; + return 'Featured article'; } - return null; + return $id->getSerialization(); } ) ); - return $entityLookup; + return $entityIdFormatter; } } -- To view, visit https://gerrit.wikimedia.org/r/200848 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I158482cb9ff225ae9389c071919edf6a3f9a3aaa Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Adrian Lang <adrian.he...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits