3 * Group all the pieces relevant to the context of a request into one instance
8 * @author Daniel Friesen
12 class RequestContext
{
45 * Set the WebRequest object
47 * @param $r WebRequest object
49 public function setRequest( WebRequest
$r ) {
54 * Get the WebRequest object
58 public function getRequest() {
59 if ( !isset( $this->mRequest
) ) {
60 global $wgRequest; # fallback to $wg till we can improve this
61 $this->mRequest
= $wgRequest;
63 return $this->mRequest
;
67 * Set the Title object
69 * @param $t Title object
71 public function setTitle( Title
$t ) {
76 * Get the Title object
80 public function getTitle() {
81 if ( !isset( $this->mTitle
) ) {
82 global $wgTitle; # fallback to $wg till we can improve this
83 $this->mTitle
= $wgTitle;
89 * @param $o OutputPage
91 public function setOutput( OutputPage
$o ) {
96 * Get the OutputPage object
98 * @return OutputPage object
100 public function getOutput() {
101 if ( !isset( $this->mOutput
) ) {
102 $this->mOutput
= new OutputPage( $this );
104 return $this->mOutput
;
108 * Set the User object
112 public function setUser( User
$u ) {
117 * Get the User object
121 public function getUser() {
122 if ( !isset( $this->mUser
) ) {
123 global $wgCommandLineMode;
124 $this->mUser
= $wgCommandLineMode
126 : User
::newFromSession( $this->getRequest() );
132 * Get the Language object
136 public function getLang() {
137 if ( !isset( $this->mLang
) ) {
138 global $wgLanguageCode, $wgContLang;
139 $code = $this->getRequest()->getVal(
141 $this->getUser()->getOption( 'language' )
143 // BCP 47 - letter case MUST NOT carry meaning
144 $code = strtolower( $code );
147 if ( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
148 wfDebug( "Invalid user language code\n" );
149 $code = $wgLanguageCode;
152 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
154 if ( $code === $wgLanguageCode ) {
155 $this->mLang
= $wgContLang;
157 $obj = Language
::factory( $code );
165 * Get the Skin object
169 public function getSkin() {
170 if ( !isset( $this->mSkin
) ) {
171 wfProfileIn( __METHOD__
. '-createskin' );
173 global $wgHiddenPrefs;
174 if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
176 $userSkin = $this->getUser()->getOption( 'skin' );
177 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
179 # if we're not allowing users to override, then use the default
180 global $wgDefaultSkin;
181 $userSkin = $wgDefaultSkin;
184 $this->mSkin
= Skin
::newFromKey( $userSkin );
185 $this->mSkin
->setContext( $this );
186 wfProfileOut( __METHOD__
. '-createskin' );
191 /** Helpful methods **/
194 * Get a Message object with context set
195 * Parameters are the same as wfMessage()
197 * @return Message object
199 public function msg() {
200 $args = func_get_args();
201 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
204 /** Static methods **/
207 * Get the RequestContext object associated with the main request
209 * @return RequestContext object
211 public static function getMain() {
212 static $instance = null;
213 if ( !isset( $instance ) ) {
214 $instance = new self
;
220 * Make these C#-style accessors, so you can do $context->user->getName() which is
221 * internally mapped to $context->__get('user')->getName() which is mapped to
222 * $context->getUser()->getName()
224 * @param $name string
228 public function __get( $name ) {
229 wfDeprecated( 'RequestContext::__get() is deprecated; use $context->getFoo() instead' );
230 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
231 $fname = 'get' . ucfirst( $name );
232 return $this->$fname();
234 trigger_error( "Undefined property {$name}", E_NOTICE
);
238 * @param $name string
242 public function __set( $name, $value ) {
243 wfDeprecated( 'RequestContext::__set() is deprecated; use $context->setFoo() instead' );
244 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
245 $fname = 'set' . ucfirst( $name );
246 return $this->$fname( $value );
248 trigger_error( "Undefined property {$name}", E_NOTICE
);