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(
$parser->getStripState(), $text, $argv, ...array_values( $arguments ) );
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\Parser\Parser;
use MediaWiki\Parser\Sanitizer;
use ParserOptions;
use StatusValue;
/**
@ -65,10 +66,10 @@ class ErrorReporter {
* @return 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 );
[ $type ] = $this->parseTypeAndIdFromMessageKey( $msg->getKey() );
[ $type ] = $this->parseTypeAndIdFromMessageKey( $key );
if ( $type === 'error' ) {
// 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!
*/
private function getInterfaceLanguageAndSplitCache( Parser $parser ): Language {
if ( !$this->cachedInterfaceLanguage ) {
$this->cachedInterfaceLanguage = $parser->getOptions()->getUserLangObj();
}
private function getInterfaceLanguageAndSplitCache( ParserOptions $parserOptions ): Language {
$this->cachedInterfaceLanguage ??= $parserOptions->getUserLangObj();
return $this->cachedInterfaceLanguage;
}

View file

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

View file

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