Merge "Inject MainConfig services into Cite"

This commit is contained in:
jenkins-bot 2024-10-04 19:34:11 +00:00 committed by Gerrit Code Review
commit 8f60a489bd
8 changed files with 72 additions and 27 deletions

View file

@ -47,7 +47,10 @@
] ]
}, },
"parser": { "parser": {
"class": "Cite\\Hooks\\CiteParserHooks" "class": "Cite\\Hooks\\CiteParserHooks",
"services": [
"MainConfig"
]
} }
}, },
"ResourceModules": { "ResourceModules": {

View file

@ -25,8 +25,8 @@
namespace Cite; namespace Cite;
use LogicException; use LogicException;
use MediaWiki\Config\Config;
use MediaWiki\Html\Html; use MediaWiki\Html\Html;
use MediaWiki\MediaWikiServices;
use MediaWiki\Parser\Parser; use MediaWiki\Parser\Parser;
use MediaWiki\Parser\Sanitizer; use MediaWiki\Parser\Sanitizer;
use StatusValue; use StatusValue;
@ -70,8 +70,9 @@ class Cite {
*/ */
private StatusValue $mReferencesErrors; private StatusValue $mReferencesErrors;
private ReferenceStack $referenceStack; private ReferenceStack $referenceStack;
private Config $config;
public function __construct( Parser $parser ) { public function __construct( Parser $parser, Config $config ) {
$this->isSectionPreview = $parser->getOptions()->getIsSectionPreview(); $this->isSectionPreview = $parser->getOptions()->getIsSectionPreview();
$messageLocalizer = new ReferenceMessageLocalizer( $parser->getContentLanguage() ); $messageLocalizer = new ReferenceMessageLocalizer( $parser->getContentLanguage() );
$this->errorReporter = new ErrorReporter( $messageLocalizer ); $this->errorReporter = new ErrorReporter( $messageLocalizer );
@ -88,6 +89,7 @@ class Cite {
$anchorFormatter, $anchorFormatter,
$messageLocalizer $messageLocalizer
); );
$this->config = $config;
} }
/** /**
@ -141,7 +143,7 @@ class Cite {
$this->referenceStack, $this->referenceStack,
$this->inReferencesGroup, $this->inReferencesGroup,
$this->isSectionPreview, $this->isSectionPreview,
MediaWikiServices::getInstance()->getMainConfig()->get( 'CiteBookReferencing' ) $this->config->get( 'CiteBookReferencing' )
); );
// @phan-suppress-next-line PhanParamTooFewUnpack No good way to document it. // @phan-suppress-next-line PhanParamTooFewUnpack No good way to document it.
$status->merge( $validator->validateRef( $text, ...array_values( $arguments ) ) ); $status->merge( $validator->validateRef( $text, ...array_values( $arguments ) ) );
@ -307,7 +309,7 @@ class Cite {
string $group, string $group,
string $responsive = null string $responsive = null
): string { ): string {
$responsiveReferences = MediaWikiServices::getInstance()->getMainConfig()->get( 'CiteResponsiveReferences' ); $responsiveReferences = $this->config->get( 'CiteResponsiveReferences' );
return $this->referenceListFormatter->formatReferences( return $this->referenceListFormatter->formatReferences(
$parser, $parser,

View file

@ -3,6 +3,7 @@
namespace Cite\Hooks; namespace Cite\Hooks;
use Cite\Cite; use Cite\Cite;
use MediaWiki\Config\Config;
use MediaWiki\Hook\ParserAfterParseHook; use MediaWiki\Hook\ParserAfterParseHook;
use MediaWiki\Hook\ParserClearStateHook; use MediaWiki\Hook\ParserClearStateHook;
use MediaWiki\Hook\ParserClonedHook; use MediaWiki\Hook\ParserClonedHook;
@ -20,13 +21,21 @@ class CiteParserHooks implements
ParserAfterParseHook ParserAfterParseHook
{ {
private CiteParserTagHooks $citeParserTagHooks;
public function __construct(
Config $config
) {
$this->citeParserTagHooks = new CiteParserTagHooks( $config );
}
/** /**
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit
* *
* @param Parser $parser * @param Parser $parser
*/ */
public function onParserFirstCallInit( $parser ) { public function onParserFirstCallInit( $parser ) {
CiteParserTagHooks::register( $parser ); $this->citeParserTagHooks->register( $parser );
} }
/** /**

View file

@ -3,6 +3,7 @@
namespace Cite\Hooks; namespace Cite\Hooks;
use Cite\Cite; use Cite\Cite;
use MediaWiki\Config\Config;
use MediaWiki\Parser\Parser; use MediaWiki\Parser\Parser;
use PPFrame; use PPFrame;
@ -11,12 +12,20 @@ use PPFrame;
*/ */
class CiteParserTagHooks { class CiteParserTagHooks {
private Config $config;
public function __construct(
Config $config
) {
$this->config = $config;
}
/** /**
* Enables the two <ref> and <references> tags. * Enables the two <ref> and <references> tags.
*/ */
public static function register( Parser $parser ): void { public function register( Parser $parser ): void {
$parser->setHook( 'ref', [ __CLASS__, 'ref' ] ); $parser->setHook( 'ref', [ $this, 'ref' ] );
$parser->setHook( 'references', [ __CLASS__, 'references' ] ); $parser->setHook( 'references', [ $this, 'references' ] );
} }
/** /**
@ -29,13 +38,13 @@ class CiteParserTagHooks {
* *
* @return string HTML * @return string HTML
*/ */
public static function ref( public function ref(
?string $text, ?string $text,
array $argv, array $argv,
Parser $parser, Parser $parser,
PPFrame $frame PPFrame $frame
): string { ): string {
$cite = self::citeForParser( $parser ); $cite = $this->citeForParser( $parser );
$result = $cite->ref( $parser, $text, $argv ); $result = $cite->ref( $parser, $text, $argv );
if ( $result === null ) { if ( $result === null ) {
@ -60,13 +69,13 @@ class CiteParserTagHooks {
* *
* @return string HTML * @return string HTML
*/ */
public static function references( public function references(
?string $text, ?string $text,
array $argv, array $argv,
Parser $parser, Parser $parser,
PPFrame $frame PPFrame $frame
): string { ): string {
$cite = self::citeForParser( $parser ); $cite = $this->citeForParser( $parser );
$result = $cite->references( $parser, $text, $argv ); $result = $cite->references( $parser, $text, $argv );
if ( $result === null ) { if ( $result === null ) {
@ -83,8 +92,8 @@ class CiteParserTagHooks {
/** /**
* Get or create Cite state for this parser. * Get or create Cite state for this parser.
*/ */
private static function citeForParser( Parser $parser ): Cite { private function citeForParser( Parser $parser ): Cite {
$parser->extCite ??= new Cite( $parser ); $parser->extCite ??= new Cite( $parser, $this->config );
return $parser->extCite; return $parser->extCite;
} }

View file

@ -94,7 +94,8 @@ class CiteIntegrationTest extends \MediaWikiIntegrationTestCase {
$mockParser = $this->createNoOpMock( Parser::class, [ 'getOptions', 'getContentLanguage' ] ); $mockParser = $this->createNoOpMock( Parser::class, [ 'getOptions', 'getContentLanguage' ] );
$mockParser->method( 'getOptions' )->willReturn( $mockOptions ); $mockParser->method( 'getOptions' )->willReturn( $mockOptions );
$mockParser->method( 'getContentLanguage' )->willReturn( $language ); $mockParser->method( 'getContentLanguage' )->willReturn( $language );
return new Cite( $mockParser ); $config = $this->getServiceContainer()->getMainConfig();
return new Cite( $mockParser, $config );
} }
} }

View file

@ -434,8 +434,9 @@ class CiteTest extends \MediaWikiIntegrationTestCase {
$parser->method( 'getOptions' )->willReturn( $parserOptions ); $parser->method( 'getOptions' )->willReturn( $parserOptions );
$parser->method( 'getContentLanguage' )->willReturn( $language ); $parser->method( 'getContentLanguage' )->willReturn( $language );
$config = $this->getServiceContainer()->getMainConfig();
/** @var Cite $cite */ /** @var Cite $cite */
$cite = TestingAccessWrapper::newFromObject( new Cite( $parser ) ); $cite = TestingAccessWrapper::newFromObject( new Cite( $parser, $config ) );
// Assume the currently parsed <ref> is wrapped in <references> // Assume the currently parsed <ref> is wrapped in <references>
$cite->inReferencesGroup = ''; $cite->inReferencesGroup = '';
@ -463,7 +464,8 @@ class CiteTest extends \MediaWikiIntegrationTestCase {
$mockParser = $this->createNoOpMock( Parser::class, [ 'getOptions', 'getContentLanguage' ] ); $mockParser = $this->createNoOpMock( Parser::class, [ 'getOptions', 'getContentLanguage' ] );
$mockParser->method( 'getOptions' )->willReturn( $mockOptions ); $mockParser->method( 'getOptions' )->willReturn( $mockOptions );
$mockParser->method( 'getContentLanguage' )->willReturn( $language ); $mockParser->method( 'getContentLanguage' )->willReturn( $language );
return new Cite( $mockParser ); $config = $this->getServiceContainer()->getMainConfig();
return new Cite( $mockParser, $config );
} }
} }

View file

@ -4,6 +4,7 @@ namespace Cite\Tests\Unit;
use Cite\Cite; use Cite\Cite;
use Cite\Hooks\CiteParserHooks; use Cite\Hooks\CiteParserHooks;
use MediaWiki\Config\Config;
use MediaWiki\Parser\Parser; use MediaWiki\Parser\Parser;
use MediaWiki\Parser\ParserOutput; use MediaWiki\Parser\ParserOutput;
use ParserOptions; use ParserOptions;
@ -15,6 +16,12 @@ use StripState;
*/ */
class CiteParserHooksTest extends \MediaWikiUnitTestCase { class CiteParserHooksTest extends \MediaWikiUnitTestCase {
private function newCiteParserHooks() {
return new CiteParserHooks(
$this->createNoOpMock( Config::class )
);
}
public function testOnParserFirstCallInit() { public function testOnParserFirstCallInit() {
$parser = $this->createNoOpMock( Parser::class, [ 'setHook' ] ); $parser = $this->createNoOpMock( Parser::class, [ 'setHook' ] );
$expectedTags = [ 'ref' => true, 'references' => true ]; $expectedTags = [ 'ref' => true, 'references' => true ];
@ -25,7 +32,7 @@ class CiteParserHooksTest extends \MediaWikiUnitTestCase {
unset( $expectedTags[$tag] ); unset( $expectedTags[$tag] );
} ); } );
$citeParserHooks = new CiteParserHooks(); $citeParserHooks = $this->newCiteParserHooks();
$citeParserHooks->onParserFirstCallInit( $parser ); $citeParserHooks->onParserFirstCallInit( $parser );
} }
@ -33,7 +40,7 @@ class CiteParserHooksTest extends \MediaWikiUnitTestCase {
$parser = $this->createNoOpMock( Parser::class, [ '__isset' ] ); $parser = $this->createNoOpMock( Parser::class, [ '__isset' ] );
$parser->extCite = $this->createMock( Cite::class ); $parser->extCite = $this->createMock( Cite::class );
$citeParserHooks = new CiteParserHooks(); $citeParserHooks = $this->newCiteParserHooks();
$citeParserHooks->onParserClearState( $parser ); $citeParserHooks->onParserClearState( $parser );
$this->assertNull( $parser->extCite ?? null ); $this->assertNull( $parser->extCite ?? null );
@ -43,7 +50,7 @@ class CiteParserHooksTest extends \MediaWikiUnitTestCase {
$parser = $this->createNoOpMock( Parser::class, [ '__isset' ] ); $parser = $this->createNoOpMock( Parser::class, [ '__isset' ] );
$parser->extCite = $this->createMock( Cite::class ); $parser->extCite = $this->createMock( Cite::class );
$citeParserHooks = new CiteParserHooks(); $citeParserHooks = $this->newCiteParserHooks();
$citeParserHooks->onParserCloned( $parser ); $citeParserHooks->onParserCloned( $parser );
$this->assertNull( $parser->extCite ?? null ); $this->assertNull( $parser->extCite ?? null );
@ -66,7 +73,7 @@ class CiteParserHooksTest extends \MediaWikiUnitTestCase {
$parser->extCite = $cite; $parser->extCite = $cite;
$text = ''; $text = '';
$citeParserHooks = new CiteParserHooks(); $citeParserHooks = $this->newCiteParserHooks();
$citeParserHooks->onParserAfterParse( $parser, $text, $this->createMock( StripState::class ) ); $citeParserHooks->onParserAfterParse( $parser, $text, $this->createMock( StripState::class ) );
} }

View file

@ -4,6 +4,7 @@ namespace Cite\Tests\Unit;
use Cite\Cite; use Cite\Cite;
use Cite\Hooks\CiteParserTagHooks; use Cite\Hooks\CiteParserTagHooks;
use MediaWiki\Config\Config;
use MediaWiki\Parser\Parser; use MediaWiki\Parser\Parser;
use MediaWiki\Parser\ParserOutput; use MediaWiki\Parser\ParserOutput;
use PPFrame; use PPFrame;
@ -14,6 +15,12 @@ use PPFrame;
*/ */
class CiteParserTagHooksTest extends \MediaWikiUnitTestCase { class CiteParserTagHooksTest extends \MediaWikiUnitTestCase {
private function newCiteParserTagHooks() {
return new CiteParserTagHooks(
$this->createNoOpMock( Config::class )
);
}
public function testRegister() { public function testRegister() {
$parser = $this->createNoOpMock( Parser::class, [ 'setHook' ] ); $parser = $this->createNoOpMock( Parser::class, [ 'setHook' ] );
$expectedTags = [ 'ref' => true, 'references' => true ]; $expectedTags = [ 'ref' => true, 'references' => true ];
@ -24,7 +31,8 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase {
unset( $expectedTags[$tag] ); unset( $expectedTags[$tag] );
} ); } );
CiteParserTagHooks::register( $parser ); $citeParserTagHooks = $this->newCiteParserTagHooks();
$citeParserTagHooks->register( $parser );
} }
public function testRef_fails() { public function testRef_fails() {
@ -37,7 +45,8 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase {
$frame = $this->createMock( PPFrame::class ); $frame = $this->createMock( PPFrame::class );
$html = CiteParserTagHooks::ref( null, [], $parser, $frame ); $citeParserTagHooks = $this->newCiteParserTagHooks();
$html = $citeParserTagHooks->ref( null, [], $parser, $frame );
$this->assertSame( '&lt;ref&gt;&lt;/ref&gt;', $html ); $this->assertSame( '&lt;ref&gt;&lt;/ref&gt;', $html );
} }
@ -60,7 +69,8 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase {
$frame = $this->createMock( PPFrame::class ); $frame = $this->createMock( PPFrame::class );
$html = CiteParserTagHooks::ref( null, [], $parser, $frame ); $citeParserTagHooks = $this->newCiteParserTagHooks();
$html = $citeParserTagHooks->ref( null, [], $parser, $frame );
$this->assertSame( '<HTML>', $html ); $this->assertSame( '<HTML>', $html );
} }
@ -74,7 +84,8 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase {
$frame = $this->createMock( PPFrame::class ); $frame = $this->createMock( PPFrame::class );
$html = CiteParserTagHooks::references( null, [], $parser, $frame ); $citeParserTagHooks = $this->newCiteParserTagHooks();
$html = $citeParserTagHooks->references( null, [], $parser, $frame );
$this->assertSame( '&lt;references/&gt;', $html ); $this->assertSame( '&lt;references/&gt;', $html );
} }
@ -89,7 +100,8 @@ class CiteParserTagHooksTest extends \MediaWikiUnitTestCase {
$frame = $this->createMock( PPFrame::class ); $frame = $this->createMock( PPFrame::class );
$html = CiteParserTagHooks::references( null, [], $parser, $frame ); $citeParserTagHooks = $this->newCiteParserTagHooks();
$html = $citeParserTagHooks->references( null, [], $parser, $frame );
$this->assertSame( '<HTML>', $html ); $this->assertSame( '<HTML>', $html );
} }