3 * Delayed loading of global objects.
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.
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.
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
24 * Class to implement stub globals, which are globals that delay loading the
25 * their associated module code by deferring initialisation until the first
28 * Note on reference parameters:
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
34 * Note on unstub loops:
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.
45 var $mGlobal, $mClass, $mParams;
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 object.
54 function __construct( $global = null, $class = null, $params = array() ) {
55 $this->mGlobal
= $global;
56 $this->mClass
= $class;
57 $this->mParams
= $params;
61 * Returns a bool value whenever $obj is a stub object. Can be used to break
62 * a infinite loop when unstubbing an object.
64 * @param object $obj Object to check.
65 * @return bool True if $obj is not an instance of StubObject class.
67 static function isRealObject( $obj ) {
68 return is_object( $obj ) && !$obj instanceof StubObject
;
72 * Unstubs an object, if it is a stub object. Can be used to break a
73 * infinite loop when unstubbing an object or to avoid reference parameter
76 * @param object $obj Object to check.
79 static function unstub( $obj ) {
80 if ( $obj instanceof StubObject
) {
81 $obj->_unstub( 'unstub', 3 );
86 * Function called if any function exists with that name in this object.
87 * It is used to unstub the object. Only used internally, PHP will call
88 * self::__call() function and that function will call this function.
89 * This function will also call the function with the same name in the real
92 * @param string $name Name of the function called
93 * @param array $args Arguments
96 function _call( $name, $args ) {
97 $this->_unstub( $name, 5 );
98 return call_user_func_array( array( $GLOBALS[$this->mGlobal
], $name ), $args );
102 * Create a new object to replace this stub object.
105 function _newObject() {
106 return MWFunction
::newObj( $this->mClass
, $this->mParams
);
110 * Function called by PHP if no function with that name exists in this
113 * @param string $name Name of the function called
114 * @param array $args Arguments
117 function __call( $name, $args ) {
118 return $this->_call( $name, $args );
122 * This function creates a new object of the real class and replace it in
123 * the global variable.
124 * This is public, for the convenience of external callers wishing to access
125 * properties, e.g. eval.php
127 * @param string $name Name of the method called in this object.
128 * @param int $level Level to go in the stack trace to get the function
129 * who called this function.
130 * @throws MWException
132 function _unstub( $name = '_unstub', $level = 2 ) {
133 static $recursionLevel = 0;
135 if ( !$GLOBALS[$this->mGlobal
] instanceof StubObject
) {
136 return $GLOBALS[$this->mGlobal
]; // already unstubbed.
139 if ( get_class( $GLOBALS[$this->mGlobal
] ) != $this->mClass
) {
140 $fname = __METHOD__
. '-' . $this->mGlobal
;
141 wfProfileIn( $fname );
142 $caller = wfGetCaller( $level );
143 if ( ++
$recursionLevel > 2 ) {
144 wfProfileOut( $fname );
145 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
147 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
148 $GLOBALS[$this->mGlobal
] = $this->_newObject();
150 wfProfileOut( $fname );
156 * Stub object for the content language of this wiki. This object have to be in
157 * $wgContLang global.
159 * @deprecated since 1.18
161 class StubContLang
extends StubObject
{
163 function __construct() {
164 wfDeprecated( __CLASS__
, '1.18' );
165 parent
::__construct( 'wgContLang' );
168 function __call( $name, $args ) {
169 return $this->_call( $name, $args );
175 function _newObject() {
176 global $wgLanguageCode;
177 $obj = Language
::factory( $wgLanguageCode );
178 $obj->initEncoding();
179 $obj->initContLang();
185 * Stub object for the user language. It depends of the user preferences and
186 * "uselang" parameter that can be passed to index.php. This object have to be
189 class StubUserLang
extends StubObject
{
191 function __construct() {
192 parent
::__construct( 'wgLang' );
195 function __call( $name, $args ) {
196 return $this->_call( $name, $args );
202 function _newObject() {
203 return RequestContext
::getMain()->getLanguage();