* Fix regression in authentication hook auto-creation on login
[mediawiki.git] / includes / StubObject.php
blob4068c31c3b3e946108bfd911539b411cccb083c2
1 <?php
3 /**
4 * Class to implement stub globals, which are globals that delay loading the
5 * their associated module code by deferring initialisation until the first
6 * method call.
8 * Note on unstub loops:
10 * Unstub loops (infinite recursion) sometimes occur when a constructor calls
11 * another function, and the other function calls some method of the stub. The
12 * best way to avoid this is to make constructors as lightweight as possible,
13 * deferring any initialisation which depends on other modules. As a last
14 * resort, you can use StubObject::isRealObject() to break the loop, but as a
15 * general rule, the stub object mechanism should be transparent, and code
16 * which refers to it should be kept to a minimum.
18 class StubObject {
19 var $mGlobal, $mClass, $mParams;
20 function __construct( $global = null, $class = null, $params = array() ) {
21 $this->mGlobal = $global;
22 $this->mClass = $class;
23 $this->mParams = $params;
26 static function isRealObject( $obj ) {
27 return is_object( $obj ) && !is_a( $obj, 'StubObject' );
30 function _call( $name, $args ) {
31 $this->_unstub( $name, 5 );
32 return call_user_func_array( array( $GLOBALS[$this->mGlobal], $name ), $args );
35 function _newObject() {
36 return wfCreateObject( $this->mClass, $this->mParams );
39 function __call( $name, $args ) {
40 return $this->_call( $name, $args );
43 /**
44 * This is public, for the convenience of external callers wishing to access
45 * properties, e.g. eval.php
47 function _unstub( $name = '_unstub', $level = 2 ) {
48 static $recursionLevel = 0;
49 if ( get_class( $GLOBALS[$this->mGlobal] ) != $this->mClass ) {
50 $fname = __METHOD__.'-'.$this->mGlobal;
51 wfProfileIn( $fname );
52 $caller = wfGetCaller( $level );
53 if ( ++$recursionLevel > 2 ) {
54 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
56 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}->$name from $caller\n" );
57 $GLOBALS[$this->mGlobal] = $this->_newObject();
58 --$recursionLevel;
59 wfProfileOut( $fname );
64 class StubContLang extends StubObject {
65 function __construct() {
66 parent::__construct( 'wgContLang' );
69 function __call( $name, $args ) {
70 return StubObject::_call( $name, $args );
73 function _newObject() {
74 global $wgContLanguageCode;
75 $obj = Language::factory( $wgContLanguageCode );
76 $obj->initEncoding();
77 $obj->initContLang();
78 return $obj;
81 class StubUserLang extends StubObject {
82 function __construct() {
83 parent::__construct( 'wgLang' );
86 function __call( $name, $args ) {
87 return $this->_call( $name, $args );
90 function _newObject() {
91 global $wgContLanguageCode, $wgRequest, $wgUser, $wgContLang;
92 $code = $wgRequest->getVal('uselang', '');
93 if ($code == '')
94 $code = $wgUser->getOption('language');
96 // if variant is explicitely selected, use it instead the one from wgUser
97 // see bug #7605
98 if($wgContLang->hasVariants()){
99 $variant = $wgContLang->getPreferredVariant(false);
100 if($variant != $wgContLanguageCode)
101 $code = $variant;
104 # Validate $code
105 if( empty( $code ) || !preg_match( '/^[a-z]+(-[a-z]+)?$/', $code ) ) {
106 $code = $wgContLanguageCode;
109 if( $code == $wgContLanguageCode ) {
110 return $wgContLang;
111 } else {
112 $obj = Language::factory( $code );
113 return $obj;
117 class StubUser extends StubObject {
118 function __construct() {
119 parent::__construct( 'wgUser' );
122 function __call( $name, $args ) {
123 return $this->_call( $name, $args );
126 function _newObject() {
127 global $wgCommandLineMode;
128 if( $wgCommandLineMode ) {
129 $user = new User;
130 } else {
131 $user = User::newFromSession();
132 wfRunHooks('AutoAuthenticate',array(&$user));
134 return $user;