Addshore has submitted this change and it was merged.

Change subject: Created a ClientSiteLinkLookup to query sitelinks
......................................................................


Created a ClientSiteLinkLookup to query sitelinks

The class allows to get the sitelinks connected with a given title and
also includes the badges of this sitelink by loading the cached entity.

Change-Id: Ibc0471ce1c08d8b52b5f56f97375022b0d8ef910
---
A client/includes/ClientSiteLinkLookup.php
A client/tests/phpunit/includes/ClientSiteLinkLookupTest.php
M client/tests/phpunit/includes/LangLinkHandlerTest.php
3 files changed, 160 insertions(+), 1 deletion(-)

Approvals:
  WikidataJenkins: Verified
  Addshore: Looks good to me, approved



diff --git a/client/includes/ClientSiteLinkLookup.php 
b/client/includes/ClientSiteLinkLookup.php
new file mode 100644
index 0000000..9801f13
--- /dev/null
+++ b/client/includes/ClientSiteLinkLookup.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace Wikibase\Client;
+
+use Wikibase\SiteLinkLookup;
+use Wikibase\EntityLookup;
+use Wikibase\DataModel\SiteLink;
+use Title;
+
+/**
+ * Provides access to sitelinks on repo.
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Bene* < benestar.wikime...@gmail.com >
+ */
+class ClientSiteLinkLookup {
+
+       /**
+        * @var string
+        */
+       protected $localSiteId;
+
+       /**
+        * @var SiteLinkLookup
+        */
+       protected $siteLinkLookup;
+
+       /**
+        * @var EntityLookup
+        */
+       protected $entityLookup;
+
+       /**
+        * @param string $localSiteId global id of the client wiki
+        * @param SiteLinkLookup $siteLinkLookup
+        * @param EntityLookup $entityLookup
+        */
+       public function __construct( $localSiteId, SiteLinkLookup 
$siteLinkLookup, EntityLookup $entityLookup ) {
+               $this->localSiteId = $localSiteId;
+               $this->siteLinkLookup = $siteLinkLookup;
+               $this->entityLookup = $entityLookup;
+       }
+
+       /**
+        * Finds the corresponding item on the repository and
+        * returns the item's site links including badges.
+        *
+        * @since 0.5
+        *
+        * @param Title $title
+        *
+        * @return SiteLink[]
+        */
+       public function getSiteLinks( Title $title ) {
+               $siteLink = new SiteLink( $this->localSiteId, $title->getText() 
);
+               $itemId = $this->siteLinkLookup->getEntityIdForSiteLink( 
$siteLink );
+
+               if ( $itemId === null ) {
+                       return array();
+               }
+
+               $item = $this->entityLookup->getEntity( $itemId );
+               if ( $item === null ) {
+                       return array();
+               }
+               return $item->getSiteLinks();
+       }
+
+}
diff --git a/client/tests/phpunit/includes/ClientSiteLinkLookupTest.php 
b/client/tests/phpunit/includes/ClientSiteLinkLookupTest.php
new file mode 100644
index 0000000..c402ae1
--- /dev/null
+++ b/client/tests/phpunit/includes/ClientSiteLinkLookupTest.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Wikibase\Client\ClientSiteLinkLookup;
+use Wikibase\DataModel\SiteLink;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\Item;
+use Title;
+
+/**
+ * @covers Wikibase\Client\ClientSiteLinkLookup
+ *
+ * @group WikibaseClient
+ * @group Wikibase
+ * @group Database
+ *
+ * @licence GNU GPL v2+
+ * @author Bene* < benestar.wikime...@gmail.com >
+ */
+class ClientSiteLinkLookupTest extends \PHPUnit_Framework_TestCase {
+
+       static $itemData = array(
+               1 => array(
+                       'id' => 1,
+                       'label' => array( 'en' => 'Foo' ),
+                       'links' => array(
+                               'dewiki' => array(
+                                       'name' => 'Foo de',
+                                       'badges' => array( 'Q3' )
+                               ),
+                               'enwiki' => array(
+                                       'name' => 'Foo en',
+                                       'badges' => array( 'Q4', 'Q123' )
+                               ),
+                               'srwiki' => 'Foo sr',
+                               'dewiktionary' => 'Foo de word',
+                               'enwiktionary' => 'Foo en word',
+                       )
+               )
+       );
+
+       private function getClientSiteLinkLookup( $localSiteId ) {
+               $mockRepo = new MockRepository();
+
+               foreach ( self::$itemData as $data ) {
+                       $item = new Item( $data );
+                       $mockRepo->putEntity( $item );
+               }
+
+               return new ClientSiteLinkLookup(
+                       $localSiteId,
+                       $mockRepo,
+                       $mockRepo
+               );
+       }
+
+       /**
+        * @dataProvider provideGetSiteLinks
+        */
+       public function testGetSiteLinks( $expected, $localSiteId, Title 
$title, $message ) {
+               $ClientSiteLinkLookup = $this->getClientSiteLinkLookup( 
$localSiteId );
+
+               $this->assertEquals(
+                       $expected,
+                       $ClientSiteLinkLookup->getSiteLinks( $title ),
+                       $message
+               );
+       }
+
+       public function provideGetSiteLinks() {
+               $sitelinks = array(
+                       new SiteLink( 'dewiki', 'Foo de', array( new ItemId( 
'Q3' ) ) ),
+                       new SiteLink( 'enwiki', 'Foo en', array( new ItemId( 
'Q4' ), new ItemId( 'Q123' ) ) ),
+                       new SiteLink( 'srwiki', 'Foo sr' ),
+                       new SiteLink( 'dewiktionary', 'Foo de word' ),
+                       new SiteLink( 'enwiktionary', 'Foo en word' )
+               );
+
+               return array(
+                       array( $sitelinks, 'dewiki', Title::newFromText( 'Foo 
de' ), 'from dewiki title' ),
+                       array( $sitelinks, 'enwiktionary', Title::newFromText( 
'Foo en word' ), 'from enwiktionary title' ),
+                       array( array(), 'enwiki', Title::newFromText( 'Bar en' 
), 'from nonexisting title' ),
+                       array( array(), 'barwiki', Title::newFromText( 'Foo 
bar' ), 'from nonexisting site' ),
+               );
+       }
+
+}
diff --git a/client/tests/phpunit/includes/LangLinkHandlerTest.php 
b/client/tests/phpunit/includes/LangLinkHandlerTest.php
index 16cccf5..ea2d40b 100644
--- a/client/tests/phpunit/includes/LangLinkHandlerTest.php
+++ b/client/tests/phpunit/includes/LangLinkHandlerTest.php
@@ -20,7 +20,7 @@
  */
 class LangLinkHandlerTest extends \MediaWikiTestCase {
 
-       /* @var MockRepository $langLinkHandler */
+       /* @var MockRepository $mockRepo */
        protected $mockRepo;
 
        /* @var LangLinkHandler $langLinkHandler */

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ibc0471ce1c08d8b52b5f56f97375022b0d8ef910
Gerrit-PatchSet: 12
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Bene <benestar.wikime...@googlemail.com>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Bene <benestar.wikime...@googlemail.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: 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