Merge "Remove Parser state from ReferencesFormatter"

This commit is contained in:
jenkins-bot 2019-12-16 12:47:57 +00:00 committed by Gerrit Code Review
commit 277d3e84da
6 changed files with 20 additions and 27 deletions

View file

@ -106,7 +106,7 @@ class Cite {
$this->footnoteMarkFormatter = new FootnoteMarkFormatter(
$this->errorReporter, $anchorFormatter, $messageLocalizer );
$this->referencesFormatter = new ReferencesFormatter(
$parser, $this->errorReporter, $anchorFormatter, $messageLocalizer );
$this->errorReporter, $anchorFormatter, $messageLocalizer );
}
/**
@ -466,7 +466,7 @@ class Cite {
return $this->errorReporter->halfParsed( $error['message'], ...$error['params'] );
}
$s = $this->formatReferences( $this->inReferencesGroup, $responsive );
$s = $this->formatReferences( $parser, $this->inReferencesGroup, $responsive );
# Append errors generated while processing <references>
if ( $this->mReferencesErrors ) {
@ -482,14 +482,18 @@ class Cite {
* If called outside of references(), caller is responsible for ensuring
* `mInReferences` is enabled before the call and disabled after call.
*
* @param Parser $parser
* @param string $group
* @param bool $responsive
*
* @return string HTML
*/
private function formatReferences( string $group, bool $responsive ) : string {
private function formatReferences( Parser $parser, string $group, bool $responsive ) : string {
$ret = $this->referencesFormatter->formatReferences(
$this->referenceStack->getGroupRefs( $group ), $responsive, $this->isSectionPreview );
$parser,
$this->referenceStack->getGroupRefs( $group ),
$responsive,
$this->isSectionPreview );
// done, clean up so we can reuse the group
$this->referenceStack->deleteGroup( $group );
@ -505,11 +509,12 @@ class Cite {
* If we are processing a section preview, this adds the missing
* references tags and does not add the errors.
*
* @param Parser $parser
* @param bool $isSectionPreview
*
* @return string HTML
*/
public function checkRefsNoReferences( bool $isSectionPreview ) : string {
public function checkRefsNoReferences( Parser $parser, bool $isSectionPreview ) : string {
global $wgCiteResponsiveReferences;
if ( !$this->referenceStack ) {
@ -520,7 +525,7 @@ class Cite {
foreach ( $this->referenceStack->getGroups() as $group ) {
if ( $group === self::DEFAULT_GROUP || $isSectionPreview ) {
$this->inReferencesGroup = $group;
$s .= $this->formatReferences( $group, $wgCiteResponsiveReferences );
$s .= $this->formatReferences( $parser, $group, $wgCiteResponsiveReferences );
$this->inReferencesGroup = null;
} else {
$s .= "\n<br />" . $this->errorReporter->halfParsed(

View file

@ -43,7 +43,7 @@ class CiteParserHooks {
if ( isset( $parser->extCite ) ) {
/** @var Cite $cite */
$cite = $parser->extCite;
$text .= $cite->checkRefsNoReferences( $parser->getOptions()->getIsSectionPreview() );
$text .= $cite->checkRefsNoReferences( $parser, $parser->getOptions()->getIsSectionPreview() );
}
}

View file

@ -29,35 +29,28 @@ class ReferencesFormatter {
*/
private $anchorFormatter;
/**
* @var Parser
*/
private $parser;
/**
* @var ReferenceMessageLocalizer
*/
private $messageLocalizer;
/**
* @param Parser $parser
* @param ErrorReporter $errorReporter
* @param AnchorFormatter $anchorFormatter
* @param ReferenceMessageLocalizer $messageLocalizer
*/
public function __construct(
Parser $parser,
ErrorReporter $errorReporter,
AnchorFormatter $anchorFormatter,
ReferenceMessageLocalizer $messageLocalizer
) {
$this->errorReporter = $errorReporter;
$this->anchorFormatter = $anchorFormatter;
$this->parser = $parser;
$this->messageLocalizer = $messageLocalizer;
}
/**
* @param Parser $parser
* @param array[] $groupRefs
* @param bool $responsive
* @param bool $isSectionPreview
@ -65,6 +58,7 @@ class ReferencesFormatter {
* @return string HTML
*/
public function formatReferences(
Parser $parser,
array $groupRefs,
bool $responsive,
bool $isSectionPreview
@ -114,7 +108,7 @@ class ReferencesFormatter {
$parserInput .= $this->closeIndention( $indented );
$parserInput = Html::rawElement( 'ol', [ 'class' => [ 'references' ] ], $parserInput );
// Live hack: parse() adds two newlines on WM, can't reproduce it locally -ævar
$ret = rtrim( $this->parser->recursiveTagParse( $parserInput ), "\n" );
$ret = rtrim( $parser->recursiveTagParse( $parserInput ), "\n" );
if ( $responsive ) {
// Use a DIV wrap because column-count on a list directly is broken in Chrome.

View file

@ -62,7 +62,8 @@ class CiteIntegrationTest extends \MediaWikiIntegrationTestCase {
$spy->referencesFormatter = $referencesFormatter;
$spy->isSectionPreview = $isSectionPreview;
$output = $cite->checkRefsNoReferences( $isSectionPreview );
$output = $cite->checkRefsNoReferences(
$this->createMock( Parser::class ), $isSectionPreview );
$this->assertSame( $expectedOutput, $output );
}

View file

@ -334,7 +334,8 @@ class CiteUnitTest extends MediaWikiUnitTestCase {
);
$spy->referencesFormatter = $this->createMock( ReferencesFormatter::class );
$spy->referencesFormatter->method( 'formatReferences' )
->with( $this->anything(), $expectedResponsive, false )->willReturn( 'references!' );
->with( $this->anything(), $this->anything(), $expectedResponsive, false )
->willReturn( 'references!' );
$spy->isSectionPreview = false;
$spy->referenceStack = $this->createMock( ReferenceStack::class );
$spy->referenceStack->method( 'getGroupRefs' )

View file

@ -45,13 +45,12 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
/** @var ReferencesFormatter $formatter */
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
$mockParser,
$mockErrorReporter,
$this->createMock( AnchorFormatter::class ),
$mockMessageLocalizer
) );
$output = $formatter->formatReferences( $refs, true, false );
$output = $formatter->formatReferences( $mockParser, $refs, true, false );
$this->assertSame( $expectedOutput, $output );
}
@ -171,7 +170,6 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
*/
public function testCloseIndention( $closingLi, $expectedOutput ) {
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
$this->createMock( Parser::class ),
$this->createMock( ErrorReporter::class ),
$this->createMock( AnchorFormatter::class ),
$this->createMock( ReferenceMessageLocalizer::class )
@ -226,9 +224,7 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
/** @var ErrorReporter $mockErrorReporter */
/** @var AnchorFormatter $anchorFormatter */
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
/** @var ReferencesFormatter $formatter */
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
$this->createMock( Parser::class ),
$mockErrorReporter,
$anchorFormatter,
$mockMessageLocalizer
@ -330,7 +326,6 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
/** @var ErrorReporter $mockErrorReporter */
/** @var ReferencesFormatter $formatter */
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
$this->createMock( Parser::class ),
$mockErrorReporter,
$this->createMock( AnchorFormatter::class ),
$this->createMock( ReferenceMessageLocalizer::class )
@ -395,7 +390,6 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
/** @var ReferencesFormatter $formatter */
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
$this->createMock( Parser::class ),
$mockErrorReporter,
$this->createMock( AnchorFormatter::class ),
$mockMessageLocalizer
@ -428,7 +422,6 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
/** @var ReferencesFormatter $formatter */
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
$this->createMock( Parser::class ),
$this->createMock( ErrorReporter::class ),
$this->createMock( AnchorFormatter::class ),
$mockMessageLocalizer
@ -462,7 +455,6 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
/** @var ReferencesFormatter $formatter */
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
$this->createMock( Parser::class ),
$this->createMock( ErrorReporter::class ),
$this->createMock( AnchorFormatter::class ),
$mockMessageLocalizer