jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/324216 )

Change subject: Provide special page with list of all available badges
......................................................................


Provide special page with list of all available badges

Bug: T114473
Change-Id: I8cd2912b629bc4e9f744156653b28cd4c5ef531a
---
M repo/Wikibase.i18n.alias.php
M repo/Wikibase.php
M repo/i18n/en.json
M repo/i18n/qqq.json
A repo/includes/Specials/SpecialAvailableBadges.php
A repo/tests/phpunit/includes/Specials/SpecialAvailableBadgesTest.php
6 files changed, 185 insertions(+), 0 deletions(-)

Approvals:
  WMDE-leszek: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/repo/Wikibase.i18n.alias.php b/repo/Wikibase.i18n.alias.php
index f8450da..06b8938 100644
--- a/repo/Wikibase.i18n.alias.php
+++ b/repo/Wikibase.i18n.alias.php
@@ -12,6 +12,7 @@
 
 /** English (English) */
 $specialPageAliases['en'] = array(
+       'AvailableBadges' => array( 'AvailableBadges' ),
        'DispatchStats' => array( 'DispatchStats' ),
        'EntitiesWithoutDescription' => array( 'EntitiesWithoutDescription' ),
        'EntitiesWithoutLabel' => array( 'EntitiesWithoutLabel' ),
diff --git a/repo/Wikibase.php b/repo/Wikibase.php
index cfb8947..9893290 100644
--- a/repo/Wikibase.php
+++ b/repo/Wikibase.php
@@ -842,6 +842,15 @@
                        )
                );
        };
+       $wgSpecialPages['AvailableBadges'] = function() {
+               $wikibaseRepo = 
Wikibase\Repo\WikibaseRepo::getDefaultInstance();
+
+               return new Wikibase\Repo\Specials\SpecialAvailableBadges(
+                       $wikibaseRepo->getPrefetchingTermLookup(),
+                       $wikibaseRepo->getEntityTitleLookup(),
+                       $wikibaseRepo->getSettings()->getSetting( 'badgeItems' )
+               );
+       };
 
        // Jobs
        $wgJobClasses['UpdateRepoOnMove'] = 
Wikibase\Repo\UpdateRepo\UpdateRepoOnMoveJob::class;
diff --git a/repo/i18n/en.json b/repo/i18n/en.json
index eb628bc..fc6e331 100644
--- a/repo/i18n/en.json
+++ b/repo/i18n/en.json
@@ -147,6 +147,8 @@
        "wikibase-wikibaserepopage-invalid-id": "The ID \"$1\" is unknown to 
the system. Please use a valid entity ID.",
        "wikibase-wikibaserepopage-unresolved-redirect": "$1 is a redirect.",
        "wikibase-wikibaserepopage-storage-exception": "An error occurred while 
trying to load $1: $2.",
+       "special-availablebadges": "Available badges",
+       "wikibase-availablebadges-emptylist": "There are no badges configured 
on this wiki.",
        "special-itembytitle": "Item by title",
        "wikibase-itembytitle-lookup-fieldset": "Search for items by site and 
title",
        "wikibase-itembytitle-lookup-site": "Site:",
diff --git a/repo/i18n/qqq.json b/repo/i18n/qqq.json
index 8fb3d5c..0b1ee99 100644
--- a/repo/i18n/qqq.json
+++ b/repo/i18n/qqq.json
@@ -179,6 +179,8 @@
        "wikibase-wikibaserepopage-invalid-id": "Response informing that the 
selected entity ID is not valid.\n\nParameters:\n* $1 - the invalid ID",
        "wikibase-wikibaserepopage-unresolved-redirect": "Error message shown 
when the user supplied an entity ID that refers to a redirect.\nParameters:\n* 
$1 - the entity ID",
        "wikibase-wikibaserepopage-storage-exception": "Error message shown 
when an entity could not be loaded due to a storage layer 
exception.\nParameters:\n* $1 - the entity ID\n* $2 - the (unlocalized and 
possibly technical) error message.",
+       "special-availablebadges": "{{doc-special|AvailableBadges}}\nTitle of 
the page containing a list of all available badges",
+       "wikibase-availablebadges-emptylist": "Message indicating that there 
are no available badges.",
        "special-itembytitle": "{{doc-special|ItemByTitle}}\nThe item is 
identified through use of the site and title, but the lookup failed and further 
qualification must be done. See also the Wikidata glossary for 
[[d:Wikidata:Glossary#languageattribute-label|label]] and 
[[d:Wikidata:Glossary#Items|items]].",
        "wikibase-itembytitle-lookup-fieldset": "This is the title for the 
fieldset on the special page for further refining the search. This is the 
search by site and title.",
        "wikibase-itembytitle-lookup-site": "Label for the textfield holding 
the site id. See also the Wikidata glossary for 
[[d:Wikidata:Glossary#sitelinks|sitelinks]].\n{{Identical|Site}}",
diff --git a/repo/includes/Specials/SpecialAvailableBadges.php 
b/repo/includes/Specials/SpecialAvailableBadges.php
new file mode 100644
index 0000000..efa26e8
--- /dev/null
+++ b/repo/includes/Specials/SpecialAvailableBadges.php
@@ -0,0 +1,107 @@
+<?php
+
+namespace Wikibase\Repo\Specials;
+
+use Html;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\ItemIdParser;
+use Wikibase\Lib\Store\EntityTitleLookup;
+use Wikibase\Lib\Store\PrefetchingTermLookup;
+
+/**
+ * Page for listing all available badges.
+ *
+ * @license GPL-2.0+
+ * @author Victor Barbu < victorbarb...@gmail.com >
+ * @author Amir Sarabadani <ladsgr...@gmail.com>
+ */
+class SpecialAvailableBadges extends SpecialWikibasePage {
+
+       /**
+        * @var PrefetchingTermLookup
+        */
+       private $prefetchingTermLookup;
+
+       /**
+        * @var EntityTitleLookup
+        */
+       private $entityTitleLookup;
+
+       /**
+        * @var string[]
+        */
+       private $badgeItems;
+
+       public function __construct(
+               PrefetchingTermLookup $prefetchingTermLookup,
+               EntityTitleLookup $entityTitleLookup,
+               array $badgeItems
+       ) {
+               parent::__construct( 'AvailableBadges' );
+
+               $this->prefetchingTermLookup = $prefetchingTermLookup;
+               $this->entityTitleLookup = $entityTitleLookup;
+               $this->badgeItems = $badgeItems;
+       }
+
+       public function execute( $subPage ) {
+               parent::execute( $subPage );
+
+               $this->displayResult();
+       }
+
+       private function displayResult() {
+               $out = $this->getOutput();
+               // XXX: Maybe we should use PrefixMappingEntityIdParser for 
federation?
+               $itemIdParser = new ItemIdParser();
+
+               $itemIds = array_map( function( $item ) use ( $itemIdParser ) {
+                       return $itemIdParser->parse( $item );
+               }, array_keys( $this->badgeItems ) );
+
+               if ( empty( $itemIds ) ) {
+                       $out->addHTML( Html::element(
+                               'p',
+                               [],
+                               $this->msg( 
'wikibase-availablebadges-emptylist' )->text()
+                       ) );
+
+                       return;
+               }
+
+               $this->prefetchingTermLookup->prefetchTerms( $itemIds );
+
+               $out->addHTML( Html::openElement( 'ol' ) );
+               foreach ( $itemIds as $item ) {
+                       $this->displayRow( $item, 
$this->badgeItems[$item->getSerialization()] );
+               }
+               $out->addHTML( Html::closeElement( 'ol' ) );
+       }
+
+       /**
+        * Render one badge.
+        *
+        * @param ItemId $item Item ID to render
+        * @param string $badgeClass The given badge class
+        */
+       private function displayRow( ItemId $item, $badgeClass ) {
+               $out = $this->getOutput();
+
+               $title = $this->entityTitleLookup->getTitleForId( $item );
+               $description = $this->prefetchingTermLookup->getDescription(
+                       $item,
+                       $this->getLanguage()->getCode()
+               );
+
+               $out->addHTML( Html::openElement( 'li' ) );
+               $out->addHTML( Html::element( 'span', [
+                       'class' => 'wb-badge ' . $badgeClass,
+               ] ) );
+               $out->addHTML( $this->getLinkRenderer()->makeLink( $title ) );
+               if ( $description !== null ) {
+                       $out->addHTML( ' - ' . $description );
+               }
+               $out->addHTML( Html::closeElement( 'li' ) );
+       }
+
+}
diff --git 
a/repo/tests/phpunit/includes/Specials/SpecialAvailableBadgesTest.php 
b/repo/tests/phpunit/includes/Specials/SpecialAvailableBadgesTest.php
new file mode 100644
index 0000000..8765146
--- /dev/null
+++ b/repo/tests/phpunit/includes/Specials/SpecialAvailableBadgesTest.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace Wikibase\Repo\Tests\Specials;
+
+use Language;
+use SpecialPageTestBase;
+use Title;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\Lib\Store\EntityTitleLookup;
+use Wikibase\Lib\Store\PrefetchingTermLookup;
+use Wikibase\Repo\Specials\SpecialAvailableBadges;
+
+/**
+ * @covers Wikibase\Repo\Specials\SpecialAvailableBadges
+ *
+ * @group Wikibase
+ * @group SpecialPage
+ * @group WikibaseSpecialPage
+ *
+ * @license GPL-2.0+
+ * @author Amir Sarabadani <ladsgr...@gamil.com>
+ */
+class SpecialAvailableBadgesTest extends SpecialPageTestBase {
+
+       protected function setUp() {
+               parent::setUp();
+
+               $this->setMwGlobals( [ 'wgContLang' => Language::factory( 'qqx' 
) ] );
+       }
+
+       protected function newSpecialPage() {
+               $prefetchingTermLookup = $this->getMockBuilder( 
PrefetchingTermLookup::class )->getMock();
+               $prefetchingTermLookup->expects( $this->any() )
+                       ->method( 'getDescription' )
+                       ->willReturn( 'Test badge item' );
+
+               $entityTitleLookup = $this->getMockBuilder( 
EntityTitleLookup::class )->getMock();
+               $entityTitleLookup->expects( $this->any() )
+                       ->method( 'getTitleForId' )
+                       ->willReturnCallback( function ( ItemId $itemId ) {
+                               return Title::makeTitle( 0, 
$itemId->getSerialization() );
+                       } );
+
+               $badgeItems = [ 'Q4' => 'test-badge' ];
+               return new SpecialAvailableBadges(
+                       $prefetchingTermLookup,
+                       $entityTitleLookup,
+                       $badgeItems
+               );
+       }
+
+       public function testExecute() {
+               list( $output, ) = $this->executeSpecialPage( '' );
+
+               $this->assertInternalType( 'string', $output );
+               $this->assertContains( 'mw-specialpage-summary', $output );
+               $this->assertContains( 'wikibase-availablebadges-summary', 
$output );
+
+               $this->assertContains( '<li><span class="wb-badge 
test-badge"></span>', $output );
+               $this->assertContains( 'Q4', $output );
+               $this->assertContains( 'Test badge item', $output );
+       }
+
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I8cd2912b629bc4e9f744156653b28cd4c5ef531a
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Victorbarbu <victorbarb...@gmail.com>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchane...@gmail.com>
Gerrit-Reviewer: Siebrand <siebr...@kitano.nl>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>
Gerrit-Reviewer: Victorbarbu <victorbarb...@gmail.com>
Gerrit-Reviewer: WMDE-leszek <leszek.mani...@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