Reverted r49019, unnecessary use of the ellipsis character, per CR
[mediawiki.git] / includes / StubObject.php
blobe18537e0549c5e3543ffeb2cd9f18d511467c75d
1 <?php
3 /**
4 * Class to implement stub globals, which are globals that delay loading the
5 * their associated module code by deferring initialisation until the first
6 * method call.
8 * Note on unstub loops:
10 * Unstub loops (infinite recursion) sometimes occur when a constructor calls
11 * another function, and the other function calls some method of the stub. The
12 * best way to avoid this is to make constructors as lightweight as possible,
13 * deferring any initialisation which depends on other modules. As a last
14 * resort, you can use StubObject::isRealObject() to break the loop, but as a
15 * general rule, the stub object mechanism should be transparent, and code
16 * which refers to it should be kept to a minimum.
18 class StubObject {
19 var $mGlobal, $mClass, $mParams;
21 /**
22 * Constructor.
24 * @param String $global name of the global variable.
25 * @param String $class name of the class of the real object.
26 * @param Array $param array of parameters to pass to contructor of the real
27 * object.
29 function __construct( $global = null, $class = null, $params = array() ) {
30 $this->mGlobal = $global;
31 $this->mClass = $class;
32 $this->mParams = $params;
35 /**
36 * Returns a bool value whetever $obj is a stub object. Can be used to break
37 * a infinite loop when unstubbing an object.
39 * @param Object $obj object to check.
40 * @return bool true if $obj is not an instance of StubObject class.
42 static function isRealObject( $obj ) {
43 return is_object( $obj ) && !($obj instanceof StubObject);
46 /**
47 * Function called if any function exists with that name in this object.
48 * It is used to unstub the object. Only used internally, PHP will call
49 * self::__call() function and that function will call this function.
50 * This function will also call the function with the same name in the real
51 * object.
53 * @param String $name name of the function called.
54 * @param Array $args array of arguments.
56 function _call( $name, $args ) {
57 $this->_unstub( $name, 5 );
58 return call_user_func_array( array( $GLOBALS[$this->mGlobal], $name ), $args );
61 /**
62 * Create a new object to replace this stub object.
64 function _newObject() {
65 return wfCreateObject( $this->mClass, $this->mParams );
68 /**
69 * Function called by PHP if no function with that name exists in this
70 * object.
72 * @param String $name name of the function called
73 * @param Array $args array of arguments
75 function __call( $name, $args ) {
76 return $this->_call( $name, $args );
79 /**
80 * This function creates a new object of the real class and replace it in
81 * the global variable.
82 * This is public, for the convenience of external callers wishing to access
83 * properties, e.g. eval.php
85 * @param String $name name of the method called in this object.
86 * @param Integer $level level to go in the stact trace to get the function
87 * who called this function.
89 function _unstub( $name = '_unstub', $level = 2 ) {
90 static $recursionLevel = 0;
91 if ( get_class( $GLOBALS[$this->mGlobal] ) != $this->mClass ) {
92 $fname = __METHOD__.'-'.$this->mGlobal;
93 wfProfileIn( $fname );
94 $caller = wfGetCaller( $level );
95 if ( ++$recursionLevel > 2 ) {
96 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
98 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
99 $GLOBALS[$this->mGlobal] = $this->_newObject();
100 --$recursionLevel;
101 wfProfileOut( $fname );
107 * Stub object for the content language of this wiki. This object have to be in
108 * $wgContLang global.
110 class StubContLang extends StubObject {
112 function __construct() {
113 parent::__construct( 'wgContLang' );
116 function __call( $name, $args ) {
117 return $this->_call( $name, $args );
120 function _newObject() {
121 global $wgContLanguageCode;
122 $obj = Language::factory( $wgContLanguageCode );
123 $obj->initEncoding();
124 $obj->initContLang();
125 return $obj;
130 * Stub object for the user language. It depends of the user preferences and
131 * "uselang" parameter that can be passed to index.php. This object have to be
132 * in $wgLang global.
134 class StubUserLang extends StubObject {
136 function __construct() {
137 parent::__construct( 'wgLang' );
140 function __call( $name, $args ) {
141 return $this->_call( $name, $args );
144 function _newObject() {
145 global $wgContLanguageCode, $wgRequest, $wgUser, $wgContLang;
146 $code = $wgRequest->getVal( 'uselang', $wgUser->getOption( 'language' ) );
148 # Validate $code
149 if( empty( $code ) || !preg_match( '/^[a-z-]+$/', $code ) || ( $code === 'qqq' ) ) {
150 wfDebug( "Invalid user language code\n" );
151 $code = $wgContLanguageCode;
154 if( $code === $wgContLanguageCode ) {
155 return $wgContLang;
156 } else {
157 $obj = Language::factory( $code );
158 return $obj;
164 * Stub object for the user variant. It depends of the user preferences and
165 * "variant" parameter that can be passed to index.php. This object have to be
166 * in $wgVariant global.
168 class StubUserVariant extends StubObject {
170 function __construct() {
171 parent::__construct( 'wgVariant' );
174 function __call( $name, $args ) {
175 return $this->_call( $name, $args );
178 function _newObject() {
179 global $wgContLanguageCode, $wgRequest, $wgUser, $wgContLang;
181 if( $wgContLang->hasVariants() ) {
182 $code = $wgRequest->getVal( 'variant', $wgUser->getOption( 'variant' ) );
183 } else {
184 $code = $wgRequest->getVal( 'variant', $wgUser->getOption( 'language' ) );
187 // if variant is explicitely selected, use it instead the one from wgUser
188 // see bug #7605
189 if( $wgContLang->hasVariants() && in_array($code, $wgContLang->getVariants()) ){
190 $variant = $wgContLang->getPreferredVariant();
191 if( $variant != $wgContLanguageCode )
192 $code = $variant;
195 # Validate $code
196 if( empty( $code ) || !preg_match( '/^[a-z-]+$/', $code ) || ( $code === 'qqq' ) ) {
197 wfDebug( "Invalid user variant code\n" );
198 $code = $wgContLanguageCode;
201 if( $code === $wgContLanguageCode ) {
202 return $wgContLang;
203 } else {
204 $obj = Language::factory( $code );
205 return $obj;
211 * Stub object for the user. The initialisation of the will depend of
212 * $wgCommandLineMode. If it's true, it will be an anonymous user and if it's
213 * false, the user will be loaded from credidentails provided by cookies. This
214 * object have to be in $wgUser global.
216 class StubUser extends StubObject {
218 function __construct() {
219 parent::__construct( 'wgUser' );
222 function __call( $name, $args ) {
223 return $this->_call( $name, $args );
226 function _newObject() {
227 global $wgCommandLineMode;
228 if( $wgCommandLineMode ) {
229 $user = new User;
230 } else {
231 $user = User::newFromSession();
233 return $user;