* Changed ParserOptions to store a Language object instead of only a string, avoids...
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Wed, 19 Oct 2011 14:16:01 +0000 (14:16 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Wed, 19 Oct 2011 14:16:01 +0000 (14:16 +0000)
* ParserOptions::getUserLang() will still return a string for compatibility, added ParserOptions::getUserLangObj() to get the object
* Added ParserOptions::newFromUserAndLang() and ParserOptions::newFromContext() to easily get a ParserOptions object when a context is available or when someone wants to force the language
* Updated OutputPage and Preferences to use newFromContext() and WikiPage to use newFromUserAndLang()
* ParserOptions::setUserLang() still accepts either a string or a Language object, but changed the calls to pass an object instead of a string
* Changed Parser::getFunctionLang() to return the Language object from ParserOptions when parsing interface messages rather than $wgLang directly and updated the documentation to say that $wgLang should not be used directly (as $wgUser, $wgTitle and $wgRequest)

includes/OutputPage.php
includes/Preferences.php
includes/WikiPage.php
includes/installer/Installer.php
includes/parser/CoreParserFunctions.php
includes/parser/Parser.php
includes/parser/ParserOptions.php

index 6b2e52c..7e3812e 100644 (file)
@@ -1249,7 +1249,7 @@ class OutputPage extends ContextSource {
         */
        public function parserOptions( $options = null ) {
                if ( !$this->mParserOptions ) {
-                       $this->mParserOptions = new ParserOptions;
+                       $this->mParserOptions = ParserOptions::newFromContext( $this->getContext() );
                        $this->mParserOptions->setEditSection( false );
                }
                return wfSetVar( $this->mParserOptions, $options );
index b060230..bfdc538 100644 (file)
@@ -305,7 +305,7 @@ class Preferences {
                }
 
                // show a preview of the old signature first
-               $oldsigWikiText = $wgParser->preSaveTransform( "~~~", $context->getTitle(), $user, new ParserOptions );
+               $oldsigWikiText = $wgParser->preSaveTransform( "~~~", $context->getTitle(), $user, ParserOptions::newFromContext( $context ) );
                $oldsigHTML = $context->getOutput()->parseInline( $oldsigWikiText, true, true );
                $defaultPreferences['oldsig'] = array(
                        'type' => 'info',
index 01ffe5e..f2c74e2 100644 (file)
@@ -1950,7 +1950,7 @@ class WikiPage extends Page {
         * Returns a stdclass with source, pst and output members
         */
        public function prepareTextForEdit( $text, $revid = null, User $user = null ) {
-               global $wgParser, $wgUser;
+               global $wgParser, $wgContLang, $wgUser;
                $user = is_null( $user ) ? $wgUser : $user;
                // @TODO fixme: check $user->getId() here???
                if ( $this->mPreparedEdit
@@ -1961,7 +1961,7 @@ class WikiPage extends Page {
                        return $this->mPreparedEdit;
                }
 
-               $popts = ParserOptions::newFromUser( $user );
+               $popts = ParserOptions::newFromUserAndLang( $user, $wgContLang );
                wfRunHooks( 'ArticlePrepareTextForEdit', array( $this, $popts ) );
 
                $edit = (object)array();
@@ -2507,12 +2507,11 @@ class WikiPage extends Page {
        * @return ParserOptions
        */
        public function makeParserOptions( $user ) {
-               global $wgLanguageCode;
+               global $wgContLang;
                if ( $user instanceof User ) { // settings per user (even anons)
                        $options = ParserOptions::newFromUser( $user );
                } else { // canonical settings
-                       $options = ParserOptions::newFromUser( new User );
-                       $options->setUserLang( $wgLanguageCode ); # Must be set explicitily
+                       $options = ParserOptions::newFromUserAndLang( new User, $wgContLang );
                }
                $options->enableLimitReport(); // show inclusion/loop reports
                $options->setTidy( true ); // fix bad HTML
index 102ac38..a1b5265 100644 (file)
@@ -1202,7 +1202,7 @@ abstract class Installer {
         */
        public function setParserLanguage( $lang ) {
                $this->parserOptions->setTargetLanguage( $lang );
-               $this->parserOptions->setUserLang( $lang->getCode() );
+               $this->parserOptions->setUserLang( $lang );
        }
 
        /**
index fec8351..70e9b01 100644 (file)
@@ -97,7 +97,7 @@ class CoreParserFunctions {
        static function intFunction( $parser, $part1 = '' /*, ... */ ) {
                if ( strval( $part1 ) !== '' ) {
                        $args = array_slice( func_get_args(), 2 );
-                       $message = wfMessage( $part1, $args )->inLanguage( $parser->getOptions()->getUserLang() )->plain();
+                       $message = wfMessage( $part1, $args )->inLanguage( $parser->getOptions()->getUserLangObj() )->plain();
                        $message = $parser->replaceVariables( $message ); // like MessageCache::transform()
                        return $message;
                } else {
index dc7e01a..a9c1dc7 100644 (file)
@@ -32,9 +32,9 @@
  *     Removes <noinclude> sections, and <includeonly> tags.
  *
  * Globals used:
- *    objects:   $wgLang, $wgContLang
+ *    object: $wgContLang
  *
- * NOT $wgUser or $wgTitle or $wgRequest. Keep them away!
+ * NOT $wgUser or $wgTitle or $wgRequest or $wgLang. Keep them away!
  *
  * settings:
  *  $wgUseDynamicDates*, $wgInterwikiMagic*,
@@ -698,8 +698,7 @@ class Parser {
                if ( $target !== null ) {
                        return $target;
                } elseif( $this->mOptions->getInterfaceMessage() ) {
-                       global $wgLang;
-                       return $wgLang;
+                       return $this->mOptions->getUserLangObj();
                } elseif( is_null( $this->mTitle ) ) {
                        throw new MWException( __METHOD__.': $this->mTitle is null' );
                }
@@ -3237,7 +3236,7 @@ class Parser {
                                        $context->setTitle( $title );
                                        $context->setRequest( new FauxRequest( $pageArgs ) );
                                        $context->setUser( $this->getUser() );
-                                       $context->setLang( Language::factory( $this->mOptions->getUserLang() ) );
+                                       $context->setLang( $this->mOptions->getUserLangObj() );
                                        $ret = SpecialPageFactory::capturePath( $title, $context );
                                        if ( $ret ) {
                                                $text = $context->getOutput()->getHTML();
index 21b1cd8..b4660a4 100644 (file)
@@ -41,7 +41,7 @@ class ParserOptions {
        var $mMath;                      # User math preference (as integer)
        var $mThumbSize;                 # Thumb size preferred by the user.
        private $mStubThreshold;         # Maximum article size of an article to be marked as "stub"
-       var $mUserLang;                  # Language code of the User language.
+       var $mUserLang;                  # Language object of the User language.
 
        /**
         * @var User
@@ -119,12 +119,23 @@ class ParserOptions {
         * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
         * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
         * producing inconsistent tables (Bug 14404).
+        *
+        * @return Language object
+        * @since 1.19
+        */
+       function getUserLangObj() {
+               $this->optionUsed( 'userlang' );
+               return $this->mUserLang;
+       }
+
+       /**
+        * Same as getUserLangObj() but returns a string instead.
+        *
         * @return String   Language code
         * @since 1.17
         */
        function getUserLang() {
-               $this->optionUsed( 'userlang' );
-               return $this->mUserLang;
+               return $this->getUserLangObj()->getCode();
        }
 
        function setUseDynamicDates( $x )           { return wfSetVar( $this->mUseDynamicDates, $x ); }
@@ -153,8 +164,8 @@ class ParserOptions {
        function setExternalLinkTarget( $x )        { return wfSetVar( $this->mExternalLinkTarget, $x ); }
        function setMath( $x )                      { return wfSetVar( $this->mMath, $x ); }
        function setUserLang( $x )                  {
-               if ( $x instanceof Language ) {
-                       $x = $x->getCode();
+               if ( is_string( $x ) ) {
+                       $x = Language::factory( $x );
                }
                return wfSetVar( $this->mUserLang, $x );
        }
@@ -173,41 +184,62 @@ class ParserOptions {
                $this->mExtraKey .= '!' . $key;
        }
 
-       function __construct( $user = null ) {
-               $this->initialiseFromUser( $user );
+       function __construct( $user = null, $lang = null ) {
+               if ( $user === null ) {
+                       global $wgUser;
+                       if ( $wgUser === null ) {
+                               $user = new User;
+                       } else {
+                               $user = $wgUser;
+                       }
+               }
+               if ( $lang === null ) {
+                       global $wgLang;
+                       $lang = $wgLang;
+               }
+               $this->initialiseFromUser( $user, $lang );
        }
 
        /**
-        * Get parser options
+        * Get a ParserOptions object from a given user.
+        * Language will be taken from $wgLang.
         *
         * @param $user User object
         * @return ParserOptions object
         */
-       static function newFromUser( $user ) {
+       public static function newFromUser( $user ) {
                return new ParserOptions( $user );
        }
 
-       /** Get user options */
-       function initialiseFromUser( $userInput ) {
-               global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
-               global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
-               global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
-               global $wgExternalLinkTarget, $wgLang;
+       /**
+        * Get a ParserOptions object from a given user and language
+        *
+        * @param $user User object
+        * @param $lang Language object
+        * @return ParserOptions object
+        */
+       public static function newFromUserAndLang( User $user, Language $lang ) {
+               return new ParserOptions( $user, $lang );
+       }
 
-               wfProfileIn( __METHOD__ );
+       /**
+        * Get a ParserOptions object from a IContextSource object
+        *
+        * @param $context IContextSource object
+        * @return ParserOptions object
+        */
+       public static function newFromContext( IContextSource $context ) {
+               return new ParserOptions( $context->getUser(), $context->getLang() );
+       }
 
-               if ( !$userInput ) {
-                       global $wgUser;
-                       if ( isset( $wgUser ) ) {
-                               $user = $wgUser;
-                       } else {
-                               $user = new User;
-                       }
-               } else {
-                       $user =& $userInput;
-               }
+       /** Get user options */
+       private function initialiseFromUser( $user, $lang ) {
+               global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages,
+                       $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
+                       $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
+                       $wgCleanSignatures, $wgExternalLinkTarget;
 
-               $this->mUser = $user;
+               wfProfileIn( __METHOD__ );
 
                $this->mUseDynamicDates = $wgUseDynamicDates;
                $this->mInterwikiMagic = $wgInterwikiMagic;
@@ -222,11 +254,12 @@ class ParserOptions {
                $this->mCleanSignatures = $wgCleanSignatures;
                $this->mExternalLinkTarget = $wgExternalLinkTarget;
 
+               $this->mUser = $user;
                $this->mNumberHeadings = $user->getOption( 'numberheadings' );
                $this->mMath = $user->getOption( 'math' );
                $this->mThumbSize = $user->getOption( 'thumbsize' );
                $this->mStubThreshold = $user->getStubThreshold();
-               $this->mUserLang = $wgLang->getCode();
+               $this->mUserLang = $lang;
 
                wfProfileOut( __METHOD__ );
        }
@@ -312,7 +345,7 @@ class ParserOptions {
                }
 
                if ( in_array( 'userlang', $forOptions ) ) {
-                       $confstr .= '!' . $this->mUserLang;
+                       $confstr .= '!' . $this->mUserLang->getCode();
                } else {
                        $confstr .= '!*';
                }