3 * Group all the pieces relevant to the context of a request into one instance
6 * @author Daniel Friesen
10 class RequestContext
{
11 private $mRequest; // / WebRequest object
12 private $mTitle; // / Title object
13 private $mOutput; // / OutputPage object
14 private $mUser; // / User object
15 private $mLang; // / Language object
16 private $mSkin; // / Skin object
19 * Set the WebRequest object
21 * @param $r WebRequest object
23 public function setRequest( WebRequest
$r ) {
28 * Get the WebRequest object
32 public function getRequest() {
33 if ( !isset( $this->mRequest
) ) {
34 global $wgRequest; # fallback to $wg till we can improve this
35 $this->mRequest
= $wgRequest;
37 return $this->mRequest
;
41 * Set the Title object
43 * @param $t Title object
45 public function setTitle( Title
$t ) {
50 * Get the Title object
54 public function getTitle() {
55 if ( !isset( $this->mTitle
) ) {
56 global $wgTitle; # fallback to $wg till we can improve this
57 $this->mTitle
= $wgTitle;
63 * Get the OutputPage object
65 * @return OutputPage object
67 public function getOutput() {
68 if ( !isset( $this->mOutput
) ) {
69 $this->mOutput
= new OutputPage
;
70 $this->mOutput
->setContext( $this );
72 return $this->mOutput
;
80 public function setUser( User
$u ) {
89 public function getUser() {
90 if ( !isset( $this->mUser
) ) {
91 global $wgCommandLineMode;
92 $this->mUser
= $wgCommandLineMode
94 : User
::newFromSession( $this->getRequest() );
100 * Get the Language object
104 public function getLang() {
105 if ( !isset( $this->mLang
) ) {
106 global $wgLanguageCode, $wgContLang;
107 $code = $this->getRequest()->getVal(
109 $this->getUser()->getOption( 'language' )
111 // BCP 47 - letter case MUST NOT carry meaning
112 $code = strtolower( $code );
115 if ( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
116 wfDebug( "Invalid user language code\n" );
117 $code = $wgLanguageCode;
120 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
122 if ( $code === $wgLanguageCode ) {
123 $this->mLang
= $wgContLang;
125 $obj = Language
::factory( $code );
133 * Get the Skin object
137 public function getSkin() {
138 if ( !isset( $this->mSkin
) ) {
139 wfProfileIn( __METHOD__
. '-createskin' );
141 global $wgHiddenPrefs;
142 if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
144 $userSkin = $this->getUser()->getOption( 'skin' );
145 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
147 # if we're not allowing users to override, then use the default
148 global $wgDefaultSkin;
149 $userSkin = $wgDefaultSkin;
152 $this->mSkin
= Skin
::newFromKey( $userSkin );
153 $this->mSkin
->setContext( $this );
154 wfProfileOut( __METHOD__
. '-createskin' );
159 /** Helpful methods **/
162 * Get a Message object with context set
163 * Parameters are the same as wfMessage()
165 * @return Message object
167 public function msg() {
168 $args = func_get_args();
169 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
172 /** Static methods **/
175 * Get the RequestContext object associated with the main request
177 * @return RequestContext object
179 public static function getMain() {
180 static $instance = null;
181 if ( !isset( $instance ) ) {
182 $instance = new self
;
188 * Make these C#-style accessors, so you can do $context->user->getName() which is
189 * internally mapped to $context->__get('user')->getName() which is mapped to
190 * $context->getUser()->getName()
192 public function __get( $name ) {
193 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
194 $fname = 'get' . ucfirst( $name );
195 return $this->$fname();
197 trigger_error( "Undefined property {$name}", E_NOTICE
);
200 public function __set( $name, $value ) {
201 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
202 $fname = 'set' . ucfirst( $name );
203 return $this->$fname( $value );
205 trigger_error( "Undefined property {$name}", E_NOTICE
);