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 static function _getCaller( $level ) {
31 $backtrace = debug_backtrace();
32 if ( isset( $backtrace[$level] ) ) {
33 if ( isset( $backtrace[$level]['class'] ) ) {
34 $caller = $backtrace[$level]['class'] . '::' . $backtrace[$level]['function'];
36 $caller = $backtrace[$level]['function'];
44 function _call( $name, $args ) {
45 $this->_unstub( $name, 5 );
46 return call_user_func_array( array( $GLOBALS[$this->mGlobal
], $name ), $args );
49 function _newObject() {
50 return wfCreateObject( $this->mClass
, $this->mParams
);
53 function __call( $name, $args ) {
54 return $this->_call( $name, $args );
58 * This is public, for the convenience of external callers wishing to access
59 * properties, e.g. eval.php
61 function _unstub( $name = '_unstub', $level = 2 ) {
62 static $recursionLevel = 0;
63 if ( get_class( $GLOBALS[$this->mGlobal
] ) != $this->mClass
) {
64 $fname = __METHOD__
.'-'.$this->mGlobal
;
65 wfProfileIn( $fname );
66 $caller = self
::_getCaller( $level );
67 if ( ++
$recursionLevel > 2 ) {
68 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
70 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}->$name from $caller\n" );
71 $GLOBALS[$this->mGlobal
] = $this->_newObject();
73 wfProfileOut( $fname );
78 class StubContLang
extends StubObject
{
79 function __construct() {
80 parent
::__construct( 'wgContLang' );
83 function __call( $name, $args ) {
84 return StubObject
::_call( $name, $args );
87 function _newObject() {
88 global $wgContLanguageCode;
89 $obj = Language
::factory( $wgContLanguageCode );
95 class StubUserLang
extends StubObject
{
96 function __construct() {
97 parent
::__construct( 'wgLang' );
100 function __call( $name, $args ) {
101 return $this->_call( $name, $args );
104 function _newObject() {
105 global $wgLanguageCode, $wgContLanguageCode, $wgRequest, $wgUser, $wgContLang;
106 // wgLanguageCode now specifically means the UI language
107 $wgLanguageCode = $wgRequest->getText('uselang', '');
108 if ($wgLanguageCode == '')
109 $wgLanguageCode = $wgUser->getOption('language');
110 # Validate $wgLanguageCode
111 if( empty( $wgLanguageCode ) ||
!preg_match( '/^[a-z]+(-[a-z]+)?$/', $wgLanguageCode ) ) {
112 $wgLanguageCode = $wgContLanguageCode;
115 if( $wgLanguageCode == $wgContLanguageCode ) {
118 $obj = Language
::factory( $wgLanguageCode );
123 class StubUser
extends StubObject
{
124 function __construct() {
125 parent
::__construct( 'wgUser' );
128 function __call( $name, $args ) {
129 return $this->_call( $name, $args );
132 function _newObject() {
133 global $wgCommandLineMode;
134 if( $wgCommandLineMode ) {
136 $user->setLoaded( true );
138 $user = User
::loadFromSession();