jenkins-bot has submitted this change and it was merged. Change subject: Language fallback for badge labels in SpecialSetSiteLink ......................................................................
Language fallback for badge labels in SpecialSetSiteLink Change-Id: Ideba18e9afee7321bcb16600aa595a949ccbbb0b --- M repo/includes/specials/SpecialSetSiteLink.php M repo/tests/phpunit/includes/specials/SpecialSetSiteLinkTest.php 2 files changed, 60 insertions(+), 39 deletions(-) Approvals: Addshore: Looks good to me, approved jenkins-bot: Verified diff --git a/repo/includes/specials/SpecialSetSiteLink.php b/repo/includes/specials/SpecialSetSiteLink.php index b8c6188..57ccd83 100644 --- a/repo/includes/specials/SpecialSetSiteLink.php +++ b/repo/includes/specials/SpecialSetSiteLink.php @@ -6,13 +6,14 @@ use InvalidArgumentException; use OutOfBoundsException; use Status; -use UserInputException; use Wikibase\ChangeOp\ChangeOpException; use Wikibase\ChangeOp\SiteLinkChangeOpFactory; use Wikibase\DataModel\Entity\Entity; -use Wikibase\DataModel\Entity\EntityId; use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\ItemId; +use Wikibase\LanguageFallbackChainFactory; +use Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookup; +use Wikibase\Lib\Store\TermLookup; use Wikibase\Repo\SiteLinkTargetProvider; use Wikibase\Repo\WikibaseRepo; use Wikibase\Summary; @@ -68,6 +69,16 @@ private $siteLinkTargetProvider; /** + * @var TermLookup + */ + private $termLookup; + + /** + * @var LanguageFallbackChainFactory + */ + private $fallbackChainFactory; + + /** * @since 0.4 */ public function __construct() { @@ -84,6 +95,9 @@ $this->siteStore, $settings->getSetting( 'specialSiteLinkGroups' ) ); + + $this->fallbackChainFactory = $wikibaseRepo->getLanguageFallbackChainFactory(); + $this->termLookup = $wikibaseRepo->getTermLookup(); } /** @@ -293,12 +307,23 @@ private function getHtmlForBadges() { $options = ''; + $fallbackChain = $this->fallbackChainFactory->newFromLanguage( + $this->getLanguage(), + LanguageFallbackChainFactory::FALLBACK_SELF + | LanguageFallbackChainFactory::FALLBACK_VARIANTS + | LanguageFallbackChainFactory::FALLBACK_OTHERS + ); + + $labelLookup = new LanguageFallbackLabelDescriptionLookup( $this->termLookup, $fallbackChain ); + foreach ( $this->badgeItems as $badgeId => $value ) { $name = 'badge-' . $badgeId; - $title = $this->getTitleForBadge( new ItemId( $badgeId ) ); - if ( $title === null ) { - continue; + try { + $term = $labelLookup->getLabel( new ItemId( $badgeId ) ); + $label = $term->getText(); + } catch ( OutOfBoundsException $ex ) { + $label = $badgeId; } $options .= Html::rawElement( @@ -318,37 +343,12 @@ array( 'for' => $name ), - $title + $label ) ); } return $options; - } - - /** - * Returns the title for the given badge id. - * @todo use TermLookup when we have one - * - * @param EntityId $badgeId - * @return string|null - */ - private function getTitleForBadge( EntityId $badgeId ) { - try { - $entity = $this->loadEntity( $badgeId )->getEntity(); - $languageCode = $this->getLanguage()->getCode(); - - $labels = $entity->getFingerprint()->getLabels(); - if ( $labels->hasTermForLanguage( $languageCode ) ) { - return $labels->getByLanguage( $languageCode )->getText(); - } else { - return $badgeId->getSerialization(); - } - } catch ( UserInputException $ex ) { - // log a warning because this indicates a wrong configuration - wfLogWarning( 'Error fetching title for badge: ' . $ex->getMessage() ); - return null; - } } /** diff --git a/repo/tests/phpunit/includes/specials/SpecialSetSiteLinkTest.php b/repo/tests/phpunit/includes/specials/SpecialSetSiteLinkTest.php index 79adbdf..dab220e 100644 --- a/repo/tests/phpunit/includes/specials/SpecialSetSiteLinkTest.php +++ b/repo/tests/phpunit/includes/specials/SpecialSetSiteLinkTest.php @@ -115,7 +115,7 @@ $store = WikibaseRepo::getDefaultInstance()->getEntityStore(); $badge = new Item(); - $badge->setLabel( 'en', 'Good article' ); + $badge->setLabel( 'de', 'Guter Artikel' ); $store->saveEntity( $badge, "testing", $GLOBALS['wgUser'], EDIT_NEW ); $item = new Item(); @@ -145,14 +145,14 @@ 'attributes' => array( 'for' => $name ), - 'content' => 'Good article' + 'content' => 'Guter Artikel' ); } public function testExecuteEmptyForm() { $matchers = self::$matchers; // Execute with no subpage value - list( $output, ) = $this->executeSpecialPage( '', null, 'en' ); + list( $output, ) = $this->executeSpecialPage( '', null, 'de' ); foreach( $matchers as $key => $matcher ){ $this->assertTag( $matcher, $output, "Failed to match html output with tag '{$key}'" ); @@ -162,7 +162,8 @@ public function testExecuteOneValuePreset() { $matchers = self::$matchers; // Execute with one subpage value - list( $output, ) = $this->executeSpecialPage( self::$itemId, null, 'en' ); + // Note: use language fallback de-ch => de + list( $output, ) = $this->executeSpecialPage( self::$itemId, null, 'de-ch' ); $matchers['id']['attributes']['value'] = self::$itemId; @@ -174,7 +175,8 @@ public function testExecuteTwoValuesPreset() { $matchers = self::$matchers; // Execute with two subpage values - list( $output, ) = $this->executeSpecialPage( self::$itemId . '/dewiki', null, 'en' ); + // Note: use language fallback de-ch => de + list( $output, ) = $this->executeSpecialPage( self::$itemId . '/dewiki', null, 'de-ch' ); $matchers['id'] = array( 'tag' => 'input', @@ -209,6 +211,26 @@ } } + public function testExecuteTwoValuesPreset_no_label() { + $matchers = self::$matchers; + // Execute with two subpage values + // Note: language fallback will fail, no label for en + list( $output, ) = $this->executeSpecialPage( self::$itemId . '/dewiki', null, 'en' ); + + // already covered by testExecuteTwoValuesPreset() + unset( $matchers['id'] ); + unset( $matchers['site'] ); + unset( $matchers['remove'] ); + + $matchers['badgelabel']['content'] = self::$badgeId; + $matchers['value']['attributes']['value'] = 'Wikidata'; + $matchers['badges']['children']['only']['attributes']['selected'] = ''; + + foreach( $matchers as $key => $matcher ) { + $this->assertTag( $matcher, $output, "Failed to match html output with tag '{$key}' passing two subpage values" ); + } + } + public function testExecuteRedirect() { list( $output, ) = $this->executeSpecialPage( self::$redirectId . '/dewiki', null, 'qqx' ); @@ -216,7 +238,6 @@ } public function testExecutePostPreserveSiteLinkWhenNothingEntered() { - $lookup = WikibaseRepo::getDefaultInstance()->getEntityLookup(); $request = new FauxRequest( array( 'id' => self::$itemId, 'site' => 'dewiki', 'page' => '' ), true ); list( $output, ) = $this->executeSpecialPage( '', $request ); @@ -236,7 +257,7 @@ $lookup = WikibaseRepo::getDefaultInstance()->getEntityLookup(); $request = new FauxRequest( array( 'id' => self::$itemId, 'site' => 'dewiki', 'page' => 'Wikipedia' ), true ); - list( $output, $response ) = $this->executeSpecialPage( '', $request ); + list( , $response ) = $this->executeSpecialPage( '', $request ); $redirect = $response instanceof FauxResponse ? $response->getHeader( 'Location' ) : null; $this->assertContains( self::$itemId, $redirect, "Should redirect to item page" ); @@ -255,7 +276,7 @@ $lookup = WikibaseRepo::getDefaultInstance()->getEntityLookup(); $request = new FauxRequest( array( 'id' => self::$itemId, 'site' => 'dewiki', 'page' => '', 'remove' => true ), true ); - list( $output, $response ) = $this->executeSpecialPage( '', $request ); + list( , $response ) = $this->executeSpecialPage( '', $request ); $redirect = $response instanceof FauxResponse ? $response->getHeader( 'Location' ) : null; $this->assertContains( self::$itemId, $redirect, "Should redirect to item page" ); -- To view, visit https://gerrit.wikimedia.org/r/204540 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ideba18e9afee7321bcb16600aa595a949ccbbb0b Gerrit-PatchSet: 6 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Bene <benestar.wikime...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: JanZerebecki <jan.wikime...@zerebecki.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