* (bug 28532) wfMsgExt() and wfMsgWikiHtml() use $wgOut->parse()
authorSam Reed <reedy@users.mediawiki.org>
Mon, 18 Apr 2011 12:43:53 +0000 (12:43 +0000)
committerSam Reed <reedy@users.mediawiki.org>
Mon, 18 Apr 2011 12:43:53 +0000 (12:43 +0000)
* (bug 16129) Transcluded special pages expose strip markers when they output parsed messages

Also adding some related documentation during my travels around the code

RELEASE-NOTES
includes/GlobalFunctions.php
includes/Message.php
includes/MessageCache.php
includes/OutputPage.php
includes/parser/Parser.php

index 9ce359f..400ad59 100644 (file)
@@ -238,6 +238,8 @@ PHP if you have not done so prior to upgrading MediaWiki.
 * UtfNormal::cleanUp on an invalid utf-8 sequence no longer returns false if intl installed.
 * (bug 28561) The css class small will no longer make nested elements even smaller.
 * (bug 13172) Array type exif data (like GPS) was not being extracted from images.
+* (bug 28532) wfMsgExt() and wfMsgWikiHtml() use $wgOut->parse()
+* (bug 16129) Transcluded special pages expose strip markers when they output parsed messages
 
 === API changes in 1.18 ===
 * (bug 26339) Throw warning when truncating an overlarge API result
index 0c88816..2f83b07 100644 (file)
@@ -716,10 +716,12 @@ function wfMsgHtml( $key ) {
  * @return string
  */
 function wfMsgWikiHtml( $key ) {
-       global $wgOut;
+       global $wgMessageCache;
        $args = func_get_args();
        array_shift( $args );
-       return wfMsgReplaceArgs( $wgOut->parse( wfMsgGetKey( $key, true ), /* can't be set to false */ true ), $args );
+       return wfMsgReplaceArgs(
+               $wgMessageCache->parse( wfMsgGetKey( $key, true ), null, /* can't be set to false */ true ),
+               $args );
 }
 
 /**
@@ -741,7 +743,7 @@ function wfMsgWikiHtml( $key ) {
  * Behavior for conflicting options (e.g., parse+parseinline) is undefined.
  */
 function wfMsgExt( $key, $options ) {
-       global $wgOut;
+       global $wgMessageCache;
 
        $args = func_get_args();
        array_shift( $args );
@@ -781,9 +783,9 @@ function wfMsgExt( $key, $options ) {
        }
 
        if( in_array( 'parse', $options, true ) ) {
-               $string = $wgOut->parse( $string, true, !$forContent, $langCodeObj );
+               $string = $wgMessageCache->parse( $string, null, true, !$forContent, $langCodeObj );
        } elseif ( in_array( 'parseinline', $options, true ) ) {
-               $string = $wgOut->parse( $string, true, !$forContent, $langCodeObj );
+               $string = $wgMessageCache->parse( $string, null, true, !$forContent, $langCodeObj );
                $m = array();
                if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
                        $string = $m[1];
index 6731f1b..8c8f4b4 100644 (file)
@@ -65,6 +65,8 @@ class Message {
        /**
         * In which language to get this message. Overrides the $interface
         * variable.
+        *
+        * @var Language
         */
        protected $language = null;
        
@@ -370,10 +372,18 @@ class Message {
                return $message === false || $message === '' || $message === '-';
        }
 
+       /**
+        * @param $value
+        * @return array
+        */
        public static function rawParam( $value ) {
                return array( 'raw' => $value );
        }
-       
+
+       /**
+        * @param $value
+        * @return array
+        */
        public static function numParam( $value ) {
                return array( 'num' => $value );
        }
@@ -419,17 +429,16 @@ class Message {
        /**
         * Wrapper for what ever method we use to parse wikitext.
         * @param $string String: Wikitext message contents
-        * @return Wikitext parsed into HTML
+        * @return string Wikitext parsed into HTML
         */
        protected function parseText( $string ) {
-               global $wgOut;
-               return $wgOut->parse( $string, /*linestart*/true, $this->interface, $this->language );
+               return MessageCache::singleton()->parse( $string, /*linestart*/true, $this->interface, $this->language );
        }
 
        /**
         * Wrapper for what ever method we use to {{-transform wikitext.
         * @param $string String: Wikitext message contents
-        * @return Wikitext with {{-constructs replaced with their values.
+        * @return string Wikitext with {{-constructs replaced with their values.
         */
        protected function transformText( $string ) {
                return MessageCache::singleton()->transform( $string, $this->interface, $this->language, $this->title );
@@ -450,6 +459,8 @@ class Message {
 
        /**
         * Wrapper for what ever method we use to get message contents
+        *
+        * @return string
         */
        protected function fetchMessage() {
                if ( !isset( $this->message ) ) {
index d907a78..ecb46ac 100644 (file)
@@ -67,6 +67,11 @@ class MessageCache {
         */
        private static $instance;
 
+       /**
+        * @var bool
+        */
+       protected $mInParser = false;
+
        /**
         * Get the signleton instance of this class
         *
@@ -102,6 +107,8 @@ class MessageCache {
 
        /**
         * ParserOptions is lazy initialised.
+        *
+        * @return ParserOptions
         */
        function getParserOptions() {
                if ( !$this->mParserOptions ) {
@@ -220,6 +227,8 @@ class MessageCache {
 
        /**
         * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
+        *
+        * @return bool
         */
        function setCache( $cache, $code ) {
                if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) {
@@ -728,12 +737,41 @@ class MessageCache {
                return $message;
        }
 
+       /**
+        * @param $message string
+        * @param $interface bool
+        * @param $language
+        * @param $title Title
+        * @return string
+        */
        function transform( $message, $interface = false, $language = null, $title = null ) {
                // Avoid creating parser if nothing to transform
                if( strpos( $message, '{{' ) === false ) {
                        return $message;
                }
 
+               if ( $this->mInParser ) {
+            return $message;
+               }
+
+               $parser = $this->getParser();
+               if ( $parser ) {
+                       $popts = $this->getParserOptions();
+                       $popts->setInterfaceMessage( $interface );
+                       $popts->setTargetLanguage( $language );
+                       $popts->setUserLang( $language );
+
+                       $this->mInParser = true;
+                       $message = $parser->transformMsg( $message, $popts, $title );
+                       $this->mInParser = false;
+               }
+               return $message;
+       }
+
+       /**
+        * @return Parser
+        */
+       function getParser() {
                global $wgParser, $wgParserConf;
                if ( !$this->mParser && isset( $wgParser ) ) {
                        # Do some initialisation so that we don't have to do it twice
@@ -746,16 +784,38 @@ class MessageCache {
                        } else {
                                $this->mParser = clone $wgParser;
                        }
-                       #wfDebug( __METHOD__ . ": following contents triggered transform: $message\n" );
                }
-               if ( $this->mParser ) {
-                       $popts = $this->getParserOptions();
-                       $popts->setInterfaceMessage( $interface );
+               return $this->mParser;
+       }
+
+       /**
+        * @param $text string
+        * @param $title Title
+        * @param $interface bool
+        * @param $linestart bool
+        * @param $language
+        * @return ParserOutput
+        */
+       public function parse( $text, $title = null, $linestart = true, $interface = false, $language = null  ) {
+               if ( $this->mInParser ) {
+                  return htmlspecialchars( $text );
+               }
+
+               $parser = $this->getParser();
+               $popts = $this->getParserOptions();
+
+               if ( $interface ) {
+                       $popts->setInterfaceMessage( true );
+               }
+               if ( $language !== null ) {
                        $popts->setTargetLanguage( $language );
-                       $popts->setUserLang( $language );
-                       $message = $this->mParser->transformMsg( $message, $popts, $title );
                }
-               return $message;
+
+               $this->mInParser = true;
+               $res = $parser->parse( $text, $title, $popts, $linestart );
+               $this->mInParser = false;
+
+               return $res;
        }
 
        function disable() {
index 120d55a..67a634d 100644 (file)
@@ -792,7 +792,7 @@ class OutputPage {
         * @param $t Title object
         */
        public function setTitle( $t ) {
-               $this->getContext()->setTitle($t);
+               $this->getContext()->setTitle( $t );
        }
 
        /**
index 13c91d8..92cff97 100644 (file)
@@ -278,7 +278,7 @@ class Parser {
         * Do not call this function recursively.
         *
         * @param $text String: text we want to parse
-        * @param $title A title object
+        * @param $title Title object
         * @param $options ParserOptions
         * @param $linestart boolean
         * @param $clearState boolean