MediaWiki  REL1_19
RequestContext.php
Go to the documentation of this file.
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         }
00097 
00103         public function getTitle() {
00104                 if ( $this->title === null ) {
00105                         global $wgTitle; # fallback to $wg till we can improve this
00106                         $this->title = $wgTitle;
00107                 }
00108                 return $this->title;
00109         }
00110 
00119         public function canUseWikiPage() {
00120                 if ( $this->wikipage !== null ) {
00121                         # If there's a WikiPage object set, we can for sure get it
00122                         return true;
00123                 }
00124                 $title = $this->getTitle();
00125                 if ( $title === null ) {
00126                         # No Title, no WikiPage
00127                         return false;
00128                 } else {
00129                         # Only namespaces whose pages are stored in the database can have WikiPage
00130                         return $title->canExist();
00131                 }
00132         }
00133 
00140         public function setWikiPage( WikiPage $p ) {
00141                 $this->wikipage = $p;
00142         }
00143 
00153         public function getWikiPage() {
00154                 if ( $this->wikipage === null ) {
00155                         $title = $this->getTitle();
00156                         if ( $title === null ) {
00157                                 throw new MWException( __METHOD__ . ' called without Title object set' );
00158                         }
00159                         $this->wikipage = WikiPage::factory( $title );
00160                 }
00161                 return $this->wikipage;
00162         }
00163 
00167         public function setOutput( OutputPage $o ) {
00168                 $this->output = $o;
00169         }
00170 
00176         public function getOutput() {
00177                 if ( $this->output === null ) {
00178                         $this->output = new OutputPage( $this );
00179                 }
00180                 return $this->output;
00181         }
00182 
00188         public function setUser( User $u ) {
00189                 $this->user = $u;
00190         }
00191 
00197         public function getUser() {
00198                 if ( $this->user === null ) {
00199                         $this->user = User::newFromSession( $this->getRequest() );
00200                 }
00201                 return $this->user;
00202         }
00203 
00210         public static function sanitizeLangCode( $code ) {
00211                 global $wgLanguageCode;
00212 
00213                 // BCP 47 - letter case MUST NOT carry meaning
00214                 $code = strtolower( $code );
00215 
00216                 # Validate $code
00217                 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
00218                         wfDebug( "Invalid user language code\n" );
00219                         $code = $wgLanguageCode;
00220                 }
00221 
00222                 return $code;
00223         }
00224 
00231         public function setLang( $l ) {
00232                 wfDeprecated( __METHOD__, '1.19' );
00233                 $this->setLanguage( $l );
00234         }
00235 
00242         public function setLanguage( $l ) {
00243                 if ( $l instanceof Language ) {
00244                         $this->lang = $l;
00245                 } elseif ( is_string( $l ) ) {
00246                         $l = self::sanitizeLangCode( $l );
00247                         $obj = Language::factory( $l );
00248                         $this->lang = $obj;
00249                 } else {
00250                         throw new MWException( __METHOD__ . " was passed an invalid type of data." );
00251                 }
00252         }
00253 
00258         public function getLang() {
00259                 wfDeprecated( __METHOD__, '1.19' );
00260                 return $this->getLanguage();
00261         }
00262 
00270         public function getLanguage() {
00271                 if ( isset( $this->recursion ) ) {
00272                         trigger_error( "Recursion detected in " . __METHOD__, E_USER_WARNING );
00273                         $e = new Exception;
00274                         wfDebugLog( 'recursion-guard', "Recursion detected:\n" . $e->getTraceAsString() );
00275 
00276                         global $wgLanguageCode;
00277                         $code = ( $wgLanguageCode ) ? $wgLanguageCode : 'en';
00278                         $this->lang = Language::factory( $code );
00279                 } elseif ( $this->lang === null ) {
00280                         $this->recursion = true;
00281 
00282                         global $wgLanguageCode, $wgContLang;
00283 
00284                         $request = $this->getRequest();
00285                         $user = $this->getUser();
00286 
00287                         $code = $request->getVal( 'uselang', $user->getOption( 'language' ) );
00288                         $code = self::sanitizeLangCode( $code );
00289 
00290                         wfRunHooks( 'UserGetLanguageObject', array( $user, &$code, $this ) );
00291 
00292                         if( $code === $wgLanguageCode ) {
00293                                 $this->lang = $wgContLang;
00294                         } else {
00295                                 $obj = Language::factory( $code );
00296                                 $this->lang = $obj;
00297                         }
00298 
00299                         unset( $this->recursion );
00300                 }
00301 
00302                 return $this->lang;
00303         }
00304 
00310         public function setSkin( Skin $s ) {
00311                 $this->skin = clone $s;
00312                 $this->skin->setContext( $this );
00313         }
00314 
00320         public function getSkin() {
00321                 if ( $this->skin === null ) {
00322                         wfProfileIn( __METHOD__ . '-createskin' );
00323                         
00324                         $skin = null;
00325                         wfRunHooks( 'RequestContextCreateSkin', array( $this, &$skin ) );
00326 
00327                         // If the hook worked try to set a skin from it
00328                         if ( $skin instanceof Skin ) {
00329                                 $this->skin = $skin;
00330                         } elseif ( is_string($skin) ) {
00331                                 $this->skin = Skin::newFromKey( $skin );
00332                         }
00333 
00334                         // If this is still null (the hook didn't run or didn't work)
00335                         // then go through the normal processing to load a skin
00336                         if ( $this->skin === null ) {
00337                                 global $wgHiddenPrefs;
00338                                 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
00339                                         # get the user skin
00340                                         $userSkin = $this->getUser()->getOption( 'skin' );
00341                                         $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
00342                                 } else {
00343                                         # if we're not allowing users to override, then use the default
00344                                         global $wgDefaultSkin;
00345                                         $userSkin = $wgDefaultSkin;
00346                                 }
00347 
00348                                 $this->skin = Skin::newFromKey( $userSkin );
00349                         }
00350 
00351                         // After all that set a context on whatever skin got created
00352                         $this->skin->setContext( $this );
00353                         wfProfileOut( __METHOD__ . '-createskin' );
00354                 }
00355                 return $this->skin;
00356         }
00357 
00366         public function msg() {
00367                 $args = func_get_args();
00368                 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
00369         }
00370 
00378         public static function getMain() {
00379                 static $instance = null;
00380                 if ( $instance === null ) {
00381                         $instance = new self;
00382                 }
00383                 return $instance;
00384         }
00385 
00400         public static function newExtraneousContext( Title $title, $request=array() ) {
00401                 $context = new self;
00402                 $context->setTitle( $title );
00403                 if ( $request instanceof WebRequest ) {
00404                         $context->setRequest( $request );
00405                 } else {
00406                         $context->setRequest( new FauxRequest( $request ) );
00407                 }
00408                 $context->user = User::newFromName( '127.0.0.1', false );
00409                 return $context;
00410         }
00411 
00412 }
00413