From: Timo Tijhof Date: Wed, 17 Apr 2019 21:08:10 +0000 (+0100) Subject: resourceloader: Omit non-existent messages in MessageBlobStore X-Git-Tag: 1.34.0-rc.0~1929^2 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=429c5acbfd6af393064f853fbc159fa5406d06e2 resourceloader: Omit non-existent messages in MessageBlobStore Previously, it was exporting the result of $msg->plain() regardless of $msg->exists(). This meant that, client-side, via mw.message the message would always make mw.Message#exists return true, even if in fact it did not exist. Bug: T221294 Change-Id: I77122777ddaaa2d43f8385df9292540a8d21b328 --- diff --git a/includes/resourceloader/MessageBlobStore.php b/includes/resourceloader/MessageBlobStore.php index b7c19042ca..635e4337d8 100644 --- a/includes/resourceloader/MessageBlobStore.php +++ b/includes/resourceloader/MessageBlobStore.php @@ -190,16 +190,18 @@ class MessageBlobStore implements LoggerAwareInterface { * @since 1.27 * @param string $key Message key * @param string $lang Language code - * @return string + * @return string|null */ protected function fetchMessage( $key, $lang ) { $message = wfMessage( $key )->inLanguage( $lang ); - $value = $message->plain(); if ( !$message->exists() ) { $this->logger->warning( 'Failed to find {messageKey} ({lang})', [ 'messageKey' => $key, 'lang' => $lang, ] ); + $value = null; + } else { + $value = $message->plain(); } return $value; } @@ -214,7 +216,10 @@ class MessageBlobStore implements LoggerAwareInterface { private function generateMessageBlob( ResourceLoaderModule $module, $lang ) { $messages = []; foreach ( $module->getMessages() as $key ) { - $messages[$key] = $this->fetchMessage( $key, $lang ); + $value = $this->fetchMessage( $key, $lang ); + if ( $value !== null ) { + $messages[$key] = $value; + } } $json = FormatJson::encode( (object)$messages ); diff --git a/tests/phpunit/includes/resourceloader/MessageBlobStoreTest.php b/tests/phpunit/includes/resourceloader/MessageBlobStoreTest.php index e57764306e..9afa232bd4 100644 --- a/tests/phpunit/includes/resourceloader/MessageBlobStoreTest.php +++ b/tests/phpunit/includes/resourceloader/MessageBlobStoreTest.php @@ -34,16 +34,27 @@ class MessageBlobStoreTest extends PHPUnit\Framework\TestCase { $this->assertEquals( '{"mainpage":"Main Page"}', $blob, 'Generated blob' ); } + public function testBlobCreation_empty() { + $module = $this->makeModule( [] ); + $rl = new ResourceLoader(); + $rl->register( $module->getName(), $module ); + + $blobStore = $this->makeBlobStore( null, $rl ); + $blob = $blobStore->getBlob( $module, 'en' ); + + $this->assertEquals( '{}', $blob, 'Generated blob' ); + } + public function testBlobCreation_unknownMessage() { - $module = $this->makeModule( [ 'i-dont-exist' ] ); + $module = $this->makeModule( [ 'i-dont-exist', 'mainpage', 'i-dont-exist2' ] ); $rl = new ResourceLoader(); $rl->register( $module->getName(), $module ); $blobStore = $this->makeBlobStore( null, $rl ); - // Generating a blob should succeed without errors, - // even if a message is unknown. + // Generating a blob should continue without errors, + // with keys of unknown messages excluded from the blob. $blob = $blobStore->getBlob( $module, 'en' ); - $this->assertEquals( '{"i-dont-exist":"\u29fci-dont-exist\u29fd"}', $blob, 'Generated blob' ); + $this->assertEquals( '{"mainpage":"Main Page"}', $blob, 'Generated blob' ); } public function testMessageCachingAndPurging() {