From a8c79f3d529db9d82d3740cf0a306d89b6ead2b3 Mon Sep 17 00:00:00 2001 From: Jan Drewniak Date: Wed, 15 Jul 2020 23:27:09 +0200 Subject: [PATCH] Add config to disable sidebar state persistence for logged-in users. Bug: T255727 Change-Id: Id28e0bc5249ba68b5de2a078a259e9964f619ef7 --- includes/Constants.php | 5 +++++ includes/Hooks.php | 28 ++++++++++++++++++++++++++++ resources/skins.vector.js/sidebar.js | 3 +-- skin.json | 7 ++++++- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/includes/Constants.php b/includes/Constants.php index 0441d81ad..3e22aa614 100644 --- a/includes/Constants.php +++ b/includes/Constants.php @@ -104,6 +104,11 @@ final class Constants { */ public const FEATURE_LATEST_SKIN = 'LatestSkin'; + /** + * @var string + */ + public const CONFIG_KEY_DISABLE_SIDEBAR_PERSISTENCE = 'VectorDisableSidebarPersistence'; + // These are used for query parameters. /** * Override the skin version user preference and site Config. See readme. diff --git a/includes/Hooks.php b/includes/Hooks.php index 01f5e8029..f1c282dd9 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -224,6 +224,34 @@ class Hooks { } } + /** + * NOTE: Please use ResourceLoaderGetConfigVars hook instead if possible + * for adding config to the page. + * Adds config variables to JS that depend on current page/request. + * + * Adds a config flag that can disable saving the VectorSidebarVisible + * user preference when the sidebar menu icon is clicked. + * + * @param array &$vars Array of variables to be added into the output. + * @param OutputPage $out OutputPage instance calling the hook + */ + public static function onMakeGlobalVariablesScript( &$vars, OutputPage $out ) { + if ( $out->getSkin() instanceof SkinVector ) { + $skinVersionLookup = new SkinVersionLookup( + $out->getRequest(), + $out->getUser(), + self::getServiceConfig() + ); + + if ( !$skinVersionLookup->isLegacy() ) { + $vars[ 'wgVectorDisableSidebarPersistence' ] = + self::getConfig( + Constants::CONFIG_KEY_DISABLE_SIDEBAR_PERSISTENCE + ); + } + } + } + /** * Get a configuration variable such as `Constants::CONFIG_KEY_SHOW_SKIN_PREFERENCES`. * diff --git a/resources/skins.vector.js/sidebar.js b/resources/skins.vector.js/sidebar.js index dda1f1f33..e9a4a1ba6 100644 --- a/resources/skins.vector.js/sidebar.js +++ b/resources/skins.vector.js/sidebar.js @@ -79,8 +79,7 @@ function init( window ) { button = window.document.getElementById( SIDEBAR_BUTTON_ID ); initCheckboxHack( checkbox, button ); - - if ( mw.config.get( 'wgUserName' ) ) { + if ( mw.config.get( 'wgUserName' ) && !mw.config.get( 'wgVectorDisableSidebarPersistence' ) ) { bindSidebarClickEvent( checkbox, button ); } } diff --git a/skin.json b/skin.json index eacab0ea8..6da39c85f 100644 --- a/skin.json +++ b/skin.json @@ -37,7 +37,8 @@ "PreferencesFormPreSave": "Vector\\Hooks::onPreferencesFormPreSave", "SkinTemplateNavigation": "Vector\\Hooks::onSkinTemplateNavigation", "LocalUserCreated": "Vector\\Hooks::onLocalUserCreated", - "OutputPageBodyAttributes": "Vector\\Hooks::onOutputPageBodyAttributes" + "OutputPageBodyAttributes": "Vector\\Hooks::onOutputPageBodyAttributes", + "MakeGlobalVariablesScript": "Vector\\Hooks::onMakeGlobalVariablesScript" }, "@note": "When modifying skins.vector.styles definition, make sure the installer still works", "ResourceModules": { @@ -164,6 +165,10 @@ "VectorLayoutMaxWidth": { "value": false, "description": "@var boolean Enables the max-width layout for the latest version of Vector. This is intended to be a very short-lived feature flag whose sole purpose is to derisk the deployment of T246420. It should be removed (and made the default) as quickly as possible." + }, + "VectorDisableSidebarPersistence": { + "value": false, + "description": "@var boolean Temporary feature flag that disables saving the sidebar expanded/collapsed state as a user-preference (triggered via clicking the main menu icon). This is intended as a temporary kill-switch in the event that the DB is overloaded with writes to the user_options table." } }, "ServiceWiringFiles": [