From 35c38ce319563889ccd61f998223e61316a35f6a Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Wed, 3 Feb 2016 15:41:00 -0500 Subject: [PATCH] Add User::isSafeToLoad() and ParserOptions::newFromAnon() Useful for avoiding "User::loadFromSession called before the end of Setup.php". Bug: T124367 Change-Id: I0b018a623fc833ca95d249ee21667a8f5690d50e --- includes/cache/MessageCache.php | 6 +++--- includes/parser/ParserOptions.php | 9 +++++++++ includes/user/User.php | 11 ++++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/includes/cache/MessageCache.php b/includes/cache/MessageCache.php index 6b938f1cb9..2fae4e3f2b 100644 --- a/includes/cache/MessageCache.php +++ b/includes/cache/MessageCache.php @@ -168,14 +168,14 @@ class MessageCache { * @return ParserOptions */ function getParserOptions() { - global $wgFullyInitialised, $wgContLang; + global $wgUser; if ( !$this->mParserOptions ) { - if ( !$wgFullyInitialised ) { + if ( !$wgUser->isSafeToLoad() ) { // $wgUser isn't unstubbable yet, so don't try to get a // ParserOptions for it. And don't cache this ParserOptions // either. - $po = new ParserOptions( new User, $wgContLang ); + $po = ParserOptions::newFromAnon(); $po->setEditSection( false ); return $po; } diff --git a/includes/parser/ParserOptions.php b/includes/parser/ParserOptions.php index e6d5274726..0e8d76d3d8 100644 --- a/includes/parser/ParserOptions.php +++ b/includes/parser/ParserOptions.php @@ -599,6 +599,15 @@ class ParserOptions { $this->initialiseFromUser( $user, $lang ); } + /** + * Get a ParserOptions object for an anonymous user + * @return ParserOptions + */ + public static function newFromAnon() { + global $wgContLang; + return new ParserOptions( new User, $wgContLang ); + } + /** * Get a ParserOptions object from a given user. * Language will be taken from $wgLang. diff --git a/includes/user/User.php b/includes/user/User.php index 0fa7d59f7b..8e3b2ecbc9 100644 --- a/includes/user/User.php +++ b/includes/user/User.php @@ -309,6 +309,15 @@ class User implements IDBAccessObject { return $this->getName(); } + /** + * Test if it's safe to load this User object + * @return bool + */ + public function isSafeToLoad() { + global $wgFullyInitialised; + return $wgFullyInitialised || $this->mLoadedItems === true || $this->mFrom !== 'session'; + } + /** * Load the user table data for this object from the source given by mFrom. * @@ -327,7 +336,7 @@ class User implements IDBAccessObject { $this->queryFlagsUsed = $flags; // If this is called too early, things are likely to break. - if ( $this->mFrom === 'session' && empty( $wgFullyInitialised ) ) { + if ( !$wgFullyInitialised && $this->mFrom === 'session' ) { \MediaWiki\Logger\LoggerFactory::getInstance( 'session' ) ->warning( 'User::loadFromSession called before the end of Setup.php', array( 'exception' => new Exception( 'User::loadFromSession called before the end of Setup.php' ), -- 2.20.1