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 if ( get_class( $GLOBALS[$this->mGlobal] ) != $this->mClass ) {
00092 $fname = __METHOD__.'-'.$this->mGlobal;
00093 wfProfileIn( $fname );
00094 $caller = wfGetCaller( $level );
00095 if ( ++$recursionLevel > 2 ) {
00096 throw new MWException( "Unstub loop detected on call of \${$this->mGlobal}->$name from $caller\n" );
00097 }
00098 wfDebug( "Unstubbing \${$this->mGlobal} on call of \${$this->mGlobal}::$name from $caller\n" );
00099 $GLOBALS[$this->mGlobal] = $this->_newObject();
00100 --$recursionLevel;
00101 wfProfileOut( $fname );
00102 }
00103 }
00104 }
00105
00110 class StubContLang extends StubObject {
00111
00112 function __construct() {
00113 parent::__construct( 'wgContLang' );
00114 }
00115
00116 function __call( $name, $args ) {
00117 return $this->_call( $name, $args );
00118 }
00119
00120 function _newObject() {
00121 global $wgContLanguageCode;
00122 $obj = Language::factory( $wgContLanguageCode );
00123 $obj->initEncoding();
00124 $obj->initContLang();
00125 return $obj;
00126 }
00127 }
00128
00134 class StubUserLang extends StubObject {
00135
00136 function __construct() {
00137 parent::__construct( 'wgLang' );
00138 }
00139
00140 function __call( $name, $args ) {
00141 return $this->_call( $name, $args );
00142 }
00143
00144 function _newObject() {
00145 global $wgContLanguageCode, $wgRequest, $wgUser, $wgContLang;
00146 $code = $wgRequest->getVal( 'uselang', $wgUser->getOption( 'language' ) );
00147
00148
00149
00150 if( $wgContLang->hasVariants() && in_array($code, $wgContLang->getVariants()) ){
00151 $variant = $wgContLang->getPreferredVariant();
00152 if( $variant != $wgContLanguageCode )
00153 $code = $variant;
00154 }
00155
00156 # Validate $code
00157 if( empty( $code ) || !preg_match( '/^[a-z-]+$/', $code ) || ( $code === 'qqq' ) ) {
00158 wfDebug( "Invalid user language code\n" );
00159 $code = $wgContLanguageCode;
00160 }
00161
00162 if( $code === $wgContLanguageCode ) {
00163 return $wgContLang;
00164 } else {
00165 $obj = Language::factory( $code );
00166 return $obj;
00167 }
00168 }
00169 }
00170
00177 class StubUser extends StubObject {
00178
00179 function __construct() {
00180 parent::__construct( 'wgUser' );
00181 }
00182
00183 function __call( $name, $args ) {
00184 return $this->_call( $name, $args );
00185 }
00186
00187 function _newObject() {
00188 global $wgCommandLineMode;
00189 if( $wgCommandLineMode ) {
00190 $user = new User;
00191 } else {
00192 $user = User::newFromSession();
00193 }
00194 return $user;
00195 }
00196 }