MediaWiki
master
|
00001 <?php 00030 class RequestContext implements IContextSource { 00031 00035 private $request; 00036 00040 private $title; 00041 00045 private $wikipage; 00046 00050 private $output; 00051 00055 private $user; 00056 00060 private $lang; 00061 00065 private $skin; 00066 00072 public function setRequest( WebRequest $r ) { 00073 $this->request = $r; 00074 } 00075 00081 public function getRequest() { 00082 if ( $this->request === null ) { 00083 global $wgRequest; # fallback to $wg till we can improve this 00084 $this->request = $wgRequest; 00085 } 00086 return $this->request; 00087 } 00088 00094 public function setTitle( Title $t ) { 00095 $this->title = $t; 00096 // Erase the WikiPage so a new one with the new title gets created. 00097 $this->wikipage = null; 00098 } 00099 00105 public function getTitle() { 00106 if ( $this->title === null ) { 00107 global $wgTitle; # fallback to $wg till we can improve this 00108 $this->title = $wgTitle; 00109 } 00110 return $this->title; 00111 } 00112 00121 public function canUseWikiPage() { 00122 if ( $this->wikipage !== null ) { 00123 # If there's a WikiPage object set, we can for sure get it 00124 return true; 00125 } 00126 $title = $this->getTitle(); 00127 if ( $title === null ) { 00128 # No Title, no WikiPage 00129 return false; 00130 } else { 00131 # Only namespaces whose pages are stored in the database can have WikiPage 00132 return $title->canExist(); 00133 } 00134 } 00135 00142 public function setWikiPage( WikiPage $p ) { 00143 $contextTitle = $this->getTitle(); 00144 $pageTitle = $p->getTitle(); 00145 if ( !$contextTitle || !$pageTitle->equals( $contextTitle ) ) { 00146 $this->setTitle( $pageTitle ); 00147 } 00148 // Defer this to the end since setTitle sets it to null. 00149 $this->wikipage = $p; 00150 } 00151 00162 public function getWikiPage() { 00163 if ( $this->wikipage === null ) { 00164 $title = $this->getTitle(); 00165 if ( $title === null ) { 00166 throw new MWException( __METHOD__ . ' called without Title object set' ); 00167 } 00168 $this->wikipage = WikiPage::factory( $title ); 00169 } 00170 return $this->wikipage; 00171 } 00172 00176 public function setOutput( OutputPage $o ) { 00177 $this->output = $o; 00178 } 00179 00185 public function getOutput() { 00186 if ( $this->output === null ) { 00187 $this->output = new OutputPage( $this ); 00188 } 00189 return $this->output; 00190 } 00191 00197 public function setUser( User $u ) { 00198 $this->user = $u; 00199 } 00200 00206 public function getUser() { 00207 if ( $this->user === null ) { 00208 $this->user = User::newFromSession( $this->getRequest() ); 00209 } 00210 return $this->user; 00211 } 00212 00219 public static function sanitizeLangCode( $code ) { 00220 global $wgLanguageCode; 00221 00222 // BCP 47 - letter case MUST NOT carry meaning 00223 $code = strtolower( $code ); 00224 00225 # Validate $code 00226 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) { 00227 wfDebug( "Invalid user language code\n" ); 00228 $code = $wgLanguageCode; 00229 } 00230 00231 return $code; 00232 } 00233 00240 public function setLang( $l ) { 00241 wfDeprecated( __METHOD__, '1.19' ); 00242 $this->setLanguage( $l ); 00243 } 00244 00252 public function setLanguage( $l ) { 00253 if ( $l instanceof Language ) { 00254 $this->lang = $l; 00255 } elseif ( is_string( $l ) ) { 00256 $l = self::sanitizeLangCode( $l ); 00257 $obj = Language::factory( $l ); 00258 $this->lang = $obj; 00259 } else { 00260 throw new MWException( __METHOD__ . " was passed an invalid type of data." ); 00261 } 00262 } 00263 00268 public function getLang() { 00269 wfDeprecated( __METHOD__, '1.19' ); 00270 return $this->getLanguage(); 00271 } 00272 00279 public function getLanguage() { 00280 if ( $this->lang === null ) { 00281 global $wgLanguageCode, $wgContLang; 00282 $code = $this->getRequest()->getVal( 00283 'uselang', 00284 $this->getUser()->getOption( 'language' ) 00285 ); 00286 $code = self::sanitizeLangCode( $code ); 00287 00288 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) ); 00289 00290 if( $code === $wgLanguageCode ) { 00291 $this->lang = $wgContLang; 00292 } else { 00293 $obj = Language::factory( $code ); 00294 $this->lang = $obj; 00295 } 00296 } 00297 return $this->lang; 00298 } 00299 00305 public function setSkin( Skin $s ) { 00306 $this->skin = clone $s; 00307 $this->skin->setContext( $this ); 00308 } 00309 00315 public function getSkin() { 00316 if ( $this->skin === null ) { 00317 wfProfileIn( __METHOD__ . '-createskin' ); 00318 00319 $skin = null; 00320 wfRunHooks( 'RequestContextCreateSkin', array( $this, &$skin ) ); 00321 00322 // If the hook worked try to set a skin from it 00323 if ( $skin instanceof Skin ) { 00324 $this->skin = $skin; 00325 } elseif ( is_string($skin) ) { 00326 $this->skin = Skin::newFromKey( $skin ); 00327 } 00328 00329 // If this is still null (the hook didn't run or didn't work) 00330 // then go through the normal processing to load a skin 00331 if ( $this->skin === null ) { 00332 global $wgHiddenPrefs; 00333 if( !in_array( 'skin', $wgHiddenPrefs ) ) { 00334 # get the user skin 00335 $userSkin = $this->getUser()->getOption( 'skin' ); 00336 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin ); 00337 } else { 00338 # if we're not allowing users to override, then use the default 00339 global $wgDefaultSkin; 00340 $userSkin = $wgDefaultSkin; 00341 } 00342 00343 $this->skin = Skin::newFromKey( $userSkin ); 00344 } 00345 00346 // After all that set a context on whatever skin got created 00347 $this->skin->setContext( $this ); 00348 wfProfileOut( __METHOD__ . '-createskin' ); 00349 } 00350 return $this->skin; 00351 } 00352 00361 public function msg() { 00362 $args = func_get_args(); 00363 return call_user_func_array( 'wfMessage', $args )->setContext( $this ); 00364 } 00365 00373 public static function getMain() { 00374 static $instance = null; 00375 if ( $instance === null ) { 00376 $instance = new self; 00377 } 00378 return $instance; 00379 } 00380 00395 public static function newExtraneousContext( Title $title, $request=array() ) { 00396 $context = new self; 00397 $context->setTitle( $title ); 00398 if ( $request instanceof WebRequest ) { 00399 $context->setRequest( $request ); 00400 } else { 00401 $context->setRequest( new FauxRequest( $request ) ); 00402 } 00403 $context->user = User::newFromName( '127.0.0.1', false ); 00404 return $context; 00405 } 00406 00407 }