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

The service 'GadgetsRepo' gets injected as optional service.

This change requires a phan dependency on extension Gadgets in
project integration/config in file zuul/parameter_functions.py:
I5052e0c666b7dc7af6061e57001f9feac666e029

Change-Id: Ib405ad79b3c348bed51a8938a6a8f73bd35267d2
This commit is contained in:
Fomafix 2024-06-29 21:36:41 +00:00
parent 08730d2a85
commit e0dc1c0c32
4 changed files with 33 additions and 12 deletions

View file

@ -1,3 +1,19 @@
<?php <?php
return require __DIR__ . '/../vendor/mediawiki/mediawiki-phan-config/src/config.php'; $cfg = require __DIR__ . '/../vendor/mediawiki/mediawiki-phan-config/src/config.php';
$cfg['directory_list'] = array_merge(
$cfg['directory_list'],
[
'../../extensions/Gadgets',
]
);
$cfg['exclude_analysis_directory_list'] = array_merge(
$cfg['exclude_analysis_directory_list'],
[
'../../extensions/Gadgets',
]
);
return $cfg;

View file

@ -310,6 +310,9 @@
"services": [ "services": [
"UserOptionsLookup", "UserOptionsLookup",
"MainConfig" "MainConfig"
],
"optional_services": [
"GadgetsRepo"
] ]
} }
}, },

View file

@ -29,19 +29,23 @@ class Hooks implements
private array $conflictingGadgets; private array $conflictingGadgets;
private bool $useV6; private bool $useV6;
private bool $isSupportedRtlWiki; private bool $isSupportedRtlWiki;
private ?GadgetRepo $gadgetRepo;
/** /**
* @param UserOptionsLookup $userOptionsLookup * @param UserOptionsLookup $userOptionsLookup
* @param Config $config * @param Config $config
* @param GadgetRepo|null $gadgetRepo
*/ */
public function __construct( public function __construct(
UserOptionsLookup $userOptionsLookup, UserOptionsLookup $userOptionsLookup,
Config $config Config $config,
?GadgetRepo $gadgetRepo
) { ) {
$this->userOptionsLookup = $userOptionsLookup; $this->userOptionsLookup = $userOptionsLookup;
$this->useV6 = $config->get( 'CodeMirrorV6' ); $this->useV6 = $config->get( 'CodeMirrorV6' );
$this->conflictingGadgets = $config->get( 'CodeMirrorConflictingGadgets' ); $this->conflictingGadgets = $config->get( 'CodeMirrorConflictingGadgets' );
$this->isSupportedRtlWiki = $config->get( 'CodeMirrorRTL' ); $this->isSupportedRtlWiki = $config->get( 'CodeMirrorRTL' );
$this->gadgetRepo = $gadgetRepo;
} }
/** /**
@ -88,15 +92,13 @@ class Hooks implements
* @return bool * @return bool
*/ */
private function conflictingGadgetsEnabled( ExtensionRegistry $extensionRegistry, User $user ): bool { private function conflictingGadgetsEnabled( ExtensionRegistry $extensionRegistry, User $user ): bool {
if ( !$extensionRegistry->isLoaded( 'Gadgets' ) ) { if ( !$extensionRegistry->isLoaded( 'Gadgets' ) || !$this->gadgetRepo ) {
return false; return false;
} }
// @phan-suppress-next-line PhanUndeclaredClassMethod Code path won't be followed if class doesn't exist. $conflictingGadgets = array_intersect( $this->conflictingGadgets, $this->gadgetRepo->getGadgetIds() );
$gadgetRepo = GadgetRepo::singleton();
$conflictingGadgets = array_intersect( $this->conflictingGadgets, $gadgetRepo->getGadgetIds() );
foreach ( $conflictingGadgets as $conflictingGadget ) { foreach ( $conflictingGadgets as $conflictingGadget ) {
try { try {
if ( $gadgetRepo->getGadget( $conflictingGadget )->isEnabled( $user ) ) { if ( $this->gadgetRepo->getGadget( $conflictingGadget )->isEnabled( $user ) ) {
return true; return true;
} }
} catch ( InvalidArgumentException $e ) { } catch ( InvalidArgumentException $e ) {

View file

@ -59,7 +59,7 @@ class HookTest extends MediaWikiIntegrationTestCase {
$isFirstCall = false; $isFirstCall = false;
} ); } );
$hooks = new Hooks( $userOptionsLookup, $this->getServiceContainer()->getMainConfig() ); $hooks = new Hooks( $userOptionsLookup, $this->getServiceContainer()->getMainConfig(), null );
$method = $readOnly ? 'onEditPage__showReadOnlyForm_initial' : 'onEditPage__showEditForm_initial'; $method = $readOnly ? 'onEditPage__showReadOnlyForm_initial' : 'onEditPage__showEditForm_initial';
$hooks->{$method}( $this->createMock( EditPage::class ), $out ); $hooks->{$method}( $this->createMock( EditPage::class ), $out );
} }
@ -97,7 +97,7 @@ class HookTest extends MediaWikiIntegrationTestCase {
// CodeMirror 5 // CodeMirror 5
$this->overrideConfigValues( [ 'CodeMirrorV6' => false ] ); $this->overrideConfigValues( [ 'CodeMirrorV6' => false ] );
$hook = new Hooks( $userOptionsLookup, $config ); $hook = new Hooks( $userOptionsLookup, $config, null );
$preferences = []; $preferences = [];
$hook->onGetPreferences( $user, $preferences ); $hook->onGetPreferences( $user, $preferences );
self::assertArrayHasKey( 'usecodemirror', $preferences ); self::assertArrayHasKey( 'usecodemirror', $preferences );
@ -107,7 +107,7 @@ class HookTest extends MediaWikiIntegrationTestCase {
// CodeMirror 6 // CodeMirror 6
$this->overrideConfigValues( [ 'CodeMirrorV6' => true ] ); $this->overrideConfigValues( [ 'CodeMirrorV6' => true ] );
$hook = new Hooks( $userOptionsLookup, $config ); $hook = new Hooks( $userOptionsLookup, $config, null );
$preferences = []; $preferences = [];
$hook->onGetPreferences( $user, $preferences ); $hook->onGetPreferences( $user, $preferences );
self::assertArrayHasKey( 'usecodemirror', $preferences ); self::assertArrayHasKey( 'usecodemirror', $preferences );
@ -153,6 +153,7 @@ class HookTest extends MediaWikiIntegrationTestCase {
->with( 'CodeMirrorContentModels' ) ->with( 'CodeMirrorContentModels' )
->willReturn( [ CONTENT_MODEL_WIKITEXT ] ); ->willReturn( [ CONTENT_MODEL_WIKITEXT ] );
$gadgetRepoMock = null;
if ( $conds['gadget'] ) { if ( $conds['gadget'] ) {
$gadgetMock = $this->createMock( Gadget::class ); $gadgetMock = $this->createMock( Gadget::class );
$gadgetMock->expects( $this->once() ) $gadgetMock->expects( $this->once() )
@ -165,10 +166,9 @@ class HookTest extends MediaWikiIntegrationTestCase {
$gadgetRepoMock->expects( $this->once() ) $gadgetRepoMock->expects( $this->once() )
->method( 'getGadgetIds' ) ->method( 'getGadgetIds' )
->willReturn( [ $conds['gadget'] ] ); ->willReturn( [ $conds['gadget'] ] );
GadgetRepo::setSingleton( $gadgetRepoMock );
} }
$hooks = new Hooks( $userOptionsLookup, $this->getServiceContainer()->getMainConfig() ); $hooks = new Hooks( $userOptionsLookup, $this->getServiceContainer()->getMainConfig(), $gadgetRepoMock );
self::assertSame( $expectation, $hooks->shouldLoadCodeMirror( $out, $extensionRegistry ) ); self::assertSame( $expectation, $hooks->shouldLoadCodeMirror( $out, $extensionRegistry ) );
} }