From bd548f36e959edfc89cf04d6136dd058a849b7b2 Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Wed, 19 Oct 2011 14:16:01 +0000 Subject: [PATCH] * 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) --- includes/OutputPage.php | 2 +- includes/Preferences.php | 2 +- includes/WikiPage.php | 9 ++- includes/installer/Installer.php | 2 +- includes/parser/CoreParserFunctions.php | 2 +- includes/parser/Parser.php | 9 ++- includes/parser/ParserOptions.php | 91 +++++++++++++++++-------- 7 files changed, 74 insertions(+), 43 deletions(-) diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 6b2e52c6a1..7e3812e345 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -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 ); diff --git a/includes/Preferences.php b/includes/Preferences.php index b060230961..bfdc538ff4 100644 --- a/includes/Preferences.php +++ b/includes/Preferences.php @@ -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', diff --git a/includes/WikiPage.php b/includes/WikiPage.php index 01ffe5ee87..f2c74e2b47 100644 --- a/includes/WikiPage.php +++ b/includes/WikiPage.php @@ -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 diff --git a/includes/installer/Installer.php b/includes/installer/Installer.php index 102ac38710..a1b526584c 100644 --- a/includes/installer/Installer.php +++ b/includes/installer/Installer.php @@ -1202,7 +1202,7 @@ abstract class Installer { */ public function setParserLanguage( $lang ) { $this->parserOptions->setTargetLanguage( $lang ); - $this->parserOptions->setUserLang( $lang->getCode() ); + $this->parserOptions->setUserLang( $lang ); } /** diff --git a/includes/parser/CoreParserFunctions.php b/includes/parser/CoreParserFunctions.php index fec8351370..70e9b01ae0 100644 --- a/includes/parser/CoreParserFunctions.php +++ b/includes/parser/CoreParserFunctions.php @@ -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 { diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index dc7e01a1d5..a9c1dc7ed0 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -32,9 +32,9 @@ * Removes sections, and 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(); diff --git a/includes/parser/ParserOptions.php b/includes/parser/ParserOptions.php index 21b1cd889f..b4660a4650 100644 --- a/includes/parser/ParserOptions.php +++ b/includes/parser/ParserOptions.php @@ -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 .= '!*'; } -- 2.20.1