http://www.mediawiki.org/wiki/Special:Code/MediaWiki/100227

Revision: 100227
Author:   ialex
Date:     2011-10-19 14:16:01 +0000 (Wed, 19 Oct 2011)
Log Message:
-----------
* Changed ParserOptions to store a Language object instead of only a string, 
avoids object -> string -> object conversion
* 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)

Modified Paths:
--------------
    trunk/phase3/includes/OutputPage.php
    trunk/phase3/includes/Preferences.php
    trunk/phase3/includes/WikiPage.php
    trunk/phase3/includes/installer/Installer.php
    trunk/phase3/includes/parser/CoreParserFunctions.php
    trunk/phase3/includes/parser/Parser.php
    trunk/phase3/includes/parser/ParserOptions.php

Modified: trunk/phase3/includes/OutputPage.php
===================================================================
--- trunk/phase3/includes/OutputPage.php        2011-10-19 14:13:41 UTC (rev 
100226)
+++ trunk/phase3/includes/OutputPage.php        2011-10-19 14:16:01 UTC (rev 
100227)
@@ -1249,7 +1249,7 @@
         */
        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 );

Modified: trunk/phase3/includes/Preferences.php
===================================================================
--- trunk/phase3/includes/Preferences.php       2011-10-19 14:13:41 UTC (rev 
100226)
+++ trunk/phase3/includes/Preferences.php       2011-10-19 14:16:01 UTC (rev 
100227)
@@ -305,7 +305,7 @@
                }
 
                // 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',

Modified: trunk/phase3/includes/WikiPage.php
===================================================================
--- trunk/phase3/includes/WikiPage.php  2011-10-19 14:13:41 UTC (rev 100226)
+++ trunk/phase3/includes/WikiPage.php  2011-10-19 14:16:01 UTC (rev 100227)
@@ -1950,7 +1950,7 @@
         * 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 @@
                        return $this->mPreparedEdit;
                }
 
-               $popts = ParserOptions::newFromUser( $user );
+               $popts = ParserOptions::newFromUserAndLang( $user, $wgContLang 
);
                wfRunHooks( 'ArticlePrepareTextForEdit', array( $this, $popts ) 
);
 
                $edit = (object)array();
@@ -2507,12 +2507,11 @@
        * @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

Modified: trunk/phase3/includes/installer/Installer.php
===================================================================
--- trunk/phase3/includes/installer/Installer.php       2011-10-19 14:13:41 UTC 
(rev 100226)
+++ trunk/phase3/includes/installer/Installer.php       2011-10-19 14:16:01 UTC 
(rev 100227)
@@ -1202,7 +1202,7 @@
         */
        public function setParserLanguage( $lang ) {
                $this->parserOptions->setTargetLanguage( $lang );
-               $this->parserOptions->setUserLang( $lang->getCode() );
+               $this->parserOptions->setUserLang( $lang );
        }
 
        /**

Modified: trunk/phase3/includes/parser/CoreParserFunctions.php
===================================================================
--- trunk/phase3/includes/parser/CoreParserFunctions.php        2011-10-19 
14:13:41 UTC (rev 100226)
+++ trunk/phase3/includes/parser/CoreParserFunctions.php        2011-10-19 
14:16:01 UTC (rev 100227)
@@ -97,7 +97,7 @@
        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 {

Modified: trunk/phase3/includes/parser/Parser.php
===================================================================
--- trunk/phase3/includes/parser/Parser.php     2011-10-19 14:13:41 UTC (rev 
100226)
+++ trunk/phase3/includes/parser/Parser.php     2011-10-19 14:16:01 UTC (rev 
100227)
@@ -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 @@
                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 @@
                                        $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();

Modified: trunk/phase3/includes/parser/ParserOptions.php
===================================================================
--- trunk/phase3/includes/parser/ParserOptions.php      2011-10-19 14:13:41 UTC 
(rev 100226)
+++ trunk/phase3/includes/parser/ParserOptions.php      2011-10-19 14:16:01 UTC 
(rev 100227)
@@ -41,7 +41,7 @@
        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 @@
         * 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 @@
        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,42 +184,63 @@
                $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 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 );
+       }
+
+       /**
+        * 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() );
+       }
+
        /** Get user options */
-       function initialiseFromUser( $userInput ) {
-               global $wgUseDynamicDates, $wgInterwikiMagic, 
$wgAllowExternalImages;
-               global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, 
$wgAllowSpecialInclusion, $wgMaxArticleSize;
-               global $wgMaxPPNodeCount, $wgMaxTemplateDepth, 
$wgMaxPPExpandDepth, $wgCleanSignatures;
-               global $wgExternalLinkTarget, $wgLang;
+       private function initialiseFromUser( $user, $lang ) {
+               global $wgUseDynamicDates, $wgInterwikiMagic, 
$wgAllowExternalImages,
+                       $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, 
$wgAllowSpecialInclusion,
+                       $wgMaxArticleSize, $wgMaxPPNodeCount, 
$wgMaxTemplateDepth, $wgMaxPPExpandDepth,
+                       $wgCleanSignatures, $wgExternalLinkTarget;
 
                wfProfileIn( __METHOD__ );
 
-               if ( !$userInput ) {
-                       global $wgUser;
-                       if ( isset( $wgUser ) ) {
-                               $user = $wgUser;
-                       } else {
-                               $user = new User;
-                       }
-               } else {
-                       $user =& $userInput;
-               }
-
-               $this->mUser = $user;
-
                $this->mUseDynamicDates = $wgUseDynamicDates;
                $this->mInterwikiMagic = $wgInterwikiMagic;
                $this->mAllowExternalImages = $wgAllowExternalImages;
@@ -222,11 +254,12 @@
                $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 @@
                }
 
                if ( in_array( 'userlang', $forOptions ) ) {
-                       $confstr .= '!' . $this->mUserLang;
+                       $confstr .= '!' . $this->mUserLang->getCode();
                } else {
                        $confstr .= '!*';
                }


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to