From d885a0c6b53e9d00179be4ff3c31bd2869d5fa3a Mon Sep 17 00:00:00 2001 From: thiemowmde Date: Tue, 6 Aug 2024 10:33:37 +0200 Subject: [PATCH] Allow disabling the alternative backlink labels message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The message cite_references_link_many_format_backlink_labels contains e.g. "a b c …" and so on, to be used as alternative backlink labels when a single reference is re-used multiple times. The default numeric rendering is "1.1 1.2 …" and so on. The two labels end in the message cite_references_link_many_format as parameters $2 and $3. But only one of the two parameters is ever used. By default the alternative label in $3 is unused. This implies that everything about these alternative backlink labels including the error message is effectively dead code most of the time, never to be seen on the majority of wikis. This change makes it possible to disable the message without breaking anything. Instead the code will silently fallback to the default behavior of showing the numeric label. This is much more efficient than rendering possibly hundreds of error messages that never show up anyhere. The same optimization is already done for the extremely similar cite_link_label_group-… messages, see FootnoteMarkFormatter::fetchCustomizedLinkLabel. This is also partly motivated by T335129 as well as T321217. Change-Id: Iab818d7db7eddaf7091234f6a22a18cdff70f8e8 --- i18n/qqq.json | 4 +-- src/ReferenceListFormatter.php | 30 +++++++++++-------- .../unit/ReferenceListFormatterTest.php | 13 ++++---- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/i18n/qqq.json b/i18n/qqq.json index 27080f038..cfe49e759 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -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", diff --git a/src/ReferenceListFormatter.php b/src/ReferenceListFormatter.php index e07cfba89..82311a347 100644 --- a/src/ReferenceListFormatter.php +++ b/src/ReferenceListFormatter.php @@ -184,16 +184,16 @@ class ReferenceListFormatter { // Named references with >1 occurrences $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(); } $linkTargetId = $ref->count > 0 ? @@ -267,12 +267,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' ); diff --git a/tests/phpunit/unit/ReferenceListFormatterTest.php b/tests/phpunit/unit/ReferenceListFormatterTest.php index 765d88fb2..04b3b7baa 100644 --- a/tests/phpunit/unit/ReferenceListFormatterTest.php +++ b/tests/phpunit/unit/ReferenceListFormatterTest.php @@ -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 ); - } + $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 ]; } /**