Notice: Use of undefined constant GAID_FOR_UPDATE - assumed 'GAID_FOR_UPDATE' in...
[mediawiki.git] / includes / RequestContext.php
blobabbdd4e278e211f10cc959b989dfb43872fe5ec8
1 <?php
2 /**
3 * Group all the pieces relevant to the context of a request into one instance
5 * @since 1.18
7 * @author IAlex
8 * @author Daniel Friesen
9 * @file
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
20 /**
21 * Set the WebRequest object
23 * @param $r WebRequest object
25 public function setRequest( WebRequest $r ) {
26 $this->mRequest = $r;
29 /**
30 * Get the WebRequest object
32 * @return WebRequest
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;
42 /**
43 * Set the Title object
45 * @param $t Title object
47 public function setTitle( Title $t ) {
48 $this->mTitle = $t;
51 /**
52 * Get the Title object
54 * @return Title
56 public function getTitle() {
57 if ( !isset( $this->mTitle ) ) {
58 global $wgTitle; # fallback to $wg till we can improve this
59 $this->mTitle = $wgTitle;
61 return $this->mTitle;
64 /**
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;
76 /**
77 * Set the User object
79 * @param $u User
81 public function setUser( User $u ) {
82 $this->mUser = $u;
85 /**
86 * Get the User object
88 * @return User
90 public function getUser() {
91 if ( !isset( $this->mUser ) ) {
92 global $wgCommandLineMode;
93 $this->mUser = $wgCommandLineMode
94 ? new User
95 : User::newFromSession( $this->getRequest() );
97 return $this->mUser;
101 * Get the Language object
103 * @return Language
105 public function getLang() {
106 if ( !isset( $this->mLang ) ) {
107 global $wgLanguageCode, $wgContLang;
108 $code = $this->getRequest()->getVal(
109 'uselang',
110 $this->getUser()->getOption( 'language' )
112 // BCP 47 - letter case MUST NOT carry meaning
113 $code = strtolower( $code );
115 # Validate $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;
125 } else {
126 $obj = Language::factory( $code );
127 $this->mLang = $obj;
130 return $this->mLang;
134 * Get the Skin object
136 * @return Skin
138 public function getSkin() {
139 if ( !isset( $this->mSkin ) ) {
140 wfProfileIn( __METHOD__ . '-createskin' );
142 global $wgHiddenPrefs;
143 if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
144 # get the user skin
145 $userSkin = $this->getUser()->getOption( 'skin' );
146 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
147 } else {
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' );
157 return $this->mSkin;
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;
185 return $instance;
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 );