Add site by navigation id feature to SiteList
authorBene <benestar.wikimedia@googlemail.com>
Tue, 4 Mar 2014 10:44:57 +0000 (11:44 +0100)
committerBene <benestar.wikimedia@googlemail.com>
Tue, 4 Mar 2014 20:09:10 +0000 (21:09 +0100)
There are some places especially in Wikibase where we need to get a site
based on the local navigation id. This feature really should go into core.

Change-Id: I02eb1673bea58f79d02db2e1ecd21cbc5b8cb0d1

includes/site/SiteList.php
tests/phpunit/includes/site/SiteListTest.php

index b0d1f95..1dd6b16 100644 (file)
@@ -46,6 +46,16 @@ class SiteList extends GenericArrayObject {
         */
        protected $byGlobalId = array();
 
+       /**
+        * Navigational site identifiers alias inter-language prefixes
+        * pointing to their sites offset value.
+        *
+        * @since 1.23
+        *
+        * @var array of string
+        */
+       protected $byNavigationId = array();
+
        /**
         * @see GenericArrayObject::getObjectType
         *
@@ -75,6 +85,11 @@ class SiteList extends GenericArrayObject {
                $this->byGlobalId[$site->getGlobalId()] = $index;
                $this->byInternalId[$site->getInternalId()] = $index;
 
+               $ids = $site->getNavigationIds();
+               foreach ( $ids as $navId ) {
+                       $this->byNavigationId[$navId] = $index;
+               }
+
                return true;
        }
 
@@ -94,6 +109,11 @@ class SiteList extends GenericArrayObject {
 
                        unset( $this->byGlobalId[$site->getGlobalId()] );
                        unset( $this->byInternalId[$site->getInternalId()] );
+
+                       $ids = $site->getNavigationIds();
+                       foreach ( $ids as $navId ) {
+                               unset( $this->byNavigationId[$navId] );
+                       }
                }
 
                parent::offsetUnset( $index );
@@ -196,6 +216,43 @@ class SiteList extends GenericArrayObject {
                $this->offsetUnset( $this->byInternalId[$id] );
        }
 
+       /**
+        * Returns if the list contains the site with the provided navigational site id.
+        *
+        * @param string $id
+        *
+        * @return boolean
+        */
+       public function hasNavigationId( $id ) {
+               return array_key_exists( $id, $this->byNavigationId );
+       }
+
+       /**
+        * Returns the Site with the provided navigational site id.
+        * The site needs to exist, so if not sure, call has first.
+        *
+        * @since 1.23
+        *
+        * @param string $id
+        *
+        * @return Site
+        */
+       public function getSiteByNavigationId( $id ) {
+               return $this->offsetGet( $this->byNavigationId[$id] );
+       }
+
+       /**
+        * Removes the site with the specified navigational site id.
+        * The site needs to exist, so if not sure, call has first.
+        *
+        * @since 1.23
+        *
+        * @param string $id
+        */
+       public function removeSiteByNavigationId( $id ) {
+               $this->offsetUnset( $this->byNavigationId[$id] );
+       }
+
        /**
         * Sets a site in the list. If the site was not there,
         * it will be added. If it was, it will be updated.
index 8af2fc1..b41f647 100644 (file)
@@ -80,16 +80,14 @@ class SiteListTest extends MediaWikiTestCase {
         * @covers SiteList::getSite
         */
        public function testGetSiteByGlobalId( SiteList $sites ) {
-               if ( $sites->isEmpty() ) {
-                       $this->assertTrue( true );
-               } else {
-                       /**
-                        * @var Site $site
-                        */
-                       foreach ( $sites as $site ) {
-                               $this->assertEquals( $site, $sites->getSite( $site->getGlobalId() ) );
-                       }
+               /**
+                * @var Site $site
+                */
+               foreach ( $sites as $site ) {
+                       $this->assertEquals( $site, $sites->getSite( $site->getGlobalId() ) );
                }
+
+               $this->assertTrue( true );
        }
 
        /**
@@ -110,6 +108,25 @@ class SiteListTest extends MediaWikiTestCase {
                $this->assertTrue( true );
        }
 
+       /**
+        * @dataProvider siteListProvider
+        * @param SiteList $sites
+        * @covers SiteList::getSiteByNavigationId
+        */
+       public function testGetSiteByNavigationId( $sites ) {
+               /**
+                * @var Site $site
+                */
+               foreach ( $sites as $site ) {
+                       $ids = $site->getNavigationIds();
+                       foreach ( $ids as $navId ) {
+                               $this->assertEquals( $site, $sites->getSiteByNavigationId( $navId ) );
+                       }
+               }
+
+               $this->assertTrue( true );
+       }
+
        /**
         * @dataProvider siteListProvider
         * @param SiteList $sites
@@ -147,6 +164,25 @@ class SiteListTest extends MediaWikiTestCase {
                $this->assertFalse( $sites->hasInternalId( -1 ) );
        }
 
+       /**
+        * @dataProvider siteListProvider
+        * @param SiteList $sites
+        * @covers SiteList::hasNavigationId
+        */
+       public function testHasNavigationId( $sites ) {
+               /**
+                * @var Site $site
+                */
+               foreach ( $sites as $site ) {
+                       $ids = $site->getNavigationIds();
+                       foreach ( $ids as $navId ) {
+                               $this->assertTrue( $sites->hasNavigationId( $navId ) );
+                       }
+               }
+
+               $this->assertFalse( $sites->hasNavigationId( 'non-existing-navigation-id' ) );
+       }
+
        /**
         * @dataProvider siteListProvider
         * @param SiteList $sites