Use slightly narrower interfaces in a few places

The idea is to make the code less ambiguous and easier to predict.
We passed the same information around two times in a few places.

Change-Id: I39c7a2962bb70bbe40074986e63b1051d0766ea2
This commit is contained in:
thiemowmde 2024-06-21 21:18:25 +02:00 committed by Thiemo Kreuz (WMDE)
parent 66fb3e661a
commit ec9c8bda00
4 changed files with 19 additions and 21 deletions

View file

@ -175,7 +175,7 @@ class Cite {
$ref = $this->referenceStack->pushRef( $ref = $this->referenceStack->pushRef(
$parser->getStripState(), $text, $argv, ...array_values( $arguments ) ); $parser->getStripState(), $text, $argv, ...array_values( $arguments ) );
return $ref return $ref
? $this->footnoteMarkFormatter->linkRef( $parser, $arguments['group'], $ref ) ? $this->footnoteMarkFormatter->linkRef( $parser, $ref )
: ''; : '';
} }

View file

@ -7,6 +7,7 @@ use MediaWiki\Html\Html;
use MediaWiki\Message\Message; use MediaWiki\Message\Message;
use MediaWiki\Parser\Parser; use MediaWiki\Parser\Parser;
use MediaWiki\Parser\Sanitizer; use MediaWiki\Parser\Sanitizer;
use ParserOptions;
use StatusValue; use StatusValue;
/** /**
@ -65,10 +66,10 @@ class ErrorReporter {
* @return Message * @return Message
*/ */
private function msg( Parser $parser, string $key, ...$params ): Message { private function msg( Parser $parser, string $key, ...$params ): Message {
$language = $this->getInterfaceLanguageAndSplitCache( $parser ); $language = $this->getInterfaceLanguageAndSplitCache( $parser->getOptions() );
$msg = $this->messageLocalizer->msg( $key, ...$params )->inLanguage( $language ); $msg = $this->messageLocalizer->msg( $key, ...$params )->inLanguage( $language );
[ $type ] = $this->parseTypeAndIdFromMessageKey( $msg->getKey() ); [ $type ] = $this->parseTypeAndIdFromMessageKey( $key );
if ( $type === 'error' ) { if ( $type === 'error' ) {
// Take care; this is a sideeffect that might not belong to this class. // Take care; this is a sideeffect that might not belong to this class.
@ -83,10 +84,8 @@ class ErrorReporter {
/** /**
* Note the startling side effect of splitting ParserCache by user interface language! * Note the startling side effect of splitting ParserCache by user interface language!
*/ */
private function getInterfaceLanguageAndSplitCache( Parser $parser ): Language { private function getInterfaceLanguageAndSplitCache( ParserOptions $parserOptions ): Language {
if ( !$this->cachedInterfaceLanguage ) { $this->cachedInterfaceLanguage ??= $parserOptions->getUserLangObj();
$this->cachedInterfaceLanguage = $parser->getOptions()->getUserLangObj();
}
return $this->cachedInterfaceLanguage; return $this->cachedInterfaceLanguage;
} }

View file

@ -36,17 +36,16 @@ class FootnoteMarkFormatter {
* *
* @suppress SecurityCheck-DoubleEscaped * @suppress SecurityCheck-DoubleEscaped
* @param Parser $parser * @param Parser $parser
* @param string $group
* @param ReferenceStackItem $ref * @param ReferenceStackItem $ref
* *
* @return string * @return string
*/ */
public function linkRef( Parser $parser, string $group, ReferenceStackItem $ref ): string { public function linkRef( Parser $parser, ReferenceStackItem $ref ): string {
$label = $this->fetchCustomizedLinkLabel( $parser, $group, $ref->number ); $label = $this->fetchCustomizedLinkLabel( $parser, $ref->group, $ref->number );
if ( $label === null ) { if ( $label === null ) {
$label = $this->messageLocalizer->localizeDigits( (string)$ref->number ); $label = $this->messageLocalizer->localizeDigits( (string)$ref->number );
if ( $group !== Cite::DEFAULT_GROUP ) { if ( $ref->group !== Cite::DEFAULT_GROUP ) {
$label = "$group $label"; $label = $ref->group . ' ' . $label;
} }
} }
if ( isset( $ref->extendsIndex ) ) { if ( isset( $ref->extendsIndex ) ) {

View file

@ -21,7 +21,7 @@ class FootnoteMarkFormatterTest extends \MediaWikiIntegrationTestCase {
/** /**
* @dataProvider provideLinkRef * @dataProvider provideLinkRef
*/ */
public function testLinkRef( string $group, array $ref, string $expectedOutput ) { public function testLinkRef( array $ref, string $expectedOutput ) {
$mockErrorReporter = $this->createMock( ErrorReporter::class ); $mockErrorReporter = $this->createMock( ErrorReporter::class );
$mockErrorReporter->method( 'plain' )->willReturnCallback( $mockErrorReporter->method( 'plain' )->willReturnCallback(
static fn ( $parser, ...$args ) => implode( '|', $args ) static fn ( $parser, ...$args ) => implode( '|', $args )
@ -55,43 +55,43 @@ class FootnoteMarkFormatterTest extends \MediaWikiIntegrationTestCase {
); );
$ref = TestUtils::refFromArray( $ref ); $ref = TestUtils::refFromArray( $ref );
$output = $formatter->linkRef( $mockParser, $group, $ref ); $output = $formatter->linkRef( $mockParser, $ref );
$this->assertSame( $expectedOutput, $output ); $this->assertSame( $expectedOutput, $output );
} }
public static function provideLinkRef() { public static function provideLinkRef() {
return [ return [
'Default label' => [ 'Default label' => [
'',
[ [
'name' => null, 'name' => null,
'group' => '',
'number' => 50003, 'number' => 50003,
'key' => 50004, 'key' => 50004,
], ],
'(cite_reference_link|50004+|50004|50003)' '(cite_reference_link|50004+|50004|50003)'
], ],
'Default label, named group' => [ 'Default label, named group' => [
'bar',
[ [
'name' => null, 'name' => null,
'group' => 'bar',
'number' => 3, 'number' => 3,
'key' => 4, 'key' => 4,
], ],
'(cite_reference_link|4+|4|bar 3)' '(cite_reference_link|4+|4|bar 3)'
], ],
'Custom label' => [ 'Custom label' => [
'foo',
[ [
'name' => null, 'name' => null,
'group' => 'foo',
'number' => 3, 'number' => 3,
'key' => 4, 'key' => 4,
], ],
'(cite_reference_link|4+|4|c)' '(cite_reference_link|4+|4|c)'
], ],
'Custom label overrun' => [ 'Custom label overrun' => [
'foo',
[ [
'name' => null, 'name' => null,
'group' => 'foo',
'number' => 10, 'number' => 10,
'key' => 4, 'key' => 4,
], ],
@ -99,9 +99,9 @@ class FootnoteMarkFormatterTest extends \MediaWikiIntegrationTestCase {
'cite_error_no_link_label_group|foo|cite_link_label_group-foo)' 'cite_error_no_link_label_group|foo|cite_link_label_group-foo)'
], ],
'Named ref' => [ 'Named ref' => [
'',
[ [
'name' => 'a', 'name' => 'a',
'group' => '',
'number' => 3, 'number' => 3,
'key' => 4, 'key' => 4,
'count' => 1, 'count' => 1,
@ -109,9 +109,9 @@ class FootnoteMarkFormatterTest extends \MediaWikiIntegrationTestCase {
'(cite_reference_link|a+4-0|a-4|3)' '(cite_reference_link|a+4-0|a-4|3)'
], ],
'Named ref reused' => [ 'Named ref reused' => [
'',
[ [
'name' => 'a', 'name' => 'a',
'group' => '',
'number' => 3, 'number' => 3,
'key' => 4, 'key' => 4,
'count' => 50002, 'count' => 50002,
@ -119,9 +119,9 @@ class FootnoteMarkFormatterTest extends \MediaWikiIntegrationTestCase {
'(cite_reference_link|a+4-50001|a-4|3)' '(cite_reference_link|a+4-50001|a-4|3)'
], ],
'Subreference' => [ 'Subreference' => [
'',
[ [
'name' => null, 'name' => null,
'group' => '',
'number' => 3, 'number' => 3,
'key' => 4, 'key' => 4,
'extends' => 'b', 'extends' => 'b',