Use service 'GadgetsRepo' instead of deprecated GadgetRepo::singleton()

The service is optional to support with and without extension Gadgets.

Change-Id: I41d1516e185ceb5c49b38361360a3adad1a333de
This commit is contained in:
Fomafix 2024-06-29 19:29:31 +00:00
parent c973491965
commit 7e3a444a99
3 changed files with 21 additions and 46 deletions

View file

@ -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;

View file

@ -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 {

View file

@ -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 );
}
}