Non-word characters don't terminate tag names.
[mediawiki.git] / includes / StubObject.php
blob59238faa901c520cca8a62004837840be6455e1f
1 <?php
2 /**
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
20 * @file
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.
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.
38 class StubObject {
39 var $mGlobal, $mClass, $mParams;
41 /**
42 * Constructor.
44 * @param string $global name of the global variable.
45 * @param string $class name of the class of the real object.
46 * @param array $params parameters to pass to constructor of the real
47 * object.
49 function __construct( $global = null, $class = null, $params = array() ) {
50 $this->mGlobal = $global;
51 $this->mClass = $class;
52 $this->mParams = $params;
55 /**
56 * Returns a bool value whenever $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;
66 /**
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
71 * object.
73 * @param string $name name of the function called
74 * @param array $args arguments
75 * @return mixed
77 function _call( $name, $args ) {
78 $this->_unstub( $name, 5 );
79 return call_user_func_array( array( $GLOBALS[$this->mGlobal], $name ), $args );
82 /**
83 * Create a new object to replace this stub object.
84 * @return object
86 function _newObject() {
87 return MWFunction::newObj( $this->mClass, $this->mParams );
90 /**
91 * Function called by PHP if no function with that name exists in this
92 * object.
94 * @param string $name name of the function called
95 * @param array $args arguments
96 * @return mixed
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 string $name name of the method called in this object.
109 * @param $level Integer: level to go in the stack trace to get the function
110 * who called this function.
111 * @throws MWException
113 function _unstub( $name = '_unstub', $level = 2 ) {
114 static $recursionLevel = 0;
116 if ( !$GLOBALS[$this->mGlobal] instanceof StubObject ) {
117 return $GLOBALS[$this->mGlobal]; // already unstubbed.
120 if ( get_class( $GLOBALS[$this->mGlobal] ) != $this->mClass ) {
121 $fname = __METHOD__ . '-' . $this->mGlobal;
122 wfProfileIn( $fname );
123 $caller = wfGetCaller( $level );
124 if ( ++$recursionLevel > 2 ) {
125 wfProfileOut( $fname );
126 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
128 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
129 $GLOBALS[$this->mGlobal] = $this->_newObject();
130 --$recursionLevel;
131 wfProfileOut( $fname );
137 * Stub object for the content language of this wiki. This object have to be in
138 * $wgContLang global.
140 * @deprecated since 1.18
142 class StubContLang extends StubObject {
144 function __construct() {
145 wfDeprecated( __CLASS__, '1.18' );
146 parent::__construct( 'wgContLang' );
149 function __call( $name, $args ) {
150 return $this->_call( $name, $args );
154 * @return Language
156 function _newObject() {
157 global $wgLanguageCode;
158 $obj = Language::factory( $wgLanguageCode );
159 $obj->initEncoding();
160 $obj->initContLang();
161 return $obj;
166 * Stub object for the user language. It depends of the user preferences and
167 * "uselang" parameter that can be passed to index.php. This object have to be
168 * in $wgLang global.
170 class StubUserLang extends StubObject {
172 function __construct() {
173 parent::__construct( 'wgLang' );
176 function __call( $name, $args ) {
177 return $this->_call( $name, $args );
181 * @return Language
183 function _newObject() {
184 return RequestContext::getMain()->getLanguage();