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 * Set the OutputPage object
65 * @param $u OutputPage
67 public function setOutput( OutputPage
$o ) {
72 * Get the OutputPage object
74 * @return OutputPage object
76 public function getOutput() {
77 if ( !isset( $this->mOutput
) ) {
78 $this->mOutput
= new OutputPage
;
79 $this->mOutput
->setContext( $this );
81 return $this->mOutput
;
89 public function setUser( User
$u ) {
98 public function getUser() {
99 if ( !isset( $this->mUser
) ) {
100 global $wgCommandLineMode;
101 $this->mUser
= $wgCommandLineMode
103 : User
::newFromSession( $this->getRequest() );
109 * Get the Language object
113 public function getLang() {
114 if ( !isset( $this->mLang
) ) {
115 global $wgLanguageCode, $wgContLang;
116 $code = $this->getRequest()->getVal(
118 $this->getUser()->getOption( 'language' )
120 // BCP 47 - letter case MUST NOT carry meaning
121 $code = strtolower( $code );
124 if ( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
125 wfDebug( "Invalid user language code\n" );
126 $code = $wgLanguageCode;
129 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
131 if ( $code === $wgLanguageCode ) {
132 $this->mLang
= $wgContLang;
134 $obj = Language
::factory( $code );
142 * Get the Skin object
146 public function getSkin() {
147 if ( !isset( $this->mSkin
) ) {
148 wfProfileIn( __METHOD__
. '-createskin' );
150 global $wgHiddenPrefs;
151 if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
153 $userSkin = $this->getUser()->getOption( 'skin' );
154 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
156 # if we're not allowing users to override, then use the default
157 global $wgDefaultSkin;
158 $userSkin = $wgDefaultSkin;
161 $this->mSkin
= Skin
::newFromKey( $userSkin );
162 $this->mSkin
->setContext( $this );
163 wfProfileOut( __METHOD__
. '-createskin' );
168 /** Helpful methods **/
171 * Get a Message object with context set
172 * Parameters are the same as wfMessage()
174 * @return Message object
176 public function msg() {
177 $args = func_get_args();
178 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
181 /** Static methods **/
184 * Get the RequestContext object associated with the main request
186 * @return RequestContext object
188 public static function getMain() {
189 static $instance = null;
190 if ( !isset( $instance ) ) {
191 $instance = new self
;
197 * Make these C#-style accessors, so you can do $context->user->getName() which is
198 * internally mapped to $context->__get('user')->getName() which is mapped to
199 * $context->getUser()->getName()
201 public function __get( $name ) {
202 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
203 $fname = 'get' . ucfirst( $name );
204 return $this->$fname();
206 trigger_error( "Undefined property {$name}", E_NOTICE
);
209 public function __set( $name, $value ) {
210 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
211 $fname = 'set' . ucfirst( $name );
212 return $this->$fname( $value );
214 trigger_error( "Undefined property {$name}", E_NOTICE
);