Ladsgroup has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/355899 )
Change subject: Move out logic of badges out of LanguageLinkBadgeDisplay to a
dedicated class
......................................................................
Move out logic of badges out of LanguageLinkBadgeDisplay to a dedicated class
It helps out for adding badges for other projects sidebar.
See I7a92b54c7b24fb23171d4eec0bd136b19fa8fdb9
Bug: T73887
Change-Id: I2ead941fb0e945a564aff6b2a4f074beedf89580
---
M client/includes/Hooks/LanguageLinkBadgeDisplay.php
A client/includes/Hooks/SidebarLinkBadgeDisplay.php
M client/includes/WikibaseClient.php
M client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php
M client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php
M client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php
A client/tests/phpunit/includes/Hooks/SidebarLinkBadgeDisplayTest.php
7 files changed, 297 insertions(+), 112 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/99/355899/1
diff --git a/client/includes/Hooks/LanguageLinkBadgeDisplay.php
b/client/includes/Hooks/LanguageLinkBadgeDisplay.php
index ae32925..3735d63 100644
--- a/client/includes/Hooks/LanguageLinkBadgeDisplay.php
+++ b/client/includes/Hooks/LanguageLinkBadgeDisplay.php
@@ -23,33 +23,15 @@
class LanguageLinkBadgeDisplay {
/**
- * @var LabelDescriptionLookup;
+ * @var SidebarLinkBadgeDisplay;
*/
- protected $labelDescriptionLookup;
+ protected $sidebarLinkBadgeDisplay;
/**
- * @var array
+ * @param SidebarLinkBadgeDisplay $sidebarLinkBadgeDisplay
*/
- protected $badgeClassNames;
-
- /**
- * @var Language
- */
- protected $language;
-
- /**
- * @param LabelDescriptionLookup $labelDescriptionLookup
- * @param array $badgeClassNames
- * @param Language $language
- */
- public function __construct(
- LabelDescriptionLookup $labelDescriptionLookup,
- array $badgeClassNames,
- Language $language
- ) {
- $this->labelDescriptionLookup = $labelDescriptionLookup;
- $this->badgeClassNames = $badgeClassNames;
- $this->language = $language;
+ public function __construct( SidebarLinkBadgeDisplay
$sidebarLinkBadgeDisplay ) {
+ $this->sidebarLinkBadgeDisplay = $sidebarLinkBadgeDisplay;
}
/**
@@ -68,7 +50,9 @@
$badges = $link->getBadges();
if ( !empty( $badges ) ) {
- $badgeInfoForAllLinks[$key] =
$this->getBadgeInfo( $badges );
+ $badgeInfoForAllLinks[$key] =
$this->sidebarLinkBadgeDisplay->getBadgeInfo(
+ $badges
+ );
}
}
@@ -99,83 +83,7 @@
return;
}
- /** @var array $linksBadgeInfo an associative array with the
keys 'class', and 'itemtitle'. */
- $linksBadgeInfo = $badges[$navId];
-
- if ( isset( $languageLink['class'] ) ) {
- $languageLink['class'] .= ' ' .
$linksBadgeInfo['class'];
- } else {
- $languageLink['class'] = $linksBadgeInfo['class'];
- }
-
- $languageLink['itemtitle'] = $linksBadgeInfo['label'];
- }
-
- /**
- * Builds badge information for the given badges.
- * CSS classes are derived from the given list of badges, and any extra
badge class
- * names specified in the badgeClassNames setting are added.
- * For badges that have a such an extra class name assigned, this also
- * adds a title according to the items' labels. Other badges do not
have labels
- * added to the link's title attribute, so the can be effectively
ignored
- * on this client wiki.
- *
- * @param ItemId[] $badgeIds
- *
- * @return array An associative array with the keys 'class' and
'itemtitle' with assigned
- * string values. These fields correspond to the fields in the
description array for language
- * links used by the SkinTemplateGetLanguageLink hook and expected by
the applyBadges()
- * function.
- */
- private function getBadgeInfo( array $badgeIds ) {
- $classes = array();
- $labels = array();
-
- foreach ( $badgeIds as $badgeId ) {
- $badgeSerialization = $badgeId->getSerialization();
- $classes[] = 'badge-' . Sanitizer::escapeClass(
$badgeSerialization );
-
- // nicer classes for well known badges
- if ( isset( $this->badgeClassNames[$badgeSerialization]
) ) {
- // add class name
- $classes[] = Sanitizer::escapeClass(
$this->badgeClassNames[$badgeSerialization] );
-
- // add label (but only if this badge is well
known on this wiki)
- $label = $this->getLabel( $badgeId );
-
- if ( $label !== null ) {
- $labels[] = $label;
- }
- }
- }
-
- $info = array(
- 'class' => implode( ' ', $classes ),
- 'label' => $this->language->commaList( $labels ),
- );
-
- return $info;
- }
-
- /**
- * Returns the label for the given badge.
- *
- * @param ItemId $badgeId
- *
- * @return string|null
- */
- private function getLabel( ItemId $badgeId ) {
- try {
- $term = $this->labelDescriptionLookup->getLabel(
$badgeId );
- } catch ( LabelDescriptionLookupException $ex ) {
- return null;
- }
-
- if ( $term !== null ) {
- return $term->getText();
- }
-
- return null;
+ $this->sidebarLinkBadgeDisplay->applyBadgeToLink(
$languageLink, $badges[$navId] );
}
}
diff --git a/client/includes/Hooks/SidebarLinkBadgeDisplay.php
b/client/includes/Hooks/SidebarLinkBadgeDisplay.php
new file mode 100644
index 0000000..a1dc2d5
--- /dev/null
+++ b/client/includes/Hooks/SidebarLinkBadgeDisplay.php
@@ -0,0 +1,145 @@
+<?php
+
+namespace Wikibase\Client\Hooks;
+
+use Language;
+use Sanitizer;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
+use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookupException;
+use Wikibase\Lib\Store\EntityLookup;
+
+/**
+ * Basic display logic to output badges in the sidebar
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Bene* < [email protected] >
+ * @author Daniel Kinzler
+ * @author Thomas Pellissier Tanon
+ */
+class SidebarLinkBadgeDisplay {
+
+ /**
+ * @var LabelDescriptionLookup
+ */
+ private $labelDescriptionLookup;
+
+ /**
+ * @var array
+ */
+ private $badgeClassNames;
+
+ /**
+ * @var Language
+ */
+ private $language;
+
+ /**
+ * @param LabelDescriptionLookup $labelDescriptionLookup
+ * @param string[] $badgeClassNames
+ * @param Language $language
+ */
+ public function __construct(
+ LabelDescriptionLookup $labelDescriptionLookup,
+ array $badgeClassNames,
+ Language $language
+ ) {
+ $this->labelDescriptionLookup = $labelDescriptionLookup;
+ $this->badgeClassNames = $badgeClassNames;
+ $this->language = $language;
+ }
+
+ /**
+ * Applies the badges described in the wikibase_badges property of
$output to
+ * the language link to $languageLinkTitle. The badge info for this
linked is
+ * looked up in the wikibase_badges data using the key returned by
+ * $languageLinkTitle->getInterwiki().
+ *
+ * This is generally called in the context of generating skin output.
+ *
+ * @since 0.5
+ *
+ * @param array $sidebarLink
+ * @param array $badgeInfo An associative array with the keys 'class'
and 'itemtitle' with assigned
+ * string values. These fields are the one outputted by the
getBadgeInfo() function.
+ */
+ public function applyBadgeToLink( array &$sidebarLink, array $badgeInfo
) {
+ if ( isset( $sidebarLink['class'] ) ) {
+ $sidebarLink['class'] .= ' ' . $badgeInfo['class'];
+ } else {
+ $sidebarLink['class'] = $badgeInfo['class'];
+ }
+
+ $sidebarLink['itemtitle'] = $badgeInfo['label'];
+ }
+
+ /**
+ * Builds badge information for the given badges.
+ * CSS classes are derived from the given list of badges, and any extra
badge class
+ * names specified in the badgeClassNames setting are added.
+ * For badges that have a such an extra class name assigned, this also
+ * adds a title according to the items' labels. Other badges do not
have labels
+ * added to the link's title attribute, so the can be effectively
ignored
+ * on this client wiki.
+ *
+ * @param ItemId[] $badgeIds
+ *
+ * @return array An associative array with the keys 'class' and
'itemtitle' with assigned
+ * string values. These fields correspond to the fields in the
description array for language
+ * links used by the SkinTemplateGetLanguageLink hook and expected by
the applyBadges()
+ * function.
+ */
+ public function getBadgeInfo( array $badgeIds ) {
+ $classes = [];
+ $labels = [];
+
+ foreach ( $badgeIds as $badgeId ) {
+ $badgeSerialization = $badgeId->getSerialization();
+ $classes[] = 'badge-' . Sanitizer::escapeClass(
$badgeSerialization );
+
+ // nicer classes for well known badges
+ if ( isset( $this->badgeClassNames[$badgeSerialization]
) ) {
+ // add class name
+ $classes[] = Sanitizer::escapeClass(
$this->badgeClassNames[$badgeSerialization] );
+
+ // add label (but only if this badge is well
known on this wiki)
+ $label = $this->getLabel( $badgeId );
+
+ if ( $label !== null ) {
+ $labels[] = $label;
+ }
+ }
+ }
+
+ $info = [
+ 'class' => implode( ' ', $classes ),
+ 'label' => $this->language->commaList( $labels ),
+ ];
+
+ return $info;
+ }
+
+ /**
+ * Returns the label for the given badge.
+ *
+ * @param ItemId $badgeId
+ *
+ * @return string|null
+ */
+ private function getLabel( ItemId $badgeId ) {
+ try {
+ $term = $this->labelDescriptionLookup->getLabel(
$badgeId );
+ } catch ( LabelDescriptionLookupException $ex ) {
+ return null;
+ }
+
+ if ( $term !== null ) {
+ return $term->getText();
+ }
+
+ return null;
+ }
+
+}
diff --git a/client/includes/WikibaseClient.php
b/client/includes/WikibaseClient.php
index 33b7578..f6cc602 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -40,6 +40,7 @@
use Wikibase\Client\Hooks\LanguageLinkBadgeDisplay;
use Wikibase\Client\Hooks\OtherProjectsSidebarGeneratorFactory;
use Wikibase\Client\Hooks\ParserFunctionRegistrant;
+use Wikibase\Client\Hooks\SidebarLinkBadgeDisplay;
use Wikibase\Client\ParserOutput\ClientParserOutputDataUpdater;
use Wikibase\Client\RecentChanges\RecentChangeFactory;
use Wikibase\Client\Serializer\ForbiddenSerializer;
@@ -901,9 +902,11 @@
$lang = $this->getUserLanguage();
return new LanguageLinkBadgeDisplay(
-
$labelDescriptionLookupFactory->newLabelDescriptionLookup( $lang ),
- is_array( $badgeClassNames ) ? $badgeClassNames :
array(),
- $lang
+ new SidebarLinkBadgeDisplay(
+
$labelDescriptionLookupFactory->newLabelDescriptionLookup( $lang ),
+ is_array( $badgeClassNames ) ? $badgeClassNames
: [],
+ $lang
+ )
);
}
diff --git
a/client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php
b/client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php
index 268f4fe..ba473a8 100644
--- a/client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php
+++ b/client/tests/phpunit/includes/Hooks/LanguageLinkBadgeDisplayTest.php
@@ -9,6 +9,7 @@
use RequestContext;
use Title;
use Wikibase\Client\Hooks\LanguageLinkBadgeDisplay;
+use Wikibase\Client\Hooks\SidebarLinkBadgeDisplay;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
@@ -47,11 +48,11 @@
$badgeClassNames = array( 'Q4' => 'foo', 'Q3' => 'bar' );
- return new LanguageLinkBadgeDisplay(
- $labelLookup,
+ $sidebarLinkBadgeDisplay = new SidebarLinkBadgeDisplay(
$labelLookup,
$badgeClassNames,
Language::factory( 'de' )
);
+ return new LanguageLinkBadgeDisplay( $sidebarLinkBadgeDisplay );
}
/**
diff --git
a/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php
b/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php
index 534c9dd..d18a014 100644
--- a/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php
+++ b/client/tests/phpunit/includes/Hooks/ParserOutputUpdateHookHandlersTest.php
@@ -13,6 +13,7 @@
use Wikibase\Client\Hooks\LanguageLinkBadgeDisplay;
use Wikibase\Client\Hooks\OtherProjectsSidebarGeneratorFactory;
use Wikibase\Client\Hooks\ParserOutputUpdateHookHandlers;
+use Wikibase\Client\Hooks\SidebarLinkBadgeDisplay;
use Wikibase\Client\ParserOutput\ClientParserOutputDataUpdater;
use Wikibase\Client\Usage\EntityUsage;
use Wikibase\Client\WikibaseClient;
@@ -186,9 +187,11 @@
->will( $this->returnValue( new Term( 'en', 'featured'
) ) );
return new LanguageLinkBadgeDisplay(
- $labelDescriptionLookup,
- array( 'Q17' => 'featured' ),
- Language::factory( 'en' )
+ new SidebarLinkBadgeDisplay(
+ $labelDescriptionLookup,
+ [ 'Q17' => 'featured' ],
+ Language::factory( 'en' )
+ )
);
}
diff --git a/client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php
b/client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php
index c11918c..9445cd0 100644
--- a/client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php
+++ b/client/tests/phpunit/includes/Hooks/SidebarHookHandlersTest.php
@@ -13,6 +13,7 @@
use Wikibase\Client\Hooks\OtherProjectsSidebarGenerator;
use Wikibase\Client\Hooks\OtherProjectsSidebarGeneratorFactory;
use Wikibase\Client\Hooks\SidebarHookHandlers;
+use Wikibase\Client\Hooks\SidebarLinkBadgeDisplay;
use Wikibase\Client\WikibaseClient;
use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
use Wikibase\NamespaceChecker;
@@ -107,9 +108,11 @@
$namespaceChecker = new NamespaceChecker( array(), $namespaces
);
$badgeDisplay = new LanguageLinkBadgeDisplay(
- $this->getLabelDescriptionLookup(),
- array( 'Q17' => 'featured' ),
- $en
+ new SidebarLinkBadgeDisplay(
+ $this->getLabelDescriptionLookup(),
+ [ 'Q17' => 'featured' ],
+ $en
+ )
);
return new SidebarHookHandlers(
diff --git
a/client/tests/phpunit/includes/Hooks/SidebarLinkBadgeDisplayTest.php
b/client/tests/phpunit/includes/Hooks/SidebarLinkBadgeDisplayTest.php
new file mode 100644
index 0000000..2626c6c
--- /dev/null
+++ b/client/tests/phpunit/includes/Hooks/SidebarLinkBadgeDisplayTest.php
@@ -0,0 +1,122 @@
+<?php
+
+namespace Wikibase\Client\Tests\Hooks;
+
+use Language;
+use Wikibase\Client\Hooks\SidebarLinkBadgeDisplay;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\Item;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
+use Wikibase\DataModel\Term\Term;
+
+/**
+ * @covers Wikibase\Client\Hooks\SidebarLinkBadgeDisplay
+ *
+ * @since 0.5
+ *
+ * @group WikibaseClient
+ * @group Wikibase
+ * @group Database
+ *
+ * @licence GNU GPL v2+
+ * @author Bene* < [email protected] >
+ * @author Thomas Pellissier Tanon
+ */
+class SidebarLinkBadgeDisplayTest extends \MediaWikiTestCase {
+
+ /**
+ * @return LabelDescriptionLookup
+ */
+ private function getLabelDescriptionLookup() {
+ $labelLookup = $this->getMock( LabelDescriptionLookup::class );
+
+ $labelLookup->expects( $this->any() )
+ ->method( 'getLabel' )
+ ->will( $this->returnCallback(
+ function ( EntityId $entityId ) {
+ $serialization =
$entityId->getSerialization();
+ if ( $serialization === 'Q3' ) {
+ return new Term( 'de',
'Lesenswerter Artikel' );
+ } elseif ( $serialization === 'Q4' ) {
+ return new Term( 'de',
'Exzellenter Artikel' );
+ } else {
+ return null;
+ }
+ }
+ ) );
+
+ return $labelLookup;
+ }
+
+ /**
+ * @return SidebarLinkBadgeDisplay
+ */
+ private function getSidebarLinkBadgeDisplay() {
+
+ $badgeClassNames = [ 'Q4' => 'foo', 'Q3' => 'bar' ];
+
+ return new SidebarLinkBadgeDisplay(
+ $this->getLabelDescriptionLookup(),
+ $badgeClassNames,
+ Language::factory( 'de' )
+ );
+ }
+
+ /**
+ * @dataProvider getBadgeInfoProvider
+ */
+ public function testGetBadgeInfo( $expected, $badges ) {
+ $sidebarLinkBadgeDisplay = $this->getSidebarLinkBadgeDisplay();
+
+ $this->assertEquals( $expected,
$sidebarLinkBadgeDisplay->getBadgeInfo( $badges ) );
+ }
+
+ public function getBadgeInfoProvider() {
+ $q2 = new ItemId( 'Q2' );
+ $q3 = new ItemId( 'Q3' );
+ $q4 = new ItemId( 'Q4' );
+
+ return [
+ [ [ 'class' => '', 'label' => '' ], [] ],
+ [
+ [
+ 'class' => 'badge-Q3 bar badge-Q2',
+ 'label' => 'Lesenswerter Artikel'
+ ],
+ [ $q3, $q2 ]
+ ],
+ [
+ [
+ 'class' => 'badge-Q3 bar badge-Q4 foo',
+ 'label' => 'Lesenswerter Artikel,
Exzellenter Artikel'
+ ],
+ [ $q3, $q4 ]
+ ],
+ ];
+ }
+
+ public function testApplyBadges() {
+ $badgeInfo = [
+ 'class' => 'badge-Q3',
+ 'label' => 'Lesenswerter Artikel',
+ ];
+
+ $link = [
+ 'href' => 'http://acme.com',
+ 'class' => 'foo',
+ ];
+
+ $expected = [
+ 'href' => 'http://acme.com',
+ 'class' => 'foo badge-Q3',
+ 'itemtitle' => 'Lesenswerter Artikel',
+ ];
+
+ $sidebarLinkBadgeDisplay = $this->getSidebarLinkBadgeDisplay();
+ $sidebarLinkBadgeDisplay->applyBadgeToLink( $link, $badgeInfo );
+
+ $this->assertEquals( $expected, $link );
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/355899
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I2ead941fb0e945a564aff6b2a4f074beedf89580
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits