b06b021d9d51876dadbfbacc9745396722e8a622
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 * Get the OutputPage object
91 * @return OutputPage object
93 public function getOutput() {
94 if ( !isset( $this->mOutput
) ) {
95 $this->mOutput
= new OutputPage( $this );
97 return $this->mOutput
;
101 * Set the User object
105 public function setUser( User
$u ) {
110 * Get the User object
114 public function getUser() {
115 if ( !isset( $this->mUser
) ) {
116 global $wgCommandLineMode;
117 $this->mUser
= $wgCommandLineMode
119 : User
::newFromSession( $this->getRequest() );
125 * Get the Language object
129 public function getLang() {
130 if ( !isset( $this->mLang
) ) {
131 global $wgLanguageCode, $wgContLang;
132 $code = $this->getRequest()->getVal(
134 $this->getUser()->getOption( 'language' )
136 // BCP 47 - letter case MUST NOT carry meaning
137 $code = strtolower( $code );
140 if ( empty( $code ) ||
!Language
::isValidCode( $code ) ||
( $code === 'qqq' ) ) {
141 wfDebug( "Invalid user language code\n" );
142 $code = $wgLanguageCode;
145 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
147 if ( $code === $wgLanguageCode ) {
148 $this->mLang
= $wgContLang;
150 $obj = Language
::factory( $code );
158 * Get the Skin object
162 public function getSkin() {
163 if ( !isset( $this->mSkin
) ) {
164 wfProfileIn( __METHOD__
. '-createskin' );
166 global $wgHiddenPrefs;
167 if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
169 $userSkin = $this->getUser()->getOption( 'skin' );
170 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
172 # if we're not allowing users to override, then use the default
173 global $wgDefaultSkin;
174 $userSkin = $wgDefaultSkin;
177 $this->mSkin
= Skin
::newFromKey( $userSkin );
178 $this->mSkin
->setContext( $this );
179 wfProfileOut( __METHOD__
. '-createskin' );
184 /** Helpful methods **/
187 * Get a Message object with context set
188 * Parameters are the same as wfMessage()
190 * @return Message object
192 public function msg() {
193 $args = func_get_args();
194 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
197 /** Static methods **/
200 * Get the RequestContext object associated with the main request
202 * @return RequestContext object
204 public static function getMain() {
205 static $instance = null;
206 if ( !isset( $instance ) ) {
207 $instance = new self
;
213 * Make these C#-style accessors, so you can do $context->user->getName() which is
214 * internally mapped to $context->__get('user')->getName() which is mapped to
215 * $context->getUser()->getName()
217 * @param $name string
221 public function __get( $name ) {
222 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
223 $fname = 'get' . ucfirst( $name );
224 return $this->$fname();
226 trigger_error( "Undefined property {$name}", E_NOTICE
);
230 * @param $name string
234 public function __set( $name, $value ) {
235 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
236 $fname = 'set' . ucfirst( $name );
237 return $this->$fname( $value );
239 trigger_error( "Undefined property {$name}", E_NOTICE
);