From: Bene Date: Tue, 4 Mar 2014 10:44:57 +0000 (+0100) Subject: Add site by navigation id feature to SiteList X-Git-Tag: 1.31.0-rc.0~16743^2 X-Git-Url: http://git.cyclocoop.org/%22.%24h.%22?a=commitdiff_plain;h=febcfe8d61196366eecfe2e3c91cd107db958d31;p=lhc%2Fweb%2Fwiklou.git Add site by navigation id feature to SiteList 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 --- diff --git a/includes/site/SiteList.php b/includes/site/SiteList.php index b0d1f95b0d..1dd6b1630c 100644 --- a/includes/site/SiteList.php +++ b/includes/site/SiteList.php @@ -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. diff --git a/tests/phpunit/includes/site/SiteListTest.php b/tests/phpunit/includes/site/SiteListTest.php index 8af2fc1b95..b41f647e90 100644 --- a/tests/phpunit/includes/site/SiteListTest.php +++ b/tests/phpunit/includes/site/SiteListTest.php @@ -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