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 unstub loops:
30 * Unstub loops (infinite recursion) sometimes occur when a constructor calls
31 * another function, and the other function calls some method of the stub. The
32 * best way to avoid this is to make constructors as lightweight as possible,
33 * deferring any initialisation which depends on other modules. As a last
34 * resort, you can use StubObject::isRealObject() to break the loop, but as a
35 * general rule, the stub object mechanism should be transparent, and code
36 * which refers to it should be kept to a minimum.
39 var $mGlobal, $mClass, $mParams;
44 * @param $global String: name of the global variable.
45 * @param $class String: name of the class of the real object.
46 * @param $params Array: parameters to pass to contructor of the real
49 function __construct( $global = null, $class = null, $params = array() ) {
50 $this->mGlobal
= $global;
51 $this->mClass
= $class;
52 $this->mParams
= $params;
56 * Returns a bool value whetever $obj is a stub object. Can be used to break
57 * a infinite loop when unstubbing an object.
59 * @param $obj Object to check.
60 * @return Boolean: true if $obj is not an instance of StubObject class.
62 static function isRealObject( $obj ) {
63 return is_object( $obj ) && !($obj instanceof StubObject
);
67 * Function called if any function exists with that name in this object.
68 * It is used to unstub the object. Only used internally, PHP will call
69 * self::__call() function and that function will call this function.
70 * This function will also call the function with the same name in the real
73 * @param $name String: name of the function called
74 * @param $args Array: arguments
77 function _call( $name, $args ) {
78 $this->_unstub( $name, 5 );
79 return call_user_func_array( array( $GLOBALS[$this->mGlobal
], $name ), $args );
83 * Create a new object to replace this stub object.
86 function _newObject() {
87 return MWFunction
::newObj( $this->mClass
, $this->mParams
);
91 * Function called by PHP if no function with that name exists in this
94 * @param $name String: name of the function called
95 * @param $args Array: arguments
98 function __call( $name, $args ) {
99 return $this->_call( $name, $args );
103 * This function creates a new object of the real class and replace it in
104 * the global variable.
105 * This is public, for the convenience of external callers wishing to access
106 * properties, e.g. eval.php
108 * @param $name String: name of the method called in this object.
109 * @param $level Integer: level to go in the stact trace to get the function
110 * who called this function.
112 function _unstub( $name = '_unstub', $level = 2 ) {
113 static $recursionLevel = 0;
115 if ( !($GLOBALS[$this->mGlobal
] instanceof StubObject
) ) {
116 return $GLOBALS[$this->mGlobal
]; // already unstubbed.
119 if ( get_class( $GLOBALS[$this->mGlobal
] ) != $this->mClass
) {
120 $fname = __METHOD__
.'-'.$this->mGlobal
;
121 wfProfileIn( $fname );
122 $caller = wfGetCaller( $level );
123 if ( ++
$recursionLevel > 2 ) {
124 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
126 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
127 $GLOBALS[$this->mGlobal
] = $this->_newObject();
129 wfProfileOut( $fname );
135 * Stub object for the content language of this wiki. This object have to be in
136 * $wgContLang global.
138 * @deprecated since 1.18
140 class StubContLang
extends StubObject
{
142 function __construct() {
143 wfDeprecated( __CLASS__
, '1.18' );
144 parent
::__construct( 'wgContLang' );
147 function __call( $name, $args ) {
148 return $this->_call( $name, $args );
154 function _newObject() {
155 global $wgLanguageCode;
156 $obj = Language
::factory( $wgLanguageCode );
157 $obj->initEncoding();
158 $obj->initContLang();
164 * Stub object for the user language. It depends of the user preferences and
165 * "uselang" parameter that can be passed to index.php. This object have to be
168 class StubUserLang
extends StubObject
{
170 function __construct() {
171 parent
::__construct( 'wgLang' );
174 function __call( $name, $args ) {
175 return $this->_call( $name, $args );
181 function _newObject() {
182 return RequestContext
::getMain()->getLanguage();