From 521f44ede50dc6510eaed8651d1e861134716828 Mon Sep 17 00:00:00 2001 From: Fomafix Date: Sat, 28 Sep 2024 21:38:19 +0000 Subject: [PATCH] Inject MainConfig services into Cite Change-Id: I24cc3e4965d4a9fe41dcd799dac4cbce762fb068 --- extension.json | 5 +++- src/Cite.php | 10 ++++--- src/Hooks/CiteParserHooks.php | 11 +++++++- src/Hooks/CiteParserTagHooks.php | 27 ++++++++++++------- tests/phpunit/CiteIntegrationTest.php | 3 ++- tests/phpunit/integration/CiteTest.php | 6 +++-- tests/phpunit/unit/CiteParserHooksTest.php | 15 ++++++++--- tests/phpunit/unit/CiteParserTagHooksTest.php | 22 +++++++++++---- 8 files changed, 72 insertions(+), 27 deletions(-) diff --git a/extension.json b/extension.json index f91eee9a9..cab5eadac 100644 --- a/extension.json +++ b/extension.json @@ -47,7 +47,10 @@ ] }, "parser": { - "class": "Cite\\Hooks\\CiteParserHooks" + "class": "Cite\\Hooks\\CiteParserHooks", + "services": [ + "MainConfig" + ] } }, "ResourceModules": { diff --git a/src/Cite.php b/src/Cite.php index 89acd94ba..549be4ae4 100644 --- a/src/Cite.php +++ b/src/Cite.php @@ -25,8 +25,8 @@ namespace Cite; use LogicException; +use MediaWiki\Config\Config; use MediaWiki\Html\Html; -use MediaWiki\MediaWikiServices; use MediaWiki\Parser\Parser; use MediaWiki\Parser\Sanitizer; use StatusValue; @@ -70,8 +70,9 @@ class Cite { */ private StatusValue $mReferencesErrors; private ReferenceStack $referenceStack; + private Config $config; - public function __construct( Parser $parser ) { + public function __construct( Parser $parser, Config $config ) { $this->isSectionPreview = $parser->getOptions()->getIsSectionPreview(); $messageLocalizer = new ReferenceMessageLocalizer( $parser->getContentLanguage() ); $this->errorReporter = new ErrorReporter( $messageLocalizer ); @@ -88,6 +89,7 @@ class Cite { $anchorFormatter, $messageLocalizer ); + $this->config = $config; } /** @@ -141,7 +143,7 @@ class Cite { $this->referenceStack, $this->inReferencesGroup, $this->isSectionPreview, - MediaWikiServices::getInstance()->getMainConfig()->get( 'CiteBookReferencing' ) + $this->config->get( 'CiteBookReferencing' ) ); // @phan-suppress-next-line PhanParamTooFewUnpack No good way to document it. $status->merge( $validator->validateRef( $text, ...array_values( $arguments ) ) ); @@ -307,7 +309,7 @@ class Cite { string $group, string $responsive = null ): string { - $responsiveReferences = MediaWikiServices::getInstance()->getMainConfig()->get( 'CiteResponsiveReferences' ); + $responsiveReferences = $this->config->get( 'CiteResponsiveReferences' ); return $this->referenceListFormatter->formatReferences( $parser, diff --git a/src/Hooks/CiteParserHooks.php b/src/Hooks/CiteParserHooks.php index 0e3792080..306ec204a 100644 --- a/src/Hooks/CiteParserHooks.php +++ b/src/Hooks/CiteParserHooks.php @@ -3,6 +3,7 @@ namespace Cite\Hooks; use Cite\Cite; +use MediaWiki\Config\Config; use MediaWiki\Hook\ParserAfterParseHook; use MediaWiki\Hook\ParserClearStateHook; use MediaWiki\Hook\ParserClonedHook; @@ -20,13 +21,21 @@ class CiteParserHooks implements ParserAfterParseHook { + private CiteParserTagHooks $citeParserTagHooks; + + public function __construct( + Config $config + ) { + $this->citeParserTagHooks = new CiteParserTagHooks( $config ); + } + /** * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit * * @param Parser $parser */ public function onParserFirstCallInit( $parser ) { - CiteParserTagHooks::register( $parser ); + $this->citeParserTagHooks->register( $parser ); } /** diff --git a/src/Hooks/CiteParserTagHooks.php b/src/Hooks/CiteParserTagHooks.php index 06dade9ef..ec0058d70 100644 --- a/src/Hooks/CiteParserTagHooks.php +++ b/src/Hooks/CiteParserTagHooks.php @@ -3,6 +3,7 @@ namespace Cite\Hooks; use Cite\Cite; +use MediaWiki\Config\Config; use MediaWiki\Parser\Parser; use PPFrame; @@ -11,12 +12,20 @@ use PPFrame; */ class CiteParserTagHooks { + private Config $config; + + public function __construct( + Config $config + ) { + $this->config = $config; + } + /** * Enables the two and tags. */ - public static function register( Parser $parser ): void { - $parser->setHook( 'ref', [ __CLASS__, 'ref' ] ); - $parser->setHook( 'references', [ __CLASS__, 'references' ] ); + public function register( Parser $parser ): void { + $parser->setHook( 'ref', [ $this, 'ref' ] ); + $parser->setHook( 'references', [ $this, 'references' ] ); } /** @@ -29,13 +38,13 @@ class CiteParserTagHooks { * * @return string HTML */ - public static function ref( + public function ref( ?string $text, array $argv, Parser $parser, PPFrame $frame ): string { - $cite = self::citeForParser( $parser ); + $cite = $this->citeForParser( $parser ); $result = $cite->ref( $parser, $text, $argv ); if ( $result === null ) { @@ -60,13 +69,13 @@ class CiteParserTagHooks { * * @return string HTML */ - public static function references( + public function references( ?string $text, array $argv, Parser $parser, PPFrame $frame ): string { - $cite = self::citeForParser( $parser ); + $cite = $this->citeForParser( $parser ); $result = $cite->references( $parser, $text, $argv ); if ( $result === null ) { @@ -83,8 +92,8 @@ class CiteParserTagHooks { /** * Get or create Cite state for this parser. */ - private static function citeForParser( Parser $parser ): Cite { - $parser->extCite ??= new Cite( $parser ); + private function citeForParser( Parser $parser ): Cite { + $parser->extCite ??= new Cite( $parser, $this->config ); return $parser->extCite; } diff --git a/tests/phpunit/CiteIntegrationTest.php b/tests/phpunit/CiteIntegrationTest.php index 0d0408eb2..76242d016 100644 --- a/tests/phpunit/CiteIntegrationTest.php +++ b/tests/phpunit/CiteIntegrationTest.php @@ -94,7 +94,8 @@ class CiteIntegrationTest extends \MediaWikiIntegrationTestCase { $mockParser = $this->createNoOpMock( Parser::class, [ 'getOptions', 'getContentLanguage' ] ); $mockParser->method( 'getOptions' )->willReturn( $mockOptions ); $mockParser->method( 'getContentLanguage' )->willReturn( $language ); - return new Cite( $mockParser ); + $config = $this->getServiceContainer()->getMainConfig(); + return new Cite( $mockParser, $config ); } } diff --git a/tests/phpunit/integration/CiteTest.php b/tests/phpunit/integration/CiteTest.php index 7a34f83d8..d9ae71fb7 100644 --- a/tests/phpunit/integration/CiteTest.php +++ b/tests/phpunit/integration/CiteTest.php @@ -434,8 +434,9 @@ class CiteTest extends \MediaWikiIntegrationTestCase { $parser->method( 'getOptions' )->willReturn( $parserOptions ); $parser->method( 'getContentLanguage' )->willReturn( $language ); + $config = $this->getServiceContainer()->getMainConfig(); /** @var Cite $cite */ - $cite = TestingAccessWrapper::newFromObject( new Cite( $parser ) ); + $cite = TestingAccessWrapper::newFromObject( new Cite( $parser, $config ) ); // Assume the currently parsed is wrapped in $cite->inReferencesGroup = ''; @@ -463,7 +464,8 @@ class CiteTest extends \MediaWikiIntegrationTestCase { $mockParser = $this->createNoOpMock( Parser::class, [ 'getOptions', 'getContentLanguage' ] ); $mockParser->method( 'getOptions' )->willReturn( $mockOptions ); $mockParser->method( 'getContentLanguage' )->willReturn( $language ); - return new Cite( $mockParser ); + $config = $this->getServiceContainer()->getMainConfig(); + return new Cite( $mockParser, $config ); } } diff --git a/tests/phpunit/unit/CiteParserHooksTest.php b/tests/phpunit/unit/CiteParserHooksTest.php index a47ec49e1..f5a3b0240 100644 --- a/tests/phpunit/unit/CiteParserHooksTest.php +++ b/tests/phpunit/unit/CiteParserHooksTest.php @@ -4,6 +4,7 @@ namespace Cite\Tests\Unit; use Cite\Cite; use Cite\Hooks\CiteParserHooks; +use MediaWiki\Config\Config; use MediaWiki\Parser\Parser; use MediaWiki\Parser\ParserOutput; use ParserOptions; @@ -15,6 +16,12 @@ use StripState; */ class CiteParserHooksTest extends \MediaWikiUnitTestCase { + private function newCiteParserHooks() { + return new CiteParserHooks( + $this->createNoOpMock( Config::class ) + ); + } + public function testOnParserFirstCallInit() { $parser = $this->createNoOpMock( Parser::class, [ 'setHook' ] ); $expectedTags = [ 'ref' => true, 'references' => true ]; @@ -25,7 +32,7 @@ class CiteParserHooksTest extends \MediaWikiUnitTestCase { unset( $expectedTags[$tag] ); } ); - $citeParserHooks = new CiteParserHooks(); + $citeParserHooks = $this->newCiteParserHooks(); $citeParserHooks->onParserFirstCallInit( $parser ); } @@ -33,7 +40,7 @@ class CiteParserHooksTest extends \MediaWikiUnitTestCase { $parser = $this->createNoOpMock( Parser::class, [ '__isset' ] ); $parser->extCite = $this->createMock( Cite::class ); - $citeParserHooks = new CiteParserHooks(); + $citeParserHooks = $this->newCiteParserHooks(); $citeParserHooks->onParserClearState( $parser ); $this->assertNull( $parser->extCite ?? null ); @@ -43,7 +50,7 @@ class CiteParserHooksTest extends \MediaWikiUnitTestCase { $parser = $this->createNoOpMock( Parser::class, [ '__isset' ] ); $parser->extCite = $this->createMock( Cite::class ); - $citeParserHooks = new CiteParserHooks(); + $citeParserHooks = $this->newCiteParserHooks(); $citeParserHooks->onParserCloned( $parser ); $this->assertNull( $parser->extCite ?? null ); @@ -66,7 +73,7 @@ class CiteParserHooksTest extends \MediaWikiUnitTestCase { $parser->extCite = $cite; $text = ''; - $citeParserHooks = new CiteParserHooks(); + $citeParserHooks = $this->newCiteParserHooks(); $citeParserHooks->onParserAfterParse( $parser, $text, $this->createMock( StripState::class ) ); } diff --git a/tests/phpunit/unit/CiteParserTagHooksTest.php b/tests/phpunit/unit/CiteParserTagHooksTest.php index 0eda825c2..ed4b5437a 100644 --- a/tests/phpunit/unit/CiteParserTagHooksTest.php +++ b/tests/phpunit/unit/CiteParserTagHooksTest.php @@ -4,6 +4,7 @@ namespace Cite\Tests\Unit; use Cite\Cite; use Cite\Hooks\CiteParserTagHooks; +use MediaWiki\Config\Config; use MediaWiki\Parser\Parser; use MediaWiki\Parser\ParserOutput; use PPFrame; @@ -14,6 +15,12 @@ use PPFrame; */ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase { + private function newCiteParserTagHooks() { + return new CiteParserTagHooks( + $this->createNoOpMock( Config::class ) + ); + } + public function testRegister() { $parser = $this->createNoOpMock( Parser::class, [ 'setHook' ] ); $expectedTags = [ 'ref' => true, 'references' => true ]; @@ -24,7 +31,8 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase { unset( $expectedTags[$tag] ); } ); - CiteParserTagHooks::register( $parser ); + $citeParserTagHooks = $this->newCiteParserTagHooks(); + $citeParserTagHooks->register( $parser ); } public function testRef_fails() { @@ -37,7 +45,8 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase { $frame = $this->createMock( PPFrame::class ); - $html = CiteParserTagHooks::ref( null, [], $parser, $frame ); + $citeParserTagHooks = $this->newCiteParserTagHooks(); + $html = $citeParserTagHooks->ref( null, [], $parser, $frame ); $this->assertSame( '<ref></ref>', $html ); } @@ -60,7 +69,8 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase { $frame = $this->createMock( PPFrame::class ); - $html = CiteParserTagHooks::ref( null, [], $parser, $frame ); + $citeParserTagHooks = $this->newCiteParserTagHooks(); + $html = $citeParserTagHooks->ref( null, [], $parser, $frame ); $this->assertSame( '', $html ); } @@ -74,7 +84,8 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase { $frame = $this->createMock( PPFrame::class ); - $html = CiteParserTagHooks::references( null, [], $parser, $frame ); + $citeParserTagHooks = $this->newCiteParserTagHooks(); + $html = $citeParserTagHooks->references( null, [], $parser, $frame ); $this->assertSame( '<references/>', $html ); } @@ -89,7 +100,8 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase { $frame = $this->createMock( PPFrame::class ); - $html = CiteParserTagHooks::references( null, [], $parser, $frame ); + $citeParserTagHooks = $this->newCiteParserTagHooks(); + $html = $citeParserTagHooks->references( null, [], $parser, $frame ); $this->assertSame( '', $html ); }