Merge "Allow disabling the alternative backlink labels message"

This commit is contained in:
jenkins-bot 2024-09-02 19:34:57 +00:00 committed by Gerrit Code Review
commit 1116cfb9a9
3 changed files with 25 additions and 22 deletions

View file

@ -57,8 +57,8 @@
"cite_reference_link": "{{notranslate}}\n\nParameters:\n* $1 - ref key\n* $2 - references key\n* $3 - link label",
"cite_references_link_one": "{{notranslate}}\n\nParameters:\n* $1 - references key\n* $2 - ref key\n* $3 - reference text\n* $4 - optional CSS class for direction",
"cite_references_link_many": "{{notranslate}}\n\nParameters:\n* $1 - references key\n* $2 - list of links\n* $3 - reference text\n* $4 - optional CSS class for direction",
"cite_references_link_many_format": "{{notranslate}}\n\nParameters:\n* $1 - reference key\n* $2 - canonical numeric backlink label for the link, always in the form \"1.2\"\n* $3 - optional alternate backlink label as defined via [[MediaWiki:cite_references_link_many_format_backlink_labels]]",
"cite_references_link_many_format_backlink_labels": "{{Optional}}",
"cite_references_link_many_format": "{{notranslate}}\n\nParameters:\n* $1 - Reference key\n* $2 - Canonical numerical label for the backlink, always in the form \"1.1\", \"1.2\", and so on.\n* $3 - Alternative alphabetic label for the backlink as defined via [[MediaWiki:cite_references_link_many_format_backlink_labels]], typically \"a\", \"b\", and so on.",
"cite_references_link_many_format_backlink_labels": "{{optional}}\nAlternative alphabetic backlink labels for use in the [[MediaWiki:cite_references_link_many_format]] message as parameter $3. Should only be customized when the alphabet is not identical to the 26 basic Latin characters \"a\" to \"z\". Make sure to include at least as many labels as in the default sequence \"a\" to \"zz\" (702 labels), possibly even more. E.g. adding \"aaa\" to \"azz\" gives 1378 total. This message can be disabled if not needed.",
"cite_references_link_many_sep": "{{notranslate}}",
"cite_references_link_many_and": "{{optional}}",
"cite_references_link_accessibility_label": "Text for screen readers to describe the jump to citation link (\"↑\").\n\nSee also:\n* {{msg-mw|Cite references link many accessibility label}} - if the citation is used multiple times",

View file

@ -184,16 +184,16 @@ class ReferenceListFormatter {
$backlinks = [];
for ( $i = 0; $i < $ref->count; $i++ ) {
$numericLabel = $this->referencesFormatEntryNumericBacklinkLabel(
$ref->number . ( $ref->extendsIndex ? '.' . $ref->extendsIndex : '' ),
$i,
$ref->count
);
$backlinks[] = $this->messageLocalizer->msg(
'cite_references_link_many_format',
$this->anchorFormatter->backLink( $key, $ref->key . '-' . $i ),
$this->referencesFormatEntryNumericBacklinkLabel(
$ref->number .
( isset( $ref->extendsIndex ) ? '.' . $ref->extendsIndex : '' ),
$i,
$ref->count
),
$this->referencesFormatEntryAlternateBacklinkLabel( $parser, $i )
$numericLabel,
$this->referencesFormatEntryAlternateBacklinkLabel( $parser, $i ) ?? $numericLabel
)->plain();
}
@ -269,12 +269,16 @@ class ReferenceListFormatter {
*/
private function referencesFormatEntryAlternateBacklinkLabel(
Parser $parser, int $offset
): string {
$this->backlinkLabels ??= preg_split(
'/\s+/',
$this->messageLocalizer->msg( 'cite_references_link_many_format_backlink_labels' )
->plain()
);
): ?string {
if ( !isset( $this->backlinkLabels ) ) {
$msg = $this->messageLocalizer->msg( 'cite_references_link_many_format_backlink_labels' );
$this->backlinkLabels = $msg->isDisabled() ? [] : preg_split( '/\s+/', $msg->plain() );
}
// Disabling the message just disables the feature
if ( !$this->backlinkLabels ) {
return null;
}
return $this->backlinkLabels[$offset]
?? $this->errorReporter->plain( $parser, 'cite_error_references_no_backlink_label' );

View file

@ -353,7 +353,8 @@ class ReferenceListFormatterTest extends \MediaWikiUnitTestCase {
public function testReferencesFormatEntryAlternateBacklinkLabel(
?string $expectedLabel, string $labelList, int $offset
) {
$mockMessage = $this->createNoOpMock( Message::class, [ 'plain' ] );
$mockMessage = $this->createNoOpMock( Message::class, [ 'isDisabled', 'plain' ] );
$mockMessage->method( 'isDisabled' )->willReturn( !$labelList );
$mockMessage->method( 'plain' )->willReturn( $labelList );
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
@ -361,8 +362,7 @@ class ReferenceListFormatterTest extends \MediaWikiUnitTestCase {
->willReturn( $mockMessage );
$errorReporter = $this->createMock( ErrorReporter::class );
$errorReporter->expects( $expectedLabel ? $this->never() : $this->once() )
->method( 'plain' );
$errorReporter->method( 'plain' )->willReturnArgument( 1 );
/** @var ReferenceListFormatter $formatter */
$formatter = TestingAccessWrapper::newFromObject( new ReferenceListFormatter(
@ -373,16 +373,15 @@ class ReferenceListFormatterTest extends \MediaWikiUnitTestCase {
$parser = $this->createNoOpMock( Parser::class );
$label = $formatter->referencesFormatEntryAlternateBacklinkLabel( $parser, $offset );
if ( $expectedLabel !== null ) {
$this->assertSame( $expectedLabel, $label );
}
}
public static function provideReferencesFormatEntryAlternateBacklinkLabel() {
yield [ 'aa', 'aa ab ac', 0 ];
yield [ 'ab', 'aa ab ac', 1 ];
yield [ 'å', 'å b c', 0 ];
yield [ null, 'a b c', 10 ];
yield [ 'cite_error_references_no_backlink_label', 'a b c', 10 ];
yield [ null, '', 0 ];
}
/**