00001 <?php
00002
00018 class StubObject {
00019 var $mGlobal, $mClass, $mParams;
00020
00029 function __construct( $global = null, $class = null, $params = array() ) {
00030 $this->mGlobal = $global;
00031 $this->mClass = $class;
00032 $this->mParams = $params;
00033 }
00034
00042 static function isRealObject( $obj ) {
00043 return is_object( $obj ) && !($obj instanceof StubObject);
00044 }
00045
00056 function _call( $name, $args ) {
00057 $this->_unstub( $name, 5 );
00058 return call_user_func_array( array( $GLOBALS[$this->mGlobal], $name ), $args );
00059 }
00060
00064 function _newObject() {
00065 return wfCreateObject( $this->mClass, $this->mParams );
00066 }
00067
00075 function __call( $name, $args ) {
00076 return $this->_call( $name, $args );
00077 }
00078
00089 function _unstub( $name = '_unstub', $level = 2 ) {
00090 static $recursionLevel = 0;
00091
00092 if ( !($GLOBALS[$this->mGlobal] instanceof StubObject) )
00093 return $GLOBALS[$this->mGlobal];
00094
00095 if ( get_class( $GLOBALS[$this->mGlobal] ) != $this->mClass ) {
00096 $fname = __METHOD__.'-'.$this->mGlobal;
00097 wfProfileIn( $fname );
00098 $caller = wfGetCaller( $level );
00099 if ( ++$recursionLevel > 2 ) {
00100 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
00101 }
00102 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
00103 $obj = $GLOBALS[$this->mGlobal] = $this->_newObject();
00104 --$recursionLevel;
00105 wfProfileOut( $fname );
00106 }
00107 }
00108 }
00109
00114 class StubContLang extends StubObject {
00115
00116 function __construct() {
00117 parent::__construct( 'wgContLang' );
00118 }
00119
00120 function __call( $name, $args ) {
00121 return $this->_call( $name, $args );
00122 }
00123
00124 function _newObject() {
00125 global $wgContLanguageCode;
00126 $obj = Language::factory( $wgContLanguageCode );
00127 $obj->initEncoding();
00128 $obj->initContLang();
00129 return $obj;
00130 }
00131 }
00132
00138 class StubUserLang extends StubObject {
00139
00140 function __construct() {
00141 parent::__construct( 'wgLang' );
00142 }
00143
00144 function __call( $name, $args ) {
00145 return $this->_call( $name, $args );
00146 }
00147
00148 function _newObject() {
00149 global $wgContLanguageCode, $wgRequest, $wgUser, $wgContLang;
00150 $code = $wgRequest->getVal( 'uselang', $wgUser->getOption( 'language' ) );
00151
00152 $code = strtolower( $code );
00153
00154 # Validate $code
00155 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
00156 wfDebug( "Invalid user language code\n" );
00157 $code = $wgContLanguageCode;
00158 }
00159
00160 if( $code === $wgContLanguageCode ) {
00161 return $wgContLang;
00162 } else {
00163 $obj = Language::factory( $code );
00164 return $obj;
00165 }
00166 }
00167 }
00168
00175 class StubUser extends StubObject {
00176
00177 function __construct() {
00178 parent::__construct( 'wgUser' );
00179 }
00180
00181 function __call( $name, $args ) {
00182 return $this->_call( $name, $args );
00183 }
00184
00185 function _newObject() {
00186 global $wgCommandLineMode;
00187 if( $wgCommandLineMode ) {
00188 $user = new User;
00189 } else {
00190 $user = User::newFromSession();
00191 }
00192 return $user;
00193 }
00194 }