Disable language button on certain pages

The logic is based on the existing logic for the "Add language links"
however with the additional constraint that the page is not a special
page.

To avoid multiple expensive calls to getLanguages method,
the getLanguagesCached method is introduced.

Bug: T273144
Change-Id: I1085efca1e10c9b6f1305c2238664e0b2ec69123
This commit is contained in:
jdlrobson 2021-02-10 11:40:47 -08:00 committed by Jdlrobson
parent 31a9e6feff
commit 52edeeea1c

View file

@ -34,6 +34,8 @@ use Vector\VectorServices;
* @internal * @internal
*/ */
class SkinVector extends SkinMustache { class SkinVector extends SkinMustache {
/** @var null|array for caching purposes */
private $languages;
/** @var int */ /** @var int */
private const MENU_TYPE_DEFAULT = 0; private const MENU_TYPE_DEFAULT = 0;
/** @var int */ /** @var int */
@ -101,12 +103,53 @@ class SkinVector extends SkinMustache {
parent::__construct( $options ); parent::__construct( $options );
} }
/**
* Calls getLanguages with caching.
* @return array
*/
private function getLanguagesCached() : array {
if ( $this->languages !== null ) {
return $this->languages;
}
$this->languages = $this->getLanguages();
return $this->languages;
}
/**
* This should be upstreamed to the Skin class in core once the logic is finalized.
* Returns false if an editor has explicitly disabled languages on the page via the property
* `noexternallanglinks`, if the page is a special page without any languages, or if an action
* other than view is being used.
* @return bool
*/
private function canHaveLanguages() : bool {
$action = Action::getActionName( $this->getContext() );
if ( $action !== 'view' ) {
return false;
}
// Wikibase introduces a magic word
// When upstreaming this should be Wikibase agnostic.
// https://www.mediawiki.org/wiki/Wikibase/Installation/Advanced_configuration#noexternallanglinks
// If the property is not set, continue safely through the other if statements.
if ( $this->getOutput()->getProperty( 'noexternallanglinks' ) ) {
return false;
}
$title = $this->getTitle();
// Defensive programming - if a special page has added languages explicitly, best to show it.
if ( $title && $title->isSpecialPage() && empty( $this->getLanguagesCached() ) ) {
return false;
}
return true;
}
/** /**
* @return bool * @return bool
*/ */
private function isLanguagesInHeader() { private function isLanguagesInHeader() {
$featureManager = VectorServices::getFeatureManager(); $featureManager = VectorServices::getFeatureManager();
return $featureManager->isFeatureEnabled( // Disable button on pages without languages (based on Wikibase RepoItemLinkGenerator class)
return $this->canHaveLanguages() && $featureManager->isFeatureEnabled(
Constants::FEATURE_LANGUAGE_IN_HEADER Constants::FEATURE_LANGUAGE_IN_HEADER
); );
} }
@ -249,7 +292,7 @@ class SkinVector extends SkinMustache {
if ( $portletData['id'] === 'p-lang' && $this->isLanguagesInHeader() ) { if ( $portletData['id'] === 'p-lang' && $this->isLanguagesInHeader() ) {
$portletData['label'] = $this->msg( $portletData['label'] = $this->msg(
'vector-language-button-label', 'vector-language-button-label',
count( $this->getLanguages() ) count( $this->getLanguagesCached() )
)->parse(); )->parse();
// Adds language icon // Adds language icon
$portletData['heading-class'] .= ' mw-ui-icon mw-ui-icon-before mw-ui-icon-wikimedia-language'; $portletData['heading-class'] .= ' mw-ui-icon mw-ui-icon-before mw-ui-icon-wikimedia-language';