mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-11-30 17:54:20 +00:00
Merge "Remove Parser state from ReferencesFormatter"
This commit is contained in:
commit
277d3e84da
17
src/Cite.php
17
src/Cite.php
|
@ -106,7 +106,7 @@ class Cite {
|
||||||
$this->footnoteMarkFormatter = new FootnoteMarkFormatter(
|
$this->footnoteMarkFormatter = new FootnoteMarkFormatter(
|
||||||
$this->errorReporter, $anchorFormatter, $messageLocalizer );
|
$this->errorReporter, $anchorFormatter, $messageLocalizer );
|
||||||
$this->referencesFormatter = new ReferencesFormatter(
|
$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'] );
|
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>
|
# Append errors generated while processing <references>
|
||||||
if ( $this->mReferencesErrors ) {
|
if ( $this->mReferencesErrors ) {
|
||||||
|
@ -482,14 +482,18 @@ class Cite {
|
||||||
* If called outside of references(), caller is responsible for ensuring
|
* If called outside of references(), caller is responsible for ensuring
|
||||||
* `mInReferences` is enabled before the call and disabled after call.
|
* `mInReferences` is enabled before the call and disabled after call.
|
||||||
*
|
*
|
||||||
|
* @param Parser $parser
|
||||||
* @param string $group
|
* @param string $group
|
||||||
* @param bool $responsive
|
* @param bool $responsive
|
||||||
*
|
*
|
||||||
* @return string HTML
|
* @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(
|
$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
|
// done, clean up so we can reuse the group
|
||||||
$this->referenceStack->deleteGroup( $group );
|
$this->referenceStack->deleteGroup( $group );
|
||||||
|
@ -505,11 +509,12 @@ class Cite {
|
||||||
* If we are processing a section preview, this adds the missing
|
* If we are processing a section preview, this adds the missing
|
||||||
* references tags and does not add the errors.
|
* references tags and does not add the errors.
|
||||||
*
|
*
|
||||||
|
* @param Parser $parser
|
||||||
* @param bool $isSectionPreview
|
* @param bool $isSectionPreview
|
||||||
*
|
*
|
||||||
* @return string HTML
|
* @return string HTML
|
||||||
*/
|
*/
|
||||||
public function checkRefsNoReferences( bool $isSectionPreview ) : string {
|
public function checkRefsNoReferences( Parser $parser, bool $isSectionPreview ) : string {
|
||||||
global $wgCiteResponsiveReferences;
|
global $wgCiteResponsiveReferences;
|
||||||
|
|
||||||
if ( !$this->referenceStack ) {
|
if ( !$this->referenceStack ) {
|
||||||
|
@ -520,7 +525,7 @@ class Cite {
|
||||||
foreach ( $this->referenceStack->getGroups() as $group ) {
|
foreach ( $this->referenceStack->getGroups() as $group ) {
|
||||||
if ( $group === self::DEFAULT_GROUP || $isSectionPreview ) {
|
if ( $group === self::DEFAULT_GROUP || $isSectionPreview ) {
|
||||||
$this->inReferencesGroup = $group;
|
$this->inReferencesGroup = $group;
|
||||||
$s .= $this->formatReferences( $group, $wgCiteResponsiveReferences );
|
$s .= $this->formatReferences( $parser, $group, $wgCiteResponsiveReferences );
|
||||||
$this->inReferencesGroup = null;
|
$this->inReferencesGroup = null;
|
||||||
} else {
|
} else {
|
||||||
$s .= "\n<br />" . $this->errorReporter->halfParsed(
|
$s .= "\n<br />" . $this->errorReporter->halfParsed(
|
||||||
|
|
|
@ -43,7 +43,7 @@ class CiteParserHooks {
|
||||||
if ( isset( $parser->extCite ) ) {
|
if ( isset( $parser->extCite ) ) {
|
||||||
/** @var Cite $cite */
|
/** @var Cite $cite */
|
||||||
$cite = $parser->extCite;
|
$cite = $parser->extCite;
|
||||||
$text .= $cite->checkRefsNoReferences( $parser->getOptions()->getIsSectionPreview() );
|
$text .= $cite->checkRefsNoReferences( $parser, $parser->getOptions()->getIsSectionPreview() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,35 +29,28 @@ class ReferencesFormatter {
|
||||||
*/
|
*/
|
||||||
private $anchorFormatter;
|
private $anchorFormatter;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Parser
|
|
||||||
*/
|
|
||||||
private $parser;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ReferenceMessageLocalizer
|
* @var ReferenceMessageLocalizer
|
||||||
*/
|
*/
|
||||||
private $messageLocalizer;
|
private $messageLocalizer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Parser $parser
|
|
||||||
* @param ErrorReporter $errorReporter
|
* @param ErrorReporter $errorReporter
|
||||||
* @param AnchorFormatter $anchorFormatter
|
* @param AnchorFormatter $anchorFormatter
|
||||||
* @param ReferenceMessageLocalizer $messageLocalizer
|
* @param ReferenceMessageLocalizer $messageLocalizer
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Parser $parser,
|
|
||||||
ErrorReporter $errorReporter,
|
ErrorReporter $errorReporter,
|
||||||
AnchorFormatter $anchorFormatter,
|
AnchorFormatter $anchorFormatter,
|
||||||
ReferenceMessageLocalizer $messageLocalizer
|
ReferenceMessageLocalizer $messageLocalizer
|
||||||
) {
|
) {
|
||||||
$this->errorReporter = $errorReporter;
|
$this->errorReporter = $errorReporter;
|
||||||
$this->anchorFormatter = $anchorFormatter;
|
$this->anchorFormatter = $anchorFormatter;
|
||||||
$this->parser = $parser;
|
|
||||||
$this->messageLocalizer = $messageLocalizer;
|
$this->messageLocalizer = $messageLocalizer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param Parser $parser
|
||||||
* @param array[] $groupRefs
|
* @param array[] $groupRefs
|
||||||
* @param bool $responsive
|
* @param bool $responsive
|
||||||
* @param bool $isSectionPreview
|
* @param bool $isSectionPreview
|
||||||
|
@ -65,6 +58,7 @@ class ReferencesFormatter {
|
||||||
* @return string HTML
|
* @return string HTML
|
||||||
*/
|
*/
|
||||||
public function formatReferences(
|
public function formatReferences(
|
||||||
|
Parser $parser,
|
||||||
array $groupRefs,
|
array $groupRefs,
|
||||||
bool $responsive,
|
bool $responsive,
|
||||||
bool $isSectionPreview
|
bool $isSectionPreview
|
||||||
|
@ -114,7 +108,7 @@ class ReferencesFormatter {
|
||||||
$parserInput .= $this->closeIndention( $indented );
|
$parserInput .= $this->closeIndention( $indented );
|
||||||
$parserInput = Html::rawElement( 'ol', [ 'class' => [ 'references' ] ], $parserInput );
|
$parserInput = Html::rawElement( 'ol', [ 'class' => [ 'references' ] ], $parserInput );
|
||||||
// Live hack: parse() adds two newlines on WM, can't reproduce it locally -ævar
|
// 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 ) {
|
if ( $responsive ) {
|
||||||
// Use a DIV wrap because column-count on a list directly is broken in Chrome.
|
// Use a DIV wrap because column-count on a list directly is broken in Chrome.
|
||||||
|
|
|
@ -62,7 +62,8 @@ class CiteIntegrationTest extends \MediaWikiIntegrationTestCase {
|
||||||
$spy->referencesFormatter = $referencesFormatter;
|
$spy->referencesFormatter = $referencesFormatter;
|
||||||
$spy->isSectionPreview = $isSectionPreview;
|
$spy->isSectionPreview = $isSectionPreview;
|
||||||
|
|
||||||
$output = $cite->checkRefsNoReferences( $isSectionPreview );
|
$output = $cite->checkRefsNoReferences(
|
||||||
|
$this->createMock( Parser::class ), $isSectionPreview );
|
||||||
$this->assertSame( $expectedOutput, $output );
|
$this->assertSame( $expectedOutput, $output );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -334,7 +334,8 @@ class CiteUnitTest extends MediaWikiUnitTestCase {
|
||||||
);
|
);
|
||||||
$spy->referencesFormatter = $this->createMock( ReferencesFormatter::class );
|
$spy->referencesFormatter = $this->createMock( ReferencesFormatter::class );
|
||||||
$spy->referencesFormatter->method( 'formatReferences' )
|
$spy->referencesFormatter->method( 'formatReferences' )
|
||||||
->with( $this->anything(), $expectedResponsive, false )->willReturn( 'references!' );
|
->with( $this->anything(), $this->anything(), $expectedResponsive, false )
|
||||||
|
->willReturn( 'references!' );
|
||||||
$spy->isSectionPreview = false;
|
$spy->isSectionPreview = false;
|
||||||
$spy->referenceStack = $this->createMock( ReferenceStack::class );
|
$spy->referenceStack = $this->createMock( ReferenceStack::class );
|
||||||
$spy->referenceStack->method( 'getGroupRefs' )
|
$spy->referenceStack->method( 'getGroupRefs' )
|
||||||
|
|
|
@ -45,13 +45,12 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
||||||
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
||||||
/** @var ReferencesFormatter $formatter */
|
/** @var ReferencesFormatter $formatter */
|
||||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||||
$mockParser,
|
|
||||||
$mockErrorReporter,
|
$mockErrorReporter,
|
||||||
$this->createMock( AnchorFormatter::class ),
|
$this->createMock( AnchorFormatter::class ),
|
||||||
$mockMessageLocalizer
|
$mockMessageLocalizer
|
||||||
) );
|
) );
|
||||||
|
|
||||||
$output = $formatter->formatReferences( $refs, true, false );
|
$output = $formatter->formatReferences( $mockParser, $refs, true, false );
|
||||||
$this->assertSame( $expectedOutput, $output );
|
$this->assertSame( $expectedOutput, $output );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,7 +170,6 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
||||||
*/
|
*/
|
||||||
public function testCloseIndention( $closingLi, $expectedOutput ) {
|
public function testCloseIndention( $closingLi, $expectedOutput ) {
|
||||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||||
$this->createMock( Parser::class ),
|
|
||||||
$this->createMock( ErrorReporter::class ),
|
$this->createMock( ErrorReporter::class ),
|
||||||
$this->createMock( AnchorFormatter::class ),
|
$this->createMock( AnchorFormatter::class ),
|
||||||
$this->createMock( ReferenceMessageLocalizer::class )
|
$this->createMock( ReferenceMessageLocalizer::class )
|
||||||
|
@ -226,9 +224,7 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
||||||
/** @var ErrorReporter $mockErrorReporter */
|
/** @var ErrorReporter $mockErrorReporter */
|
||||||
/** @var AnchorFormatter $anchorFormatter */
|
/** @var AnchorFormatter $anchorFormatter */
|
||||||
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
||||||
/** @var ReferencesFormatter $formatter */
|
|
||||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||||
$this->createMock( Parser::class ),
|
|
||||||
$mockErrorReporter,
|
$mockErrorReporter,
|
||||||
$anchorFormatter,
|
$anchorFormatter,
|
||||||
$mockMessageLocalizer
|
$mockMessageLocalizer
|
||||||
|
@ -330,7 +326,6 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
||||||
/** @var ErrorReporter $mockErrorReporter */
|
/** @var ErrorReporter $mockErrorReporter */
|
||||||
/** @var ReferencesFormatter $formatter */
|
/** @var ReferencesFormatter $formatter */
|
||||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||||
$this->createMock( Parser::class ),
|
|
||||||
$mockErrorReporter,
|
$mockErrorReporter,
|
||||||
$this->createMock( AnchorFormatter::class ),
|
$this->createMock( AnchorFormatter::class ),
|
||||||
$this->createMock( ReferenceMessageLocalizer::class )
|
$this->createMock( ReferenceMessageLocalizer::class )
|
||||||
|
@ -395,7 +390,6 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
||||||
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
||||||
/** @var ReferencesFormatter $formatter */
|
/** @var ReferencesFormatter $formatter */
|
||||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||||
$this->createMock( Parser::class ),
|
|
||||||
$mockErrorReporter,
|
$mockErrorReporter,
|
||||||
$this->createMock( AnchorFormatter::class ),
|
$this->createMock( AnchorFormatter::class ),
|
||||||
$mockMessageLocalizer
|
$mockMessageLocalizer
|
||||||
|
@ -428,7 +422,6 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
||||||
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
||||||
/** @var ReferencesFormatter $formatter */
|
/** @var ReferencesFormatter $formatter */
|
||||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||||
$this->createMock( Parser::class ),
|
|
||||||
$this->createMock( ErrorReporter::class ),
|
$this->createMock( ErrorReporter::class ),
|
||||||
$this->createMock( AnchorFormatter::class ),
|
$this->createMock( AnchorFormatter::class ),
|
||||||
$mockMessageLocalizer
|
$mockMessageLocalizer
|
||||||
|
@ -462,7 +455,6 @@ class ReferencesFormatterTest extends MediaWikiUnitTestCase {
|
||||||
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
/** @var ReferenceMessageLocalizer $mockMessageLocalizer */
|
||||||
/** @var ReferencesFormatter $formatter */
|
/** @var ReferencesFormatter $formatter */
|
||||||
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
$formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter(
|
||||||
$this->createMock( Parser::class ),
|
|
||||||
$this->createMock( ErrorReporter::class ),
|
$this->createMock( ErrorReporter::class ),
|
||||||
$this->createMock( AnchorFormatter::class ),
|
$this->createMock( AnchorFormatter::class ),
|
||||||
$mockMessageLocalizer
|
$mockMessageLocalizer
|
||||||
|
|
Loading…
Reference in a new issue