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( $this );
71 return $this->mOutput
;
79 public function setUser( User
$u ) {
88 public function getUser() {
89 if ( !isset( $this->mUser
) ) {
90 global $wgCommandLineMode;
91 $this->mUser
= $wgCommandLineMode
93 : User
::newFromSession( $this->getRequest() );
99 * Get the Language object
103 public function getLang() {
104 if ( !isset( $this->mLang
) ) {
105 global $wgLanguageCode, $wgContLang;
106 $code = $this->getRequest()->getVal(
108 $this->getUser()->getOption( 'language' )
110 // BCP 47 - letter case MUST NOT carry meaning
111 $code = strtolower( $code );
114 if ( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
115 wfDebug( "Invalid user language code\n" );
116 $code = $wgLanguageCode;
119 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
121 if ( $code === $wgLanguageCode ) {
122 $this->mLang
= $wgContLang;
124 $obj = Language
::factory( $code );
132 * Get the Skin object
136 public function getSkin() {
137 if ( !isset( $this->mSkin
) ) {
138 wfProfileIn( __METHOD__
. '-createskin' );
140 global $wgHiddenPrefs;
141 if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
143 $userSkin = $this->getUser()->getOption( 'skin' );
144 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
146 # if we're not allowing users to override, then use the default
147 global $wgDefaultSkin;
148 $userSkin = $wgDefaultSkin;
151 $this->mSkin
= Skin
::newFromKey( $userSkin );
152 $this->mSkin
->setContext( $this );
153 wfProfileOut( __METHOD__
. '-createskin' );
158 /** Helpful methods **/
161 * Get a Message object with context set
162 * Parameters are the same as wfMessage()
164 * @return Message object
166 public function msg() {
167 $args = func_get_args();
168 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
171 /** Static methods **/
174 * Get the RequestContext object associated with the main request
176 * @return RequestContext object
178 public static function getMain() {
179 static $instance = null;
180 if ( !isset( $instance ) ) {
181 $instance = new self
;
187 * Make these C#-style accessors, so you can do $context->user->getName() which is
188 * internally mapped to $context->__get('user')->getName() which is mapped to
189 * $context->getUser()->getName()
191 public function __get( $name ) {
192 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
193 $fname = 'get' . ucfirst( $name );
194 return $this->$fname();
196 trigger_error( "Undefined property {$name}", E_NOTICE
);
199 public function __set( $name, $value ) {
200 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
201 $fname = 'set' . ucfirst( $name );
202 return $this->$fname( $value );
204 trigger_error( "Undefined property {$name}", E_NOTICE
);