From 6d298e9f8c5b91c771ca10308a4c405691c932ab Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 26 Jul 2012 19:43:23 +0200 Subject: [PATCH] SiteLink::getSiteIDs to get list of site ids. Some parts of the code need an array of site IDs to work with. Provide a utility method to get a list of IDs from a list of SiteLinks. Change-Id: I194f5a75090c1024696c777b4b5b0ddb60dca7dc --- client/includes/LocalItem.php | 2 +- lib/includes/SiteLink.php | 18 ++++++++++++++++++ lib/tests/phpunit/SiteLinkTest.php | 26 ++++++++++++++++++++++++++ repo/includes/ItemView.php | 2 +- 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/client/includes/LocalItem.php b/client/includes/LocalItem.php index 6dc034ca3a..2b1b9443a6 100644 --- a/client/includes/LocalItem.php +++ b/client/includes/LocalItem.php @@ -57,7 +57,7 @@ public static function newFromItem( Item $item, $loadFromDB = true ) { // TODO: obtain local wiki global id // TODO: properly manage this field - if ( array_key_exists( 'enwiki', $siteLinks ) ) { + if ( in_array( 'enwiki', SiteLink::getSiteIDs( $siteLinks ) ) ) { $localItem->setField( 'page_title', $siteLinks['enwiki'] ); } diff --git a/lib/includes/SiteLink.php b/lib/includes/SiteLink.php index 49a0e76e55..4d04c657c7 100644 --- a/lib/includes/SiteLink.php +++ b/lib/includes/SiteLink.php @@ -170,4 +170,22 @@ public function __toString() { public static function toDBKey( $title ) { return str_replace( ' ', '_', $title ); } + + /** + * Returns the list of site IDs for a given list of site Links. + * Each site will only occur once in the result. + * The order of the site ids in the result is undefined. + * + * @param $siteLinks array a list of SiteLink objects + * @return array the list of site ids. + */ + public static function getSiteIDs( $siteLinks ) { + $siteIds = array(); + + foreach ( $siteLinks as $link /* @var SiteLink $link */) { + $siteIds[] = $link->getSiteID(); + } + + return array_unique( $siteIds ); + } } \ No newline at end of file diff --git a/lib/tests/phpunit/SiteLinkTest.php b/lib/tests/phpunit/SiteLinkTest.php index 4258e1ba3a..ecf930315e 100644 --- a/lib/tests/phpunit/SiteLinkTest.php +++ b/lib/tests/phpunit/SiteLinkTest.php @@ -110,4 +110,30 @@ public function testToString() { $this->assertEquals( "enwiki:Foo_Bar", "$link" ); } + public function dataGetSiteIDs() { + return array( + array( + array(), + array() ), + + array( + array( SiteLink::newFromText( 'enwiki', "Foo Bar" ), SiteLink::newFromText( 'dewiki', "Bla bla" ) ), + array( 'enwiki', 'dewiki' ) ), + + array( + array( SiteLink::newFromText( 'enwiki', "Foo Bar" ), SiteLink::newFromText( 'dewiki', "Bla bla" ), SiteLink::newFromText( 'enwiki', "More Stuff" ) ), + array( 'enwiki', 'dewiki' ) ), + ); + } + + /** + * + * @dataProvider dataGetSiteIDs + * @depends testNewFromText + */ + public function testGetSiteIDs( $links, $expected ) { + $ids = SiteLink::getSiteIDs( $links ); + + $this->assertArrayEquals( $expected, $ids ); + } } diff --git a/repo/includes/ItemView.php b/repo/includes/ItemView.php index 84f499bf38..3dae501bd2 100644 --- a/repo/includes/ItemView.php +++ b/repo/includes/ItemView.php @@ -107,7 +107,7 @@ public function getHTML( ItemContent $item, Language $lang = null, $editable = t $i = 0; // Batch load the sites we need info about during the building of the sitelink list. - Sites::singleton()->loadSites( array( 'global_key' => array_keys( $siteLinks ) ) ); + Sites::singleton()->loadSites( array( 'global_key' => SiteLink::getSiteIds( $siteLinks ) ) ); foreach( $siteLinks as $link ) { /* @var SiteLink $link */