Unstub $wgLang for PageContentLanguage hook
authorNiklas Laxström <niklas.laxstrom@gmail.com>
Thu, 7 Mar 2019 11:34:26 +0000 (12:34 +0100)
committerNiklas Laxström <niklas.laxstrom@gmail.com>
Thu, 16 May 2019 16:49:53 +0000 (18:49 +0200)
This is to avoid annoying fatal errors when someone annotated their
hook handler to only expect Language objects, but that expectation
is violated due to this code possibly passing StubUserLang to hooks,
some of which may also assign it to $pageLang.

Even with this in place, it is probably a good idea for hook handlers
to refrain from type hinting parameters that are passed by reference
because their types cannot be guaranteed.

Bug: T214358
Change-Id: I88405a8de4b13675eb5a9d11e9ddc87e20a85fb4

docs/hooks.txt
includes/content/ContentHandler.php

index e90468d..1419d0a 100644 (file)
@@ -2448,7 +2448,7 @@ $title: Title object
 &$pageLang: the page content language. Input can be anything (under control of
   hook subscribers), but hooks should return Language objects. Language code
   strings are deprecated.
-$userLang: the user language (Language or StubUserLang object)
+$userLang: the user language (Language object)
 
 'PageContentSave': Before an article is saved.
 $wikiPage: the WikiPage (object) being saved
index cc73dd2..b62737d 100644 (file)
@@ -691,6 +691,10 @@ abstract class ContentHandler {
                        $pageLang = Language::factory( $lang );
                }
 
+               // Simplify hook handlers by only passing objects of one type, in case nothing
+               // else has unstubbed the StubUserLang object by now.
+               StubObject::unstub( $wgLang );
+
                Hooks::run( 'PageContentLanguage', [ $title, &$pageLang, $wgLang ] );
 
                return wfGetLangObj( $pageLang );