jenkins-bot has submitted this change and it was merged. Change subject: Fix SiteLinkUsageLookup::getPagesUsing ......................................................................
Fix SiteLinkUsageLookup::getPagesUsing Bug:T86714 Change-Id: I626defcdf26174d706a509c1b31d5ff6af62357e --- M client/includes/Usage/SiteLinkUsageLookup.php M client/tests/phpunit/includes/Usage/SiteLinkUsageLookupTest.php 2 files changed, 32 insertions(+), 10 deletions(-) Approvals: Aude: Looks good to me, approved jenkins-bot: Verified diff --git a/client/includes/Usage/SiteLinkUsageLookup.php b/client/includes/Usage/SiteLinkUsageLookup.php index 6fe794e..b1c11b4 100644 --- a/client/includes/Usage/SiteLinkUsageLookup.php +++ b/client/includes/Usage/SiteLinkUsageLookup.php @@ -81,8 +81,7 @@ * @param string[] $aspects Which aspects to consider (if omitted, all aspects are considered). * Use the EntityUsage::XXX_USAGE constants to represent aspects. * - * @return Iterator An iterator over the IDs of pages using any of the given entities. - * If $aspects is given, only usages of these aspects are included in the result. + * @return Iterator<PageEntityUsages> * @throws UsageTrackerException */ public function getPagesUsing( array $entityIds, array $aspects = array() ) { @@ -97,7 +96,7 @@ $numericItemIds = $this->getNumericItemIds( $entityIds ); $rows = $this->siteLinkLookup->getLinks( $numericItemIds, array( $this->clientSiteId ) ); - $pageIds = $this->getPageIdsFromSiteLinkRows( $rows ); + $pageIds = $this->getPageEntityUsagesFromSiteLinkRows( $rows ); return new ArrayIterator( $pageIds ); } @@ -124,20 +123,29 @@ /** * @param array[] $rows Rows as returned by SiteLinkLookup::getLinks * - * @return int[] + * @return PageEntityUsages[] */ - private function getPageIdsFromSiteLinkRows( array $rows ) { + private function getPageEntityUsagesFromSiteLinkRows( array $rows ) { $titleFactory = $this->titleFactory; - $pageIds = array_map( + $pageEntityUsages = array_map( function ( array $row ) use ( $titleFactory ) { + // $row = array( $siteId, $pageName, $numericItemId ); + $itemId = ItemId::newFromNumber( $row[2] ); $title = $titleFactory->newFromText( $row[1] ); - return $title ? $title->getArticleID() : 0; + + if ( !$title ) { + return null; + } + + // NOTE: since we don't know how the item is used on the linked page, assume "all" usage. + $usage = new EntityUsage( $itemId, EntityUsage::ALL_USAGE ); + return new PageEntityUsages( $title->getArticleID(), array( $usage ) ); }, $rows ); - array_unique( $pageIds ); - return $pageIds; + $pageEntityUsages = array_filter( $pageEntityUsages ); + return $pageEntityUsages; } /** diff --git a/client/tests/phpunit/includes/Usage/SiteLinkUsageLookupTest.php b/client/tests/phpunit/includes/Usage/SiteLinkUsageLookupTest.php index a186f20..de7c964 100644 --- a/client/tests/phpunit/includes/Usage/SiteLinkUsageLookupTest.php +++ b/client/tests/phpunit/includes/Usage/SiteLinkUsageLookupTest.php @@ -3,6 +3,8 @@ namespace Wikibase\Client\Tests\Usage\Sql; use Title; +use Wikibase\Client\Usage\EntityUsage; +use Wikibase\Client\Usage\PageEntityUsages; use Wikibase\Client\Usage\SiteLinkUsageLookup; use Wikibase\DataModel\Entity\Item; use Wikibase\DataModel\Entity\ItemId; @@ -108,7 +110,19 @@ $actual = iterator_to_array( $actual ); $this->assertCount( 1, $actual ); - $this->assertEquals( 23, $actual[0] ); + $this->assertInstanceOf( 'Wikibase\Client\Usage\PageEntityUsages', $actual[0] ); + + /** @var PageEntityUsages $pageUsageObject */ + /** @var EntityUsage[] $usages */ + $pageUsageObject = reset( $actual ); + $usages = $pageUsageObject->getUsages(); + + $this->assertEquals( 23, $pageUsageObject->getPageId() ); + $this->assertCount( 1, $usages ); + + $usage = reset( $usages ); + $this->assertEquals( EntityUsage::ALL_USAGE, $usage->getAspect() ); + $this->assertEquals( $q23, $usage->getEntityId() ); } public function testGetUnusedEntities() { -- To view, visit https://gerrit.wikimedia.org/r/184914 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I626defcdf26174d706a509c1b31d5ff6af62357e Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: wmf/1.25wmf14 Gerrit-Owner: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits