API: Avoid unstubbing User for language pref when not needed
authorBrad Jorsch <bjorsch@wikimedia.org>
Mon, 5 Jan 2015 16:59:48 +0000 (11:59 -0500)
committerBrad Jorsch <bjorsch@wikimedia.org>
Mon, 5 Jan 2015 16:59:48 +0000 (11:59 -0500)
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
includes/context/RequestContext.php

index 1610679..82ed295 100644 (file)
@@ -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();
index 7cd2290..a0e7f99 100644 (file)
@@ -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 ) );