Fix html <title> for exceptions during message parsing.
authorBrian Wolff <bawolff+wn@gmail.com>
Wed, 18 Oct 2017 06:27:12 +0000 (06:27 +0000)
committerBrian Wolff <bawolff+wn@gmail.com>
Sat, 1 Sep 2018 20:19:45 +0000 (20:19 +0000)
Previously an ugly {{SITENAME}} would show up for exceptions
that happened in the middle of processing a message

Change-Id: I4e3b675673dc3b74f89e4325f6a0a8b44162f478

includes/exception/MWException.php
includes/exception/MWExceptionRenderer.php

index b3e9422..652a87d 100644 (file)
@@ -73,15 +73,23 @@ class MWException extends Exception {
         * @return string Message with arguments replaced
         */
        public function msg( $key, $fallback /*[, params...] */ ) {
+               global $wgSitename;
                $args = array_slice( func_get_args(), 2 );
 
+               $res = false;
                if ( $this->useMessageCache() ) {
                        try {
-                               return wfMessage( $key, $args )->text();
+                               $res = wfMessage( $key, $args )->text();
                        } catch ( Exception $e ) {
                        }
                }
-               return wfMsgReplaceArgs( $fallback, $args );
+               if ( $res === false ) {
+                       $res = wfMsgReplaceArgs( $fallback, $args );
+                       // If an exception happens inside message rendering,
+                       // {{SITENAME}} sometimes won't be replaced.
+                       $res = preg_replace( '/\{\{SITENAME\}\}/', $wgSitename, $res );
+               }
+               return $res;
        }
 
        /**
@@ -154,6 +162,16 @@ class MWException extends Exception {
                global $wgOut, $wgSitename;
                if ( $this->useOutputPage() ) {
                        $wgOut->prepareErrorPage( $this->getPageTitle() );
+                       // Manually set the html title, since sometimes
+                       // {{SITENAME}} does not get replaced for exceptions
+                       // happening inside message rendering.
+                       $wgOut->setHTMLTitle(
+                               $this->msg(
+                                       'pagetitle',
+                                       "$1 - $wgSitename",
+                                       $this->getPageTitle()
+                               )
+                       );
 
                        $wgOut->addHTML( $this->getHTML() );
 
index 49cf71e..1f1cabe 100644 (file)
@@ -197,12 +197,17 @@ class MWExceptionRenderer {
         * @return string Message with arguments replaced
         */
        private static function msg( $key, $fallback /*[, params...] */ ) {
+               global $wgSitename;
                $args = array_slice( func_get_args(), 2 );
                try {
-                       return wfMessage( $key, $args )->text();
+                       $res = wfMessage( $key, $args )->text();
                } catch ( Exception $e ) {
-                       return wfMsgReplaceArgs( $fallback, $args );
+                       $res = wfMsgReplaceArgs( $fallback, $args );
+                       // If an exception happens inside message rendering,
+                       // {{SITENAME}} sometimes won't be replaced.
+                       $res = preg_replace( '/\{\{SITENAME\}\}/', $wgSitename, $res );
                }
+               return $res;
        }
 
        /**