diff --git a/includes/Hooks.php b/includes/Hooks.php index 8d405b2..47dcba3 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -7,7 +7,6 @@ use ExtensionRegistry; use HTMLForm; use MediaWiki\MediaWikiServices; use OutputPage; -use RequestContext; use ResourceLoaderContext; use Skin; use SkinTemplate; @@ -52,10 +51,6 @@ class Hooks { return; } - $skinVersionLookup = new SkinVersionLookup( - $out->getRequest(), $sk->getUser(), self::getServiceConfig() - ); - $mobile = false; if ( ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) ) { @@ -63,9 +58,9 @@ class Hooks { $mobile = $mobFrontContext->shouldDisplayMobileView(); } - if ( $skinVersionLookup->isLegacy() - && ( $mobile || $sk->getConfig()->get( 'VectorResponsive' ) ) - ) { + if ( + self::isSkinVersionLegacy() + && ( $mobile || $sk->getConfig()->get( 'VectorResponsive' ) ) ) { $out->addMeta( 'viewport', 'width=device-width, initial-scale=1' ); $out->addModuleStyles( 'skins.vector.styles.responsive' ); } @@ -158,10 +153,6 @@ class Hooks { return; } - $skinVersionLookup = new SkinVersionLookup( - RequestContext::getMain()->getRequest(), $user, self::getServiceConfig() - ); - // Preferences to add. $vectorPrefs = [ Constants::PREF_KEY_SKIN_VERSION => [ @@ -174,7 +165,7 @@ class Hooks { // indicates that a prefs-skin-prefs string will be provided. 'section' => 'rendering/skin/skin-prefs', // Convert the preference string to a boolean presentation. - 'default' => $skinVersionLookup->isLegacy() ? '1' : '0', + 'default' => self::isSkinVersionLegacy() ? '1' : '0', // Only show this section when the Vector skin is checked. The JavaScript client also uses // this state to determine whether to show or hide the whole section. 'hide-if' => [ '!==', 'wpskin', Constants::SKIN_NAME ] @@ -264,20 +255,15 @@ class Hooks { return; } - $skinVersionLookup = new SkinVersionLookup( - $out->getRequest(), $sk->getUser(), self::getServiceConfig() - ); - - if ( $skinVersionLookup->isLegacy() ) { - - // As of 2020/08/13, this CSS class is referred to by the following deployed extensions: - // - // - VisualEditor - // - CodeMirror - // - WikimediaEvents - // - // See https://codesearch.wmcloud.org/deployed/?q=skin-vector-legacy for an up-to-date - // list. + // As of 2020/08/13, this CSS class is referred to by the following deployed extensions: + // + // - VisualEditor + // - CodeMirror + // - WikimediaEvents + // + // See https://codesearch.wmcloud.org/deployed/?q=skin-vector-legacy for an up-to-date + // list. + if ( self::isSkinVersionLegacy() ) { $bodyAttrs['class'] .= ' skin-vector-legacy'; return; @@ -313,21 +299,17 @@ class Hooks { * @param OutputPage $out OutputPage instance calling the hook */ public static function onMakeGlobalVariablesScript( &$vars, OutputPage $out ) { + if ( !$out->getSkin() instanceof SkinVector ) { + return; + } + $user = $out->getUser(); - if ( $out->getSkin() instanceof SkinVector && $user->isRegistered() ) { - $skinVersionLookup = new SkinVersionLookup( - $out->getRequest(), - $user, - self::getServiceConfig() - ); - - if ( !$skinVersionLookup->isLegacy() ) { - $vars[ 'wgVectorDisableSidebarPersistence' ] = + if ( $user->isLoggedIn() && self::isSkinVersionLegacy() ) { + $vars[ 'wgVectorDisableSidebarPersistence' ] = self::getConfig( Constants::CONFIG_KEY_DISABLE_SIDEBAR_PERSISTENCE ); - } } } @@ -348,4 +330,15 @@ class Hooks { private static function getServiceConfig() { return MediaWikiServices::getInstance()->getService( Constants::SERVICE_CONFIG ); } + + /** + * Gets whether the current skin version is the legacy version. + * + * @see VectorServices::getFeatureManager + * + * @return bool + */ + private static function isSkinVersionLegacy(): bool { + return !VectorServices::getFeatureManager()->isFeatureEnabled( Constants::FEATURE_LATEST_SKIN ); + } } diff --git a/includes/VectorServices.php b/includes/VectorServices.php new file mode 100644 index 0000000..e093d32 --- /dev/null +++ b/includes/VectorServices.php @@ -0,0 +1,27 @@ +getService( Constants::SERVICE_FEATURE_MANAGER ); + } +} diff --git a/tests/phpunit/integration/VectorHooksTest.php b/tests/phpunit/integration/VectorHooksTest.php index 06dc481..1ec3bc8 100644 --- a/tests/phpunit/integration/VectorHooksTest.php +++ b/tests/phpunit/integration/VectorHooksTest.php @@ -4,6 +4,8 @@ * @ingroup skins */ +use Vector\Constants; +use Vector\FeatureManagement\FeatureManager; use Vector\Hooks; const SKIN_PREFS_SECTION = 'rendering/skin/skin-prefs'; @@ -29,17 +31,21 @@ class VectorHooksTest extends \MediaWikiTestCase { $this->assertSame( $prefs, [], 'No preferences are added.' ); } + private function setFeatureLatestSkinVersionIsEnabled( $isEnabled ) { + $featureManager = new FeatureManager(); + $featureManager->registerSimpleRequirement( Constants::REQUIREMENT_LATEST_SKIN_VERSION, $isEnabled ); + $featureManager->registerFeature( Constants::FEATURE_LATEST_SKIN, [ + Constants::REQUIREMENT_LATEST_SKIN_VERSION + ] ); + + $this->setService( Constants::SERVICE_FEATURE_MANAGER, $featureManager ); + } + /** * @covers ::onGetPreferences */ public function testOnGetPreferencesShowPreferencesEnabledSkinSectionFoundLegacy() { - $config = new HashConfig( [ - 'VectorShowSkinPreferences' => true, - // '1' is Legacy. - 'VectorDefaultSkinVersionForExistingAccounts' => '1', - 'VectorDefaultSidebarVisibleForAuthorisedUser' => true - ] ); - $this->setService( 'Vector.Config', $config ); + $this->setFeatureLatestSkinVersionIsEnabled( false ); $prefs = [ 'foo' => [], @@ -75,13 +81,7 @@ class VectorHooksTest extends \MediaWikiTestCase { * @covers ::onGetPreferences */ public function testOnGetPreferencesShowPreferencesEnabledSkinSectionMissingLegacy() { - $config = new HashConfig( [ - 'VectorShowSkinPreferences' => true, - // '1' is Legacy. - 'VectorDefaultSkinVersionForExistingAccounts' => '1', - 'VectorDefaultSidebarVisibleForAuthorisedUser' => true - ] ); - $this->setService( 'Vector.Config', $config ); + $this->setFeatureLatestSkinVersionIsEnabled( false ); $prefs = [ 'foo' => [], @@ -115,13 +115,7 @@ class VectorHooksTest extends \MediaWikiTestCase { * @covers ::onGetPreferences */ public function testOnGetPreferencesShowPreferencesEnabledSkinSectionMissingLatest() { - $config = new HashConfig( [ - 'VectorShowSkinPreferences' => true, - // '2' is latest. - 'VectorDefaultSkinVersionForExistingAccounts' => '2', - 'VectorDefaultSidebarVisibleForAuthorisedUser' => true - ] ); - $this->setService( 'Vector.Config', $config ); + $this->setFeatureLatestSkinVersionIsEnabled( true ); $prefs = [ 'foo' => [],