erghhhh
[lhc/web/wiklou.git] / includes / StubObject.php
1 <?php
2
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.
7 *
8 * Note on unstub loops:
9 *
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.
17 */
18 class StubObject {
19 var $mGlobal, $mClass, $mParams;
20 function __construct( $global = null, $class = null, $params = array() ) {
21 $this->mGlobal = $global;
22 $this->mClass = $class;
23 $this->mParams = $params;
24 }
25
26 static function isRealObject( $obj ) {
27 return is_object( $obj ) && !is_a( $obj, 'StubObject' );
28 }
29
30 function _call( $name, $args ) {
31 $this->_unstub( $name, 5 );
32 return call_user_func_array( array( $GLOBALS[$this->mGlobal], $name ), $args );
33 }
34
35 function _newObject() {
36 return wfCreateObject( $this->mClass, $this->mParams );
37 }
38
39 function __call( $name, $args ) {
40 return $this->_call( $name, $args );
41 }
42
43 /**
44 * This is public, for the convenience of external callers wishing to access
45 * properties, e.g. eval.php
46 */
47 function _unstub( $name = '_unstub', $level = 2 ) {
48 static $recursionLevel = 0;
49 if ( get_class( $GLOBALS[$this->mGlobal] ) != $this->mClass ) {
50 $fname = __METHOD__.'-'.$this->mGlobal;
51 wfProfileIn( $fname );
52 $caller = wfGetCaller( $level );
53 if ( ++$recursionLevel > 2 ) {
54 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
55 }
56 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}->$name from $caller\n" );
57 $GLOBALS[$this->mGlobal] = $this->_newObject();
58 --$recursionLevel;
59 wfProfileOut( $fname );
60 }
61 }
62 }
63
64 class StubContLang extends StubObject {
65 function __construct() {
66 parent::__construct( 'wgContLang' );
67 }
68
69 function __call( $name, $args ) {
70 return StubObject::_call( $name, $args );
71 }
72
73 function _newObject() {
74 global $wgContLanguageCode;
75 $obj = Language::factory( $wgContLanguageCode );
76 $obj->initEncoding();
77 $obj->initContLang();
78 return $obj;
79 }
80 }
81 class StubUserLang extends StubObject {
82 function __construct() {
83 parent::__construct( 'wgLang' );
84 }
85
86 function __call( $name, $args ) {
87 return $this->_call( $name, $args );
88 }
89
90 function _newObject() {
91 global $wgContLanguageCode, $wgRequest, $wgUser, $wgContLang;
92 $code = $wgRequest->getText('uselang', '');
93 if ($code == '')
94 $code = $wgUser->getOption('language');
95 # Validate $code
96 if( empty( $code ) || !preg_match( '/^[a-z]+(-[a-z]+)?$/', $code ) ) {
97 $code = $wgContLanguageCode;
98 }
99
100 if( $code == $wgContLanguageCode ) {
101 return $wgContLang;
102 } else {
103 $obj = Language::factory( $code );
104 return $obj;
105 }
106 }
107 }
108 class StubUser extends StubObject {
109 function __construct() {
110 parent::__construct( 'wgUser' );
111 }
112
113 function __call( $name, $args ) {
114 return $this->_call( $name, $args );
115 }
116
117 function _newObject() {
118 global $wgCommandLineMode;
119 if( $wgCommandLineMode ) {
120 $user = new User;
121 $user->setLoaded( true );
122 } else {
123 $user = User::loadFromSession();
124 }
125 return $user;
126 }
127 }
128
129 ?>