From 87070fc6743bfe5da7b49f07561fc1e0b03897c4 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Mon, 5 Jan 2015 11:59:48 -0500 Subject: [PATCH] API: Avoid unstubbing User for language pref when not needed It's fairly common that the API doesn't need to load the user preferences, except to implement the unfortunate default uselang=user. So let's move the handling of uselang=user to RequestContext::getLanguage(), and have the API just assume that its parent context will eventually fall back to that for uselang=user. Bug: T85635 Change-Id: I947348d87b31808d331055dac6feb0cc2e1dd15d --- includes/api/ApiMain.php | 27 ++++++++++++++------------- includes/context/RequestContext.php | 5 ++++- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 1610679318..82ed295696 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -190,19 +190,20 @@ class ApiMain extends ApiBase { $uselang = $this->getParameter( 'uselang' ); if ( $uselang === 'user' ) { - $uselang = $this->getUser()->getOption( 'language' ); - $uselang = RequestContext::sanitizeLangCode( $uselang ); - Hooks::run( 'UserGetLanguageObject', array( $this->getUser(), &$uselang, $this ) ); - } elseif ( $uselang === 'content' ) { - global $wgContLang; - $uselang = $wgContLang->getCode(); - } - $code = RequestContext::sanitizeLangCode( $uselang ); - $this->getContext()->setLanguage( $code ); - if ( !$this->mInternalMode ) { - global $wgLang; - $wgLang = $this->getContext()->getLanguage(); - RequestContext::getMain()->setLanguage( $wgLang ); + // Assume the parent context is going to return the user language + // for uselang=user (see T85635). + } else { + if ( $uselang === 'content' ) { + global $wgContLang; + $uselang = $wgContLang->getCode(); + } + $code = RequestContext::sanitizeLangCode( $uselang ); + $this->getContext()->setLanguage( $code ); + if ( !$this->mInternalMode ) { + global $wgLang; + $wgLang = $this->getContext()->getLanguage(); + RequestContext::getMain()->setLanguage( $wgLang ); + } } $config = $this->getConfig(); diff --git a/includes/context/RequestContext.php b/includes/context/RequestContext.php index 7cd229094d..a0e7f999d2 100644 --- a/includes/context/RequestContext.php +++ b/includes/context/RequestContext.php @@ -318,7 +318,10 @@ class RequestContext implements IContextSource { $request = $this->getRequest(); $user = $this->getUser(); - $code = $request->getVal( 'uselang', $user->getOption( 'language' ) ); + $code = $request->getVal( 'uselang', 'user' ); + if ( $code === 'user' ) { + $code = $user->getOption( 'language' ); + } $code = self::sanitizeLangCode( $code ); Hooks::run( 'UserGetLanguageObject', array( $user, &$code, $this ) ); -- 2.20.1