* Group all the pieces relevant to the context of a request into one instance
*/
class RequestContext implements IContextSource {
-
/**
* @var WebRequest
*/
/**
* Set the WebRequest object
*
- * @param $r WebRequest object
+ * @param WebRequest $r
*/
public function setRequest( WebRequest $r ) {
$this->request = $r;
/**
* Set the Title object
*
- * @param $t Title object
+ * @param Title $t
*/
public function setTitle( Title $t ) {
$this->title = $t;
+ // Erase the WikiPage so a new one with the new title gets created.
+ $this->wikipage = null;
}
/**
* Set the WikiPage object
*
* @since 1.19
- * @param $p WikiPage object
+ * @param WikiPage $p
*/
public function setWikiPage( WikiPage $p ) {
+ $contextTitle = $this->getTitle();
+ $pageTitle = $p->getTitle();
+ if ( !$contextTitle || !$pageTitle->equals( $contextTitle ) ) {
+ $this->setTitle( $pageTitle );
+ }
+ // Defer this to the end since setTitle sets it to null.
$this->wikipage = $p;
}
/**
* Get the OutputPage object
*
- * @return OutputPage object
+ * @return OutputPage
*/
public function getOutput() {
if ( $this->output === null ) {
/**
* Set the User object
*
- * @param $u User
+ * @param User $u
*/
public function setUser( User $u ) {
$this->user = $u;
/**
* Accepts a language code and ensures it's sane. Outputs a cleaned up language
* code and replaces with $wgLanguageCode if not sane.
- * @param $code string
+ * @param string $code Language code
* @return string
*/
public static function sanitizeLangCode( $code ) {
$code = strtolower( $code );
# Validate $code
- if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
+ if ( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
wfDebug( "Invalid user language code\n" );
$code = $wgLanguageCode;
}
* Set the Language object
*
* @deprecated 1.19 Use setLanguage instead
- * @param $l Mixed Language instance or language code
+ * @param Language|string $l Language instance or language code
*/
public function setLang( $l ) {
wfDeprecated( __METHOD__, '1.19' );
/**
* Set the Language object
*
- * @param $l Mixed Language instance or language code
+ * @param Language|string $l Language instance or language code
* @throws MWException
* @since 1.19
*/
}
/**
- * Get the Language object
+ * Get the Language object.
+ * Initialization of user or request objects can depend on this.
*
* @return Language
* @since 1.19
*/
public function getLanguage() {
+ if ( isset( $this->recursion ) ) {
+ throw new MWException( 'Recursion detected' );
+ }
+
if ( $this->lang === null ) {
+ $this->recursion = true;
+
global $wgLanguageCode, $wgContLang;
- $code = $this->getRequest()->getVal(
- 'uselang',
- $this->getUser()->getOption( 'language' )
- );
+
+ $request = $this->getRequest();
+ $user = $this->getUser();
+
+ $code = $request->getVal( 'uselang', $user->getOption( 'language' ) );
$code = self::sanitizeLangCode( $code );
- wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
+ wfRunHooks( 'UserGetLanguageObject', array( $user, &$code, $this ) );
- if( $code === $wgLanguageCode ) {
+ if ( $code === $wgLanguageCode ) {
$this->lang = $wgContLang;
} else {
$obj = Language::factory( $code );
$this->lang = $obj;
}
+
+ unset( $this->recursion );
}
+
return $this->lang;
}
/**
* Set the Skin object
*
- * @param $s Skin
+ * @param Skin $s
*/
public function setSkin( Skin $s ) {
$this->skin = clone $s;
// If the hook worked try to set a skin from it
if ( $skin instanceof Skin ) {
$this->skin = $skin;
- } elseif ( is_string($skin) ) {
+ } elseif ( is_string( $skin ) ) {
$this->skin = Skin::newFromKey( $skin );
}
// then go through the normal processing to load a skin
if ( $this->skin === null ) {
global $wgHiddenPrefs;
- if( !in_array( 'skin', $wgHiddenPrefs ) ) {
+ if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
# get the user skin
$userSkin = $this->getUser()->getOption( 'skin' );
$userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
* Get a Message object with context set
* Parameters are the same as wfMessage()
*
- * @return Message object
+ * @return Message
*/
public function msg() {
$args = func_get_args();
/**
* Get the RequestContext object associated with the main request
*
- * @return RequestContext object
+ * @return RequestContext
*/
public static function getMain() {
static $instance = null;
* language or a uselang param in the fauxrequest data may change the lang
* - Skin will be based on the anonymous user, should be the wiki's default skin
*
- * @param $title Title Title to use for the extraneous request
- * @param $request Mixed A WebRequest or data to use for a FauxRequest
+ * @param Title $title Title to use for the extraneous request
+ * @param WebRequest|array $request A WebRequest or data to use for a FauxRequest
* @return RequestContext
*/
- public static function newExtraneousContext( Title $title, $request=array() ) {
+ public static function newExtraneousContext( Title $title, $request = array() ) {
$context = new self;
$context->setTitle( $title );
if ( $request instanceof WebRequest ) {
$context->user = User::newFromName( '127.0.0.1', false );
return $context;
}
-
}