$wgLanguageCode no longer indicates the user language, for that use $wgLang->getCode...
[lhc/web/wiklou.git] / includes / StubObject.php
1 <?php
2
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.
7 *
8 * Note on unstub loops:
9 *
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.
17 */
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;
24 }
25
26 static function isRealObject( $obj ) {
27 return is_object( $obj ) && !is_a( $obj, 'StubObject' );
28 }
29
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'];
35 } else {
36 $caller = $backtrace[$level]['function'];
37 }
38 } else {
39 $caller = 'unknown';
40 }
41 return $caller;
42 }
43
44 function _call( $name, $args ) {
45 $this->_unstub( $name, 5 );
46 return call_user_func_array( array( $GLOBALS[$this->mGlobal], $name ), $args );
47 }
48
49 function _newObject() {
50 return wfCreateObject( $this->mClass, $this->mParams );
51 }
52
53 function __call( $name, $args ) {
54 return $this->_call( $name, $args );
55 }
56
57 /**
58 * This is public, for the convenience of external callers wishing to access
59 * properties, e.g. eval.php
60 */
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" );
69 }
70 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}->$name from $caller\n" );
71 $GLOBALS[$this->mGlobal] = $this->_newObject();
72 --$recursionLevel;
73 wfProfileOut( $fname );
74 }
75 }
76 }
77
78 class StubContLang extends StubObject {
79 function __construct() {
80 parent::__construct( 'wgContLang' );
81 }
82
83 function __call( $name, $args ) {
84 return StubObject::_call( $name, $args );
85 }
86
87 function _newObject() {
88 global $wgContLanguageCode;
89 $obj = Language::factory( $wgContLanguageCode );
90 $obj->initEncoding();
91 $obj->initContLang();
92 return $obj;
93 }
94 }
95 class StubUserLang extends StubObject {
96 function __construct() {
97 parent::__construct( 'wgLang' );
98 }
99
100 function __call( $name, $args ) {
101 return $this->_call( $name, $args );
102 }
103
104 function _newObject() {
105 global $wgContLanguageCode, $wgRequest, $wgUser, $wgContLang;
106 $code = $wgRequest->getText('uselang', '');
107 if ($code == '')
108 $code = $wgUser->getOption('language');
109 # Validate $code
110 if( empty( $code ) || !preg_match( '/^[a-z]+(-[a-z]+)?$/', $code ) ) {
111 $code = $wgContLanguageCode;
112 }
113
114 if( $code == $wgContLanguageCode ) {
115 return $wgContLang;
116 } else {
117 $obj = Language::factory( $code );
118 return $obj;
119 }
120 }
121 }
122 class StubUser extends StubObject {
123 function __construct() {
124 parent::__construct( 'wgUser' );
125 }
126
127 function __call( $name, $args ) {
128 return $this->_call( $name, $args );
129 }
130
131 function _newObject() {
132 global $wgCommandLineMode;
133 if( $wgCommandLineMode ) {
134 $user = new User;
135 $user->setLoaded( true );
136 } else {
137 $user = User::loadFromSession();
138 }
139 return $user;
140 }
141 }
142
143 ?>