Add forgotten RELEASE-NOTES line
[lhc/web/wiklou.git] / includes / StubObject.php
1 <?php
2 /**
3 * Delayed loading of global objects.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Class to implement stub globals, which are globals that delay loading the
25 * their associated module code by deferring initialisation until the first
26 * method call.
27 *
28 * Note on reference parameters:
29 *
30 * If the called method takes any parameters by reference, the __call magic
31 * here won't work correctly. The solution is to unstub the object before
32 * calling the method.
33 *
34 * Note on unstub loops:
35 *
36 * Unstub loops (infinite recursion) sometimes occur when a constructor calls
37 * another function, and the other function calls some method of the stub. The
38 * best way to avoid this is to make constructors as lightweight as possible,
39 * deferring any initialisation which depends on other modules. As a last
40 * resort, you can use StubObject::isRealObject() to break the loop, but as a
41 * general rule, the stub object mechanism should be transparent, and code
42 * which refers to it should be kept to a minimum.
43 */
44 class StubObject {
45 var $mGlobal, $mClass, $mParams;
46
47 /**
48 * Constructor.
49 *
50 * @param string $global name of the global variable.
51 * @param string $class name of the class of the real object.
52 * @param array $params parameters to pass to constructor of the real
53 * object.
54 */
55 function __construct( $global = null, $class = null, $params = array() ) {
56 $this->mGlobal = $global;
57 $this->mClass = $class;
58 $this->mParams = $params;
59 }
60
61 /**
62 * Returns a bool value whenever $obj is a stub object. Can be used to break
63 * a infinite loop when unstubbing an object.
64 *
65 * @param $obj Object to check.
66 * @return Boolean: true if $obj is not an instance of StubObject class.
67 */
68 static function isRealObject( $obj ) {
69 return is_object( $obj ) && !$obj instanceof StubObject;
70 }
71
72 /**
73 * Unstubs an object, if it is a stub object. Can be used to break a
74 * infinite loop when unstubbing an object or to avoid reference parameter
75 * breakage.
76 *
77 * @param $obj Object to check.
78 * @return void
79 */
80 static function unstub( $obj ) {
81 if ( $obj instanceof StubObject ) {
82 $obj->_unstub( 'unstub', 3 );
83 }
84 }
85
86 /**
87 * Function called if any function exists with that name in this object.
88 * It is used to unstub the object. Only used internally, PHP will call
89 * self::__call() function and that function will call this function.
90 * This function will also call the function with the same name in the real
91 * object.
92 *
93 * @param string $name name of the function called
94 * @param array $args arguments
95 * @return mixed
96 */
97 function _call( $name, $args ) {
98 $this->_unstub( $name, 5 );
99 return call_user_func_array( array( $GLOBALS[$this->mGlobal], $name ), $args );
100 }
101
102 /**
103 * Create a new object to replace this stub object.
104 * @return object
105 */
106 function _newObject() {
107 return MWFunction::newObj( $this->mClass, $this->mParams );
108 }
109
110 /**
111 * Function called by PHP if no function with that name exists in this
112 * object.
113 *
114 * @param string $name name of the function called
115 * @param array $args arguments
116 * @return mixed
117 */
118 function __call( $name, $args ) {
119 return $this->_call( $name, $args );
120 }
121
122 /**
123 * This function creates a new object of the real class and replace it in
124 * the global variable.
125 * This is public, for the convenience of external callers wishing to access
126 * properties, e.g. eval.php
127 *
128 * @param string $name name of the method called in this object.
129 * @param $level Integer: level to go in the stack trace to get the function
130 * who called this function.
131 * @throws MWException
132 */
133 function _unstub( $name = '_unstub', $level = 2 ) {
134 static $recursionLevel = 0;
135
136 if ( !$GLOBALS[$this->mGlobal] instanceof StubObject ) {
137 return $GLOBALS[$this->mGlobal]; // already unstubbed.
138 }
139
140 if ( get_class( $GLOBALS[$this->mGlobal] ) != $this->mClass ) {
141 $fname = __METHOD__ . '-' . $this->mGlobal;
142 wfProfileIn( $fname );
143 $caller = wfGetCaller( $level );
144 if ( ++$recursionLevel > 2 ) {
145 wfProfileOut( $fname );
146 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
147 }
148 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
149 $GLOBALS[$this->mGlobal] = $this->_newObject();
150 --$recursionLevel;
151 wfProfileOut( $fname );
152 }
153 }
154 }
155
156 /**
157 * Stub object for the content language of this wiki. This object have to be in
158 * $wgContLang global.
159 *
160 * @deprecated since 1.18
161 */
162 class StubContLang extends StubObject {
163
164 function __construct() {
165 wfDeprecated( __CLASS__, '1.18' );
166 parent::__construct( 'wgContLang' );
167 }
168
169 function __call( $name, $args ) {
170 return $this->_call( $name, $args );
171 }
172
173 /**
174 * @return Language
175 */
176 function _newObject() {
177 global $wgLanguageCode;
178 $obj = Language::factory( $wgLanguageCode );
179 $obj->initEncoding();
180 $obj->initContLang();
181 return $obj;
182 }
183 }
184
185 /**
186 * Stub object for the user language. It depends of the user preferences and
187 * "uselang" parameter that can be passed to index.php. This object have to be
188 * in $wgLang global.
189 */
190 class StubUserLang extends StubObject {
191
192 function __construct() {
193 parent::__construct( 'wgLang' );
194 }
195
196 function __call( $name, $args ) {
197 return $this->_call( $name, $args );
198 }
199
200 /**
201 * @return Language
202 */
203 function _newObject() {
204 return RequestContext::getMain()->getLanguage();
205 }
206 }