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": {
"class": "Cite\\Hooks\\CiteParserHooks"
"class": "Cite\\Hooks\\CiteParserHooks",
"services": [
"MainConfig"
]
}
},
"ResourceModules": {

View file

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

View file

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

View file

@ -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 <ref> and <references> 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;
}

View file

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

View file

@ -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 <ref> is wrapped in <references>
$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 );
}
}

View file

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

View file

@ -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( '&lt;ref&gt;&lt;/ref&gt;', $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>', $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( '&lt;references/&gt;', $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>', $html );
}