From 7e3a444a99fb9c290a85cd46d428025b3c7f4270 Mon Sep 17 00:00:00 2001 From: Fomafix Date: Sat, 29 Jun 2024 19:29:31 +0000 Subject: [PATCH] Use service 'GadgetsRepo' instead of deprecated GadgetRepo::singleton() The service is optional to support with and without extension Gadgets. Change-Id: I41d1516e185ceb5c49b38361360a3adad1a333de --- includes/PopupsGadgetsIntegration.php | 38 +++++++------------ includes/ServiceWiring.php | 4 +- .../phpunit/PopupsGadgetsIntegrationTest.php | 25 ++---------- 3 files changed, 21 insertions(+), 46 deletions(-) diff --git a/includes/PopupsGadgetsIntegration.php b/includes/PopupsGadgetsIntegration.php index ca157eac7..1d4cd01b0 100644 --- a/includes/PopupsGadgetsIntegration.php +++ b/includes/PopupsGadgetsIntegration.php @@ -19,7 +19,6 @@ */ namespace Popups; -use ExtensionRegistry; use MediaWiki\Config\Config; use MediaWiki\Extension\Gadgets\GadgetRepo; use MediaWiki\User\User; @@ -35,11 +34,6 @@ class PopupsGadgetsIntegration { public const CONFIG_REFERENCE_TOOLTIPS_NAME = 'PopupsConflictingRefTooltipsGadgetName'; - /** - * @var \ExtensionRegistry - */ - private $extensionRegistry; - /** * @var string */ @@ -50,16 +44,21 @@ class PopupsGadgetsIntegration { */ private $refTooltipsGadgetName; + private ?GadgetRepo $gadgetRepo; + /** * @param Config $config MediaWiki configuration - * @param ExtensionRegistry $extensionRegistry MediaWiki extension registry + * @param GadgetRepo|null $gadgetRepo */ - public function __construct( Config $config, ExtensionRegistry $extensionRegistry ) { - $this->extensionRegistry = $extensionRegistry; + public function __construct( + Config $config, + ?GadgetRepo $gadgetRepo + ) { $this->navPopupsGadgetName = $this->sanitizeGadgetName( $config->get( self::CONFIG_NAVIGATION_POPUPS_NAME ) ); $this->refTooltipsGadgetName = $this->sanitizeGadgetName( $config->get( self::CONFIG_REFERENCE_TOOLTIPS_NAME ) ); + $this->gadgetRepo = $gadgetRepo; } /** @@ -70,13 +69,6 @@ class PopupsGadgetsIntegration { return str_replace( ' ', '_', trim( $gadgetName ) ); } - /** - * @return bool - */ - private function isGadgetExtensionEnabled() { - return $this->extensionRegistry->isLoaded( 'Gadgets' ); - } - /** * Check if Popups conflicts with Nav Popups Gadget * If user enabled Nav Popups, Popups is unavailable @@ -85,12 +77,11 @@ class PopupsGadgetsIntegration { * @return bool */ public function conflictsWithNavPopupsGadget( User $user ) { - if ( $this->isGadgetExtensionEnabled() ) { - $gadgetsRepo = GadgetRepo::singleton(); - $match = array_search( $this->navPopupsGadgetName, $gadgetsRepo->getGadgetIds() ); + if ( $this->gadgetRepo ) { + $match = array_search( $this->navPopupsGadgetName, $this->gadgetRepo->getGadgetIds() ); if ( $match !== false ) { try { - return $gadgetsRepo->getGadget( $this->navPopupsGadgetName ) + return $this->gadgetRepo->getGadget( $this->navPopupsGadgetName ) ->isEnabled( $user ); } catch ( \InvalidArgumentException $e ) { return false; @@ -108,12 +99,11 @@ class PopupsGadgetsIntegration { * @return bool */ public function conflictsWithRefTooltipsGadget( User $user ) { - if ( $this->isGadgetExtensionEnabled() ) { - $gadgetsRepo = GadgetRepo::singleton(); - $match = array_search( $this->refTooltipsGadgetName, $gadgetsRepo->getGadgetIds() ); + if ( $this->gadgetRepo ) { + $match = array_search( $this->refTooltipsGadgetName, $this->gadgetRepo->getGadgetIds() ); if ( $match !== false ) { try { - return $gadgetsRepo->getGadget( $this->refTooltipsGadgetName ) + return $this->gadgetRepo->getGadget( $this->refTooltipsGadgetName ) ->isEnabled( $user ); } catch ( \InvalidArgumentException $e ) { return false; diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index 000ae7627..b3ca3844a 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -18,7 +18,9 @@ return [ 'Popups.GadgetsIntegration' => static function ( MediaWikiServices $services ): PopupsGadgetsIntegration { return new PopupsGadgetsIntegration( $services->getService( 'Popups.Config' ), - ExtensionRegistry::getInstance() + ExtensionRegistry::getInstance()->isLoaded( 'Gadgets' ) ? + $services->getService( 'GadgetsRepo' ) : + null ); }, 'Popups.Logger' => static function ( MediaWikiServices $services ): LoggerInterface { diff --git a/tests/phpunit/PopupsGadgetsIntegrationTest.php b/tests/phpunit/PopupsGadgetsIntegrationTest.php index 97428ee43..d73f514c3 100644 --- a/tests/phpunit/PopupsGadgetsIntegrationTest.php +++ b/tests/phpunit/PopupsGadgetsIntegrationTest.php @@ -23,7 +23,6 @@ use MediaWiki\Config\HashConfig; use MediaWiki\Extension\Gadgets\Gadget; use MediaWiki\Extension\Gadgets\GadgetRepo; use MediaWiki\User\User; -use PHPUnit\Framework\MockObject\MockObject; use Popups\PopupsGadgetsIntegration; /** @@ -56,18 +55,6 @@ class PopupsGadgetsIntegrationTest extends MediaWikiIntegrationTestCase { } } - /** - * @param bool $gadgetsEnabled - * @return MockObject|ExtensionRegistry - */ - private function getExtensionRegistryMock( $gadgetsEnabled ) { - $mock = $this->createMock( ExtensionRegistry::class ); - $mock->method( 'isLoaded' ) - ->with( 'Gadgets' ) - ->willReturn( $gadgetsEnabled ); - return $mock; - } - private function getConfig( string $name = self::NAV_POPUPS_GADGET_NAME ): Config { return new HashConfig( [ PopupsGadgetsIntegration::CONFIG_NAVIGATION_POPUPS_NAME => $name, @@ -77,14 +64,14 @@ class PopupsGadgetsIntegrationTest extends MediaWikiIntegrationTestCase { /** * @covers ::conflictsWithNavPopupsGadget - * @covers ::isGadgetExtensionEnabled * @covers ::__construct * @covers ::sanitizeGadgetName */ public function testConflictsWithNavPopupsGadgetIfGadgetsExtensionIsNotLoaded() { $user = $this->createMock( User::class ); $integration = new PopupsGadgetsIntegration( $this->getConfig(), - $this->getExtensionRegistryMock( false ) ); + null + ); $this->assertFalse( $integration->conflictsWithNavPopupsGadget( $user ), 'No conflict is identified.' ); @@ -207,16 +194,12 @@ class PopupsGadgetsIntegrationTest extends MediaWikiIntegrationTestCase { GadgetRepo $repoMock, $expected ) { - $origGadgetsRepo = GadgetRepo::singleton(); - GadgetRepo::setSingleton( $repoMock ); - $integration = new PopupsGadgetsIntegration( $config, - $this->getExtensionRegistryMock( true ) ); + $repoMock + ); $this->assertSame( $expected, $integration->conflictsWithNavPopupsGadget( $user ), ( $expected ? 'A' : 'No' ) . ' conflict is identified.' ); - - GadgetRepo::setSingleton( $origGadgetsRepo ); } }