Make it possible to disable the "cite_error" wrapper message

… as well as "cite_warning". Both are extremely trivial and don't
really do anything by default. All they do is to add the prefix
"Cite error:" or "Cite warning:" to all error messages.

This patch will make it possible to disable both messages by
default, i.e. replace their default in en.json with "-" without
breaking anything. That's part of the plan outlined in T353695.
Local on-wiki overrides will continue to work.

Bug: T353695
Change-Id: I374800d0d0b837cd17ed3a1fdde20b70325b06de
This commit is contained in:
thiemowmde 2024-02-01 17:17:53 +01:00
parent 5eea8ebd90
commit 802b495160
2 changed files with 18 additions and 5 deletions

View file

@ -75,8 +75,9 @@ class ErrorReporter {
$parser->addTrackingCategory( 'cite-tracking-category-cite-error' );
}
// Messages: cite_error, cite_warning
return $this->messageLocalizer->msg( "cite_$type", $msg->plain() )->inLanguage( $language );
// Optional wrapper messages: cite_error, cite_warning
$wrapper = $this->messageLocalizer->msg( "cite_$type", $msg->plain() )->inLanguage( $language );
return $wrapper->isDisabled() ? $msg : $wrapper;
}
/**
@ -116,7 +117,7 @@ class ErrorReporter {
/**
* @param string $messageKey Expected to be a message key like "cite_error_ref_numeric_key"
*
* @return string[] Two elements, e.g. "error" and "ref_too_many_keys"
* @return string[] Two elements, e.g. "error" and "ref_numeric_key"
*/
private function parseTypeAndIdFromMessageKey( string $messageKey ): array {
return array_slice( explode( '_', str_replace( '-', '_', $messageKey ), 3 ), 1 );

View file

@ -31,6 +31,17 @@ class ErrorReporterTest extends \MediaWikiUnitTestCase {
$reporter->plain( $mockParser, $key, 'first param' ) );
}
public function testDisabledWrapperMessages() {
$language = $this->createLanguage();
$reporter = $this->createReporter( $language, true );
$mockParser = $this->createParser( $language );
$this->assertSame(
'<span class="warning mw-ext-cite-warning mw-ext-cite-warning-a" lang="qqx" ' .
'dir="rtl">(cite_warning_a)</span>',
$reporter->plain( $mockParser, 'cite_warning_a' )
);
}
public function testHalfParsed() {
$language = $this->createLanguage();
$reporter = $this->createReporter( $language );
@ -65,11 +76,12 @@ class ErrorReporterTest extends \MediaWikiUnitTestCase {
return $language;
}
private function createReporter( Language $language ): ErrorReporter {
private function createReporter( Language $language, bool $disabled = false ): ErrorReporter {
$mockMessageLocalizer = $this->createMock( ReferenceMessageLocalizer::class );
$mockMessageLocalizer->method( 'msg' )->willReturnCallback(
function ( ...$args ) use ( $language ) {
function ( ...$args ) use ( $language, $disabled ) {
$message = $this->createMock( Message::class );
$message->method( 'isDisabled' )->willReturn( $disabled );
$message->method( 'getKey' )->willReturn( $args[0] );
$message->method( 'plain' )->willReturn( '(' . implode( '|', $args ) . ')' );
$message->method( 'inLanguage' )->with( $language )->willReturnSelf();