jenkins-bot has submitted this change and it was merged. Change subject: Clean-up OtherProjectsSidebarGenerator ......................................................................
Clean-up OtherProjectsSidebarGenerator * Drop superfluous array initialization. * Make comments more specific. * Introduce some variables for readability/debugging. Change-Id: I40383f8a0ba0ac806cd3e75ae3dffe17509b4ebb --- M client/includes/Hooks/OtherProjectsSidebarGenerator.php 1 file changed, 35 insertions(+), 22 deletions(-) Approvals: Adrian Lang: Looks good to me, approved jenkins-bot: Verified diff --git a/client/includes/Hooks/OtherProjectsSidebarGenerator.php b/client/includes/Hooks/OtherProjectsSidebarGenerator.php index a33a1dd..7402428 100644 --- a/client/includes/Hooks/OtherProjectsSidebarGenerator.php +++ b/client/includes/Hooks/OtherProjectsSidebarGenerator.php @@ -45,7 +45,10 @@ * @param SiteStore $siteStore * @param string[] $siteIdsToOutput */ - public function __construct( $localSiteId, SiteLinkLookup $siteLinkLookup, SiteStore $siteStore, + public function __construct( + $localSiteId, + SiteLinkLookup $siteLinkLookup, + SiteStore $siteStore, array $siteIdsToOutput ) { $this->localSiteId = $localSiteId; @@ -57,7 +60,8 @@ /** * @param Title $title * - * @return array[] array of arrays of link attributes, sorted by site group id + * @return array[] Array of arrays of attributes describing sidebar links, sorted by the site's + * group and global ids. */ public function buildProjectLinkSidebar( Title $title ) { return $this->buildSidebarFromSiteLinks( $this->getSiteLinks( $title ) ); @@ -66,45 +70,45 @@ /** * @param SiteLink[] $siteLinks * - * @return array[] array of arrays of link attributes, sorted by site group id + * @return array[] Array of arrays of attributes describing sidebar links, sorted by the site's + * group and global ids. */ private function buildSidebarFromSiteLinks( array $siteLinks ) { - $result = array(); + $linksByGroup = array(); foreach ( $siteLinks as $siteLink ) { if ( !in_array( $siteLink->getSiteId(), $this->siteIdsToOutput ) ) { continue; } + $site = $this->siteStore->getSite( $siteLink->getSiteId() ); - if ( $site === null ) { - continue; - } - if ( !isset( $result[$site->getGroup()] ) ) { - $result[$site->getGroup()] = array(); + if ( $site !== null ) { + $group = $site->getGroup(); + $globalId = $site->getGlobalId(); + // Index by site group and global id + $linksByGroup[$group][$globalId] = $this->buildSidebarLink( $siteLink, $site ); } - - // Index by site group and global id - $result[$site->getGroup()][$site->getGlobalId()] = $this->buildSidebarLink( $siteLink, $site ); } - return $this->sortAndFlattenSidebar( $result ); + return $this->sortAndFlattenSidebar( $linksByGroup ); } /** * The arrays of link attributes are indexed by site group and by global site id. * Sort them by both and then return the flattened array. * - * @param array[] + * @param array[] $linksByGroup * - * @return array[] array of arrays of link attributes, sorted by site group id + * @return array[] Array of arrays of attributes describing sidebar links, sorted by the site's + * group and global ids. */ - private function sortAndFlattenSidebar( array $links ) { + private function sortAndFlattenSidebar( array $linksByGroup ) { $result = array(); - ksort( $links ); // Sort by group id + ksort( $linksByGroup ); // Sort by group id - foreach ( $links as $linksPerGroup ) { + foreach ( $linksByGroup as $linksPerGroup ) { ksort( $linksPerGroup ); // Sort individual arrays by global site id $result = array_merge( $result, array_values( $linksPerGroup ) ); } @@ -132,10 +136,19 @@ * @param SiteLink $siteLink * @param Site $site * - * @return string[] + * @return string[] Array of attributes describing a sidebar link. */ private function buildSidebarLink( SiteLink $siteLink, Site $site ) { - $node = array( + // Messages in the WikimediaMessages extension (as of 2015-03-31): + // wikibase-otherprojects-commons + // wikibase-otherprojects-testwikidata + // wikibase-otherprojects-wikidata + // wikibase-otherprojects-wikinews + // wikibase-otherprojects-wikipedia + // wikibase-otherprojects-wikiquote + // wikibase-otherprojects-wikisource + // wikibase-otherprojects-wikivoyage + $attributes = array( 'msg' => 'wikibase-otherprojects-' . $site->getGroup(), 'class' => 'wb-otherproject-link wb-otherproject-' . $site->getGroup(), 'href' => $site->getPageUrl( $siteLink->getPageName() ) @@ -143,10 +156,10 @@ $siteLanguageCode = $site->getLanguageCode(); if ( $siteLanguageCode !== null ) { - $node['hreflang'] = $siteLanguageCode; + $attributes['hreflang'] = $siteLanguageCode; } - return $node; + return $attributes; } } -- To view, visit https://gerrit.wikimedia.org/r/191351 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I40383f8a0ba0ac806cd3e75ae3dffe17509b4ebb Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: Adrian Lang <adrian.he...@wikimedia.de> Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits