From 7701ce26411ebbae1bf6530456b8e60834ced574 Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Tue, 15 Aug 2023 17:02:31 +0200 Subject: [PATCH] Use HookHandlers for MobileFrontend hooks The use of "HookHandlers" attribute in extension.json makes it possible to inject services into hook handler classes in a future patch. Bug: T315823 Change-Id: I9422f0cf7d5c14db544ea67c564d34e9eca3813b --- includes/Hooks.php | 94 +------------------------- includes/MobileFrontendHooks.php | 109 +++++++++++++++++++++++++++++++ skin.json | 11 ++-- 3 files changed, 116 insertions(+), 98 deletions(-) create mode 100644 includes/MobileFrontendHooks.php diff --git a/includes/Hooks.php b/includes/Hooks.php index 714fda1d9..42d26dc70 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -39,11 +39,8 @@ use MediaWiki\ResourceLoader\ResourceLoader; use MediaWiki\Skins\Hook\SkinPageReadyConfigHook; use MediaWiki\SpecialPage\Hook\SpecialPageBeforeExecuteHook; use MobileContext; -use MobileFrontend\Features\Feature; -use MobileFrontend\Features\FeaturesManager; use OldChangesList; use OutputPage; -use RuntimeException; use Skin; use SpecialPage; use User; @@ -64,7 +61,7 @@ class Hooks implements SpecialPageBeforeExecuteHook, UserLogoutCompleteHook { - private const FEATURE_OVERFLOW_PAGE_ACTIONS = 'MinervaOverflowInPageActions'; + public const FEATURE_OVERFLOW_PAGE_ACTIONS = 'MinervaOverflowInPageActions'; /** * ResourceLoaderRegisterModules hook handler. @@ -128,81 +125,6 @@ class Hooks implements } } - /** - * Register mobile web beta features - * @see https://www.mediawiki.org/wiki/ - * Extension:MobileFrontend/MobileFrontendFeaturesRegistration - * - * @param FeaturesManager $featureManager - */ - public static function onMobileFrontendFeaturesRegistration( $featureManager ) { - $config = MediaWikiServices::getInstance()->getConfigFactory() - ->makeConfig( 'minerva' ); - - try { - $featureManager->registerFeature( - new Feature( - 'MinervaShowCategories', - 'skin-minerva', - $config->get( 'MinervaShowCategories' ) - ) - ); - $featureManager->registerFeature( - new Feature( - 'MinervaPageIssuesNewTreatment', - 'skin-minerva', - $config->get( 'MinervaPageIssuesNewTreatment' ) - ) - ); - $featureManager->registerFeature( - new Feature( - 'MinervaTalkAtTop', - 'skin-minerva', - $config->get( 'MinervaTalkAtTop' ) - ) - ); - $featureManager->registerFeature( - new Feature( - 'MinervaDonateLink', - 'skin-minerva', - $config->get( 'MinervaDonateLink' ) - ) - ); - $featureManager->registerFeature( - new Feature( - 'MinervaHistoryInPageActions', - 'skin-minerva', - $config->get( 'MinervaHistoryInPageActions' ) - ) - ); - $featureManager->registerFeature( - new Feature( - self::FEATURE_OVERFLOW_PAGE_ACTIONS, - 'skin-minerva', - $config->get( self::FEATURE_OVERFLOW_PAGE_ACTIONS ) - ) - ); - $featureManager->registerFeature( - new Feature( - 'MinervaAdvancedMainMenu', - 'skin-minerva', - $config->get( 'MinervaAdvancedMainMenu' ) - ) - ); - $featureManager->registerFeature( - new Feature( - 'MinervaPersonalMenu', - 'skin-minerva', - $config->get( 'MinervaPersonalMenu' ) - ) - ); - } catch ( RuntimeException $e ) { - // features already registered... - // due to a bug it's possible for this to run twice - // https://phabricator.wikimedia.org/T165068 - } - } - /** * Invocation of hook SpecialPageBeforeExecute * @@ -255,7 +177,7 @@ class Hooks implements * @param MobileContext $mobileContext * @param Skin $skin */ - private static function setMinervaSkinOptions( + public static function setMinervaSkinOptions( MobileContext $mobileContext, Skin $skin ) { // setSkinOptions is not available @@ -343,18 +265,6 @@ class Hooks implements } } - /** - * BeforePageDisplayMobile hook handler. - * - * @param MobileContext $mobileContext - * @param Skin $skin - */ - public static function onRequestContextCreateSkinMobile( - MobileContext $mobileContext, Skin $skin - ) { - self::setMinervaSkinOptions( $mobileContext, $skin ); - } - /** * ResourceLoaderGetConfigVars hook handler. * Used for setting JS variables which are pulled in dynamically with RL diff --git a/includes/MobileFrontendHooks.php b/includes/MobileFrontendHooks.php new file mode 100644 index 000000000..c7f136690 --- /dev/null +++ b/includes/MobileFrontendHooks.php @@ -0,0 +1,109 @@ +getConfigFactory() + ->makeConfig( 'minerva' ); + + try { + $featureManager->registerFeature( + new Feature( + 'MinervaShowCategories', + 'skin-minerva', + $config->get( 'MinervaShowCategories' ) + ) + ); + $featureManager->registerFeature( + new Feature( + 'MinervaPageIssuesNewTreatment', + 'skin-minerva', + $config->get( 'MinervaPageIssuesNewTreatment' ) + ) + ); + $featureManager->registerFeature( + new Feature( + 'MinervaTalkAtTop', + 'skin-minerva', + $config->get( 'MinervaTalkAtTop' ) + ) + ); + $featureManager->registerFeature( + new Feature( + 'MinervaDonateLink', + 'skin-minerva', + $config->get( 'MinervaDonateLink' ) + ) + ); + $featureManager->registerFeature( + new Feature( + 'MinervaHistoryInPageActions', + 'skin-minerva', + $config->get( 'MinervaHistoryInPageActions' ) + ) + ); + $featureManager->registerFeature( + new Feature( + Hooks::FEATURE_OVERFLOW_PAGE_ACTIONS, + 'skin-minerva', + $config->get( Hooks::FEATURE_OVERFLOW_PAGE_ACTIONS ) + ) + ); + $featureManager->registerFeature( + new Feature( + 'MinervaAdvancedMainMenu', + 'skin-minerva', + $config->get( 'MinervaAdvancedMainMenu' ) + ) + ); + $featureManager->registerFeature( + new Feature( + 'MinervaPersonalMenu', + 'skin-minerva', + $config->get( 'MinervaPersonalMenu' ) + ) + ); + } catch ( RuntimeException $e ) { + // features already registered... + // due to a bug it's possible for this to run twice + // https://phabricator.wikimedia.org/T165068 + } + } + + /** + * BeforePageDisplayMobile hook handler. + * + * @param MobileContext $mobileContext + * @param Skin $skin + */ + public function onRequestContextCreateSkinMobile( + MobileContext $mobileContext, Skin $skin + ) { + Hooks::setMinervaSkinOptions( $mobileContext, $skin ); + } +} diff --git a/skin.json b/skin.json index 9111193e5..f1ed92c57 100644 --- a/skin.json +++ b/skin.json @@ -137,6 +137,9 @@ "HookHandlers": { "main": { "class": "MediaWiki\\Minerva\\Hooks" + }, + "mobilefrontend": { + "class": "MediaWiki\\Minerva\\MobileFrontendHooks" } }, "Hooks": { @@ -144,12 +147,8 @@ "SkinPageReadyConfig": "main", "UserLogoutComplete": "main", "FetchChangesList": "main", - "MobileFrontendFeaturesRegistration": [ - "MediaWiki\\Minerva\\Hooks::onMobileFrontendFeaturesRegistration" - ], - "RequestContextCreateSkinMobile": [ - "MediaWiki\\Minerva\\Hooks::onRequestContextCreateSkinMobile" - ], + "MobileFrontendFeaturesRegistration": "mobilefrontend", + "RequestContextCreateSkinMobile": "mobilefrontend", "PreferencesGetLayout": "main", "SpecialPageBeforeExecute": "main", "ResourceLoaderGetConfigVars": "main",