No expensive transformations on prefix/suffix messages

This is a mistake that exists in this codebase for who knows how
long.

Cite mis-uses the messaging system a lot for internal things we still
want to customize somehow, but are not labels that will ever be shown
on the screen. The prefix/suffix messages in this patch are meant to be
part of the HTML in id="…" attributes. Prefix/suffix must be a static
plain text strings. Using e.g. {{GENDER}} or {{PLURAL}} in these
messages is not even possible because there is no $1 parameter to use.

Note how all other similar messages already use ->plain().

A few wikis override these messages, but stick to the plain-text
convention, as they should:
https://global-search.toolforge.org/?q=.&regex=1&namespaces=8&title=Cite.*reference.*fix
This will continue to work.

This has minor performance implications. Fetching these messages is
faster if we can skip transformations.

Bug: T321217
Change-Id: I7969c255fe4ce897e904897081da5f52678721aa
This commit is contained in:
thiemowmde 2023-07-20 16:22:46 +02:00
parent 3ff81498ed
commit 25e7aa44dd
2 changed files with 9 additions and 10 deletions

View file

@ -26,8 +26,8 @@ class AnchorFormatter {
* @return string A key for use in wikitext
*/
public function refKey( string $key, string $num = null ): string {
$prefix = $this->messageLocalizer->msg( 'cite_reference_link_prefix' )->text();
$suffix = $this->messageLocalizer->msg( 'cite_reference_link_suffix' )->text();
$prefix = $this->messageLocalizer->msg( 'cite_reference_link_prefix' )->plain();
$suffix = $this->messageLocalizer->msg( 'cite_reference_link_suffix' )->plain();
if ( $num !== null ) {
$key = $this->messageLocalizer->msg( 'cite_reference_link_key_with_num', $key, $num )
->plain();
@ -46,8 +46,8 @@ class AnchorFormatter {
* @return string A key for use in wikitext
*/
public function getReferencesKey( string $key ): string {
$prefix = $this->messageLocalizer->msg( 'cite_references_link_prefix' )->text();
$suffix = $this->messageLocalizer->msg( 'cite_references_link_suffix' )->text();
$prefix = $this->messageLocalizer->msg( 'cite_references_link_prefix' )->plain();
$suffix = $this->messageLocalizer->msg( 'cite_references_link_suffix' )->plain();
return $this->normalizeKey( $prefix . $key . $suffix );
}

View file

@ -29,19 +29,18 @@ class AnchorFormatterTest extends \MediaWikiIntegrationTestCase {
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
function ( ...$args ) {
$msg = $this->createMock( Message::class );
$msg->method( 'plain' )->willReturn( '(plain:' . implode( '|', $args ) . ')' );
$msg->method( 'text' )->willReturn( '(text:' . implode( '|', $args ) . ')' );
$msg->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
return $msg;
}
);
$formatter = new AnchorFormatter( $mockMessageLocalizer );
$this->assertSame(
'(text:cite_reference_link_prefix)key(text:cite_reference_link_suffix)',
'(cite_reference_link_prefix)key(cite_reference_link_suffix)',
$formatter->refKey( 'key', null ) );
$this->assertSame(
'(text:cite_reference_link_prefix)' .
'(plain:cite_reference_link_key_with_num|key|2)(text:cite_reference_link_suffix)',
'(cite_reference_link_prefix)' .
'(cite_reference_link_key_with_num|key|2)(cite_reference_link_suffix)',
$formatter->refKey( 'key', '2' ) );
}
@ -53,7 +52,7 @@ class AnchorFormatterTest extends \MediaWikiIntegrationTestCase {
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
function ( ...$args ) {
$msg = $this->createMock( Message::class );
$msg->method( 'text' )->willReturn( '(' . implode( '|', $args ) . ')' );
$msg->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
return $msg;
}
);