Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
SiteLink::getSiteIDs to get list of site ids.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
daniel committed Jul 26, 2012
1 parent c789c81 commit 6d298e9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion client/includes/LocalItem.php
Expand Up @@ -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'] );
}

Expand Down
18 changes: 18 additions & 0 deletions lib/includes/SiteLink.php
Expand Up @@ -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 );
}
}
26 changes: 26 additions & 0 deletions lib/tests/phpunit/SiteLinkTest.php
Expand Up @@ -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 );
}
}
2 changes: 1 addition & 1 deletion repo/includes/ItemView.php
Expand Up @@ -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 */
Expand Down

0 comments on commit 6d298e9

Please sign in to comment.