From 79274e1f4404721a1d4236c2068310be5e378ee1 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Tue, 1 Nov 2016 14:58:13 -0400 Subject: [PATCH] Better handling of Message objects as Message parameters If a Message object is a parameter of another Message object, it should use the same language, use-database flag, and so on as the outer Message when it's being stringified. Change-Id: I92762a1a63c90a16e8581edc96bd1da699880157 --- includes/Message.php | 20 +++++++++++++++++- tests/phpunit/includes/MessageTest.php | 28 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/includes/Message.php b/includes/Message.php index 7d86d07517..85c78d640c 100644 --- a/includes/Message.php +++ b/includes/Message.php @@ -766,6 +766,7 @@ class Message implements MessageSpecifier, Serializable { */ public function useDatabase( $useDatabase ) { $this->useDatabase = (bool)$useDatabase; + $this->message = null; return $this; } @@ -1132,10 +1133,27 @@ class Message implements MessageSpecifier, Serializable { return [ 'before', '[INVALID]' ]; } } elseif ( $param instanceof Message ) { + // Match language, flags, etc. to the current message. + $msg = clone $param; + if ( $msg->language !== $this->language || $msg->useDatabase !== $this->useDatabase ) { + // Cache depends on these parameters + $msg->message = null; + } + $msg->interface = $this->interface; + $msg->language = $this->language; + $msg->useDatabase = $this->useDatabase; + $msg->title = $this->title; + + // DWIM + if ( $format === 'block-parse' ) { + $format = 'parse'; + } + $msg->format = $format; + // Message objects should not be before parameters because // then they'll get double escaped. If the message needs to be // escaped, it'll happen right here when we call toString(). - return [ 'after', $param->toString( $format ) ]; + return [ 'after', $msg->toString( $format ) ]; } else { return [ 'before', $param ]; } diff --git a/tests/phpunit/includes/MessageTest.php b/tests/phpunit/includes/MessageTest.php index 8390e1fd39..e8afb4ccee 100644 --- a/tests/phpunit/includes/MessageTest.php +++ b/tests/phpunit/includes/MessageTest.php @@ -512,6 +512,34 @@ class MessageTest extends MediaWikiLangTestCase { ); } + /** + * @covers Message::extractParam + */ + public function testMessageAsParam() { + $this->setMwGlobals( [ + 'wgScript' => '/wiki/index.php', + 'wgArticlePath' => '/wiki/$1', + ] ); + + $msg = new Message( 'returnto', [ + new Message( 'apihelp-link', [ + 'foo', new Message( 'mainpage', [], Language::factory( 'en' ) ) + ], Language::factory( 'de' ) ) + ], Language::factory( 'es' ) ); + + $this->assertEquals( + 'Volver a [[Special:ApiHelp/foo|Página principal]].', + $msg->text(), + 'Process with ->text()' + ); + $this->assertEquals( + '

Volver a Página ' + . "principal.\n

", + $msg->parseAsBlock(), + 'Process with ->parseAsBlock()' + ); + } + public static function provideParser() { return [ [ -- 2.20.1