4 * Class to implement stub globals, which are globals that delay loading the
5 * their associated module code by deferring initialisation until the first
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.
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 );
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();
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 );
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->getText('uselang', '');
94 $code = $wgUser->getOption('language');
96 if( empty( $code ) ||
!preg_match( '/^[a-z]+(-[a-z]+)?$/', $code ) ) {
97 $code = $wgContLanguageCode;
100 if( $code == $wgContLanguageCode ) {
103 $obj = Language
::factory( $code );
108 class StubUser
extends StubObject
{
109 function __construct() {
110 parent
::__construct( 'wgUser' );
113 function __call( $name, $args ) {
114 return $this->_call( $name, $args );
117 function _newObject() {
118 global $wgCommandLineMode;
119 if( $wgCommandLineMode ) {
121 $user->setLoaded( true );
123 $user = User
::loadFromSession();