From bc9added4f24b892da83d8cd1769e60688dfc147 Mon Sep 17 00:00:00 2001 From: DannyS712 Date: Mon, 5 Feb 2024 18:19:28 +0000 Subject: [PATCH] Hooks: inject DisambiguatorLookup as an optional service Follow-up: Id2a10f2f1be0e38ddb8ebef5d5babb3daac3ce9a Change-Id: Ib1a0297fd17e1cec0847d68a0c184e3c713f0111 --- extension.json | 5 ++++- includes/Hooks.php | 26 +++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/extension.json b/extension.json index 2cb68537..c27b10f1 100644 --- a/extension.json +++ b/extension.json @@ -31,7 +31,10 @@ }, "HookHandlers": { "main": { - "class": "RelatedArticles\\Hooks" + "class": "RelatedArticles\\Hooks", + "optional_services": [ + "DisambiguatorLookup" + ] } }, "MessagesDirs": { diff --git a/includes/Hooks.php b/includes/Hooks.php index 65377af1..a05e7939 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -4,6 +4,7 @@ namespace RelatedArticles; use IContextSource; use MediaWiki\Config\Config; +use MediaWiki\Extension\Disambiguator\Lookup; use MediaWiki\Hook\BeforePageDisplayHook; use MediaWiki\Hook\MakeGlobalVariablesScriptHook; use MediaWiki\Hook\OutputPageParserOutputHook; @@ -27,6 +28,13 @@ class Hooks implements SkinAfterContentHook { + /** Either a Lookup from the Disambiguator extension, or null if that is not installed */ + private ?Lookup $disambiguatorLookup; + + public function __construct( ?Lookup $disambiguatorLookup ) { + $this->disambiguatorLookup = $disambiguatorLookup; + } + /** * Handler for the MakeGlobalVariablesScript hook. * @@ -52,13 +60,9 @@ class Hooks implements * @param Title $title * @return bool */ - private static function isDisambiguationPage( Title $title ) { - $services = MediaWikiServices::getInstance(); - if ( !$services->hasService( 'DisambiguatorLookup' ) ) { - return false; - } - return $services->getService( 'DisambiguatorLookup' ) - ->isDisambiguationPage( $title ); + private function isDisambiguationPage( Title $title ) { + return $this->disambiguatorLookup && + $this->disambiguatorLookup->isDisambiguationPage( $title ); } /** @@ -101,7 +105,7 @@ class Hooks implements * @param Skin $skin * @return bool */ - private static function hasRelatedArticles( Skin $skin ): bool { + private function hasRelatedArticles( Skin $skin ): bool { $title = $skin->getTitle(); $action = $skin->getRequest()->getRawVal( 'action', 'view' ); return $title->inNamespace( NS_MAIN ) && @@ -110,7 +114,7 @@ class Hooks implements !$title->isMainPage() && $title->exists() && !self::isDiffPage( $skin ) && - !self::isDisambiguationPage( $title ) && + !$this->isDisambiguationPage( $title ) && self::isReadMoreAllowedOnSkin( $skin ); } @@ -135,7 +139,7 @@ class Hooks implements * @param Skin $skin Skin object that will be used to generate the page */ public function onBeforePageDisplay( $out, $skin ): void { - if ( self::hasRelatedArticles( $skin ) ) { + if ( $this->hasRelatedArticles( $skin ) ) { $out->addModules( [ 'ext.relatedArticles.readMore.bootstrap' ] ); $out->addModuleStyles( [ 'ext.relatedArticles.styles' ] ); } @@ -230,7 +234,7 @@ class Hooks implements * @param Skin $skin */ public function onSkinAfterContent( &$data, $skin ) { - if ( self::hasRelatedArticles( $skin ) ) { + if ( $this->hasRelatedArticles( $skin ) ) { $data .= Html::element( 'div', [ 'class' => 'read-more-container' ] ); } }