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

Change subject: Fail gracefully if SiteLink not found in ParserOutputDataUpdater
......................................................................


Fail gracefully if SiteLink not found in ParserOutputDataUpdater

Bug: T114576
Change-Id: I4f0578065cfa357dff56c98043fd8a0f9b93dfe1
---
M client/includes/ParserOutputDataUpdater.php
M client/tests/phpunit/includes/ParserOutputDataUpdaterTest.php
2 files changed, 55 insertions(+), 7 deletions(-)

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



diff --git a/client/includes/ParserOutputDataUpdater.php 
b/client/includes/ParserOutputDataUpdater.php
index da47ad0..774659f 100644
--- a/client/includes/ParserOutputDataUpdater.php
+++ b/client/includes/ParserOutputDataUpdater.php
@@ -118,13 +118,28 @@
                }
 
                if ( $itemId ) {
-                       /** @var Item $item */
-                       $item = $this->entityLookup->getEntity( $itemId );
-                       $siteLink = $item->getSiteLinkList()->getBySiteId( 
$this->siteId );
+                       $this->setBadgesProperty( $itemId, $out );
+               }
+       }
 
-                       foreach ( $siteLink->getBadges() as $badge ) {
-                               $out->setProperty( 'wikibase-badge-' . 
$badge->getSerialization(), true );
-                       }
+       private function setBadgesProperty( ItemId $itemId, ParserOutput $out ) 
{
+               /** @var Item $item */
+               $item = $this->entityLookup->getEntity( $itemId );
+
+               if ( !$item->getSiteLinkList()->hasLinkWithSiteId( 
$this->siteId ) ) {
+                       // Probably some sort of race condition or data 
inconsistency, better log a warning
+                       wfLogWarning(
+                               'According to a SiteLinkLookup ' . 
$itemId->getSerialization() .
+                               ' is linked to ' . $this->siteId . ' while it 
apparently is not.'
+                       );
+
+                       return;
+               }
+
+               $siteLink = $item->getSiteLinkList()->getBySiteId( 
$this->siteId );
+
+               foreach ( $siteLink->getBadges() as $badge ) {
+                       $out->setProperty( 'wikibase-badge-' . 
$badge->getSerialization(), true );
                }
        }
 
diff --git a/client/tests/phpunit/includes/ParserOutputDataUpdaterTest.php 
b/client/tests/phpunit/includes/ParserOutputDataUpdaterTest.php
index af2f10e..f2d93a5 100644
--- a/client/tests/phpunit/includes/ParserOutputDataUpdaterTest.php
+++ b/client/tests/phpunit/includes/ParserOutputDataUpdaterTest.php
@@ -11,6 +11,7 @@
 use Wikibase\Client\Usage\ParserOutputUsageAccumulator;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\SiteLinkList;
 use Wikibase\Test\MockRepository;
 
 /**
@@ -203,7 +204,7 @@
                $this->assertTrue( $parserOutput->getProperty( 
'wikibase-badge-Q17' ) );
        }
 
-       public function testUpdateBadgesPropertyRemovesPreviousData() {
+       public function testUpdateBadgesProperty_removesPreviousData() {
                $parserOutput = new ParserOutput();
                $parserOutput->setProperty( 'wikibase-badge-Q17', true );
 
@@ -215,4 +216,36 @@
                $this->assertFalse( $parserOutput->getProperty( 
'wikibase-badge-Q17' ) );
        }
 
+       public function testUpdateBadgesProperty_inconsistentSiteLinkLookup() {
+               $parserOutput = new ParserOutput();
+
+               $title = Title::newFromText( 'Foo sr' );
+
+               $siteLinkLookup = new MockRepository();
+               $mockRepoNoSiteLinks = new MockRepository();
+               foreach ( $this->getItems() as $item ) {
+                       $siteLinkLookup->putEntity( $item );
+
+                       $itemNoSiteLinks = unserialize( serialize( $item ) );
+                       $itemNoSiteLinks->setSiteLinkList( new SiteLinkList() );
+
+                       $mockRepoNoSiteLinks->putEntity( $itemNoSiteLinks );
+               }
+
+               $parserOutputDataUpdater = new ParserOutputDataUpdater(
+                       $this->getOtherProjectsSidebarGeneratorFactory( array() 
),
+                       $siteLinkLookup,
+                       $mockRepoNoSiteLinks,
+                       'srwiki'
+               );
+
+               // Suppress warnings as this is supposed to throw one.
+               \MediaWiki\suppressWarnings();
+               $parserOutputDataUpdater->updateBadgesProperty( $title, 
$parserOutput );
+               \MediaWiki\restoreWarnings();
+
+               // Stuff didn't blow up
+               $this->assertTrue( true );
+       }
+
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I4f0578065cfa357dff56c98043fd8a0f9b93dfe1
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: JanZerebecki <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to