Bene has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/235043

Change subject: Add badges to page props
......................................................................

Add badges to page props

Bug: T72229
Change-Id: I6e4a86a9b009e8f17b4777c3437797599d4671e7
---
M client/includes/Hooks/ParserOutputUpdateHookHandlers.php
M client/includes/ParserOutputDataUpdater.php
M client/includes/WikibaseClient.php
M client/tests/phpunit/includes/ParserOutputDataUpdaterTest.php
4 files changed, 64 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/43/235043/1

diff --git a/client/includes/Hooks/ParserOutputUpdateHookHandlers.php 
b/client/includes/Hooks/ParserOutputUpdateHookHandlers.php
index a9b0c7e..b70c8db 100644
--- a/client/includes/Hooks/ParserOutputUpdateHookHandlers.php
+++ b/client/includes/Hooks/ParserOutputUpdateHookHandlers.php
@@ -134,6 +134,7 @@
 
                $this->parserOutputDataUpdater->updateItemIdProperty( $title, 
$parserOutput );
                $this->parserOutputDataUpdater->updateOtherProjectsLinksData( 
$title, $parserOutput );
+               $this->parserOutputDataUpdater->updateBadgesProperty( $title, 
$parserOutput );
 
                if ( $useRepoLinks || $this->alwaysSort ) {
                        $interwikiLinks = $parserOutput->getLanguageLinks();
diff --git a/client/includes/ParserOutputDataUpdater.php 
b/client/includes/ParserOutputDataUpdater.php
index bf5bb99..da47ad0 100644
--- a/client/includes/ParserOutputDataUpdater.php
+++ b/client/includes/ParserOutputDataUpdater.php
@@ -7,7 +7,9 @@
 use Title;
 use Wikibase\Client\Hooks\OtherProjectsSidebarGeneratorFactory;
 use Wikibase\Client\Usage\ParserOutputUsageAccumulator;
+use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Services\Lookup\EntityLookup;
 use Wikibase\Lib\Store\SiteLinkLookup;
 
 /**
@@ -27,6 +29,11 @@
        private $otherProjectsSidebarGeneratorFactory;
 
        /**
+        * @var EntityLookup
+        */
+       private $entityLookup;
+
+       /**
         * @var SiteLinkLookup
         */
        private $siteLinkLookup;
@@ -40,11 +47,13 @@
         * @param OtherProjectsSidebarGeneratorFactory 
$otherProjectsSidebarGeneratorFactory
         *            Use the factory here to defer initialization of things 
like Site objects.
         * @param SiteLinkLookup $siteLinkLookup
+        * @param EntityLookup $entityLookup
         * @param string $siteId The global site ID for the local wiki
         */
        public function __construct(
                OtherProjectsSidebarGeneratorFactory 
$otherProjectsSidebarGeneratorFactory,
                SiteLinkLookup $siteLinkLookup,
+               EntityLookup $entityLookup,
                $siteId
        ) {
                if ( !is_string( $siteId ) ) {
@@ -52,6 +61,7 @@
                }
 
                $this->otherProjectsSidebarGeneratorFactory = 
$otherProjectsSidebarGeneratorFactory;
+               $this->entityLookup = $entityLookup;
                $this->siteLinkLookup = $siteLinkLookup;
                $this->siteId = $siteId;
        }
@@ -83,8 +93,8 @@
                $itemId = $this->getItemIdForTitle( $title );
 
                if ( $itemId ) {
-                       $otherProjectsSidebarGenerator = 
$this->otherProjectsSidebarGeneratorFactory->
-                               getOtherProjectsSidebarGenerator();
+                       $otherProjectsSidebarGenerator = 
$this->otherProjectsSidebarGeneratorFactory
+                               ->getOtherProjectsSidebarGenerator();
 
                        $otherProjects = 
$otherProjectsSidebarGenerator->buildProjectLinkSidebar( $title );
                        $out->setExtensionData( 
'wikibase-otherprojects-sidebar', $otherProjects );
@@ -95,6 +105,31 @@
 
        /**
         * @param Title $title
+        * @param ParserOutput $out
+        */
+       public function updateBadgesProperty( Title $title, ParserOutput $out ) 
{
+               $itemId = $this->getItemIdForTitle( $title );
+
+               // first reset all badges in case one got removed
+               foreach ( $out->getProperties() as $name => $property ) {
+                       if ( strpos( $name, 'wikibase-badge-' ) === 0 ) {
+                               $out->unsetProperty( $name );
+                       }
+               }
+
+               if ( $itemId ) {
+                       /** @var Item $item */
+                       $item = $this->entityLookup->getEntity( $itemId );
+                       $siteLink = $item->getSiteLinkList()->getBySiteId( 
$this->siteId );
+
+                       foreach ( $siteLink->getBadges() as $badge ) {
+                               $out->setProperty( 'wikibase-badge-' . 
$badge->getSerialization(), true );
+                       }
+               }
+       }
+
+       /**
+        * @param Title $title
         *
         * @return ItemId|null
         */
diff --git a/client/includes/WikibaseClient.php 
b/client/includes/WikibaseClient.php
index d2d7e32..2cde41a 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -582,6 +582,7 @@
                        $this->parserOutputDataUpdater = new 
ParserOutputDataUpdater(
                                
$this->getOtherProjectsSidebarGeneratorFactory(),
                                $this->getStore()->getSiteLinkLookup(),
+                               $this->getStore()->getEntityLookup(),
                                $this->settings->getSetting( 'siteGlobalID' )
                        );
                }
diff --git a/client/tests/phpunit/includes/ParserOutputDataUpdaterTest.php 
b/client/tests/phpunit/includes/ParserOutputDataUpdaterTest.php
index 6186af2..c66e054 100644
--- a/client/tests/phpunit/includes/ParserOutputDataUpdaterTest.php
+++ b/client/tests/phpunit/includes/ParserOutputDataUpdaterTest.php
@@ -49,7 +49,7 @@
                $links = $item->getSiteLinkList();
                $links->addNewSiteLink( 'dewiki', 'Talk:Foo de' );
                $links->addNewSiteLink( 'enwiki', 'Talk:Foo en' );
-               $links->addNewSiteLink( 'srwiki', 'Talk:Foo sr' );
+               $links->addNewSiteLink( 'srwiki', 'Talk:Foo sr', array( new 
ItemId( 'Q17' ) ) );
                $items[] = $item;
 
                return $items;
@@ -69,6 +69,7 @@
 
                return new ParserOutputDataUpdater(
                        $this->getOtherProjectsSidebarGeneratorFactory( 
$otherProjects ),
+                       $this->mockRepo,
                        $this->mockRepo,
                        'srwiki'
                );
@@ -191,4 +192,27 @@
                );
        }
 
+       public function testUpdateBadgesProperty() {
+               $parserOutput = new ParserOutput();
+
+               $title = Title::newFromText( 'Talk:Foo sr' );
+
+               $parserOutputDataUpdater = $this->getParserOutputDataUpdater();
+
+               $parserOutputDataUpdater->updateBadgesProperty( $title, 
$parserOutput );
+               $this->assertTrue( $parserOutput->getProperty( 
'wikibase-badge-Q17' ) );
+       }
+
+       public function testUpdateBadgesPropertyRemovesPreviousData() {
+               $parserOutput = new ParserOutput();
+               $parserOutput->setProperty( 'wikibase-badge-Q17', true );
+
+               $title = Title::newFromText( 'Foo sr' );
+
+               $parserOutputDataUpdater = $this->getParserOutputDataUpdater();
+
+               $parserOutputDataUpdater->updateBadgesProperty( $title, 
$parserOutput );
+               $this->assertFalse( $parserOutput->getProperty( 
'wikibase-badge-Q17' ) );
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6e4a86a9b009e8f17b4777c3437797599d4671e7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Bene <benestar.wikime...@gmail.com>

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

Reply via email to