(bug 44010) Pass context to UserGetLanguageObject
authorNiklas Laxström <niklas.laxstrom@gmail.com>
Wed, 16 Jan 2013 09:47:06 +0000 (09:47 +0000)
committerAlexandre Emsenhuber <ialex.wiki@gmail.com>
Fri, 18 Jan 2013 09:56:27 +0000 (10:56 +0100)
Only way to avoid messing with wrong request contexts.
Had to add recursion guard too.

Change-Id: Idc11b54752450321e01d92004e08fc95fb6170e6

docs/hooks.txt
includes/context/RequestContext.php

index 8905d7e..e182a5a 100644 (file)
@@ -2473,6 +2473,7 @@ $user: User object
 'UserGetLanguageObject': Called when getting user's interface language object.
 $user: User object
 &$code: Langauge code that will be used to create the object
+$context: RequestContext object
 
 'UserGetReservedNames': Allows to modify $wgReservedUsernames at run time.
 &$reservedUsernames: $wgReservedUsernames
index c1ce751..96d27b0 100644 (file)
@@ -270,21 +270,29 @@ class RequestContext implements IContextSource {
        }
 
        /**
-        * Get the Language object
+        * Get the Language object.
+        * Initialization of user or request objects can depend on this.
         *
         * @return Language
         * @since 1.19
         */
        public function getLanguage() {
+               if ( isset( $this->recursion ) ) {
+                       throw new MWException( 'Recursion detected' );
+               }
+
                if ( $this->lang === null ) {
+                       $this->recursion = true;
+
                        global $wgLanguageCode, $wgContLang;
-                       $code = $this->getRequest()->getVal(
-                               'uselang',
-                               $this->getUser()->getOption( 'language' )
-                       );
+
+                       $request = $this->getRequest();
+                       $user = $this->getUser();
+
+                       $code = $request->getVal( 'uselang', $user->getOption( 'language' ) );
                        $code = self::sanitizeLangCode( $code );
 
-                       wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
+                       wfRunHooks( 'UserGetLanguageObject', array( $user, &$code, $this ) );
 
                        if ( $code === $wgLanguageCode ) {
                                $this->lang = $wgContLang;
@@ -292,7 +300,10 @@ class RequestContext implements IContextSource {
                                $obj = Language::factory( $code );
                                $this->lang = $obj;
                        }
+
+                       unset( $this->recursion );
                }
+
                return $this->lang;
        }
 
@@ -388,7 +399,7 @@ class RequestContext implements IContextSource {
         * - Skin will be based on the anonymous user, should be the wiki's default skin
         *
         * @param Title $title Title to use for the extraneous request
-        * @param mixed $request A WebRequest or data to use for a FauxRequest
+        * @param WebRequest|array $request A WebRequest or data to use for a FauxRequest
         * @return RequestContext
         */
        public static function newExtraneousContext( Title $title, $request = array() ) {