testPngNativetZtxt requires zlib extension
[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 unstub loops:
29 *
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.
37 */
38 class StubObject {
39 var $mGlobal, $mClass, $mParams;
40
41 /**
42 * Constructor.
43 *
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
47 * object.
48 */
49 function __construct( $global = null, $class = null, $params = array() ) {
50 $this->mGlobal = $global;
51 $this->mClass = $class;
52 $this->mParams = $params;
53 }
54
55 /**
56 * Returns a bool value whetever $obj is a stub object. Can be used to break
57 * a infinite loop when unstubbing an object.
58 *
59 * @param $obj Object to check.
60 * @return Boolean: true if $obj is not an instance of StubObject class.
61 */
62 static function isRealObject( $obj ) {
63 return is_object( $obj ) && !($obj instanceof StubObject);
64 }
65
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.
72 *
73 * @param $name String: name of the function called
74 * @param $args Array: arguments
75 * @return mixed
76 */
77 function _call( $name, $args ) {
78 $this->_unstub( $name, 5 );
79 return call_user_func_array( array( $GLOBALS[$this->mGlobal], $name ), $args );
80 }
81
82 /**
83 * Create a new object to replace this stub object.
84 * @return object
85 */
86 function _newObject() {
87 return MWFunction::newObj( $this->mClass, $this->mParams );
88 }
89
90 /**
91 * Function called by PHP if no function with that name exists in this
92 * object.
93 *
94 * @param $name String: name of the function called
95 * @param $args Array: arguments
96 * @return mixed
97 */
98 function __call( $name, $args ) {
99 return $this->_call( $name, $args );
100 }
101
102 /**
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
107 *
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.
111 * @throws MWException
112 */
113 function _unstub( $name = '_unstub', $level = 2 ) {
114 static $recursionLevel = 0;
115
116 if ( !($GLOBALS[$this->mGlobal] instanceof StubObject) ) {
117 return $GLOBALS[$this->mGlobal]; // already unstubbed.
118 }
119
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 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
126 }
127 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
128 $GLOBALS[$this->mGlobal] = $this->_newObject();
129 --$recursionLevel;
130 wfProfileOut( $fname );
131 }
132 }
133 }
134
135 /**
136 * Stub object for the content language of this wiki. This object have to be in
137 * $wgContLang global.
138 *
139 * @deprecated since 1.18
140 */
141 class StubContLang extends StubObject {
142
143 function __construct() {
144 wfDeprecated( __CLASS__, '1.18' );
145 parent::__construct( 'wgContLang' );
146 }
147
148 function __call( $name, $args ) {
149 return $this->_call( $name, $args );
150 }
151
152 /**
153 * @return Language
154 */
155 function _newObject() {
156 global $wgLanguageCode;
157 $obj = Language::factory( $wgLanguageCode );
158 $obj->initEncoding();
159 $obj->initContLang();
160 return $obj;
161 }
162 }
163
164 /**
165 * Stub object for the user language. It depends of the user preferences and
166 * "uselang" parameter that can be passed to index.php. This object have to be
167 * in $wgLang global.
168 */
169 class StubUserLang extends StubObject {
170
171 function __construct() {
172 parent::__construct( 'wgLang' );
173 }
174
175 function __call( $name, $args ) {
176 return $this->_call( $name, $args );
177 }
178
179 /**
180 * @return Language
181 */
182 function _newObject() {
183 return RequestContext::getMain()->getLanguage();
184 }
185 }