3 * Group all the pieces relevant to the context of a request into one instance
8 * @author Daniel Friesen
12 class RequestContext
{
13 private $mRequest; // / WebRequest object
14 private $mTitle; // / Title object
15 private $mOutput; // / OutputPage object
16 private $mUser; // / User object
17 private $mLang; // / Language object
18 private $mSkin; // / Skin object
21 * Set the WebRequest object
23 * @param $r WebRequest object
25 public function setRequest( WebRequest
$r ) {
30 * Get the WebRequest object
34 public function getRequest() {
35 if ( !isset( $this->mRequest
) ) {
36 global $wgRequest; # fallback to $wg till we can improve this
37 $this->mRequest
= $wgRequest;
39 return $this->mRequest
;
43 * Set the Title object
45 * @param $t Title object
47 public function setTitle( Title
$t ) {
52 * Get the Title object
56 public function getTitle() {
57 if ( !isset( $this->mTitle
) ) {
58 global $wgTitle; # fallback to $wg till we can improve this
59 $this->mTitle
= $wgTitle;
65 * Get the OutputPage object
67 * @return OutputPage object
69 public function getOutput() {
70 if ( !isset( $this->mOutput
) ) {
71 $this->mOutput
= new OutputPage( $this );
73 return $this->mOutput
;
81 public function setUser( User
$u ) {
90 public function getUser() {
91 if ( !isset( $this->mUser
) ) {
92 global $wgCommandLineMode;
93 $this->mUser
= $wgCommandLineMode
95 : User
::newFromSession( $this->getRequest() );
101 * Get the Language object
105 public function getLang() {
106 if ( !isset( $this->mLang
) ) {
107 global $wgLanguageCode, $wgContLang;
108 $code = $this->getRequest()->getVal(
110 $this->getUser()->getOption( 'language' )
112 // BCP 47 - letter case MUST NOT carry meaning
113 $code = strtolower( $code );
116 if ( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
117 wfDebug( "Invalid user language code\n" );
118 $code = $wgLanguageCode;
121 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
123 if ( $code === $wgLanguageCode ) {
124 $this->mLang
= $wgContLang;
126 $obj = Language
::factory( $code );
134 * Get the Skin object
138 public function getSkin() {
139 if ( !isset( $this->mSkin
) ) {
140 wfProfileIn( __METHOD__
. '-createskin' );
142 global $wgHiddenPrefs;
143 if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
145 $userSkin = $this->getUser()->getOption( 'skin' );
146 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
148 # if we're not allowing users to override, then use the default
149 global $wgDefaultSkin;
150 $userSkin = $wgDefaultSkin;
153 $this->mSkin
= Skin
::newFromKey( $userSkin );
154 $this->mSkin
->setContext( $this );
155 wfProfileOut( __METHOD__
. '-createskin' );
160 /** Helpful methods **/
163 * Get a Message object with context set
164 * Parameters are the same as wfMessage()
166 * @return Message object
168 public function msg() {
169 $args = func_get_args();
170 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
173 /** Static methods **/
176 * Get the RequestContext object associated with the main request
178 * @return RequestContext object
180 public static function getMain() {
181 static $instance = null;
182 if ( !isset( $instance ) ) {
183 $instance = new self
;
189 * Make these C#-style accessors, so you can do $context->user->getName() which is
190 * internally mapped to $context->__get('user')->getName() which is mapped to
191 * $context->getUser()->getName()
193 public function __get( $name ) {
194 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
195 $fname = 'get' . ucfirst( $name );
196 return $this->$fname();
198 trigger_error( "Undefined property {$name}", E_NOTICE
);
201 public function __set( $name, $value ) {
202 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
203 $fname = 'set' . ucfirst( $name );
204 return $this->$fname( $value );
206 trigger_error( "Undefined property {$name}", E_NOTICE
);