From d771a1d60db4999964cb3d376cd1f6866f61f6a4 Mon Sep 17 00:00:00 2001 From: Jon Robson Date: Thu, 4 Aug 2022 22:11:54 +0100 Subject: [PATCH] Allow preview on certain article titles The list is exposed in mw.config so banners can work out if the preview mode is active. We change the skin the same way MobileFrontend changes the skin. Bug: T314286 Change-Id: Ib3c90d2640f2fa8cac17514b59aa720e4aba6d96 --- includes/Hooks.php | 34 ++++++++++++++++++++++++++++++++-- skin.json | 5 +++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/includes/Hooks.php b/includes/Hooks.php index e14ee3f9..c47cb16c 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -3,9 +3,11 @@ namespace MediaWiki\Skins\Vector; use Config; +use IContextSource; use MediaWiki\Auth\Hook\LocalUserCreatedHook; use MediaWiki\Hook\MakeGlobalVariablesScriptHook; use MediaWiki\Hook\OutputPageBodyAttributesHook; +use MediaWiki\Hook\RequestContextCreateSkinHook; use MediaWiki\MediaWikiServices; use MediaWiki\Preferences\Hook\GetPreferencesHook; use MediaWiki\ResourceLoader as RL; @@ -34,6 +36,7 @@ class Hooks implements OutputPageBodyAttributesHook, ResourceLoaderSiteModulePagesHook, ResourceLoaderSiteStylesModulePagesHook, + RequestContextCreateSkinHook, SkinPageReadyConfigHook { /** @@ -617,6 +620,31 @@ class Hooks implements } } + /** + * Temporary RequestContextCreateSkin hook handler. + * Switches to new Vector on certain pages. + * + * @see https://www.mediawiki.org/wiki/Manual:Hooks/RequestContextCreateSkin + * + * @param IContextSource $context The RequestContext object the skin is being created for. + * @param Skin|null|string &$skin A variable reference you may set a Skin instance or string + * key on to override the skin that will be used for the context. + * @return bool|void + */ + public function onRequestContextCreateSkin( $context, &$skin ) { + if ( !$skin ) { + // user is anonymous + $user = $context->getUser(); + $config = $context->getConfig(); + $titles = $config->get( 'Vector2022PreviewPages' ); + $title = $context->getTitle(); + $titleText = $title ? $title->getPrefixedText() : null; + if ( $titleText && $user->isAnon() && in_array( $titleText, $titles ) ) { + $skin = 'vector-2022'; + } + } + } + /** * Per the $options configuration (for use with $wgVectorMaxWidthOptions) * determine whether max-width should be disabled on the page. @@ -704,15 +732,17 @@ class Hooks implements if ( !self::isVectorSkin( $skinName ) ) { return; } - + $config = $out->getConfig(); $user = $out->getUser(); if ( $user->isRegistered() && self::isSkinVersionLegacy( $skinName ) ) { $vars[ 'wgVectorDisableSidebarPersistence' ] = - $out->getConfig()->get( + $config->get( Constants::CONFIG_KEY_DISABLE_SIDEBAR_PERSISTENCE ); } + // Must be exposed to CentralNotice banners via mw.config + $vars[ 'wgVector2022PreviewPages' ] = $config->get( 'Vector2022PreviewPages' ); } /** diff --git a/skin.json b/skin.json index ef06472e..2c8bc866 100644 --- a/skin.json +++ b/skin.json @@ -147,6 +147,7 @@ } }, "Hooks": { + "RequestContextCreateSkin": "VectorHooks", "GetPreferences": "VectorHooks", "LocalUserCreated": "VectorHooks", "MakeGlobalVariablesScript": "VectorHooks", @@ -419,6 +420,10 @@ } }, "config": { + "Vector2022PreviewPages": { + "value": [], + "description": "Temporary configuration flag for allowing users to preview the new Vector skin." + }, "VectorSearchHost": { "value": "", "description": "Override default search API. Can be used with $wgDisableTextSearch and $wgSearchForwardUrl to mimic user experience on production."