jenkins-bot has submitted this change and it was merged.

Change subject: Insert classes for badges on client
......................................................................


Insert classes for badges on client

Uses the created hook SkinTemplateGetLanguageLink to add a CSS class
for each badge of a sitelink. The setting badgeClassNames introduced in
another change is also used to determine which badges should be displayed
primarily. For the purpose another css class specified by this setting is
added and the label of this badges' item is used as the title of the
icon indicating the badge using the 'itemtitle' property to set the title
of the <li> element. The icons themselves are also added in another
change.

The clients can add additional styles or override the ones from the config
by using their local Commmon.css page in the MediaWiki namespace. They can
rely on the fact that each badge is always added as a class in the form
"badge-Q123" regardless of whether it is specified in the config or not.

Bug: 60717
Change-Id: If4252b7804ad017075684b794d572843ab6e1ccb
---
M client/WikibaseClient.hooks.php
M client/WikibaseClient.php
A client/includes/hooks/LanguageLinkBadgeDisplay.php
A client/tests/phpunit/includes/hooks/LanguageLinkBadgeDisplayTest.php
4 files changed, 332 insertions(+), 1 deletion(-)

Approvals:
  WikidataJenkins: Verified
  Daniel Kinzler: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index b17de43..66e730d 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -33,6 +33,8 @@
 use Wikibase\Client\Hooks\SpecialWatchlistQueryHandler;
 use Wikibase\Client\MovePageNotice;
 use Wikibase\Client\Hooks\OtherProjectsSidebarGenerator;
+use Wikibase\Client\Hooks\LanguageLinkBadgeDisplay;
+use Wikibase\Client\ClientSiteLinkLookup;
 use Wikibase\Client\WikibaseClient;
 
 /**
@@ -48,6 +50,7 @@
  * @author Tobias Gritschacher
  * @author Jeroen De Dauw < jeroended...@gmail.com >
  * @author Marius Hoch < h...@online.de >
+ * @author Bene* < benestar.wikime...@gmail.com >
  */
 final class ClientHooks {
 
@@ -400,6 +403,48 @@
        }
 
        /**
+        * Add badges to the language links.
+        *
+        * @since 0.5
+        *
+        * @param array &$languageLink
+        * @param Title $languageLinkTitle
+        * @param Title $title
+        *
+        * @return bool
+        */
+       public static function onSkinTemplateGetLanguageLink( &$languageLink, 
Title $languageLinkTitle, Title $title ) {
+               wfProfileIn( __METHOD__ );
+
+               global $wgLang;
+
+               $wikibaseClient = WikibaseClient::getDefaultInstance();
+               $settings = $wikibaseClient->getSettings();
+
+               $clientSiteLinkLookup = 
$wikibaseClient->getClientSiteLinkLookup();
+               $entityLookup = $wikibaseClient->getStore()->getEntityLookup();
+               $sites = $wikibaseClient->getSiteStore()->getSites();
+               $badgeClassNames = $settings->getSetting( 'badgeClassNames' );
+
+               if ( !is_array( $badgeClassNames ) ) {
+                       $badgeClassNames = array();
+               }
+
+               $languageLinkBadgeDisplay = new LanguageLinkBadgeDisplay(
+                       $clientSiteLinkLookup,
+                       $entityLookup,
+                       $sites,
+                       $badgeClassNames,
+                       $wgLang
+               );
+
+               $languageLinkBadgeDisplay->assignBadges( $title, 
$languageLinkTitle, $languageLink );
+
+               wfProfileOut( __METHOD__ );
+               return true;
+       }
+
+       /**
         * Add Wikibase item link in toolbox
         *
         * @since 0.4
@@ -407,7 +452,7 @@
         * @param QuickTemplate &$sk
         * @param array &$toolbox
         *
-        * @return boolean
+        * @return bool
         */
        public static function onBaseTemplateToolbox( QuickTemplate &$sk, 
&$toolbox ) {
                $prefixedId = $sk->getSkin()->getOutput()->getProperty( 
'wikibase_item' );
diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php
index fca740c..f028477 100644
--- a/client/WikibaseClient.php
+++ b/client/WikibaseClient.php
@@ -75,6 +75,7 @@
        $wgHooks['ParserFirstCallInit'][]                       = 
'\Wikibase\ClientHooks::onParserFirstCallInit';
        $wgHooks['MagicWordwgVariableIDs'][]                    = 
'\Wikibase\ClientHooks::onMagicWordwgVariableIDs';
        $wgHooks['ParserGetVariableValueSwitch'][]              = 
'\Wikibase\ClientHooks::onParserGetVariableValueSwitch';
+       $wgHooks['SkinTemplateGetLanguageLink'][]               = 
'\Wikibase\ClientHooks::onSkinTemplateGetLanguageLink';
        $wgHooks['SkinTemplateOutputPageBeforeExec'][]          = 
'\Wikibase\ClientHooks::onSkinTemplateOutputPageBeforeExec';
        $wgHooks['SkinBuildSidebar'][]              = 
'\Wikibase\ClientHooks::onSkinBuildSidebar';
        $wgHooks['SpecialMovepageAfterMove'][]                          = 
'\Wikibase\ClientHooks::onSpecialMovepageAfterMove';
diff --git a/client/includes/hooks/LanguageLinkBadgeDisplay.php 
b/client/includes/hooks/LanguageLinkBadgeDisplay.php
new file mode 100644
index 0000000..449d2c8
--- /dev/null
+++ b/client/includes/hooks/LanguageLinkBadgeDisplay.php
@@ -0,0 +1,170 @@
+<?php
+
+namespace Wikibase\Client\Hooks;
+
+use Title;
+use Language;
+use SiteList;
+use Sanitizer;
+use Wikibase\EntityLookup;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\Client\ClientSiteLinkLookup;
+
+/**
+ * Provides access to the badges of the current page's sitelinks
+ * and adds some properties to the HTML output to display them.
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Bene* < benestar.wikime...@gmail.com >
+ */
+class LanguageLinkBadgeDisplay {
+
+       /**
+        * @var ClientSiteLinkLookup
+        */
+       protected $clientSiteLinkLookup;
+
+       /**
+        * @var EntityLookup
+        */
+       protected $entityLookup;
+
+       /**
+        * @var SiteList
+        */
+       protected $sites;
+
+       /**
+        * @var array
+        */
+       protected $badgeClassNames;
+
+       /**
+        * @var Language
+        */
+       protected $language;
+
+       /**
+        * @param ClientSiteLinkLookup $clientSiteLinkLookup
+        * @param EntityLookup $entityLookup
+        * @param SiteList $sites
+        * @param array $badgeClassNames
+        * @param Language $language
+        */
+       public function __construct( ClientSiteLinkLookup $clientSiteLinkLookup,
+                       EntityLookup $entityLookup, SiteList $sites, array 
$badgeClassNames, Language $language ) {
+               $this->clientSiteLinkLookup = $clientSiteLinkLookup;
+               $this->entityLookup = $entityLookup;
+               $this->sites = $sites;
+               $this->badgeClassNames = $badgeClassNames;
+               $this->language = $language;
+       }
+
+       /**
+        * Looks up the item of the given title and gets all badges for the 
sitelink to
+        * the language link title. These badges are added as CSS classes to 
the language link.
+        *
+        * @since 0.5
+        *
+        * @param Title $title
+        * @param Title $languageLinkTitle
+        * @param array &$languageLink
+        */
+       public function assignBadges( Title $title, Title $languageLinkTitle, 
array &$languageLink ) {
+               $navId = $languageLinkTitle->getInterwiki();
+               if ( !$this->sites->hasNavigationId( $navId ) ) {
+                       return;
+               }
+
+               $site = $this->sites->getSiteByNavigationId( $navId );
+               $siteLink = $this->clientSiteLinkLookup->getSiteLink( $title, 
$site->getGlobalId() );
+               if ( $siteLink === null ) {
+                       return;
+               }
+
+               $badges = $siteLink->getBadges();
+               if ( empty( $badges ) ) {
+                       return;
+               }
+
+               $classes = $this->formatClasses( $badges );
+               if ( !isset( $languageLink['class'] ) ) {
+                       $languageLink['class'] = $classes;
+               } else {
+                       $languageLink['class'] .= ' ' . $classes;
+               }
+
+               $this->assignExtraBadges( $badges, $languageLink );
+       }
+
+       /**
+        * Formats the badges array into a string of classes.
+        *
+        * @param ItemId[] $badges
+        *
+        * @return string
+        */
+       private function formatClasses( array $badges ) {
+               $classes = '';
+               /* @var ItemId $badge */
+               foreach ( $badges as $badge ) {
+                       $class = Sanitizer::escapeClass( 
$badge->getSerialization() );
+                       $classes .= "badge-$class ";
+               }
+               return rtrim( $classes );
+       }
+
+       /**
+        * Assigns the extra badge class names specified in the badgeClassNames 
setting
+        * to the language link and also adds a title according to the items' 
labels.
+        *
+        * @param array $badges
+        * @param array &$languageLink
+        */
+       private function assignExtraBadges( array $badges, array &$languageLink 
) {
+               $titles = array();
+               /** @var ItemId $badge */
+               foreach ( $badges as $badge ) {
+                       $badgeSerialization = $badge->getSerialization();
+                       if ( isset( $this->badgeClassNames[$badgeSerialization] 
) ) {
+                               // add class name
+                               $className = Sanitizer::escapeClass( 
$this->badgeClassNames[$badgeSerialization] );
+                               $languageLink['class'] .= ' ' . $className;
+
+                               // add title
+                               $title = $this->getTitle( $badge );
+                               if ( $title !== null ) {
+                                       $titles[] = $title;
+                               }
+                       }
+               }
+               if ( !empty( $titles ) ) {
+                       $languageLink['itemtitle'] = 
$this->language->commaList( $titles );
+               }
+       }
+
+       /**
+        * Returns the title for the given badge.
+        *
+        * @since 0.5
+        *
+        * @param ItemId $badge
+        *
+        * @return string|null
+        */
+       private function getTitle( ItemId $badge ) {
+               $entity = $this->entityLookup->getEntity( $badge );
+               if ( !$entity ) {
+                       return null;
+               }
+
+               $title = $entity->getLabel( $this->language->getCode() );
+               if ( !$title ) {
+                       return null;
+               }
+               return $title;
+       }
+
+}
diff --git 
a/client/tests/phpunit/includes/hooks/LanguageLinkBadgeDisplayTest.php 
b/client/tests/phpunit/includes/hooks/LanguageLinkBadgeDisplayTest.php
new file mode 100644
index 0000000..691b1a4
--- /dev/null
+++ b/client/tests/phpunit/includes/hooks/LanguageLinkBadgeDisplayTest.php
@@ -0,0 +1,115 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Wikibase\Client\Hooks\LanguageLinkBadgeDisplay;
+use Wikibase\Client\ClientSiteLinkLookup;
+use Wikibase\DataModel\SiteLink;
+use Wikibase\DataModel\Entity\Item;
+use Language;
+use Title;
+
+/**
+ * @covers Wikibase\Client\Hooks\LanguageLinkBadgeDisplay
+ *
+ * @since 0.5
+ *
+ * @group WikibaseClient
+ * @group Wikibase
+ * @group Database
+ *
+ * @licence GNU GPL v2+
+ * @author Bene* < benestar.wikime...@gmail.com >
+ */
+class LanguageLinkBadgeDisplayTest extends \MediaWikiTestCase {
+
+       static $itemData = array(
+               1 => array(
+                       'id' => 1,
+                       'links' => array(
+                               'dewiki' => 'Georg Friedrich Haendel',
+                               'enwiki' => array(
+                                       'name' => 'George Frideric Handel',
+                                       'badges' => array( 'Q3', 'Q2' )
+                               ),
+                               'nlwiki' => 'Georg Friedrich Haendel'
+                       )
+               ),
+               2 => array(
+                       'id' => 2,
+                       'links' => array(
+                               'dewiki' => 'Benutzer:Testbenutzer',
+                               'enwiki' => array(
+                                       'name' => 'User:Testuser',
+                                       'badges' => array( 'Q3', 'Q4' )
+                               )
+                       )
+               ),
+               3 => array(
+                       'id' => 3,
+                       'label' => array(
+                               'en' => 'Good article',
+                               'de' => 'Lesenswerter Artikel'
+                       )
+               ),
+               4 => array(
+                       'id' => 4,
+                       'label' => array(
+                               'en' => 'Featured article',
+                               'de' => 'Exzellenter Artikel'
+                       )
+               )
+       );
+
+       private function getLanguageLinkBadgeDisplay() {
+               $mockRepo = new MockRepository();
+
+               foreach ( self::$itemData as $data ) {
+                       $item = new Item( $data );
+                       $mockRepo->putEntity( $item );
+               }
+
+               $sites = MockSiteStore::newFromTestSites()->getSites();
+               $clientSiteLinkLookup = new ClientSiteLinkLookup( 'dewiki', 
$mockRepo, $mockRepo );
+               $badgeClassNames = array( 'Q4' => 'foo', 'Q3' => 'bar' );
+
+               return new LanguageLinkBadgeDisplay(
+                       $clientSiteLinkLookup,
+                       $mockRepo,
+                       $sites,
+                       $badgeClassNames,
+                       Language::factory( 'de' )
+               );
+       }
+
+       /**
+        * @dataProvider assignBadgesProvider
+        */
+       public function testAssignBadges( $expected, Title $title, Title 
$languageLinkTitle, $message ) {
+               $languageLinkBadgeDisplay = 
$this->getLanguageLinkBadgeDisplay();
+
+               $languageLink = array();
+               $languageLinkBadgeDisplay->assignBadges( $title, 
$languageLinkTitle, $languageLink );
+
+               $this->assertEquals( $expected, $languageLink, $message );
+       }
+
+       public function assignBadgesProvider() {
+               $languageLink1 = array(
+                       'class' => 'badge-Q3 badge-Q2 bar',
+                       'itemtitle' => 'Lesenswerter Artikel'
+               );
+               $languageLink2 = array(
+                       'class' => 'badge-Q3 badge-Q4 bar foo',
+                       'itemtitle' => 'Lesenswerter Artikel, Exzellenter 
Artikel'
+               );
+               return array(
+                       array( $languageLink1, Title::newFromText( 'Georg 
Friedrich Haendel' ), Title::makeTitle( NS_MAIN, 'George Frideric Handel', '', 
'en' ), 'passing enwiki title' ),
+                       array( $languageLink2, Title::newFromText( 
'Benutzer:Testbenutzer' ), Title::makeTitle( NS_USER, 'Testuser', '', 'en' ), 
'passing enwiki non-main namespace title' ),
+                       array( array(), Title::newFromText( 'Georg Friedrich 
Haendel' ), Title::makeTitle( NS_MAIN, 'Georg Friedrich Haendel', '', 'nl' ), 
'passing nlwiki title' ),
+                       array( array(), Title::newFromText( 'Johann Sebastian 
Bach' ), Title::makeTitle( NS_MAIN, 'Johann Sebastian Bach', '', 'en' ), 
'passing an unknown title' ),
+                       array( array(), Title::newFromText( 'Georg Friedrich 
Haendel' ), Title::makeTitle( NS_MAIN, 'Georg Friedrich Haendel', '', 'it' ), 
'passing a site without link' ),
+               );
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/124605
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If4252b7804ad017075684b794d572843ab6e1ccb
Gerrit-PatchSet: 12
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Bene <benestar.wikime...@gmail.com>
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: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: Lydia Pintscher <lydia.pintsc...@wikimedia.de>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>
Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de>
Gerrit-Reviewer: WikidataJenkins <wikidata-servi...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to