Merged localisation-work branch:
[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 $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;
113 }
114
115 if( $wgLanguageCode == $wgContLanguageCode ) {
116 return $wgContLang;
117 } else {
118 $obj = Language::factory( $wgLanguageCode );
119 return $obj;
120 }
121 }
122 }
123 class StubUser extends StubObject {
124 function __construct() {
125 parent::__construct( 'wgUser' );
126 }
127
128 function __call( $name, $args ) {
129 return $this->_call( $name, $args );
130 }
131
132 function _newObject() {
133 global $wgCommandLineMode;
134 if( $wgCommandLineMode ) {
135 $user = new User;
136 $user->setLoaded( true );
137 } else {
138 $user = User::loadFromSession();
139 }
140 return $user;
141 }
142 }
143
144 ?>