Message: Correct output of wfMessage( 'non-existent-msg' )->text()
authorErik Bernhardson <ebernhardson@wikimedia.org>
Wed, 4 Jun 2014 21:06:03 +0000 (14:06 -0700)
committerKrinkle <krinklemail@gmail.com>
Tue, 23 Sep 2014 21:06:22 +0000 (21:06 +0000)
The output of Message::text() should always be acceptable to pass into
external html escaping, such as when the response is returned over an
API request and escaped by the client side code. Calling ->text() on a
non-existent key was returning the entity encoded value which leads to
double encoding down the line, this patch fixes that oversight.

Bug: 66199
Change-Id: Ieec94d4e4c7e5c36e5e68bbf01792e96368e54e0

includes/Message.php
tests/phpunit/includes/MessageTest.php

index 4df0d80..59c5120 100644 (file)
@@ -674,11 +674,10 @@ class Message {
                $string = $this->fetchMessage();
 
                if ( $string === false ) {
-                       $key = htmlspecialchars( $this->key );
-                       if ( $this->format === 'plain' ) {
-                               return '<' . $key . '>';
+                       if ( $this->format === 'plain' || $this->format === 'text' ) {
+                               return '<' . $this->key . '>';
                        }
-                       return '&lt;' . $key . '&gt;';
+                       return '&lt;' . htmlspecialchars( $this->key ) . '&gt;';
                }
 
                # Replace $* with a list of parameters for &uselang=qqx.
@@ -735,10 +734,10 @@ class Message {
                                // Doh! Cause a fatal error after all?
                        }
 
-                       if ( $this->format === 'plain' ) {
+                       if ( $this->format === 'plain' || $this->format === 'text' ) {
                                return '<' . $this->key . '>';
                        }
-                       return '&lt;' . $this->key . '&gt;';
+                       return '&lt;' . htmlspecialchars( $this->key ) . '&gt;';
                }
        }
 
index f3d2a84..47560e6 100644 (file)
@@ -109,9 +109,15 @@ class MessageTest extends MediaWikiLangTestCase {
                $this->assertInstanceOf( 'Message', wfMessage( 'mainpage' ) );
                $this->assertInstanceOf( 'Message', wfMessage( 'i-dont-exist-evar' ) );
                $this->assertEquals( 'Main Page', wfMessage( 'mainpage' )->text() );
-               $this->assertEquals( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->text() );
+               $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->text() );
+               $this->assertEquals( '<i<dont>exist-evar>', wfMessage( 'i<dont>exist-evar' )->text() );
                $this->assertEquals( '<i-dont-exist-evar>', wfMessage( 'i-dont-exist-evar' )->plain() );
+               $this->assertEquals( '<i<dont>exist-evar>', wfMessage( 'i<dont>exist-evar' )->plain() );
                $this->assertEquals( '&lt;i-dont-exist-evar&gt;', wfMessage( 'i-dont-exist-evar' )->escaped() );
+               $this->assertEquals(
+                       '&lt;i&lt;dont&gt;exist-evar&gt;',
+                       wfMessage( 'i<dont>exist-evar' )->escaped()
+               );
        }
 
        /**