Thiemo Mättig (WMDE) has uploaded a new change for review. https://gerrit.wikimedia.org/r/191351
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, 26 insertions(+), 22 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/51/191351/1 diff --git a/client/includes/Hooks/OtherProjectsSidebarGenerator.php b/client/includes/Hooks/OtherProjectsSidebarGenerator.php index a33a1dd..adb5b9e 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 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 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 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,10 @@ * @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( + $attributes = array( 'msg' => 'wikibase-otherprojects-' . $site->getGroup(), 'class' => 'wb-otherproject-link wb-otherproject-' . $site->getGroup(), 'href' => $site->getPageUrl( $siteLink->getPageName() ) @@ -143,10 +147,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: newchange Gerrit-Change-Id: I40383f8a0ba0ac806cd3e75ae3dffe17509b4ebb Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits