Replacing strictEqual-assertion with a more useful QUnit.push that includes the expec...
[lhc/web/wiklou.git] / includes / RequestContext.php
1 <?php
2 /**
3 * Group all the pieces relevant to the context of a request into one instance
4 *
5 * @since 1.18
6 *
7 * @author IAlex
8 * @author Daniel Friesen
9 * @file
10 */
11
12 class RequestContext {
13
14 /**
15 * @var WebRequest
16 */
17 private $mRequest;
18
19 /**
20 * @var Title
21 */
22 private $mTitle;
23
24 /**
25 * @var OutputPage
26 */
27 private $mOutput;
28
29 /**
30 * @var User
31 */
32 private $mUser;
33
34 /**
35 * @var Language
36 */
37 private $mLang;
38
39 /**
40 * @var Skin
41 */
42 private $mSkin;
43
44 /**
45 * Set the WebRequest object
46 *
47 * @param $r WebRequest object
48 */
49 public function setRequest( WebRequest $r ) {
50 $this->mRequest = $r;
51 }
52
53 /**
54 * Get the WebRequest object
55 *
56 * @return WebRequest
57 */
58 public function getRequest() {
59 if ( !isset( $this->mRequest ) ) {
60 global $wgRequest; # fallback to $wg till we can improve this
61 $this->mRequest = $wgRequest;
62 }
63 return $this->mRequest;
64 }
65
66 /**
67 * Set the Title object
68 *
69 * @param $t Title object
70 */
71 public function setTitle( Title $t ) {
72 $this->mTitle = $t;
73 }
74
75 /**
76 * Get the Title object
77 *
78 * @return Title
79 */
80 public function getTitle() {
81 if ( !isset( $this->mTitle ) ) {
82 global $wgTitle; # fallback to $wg till we can improve this
83 $this->mTitle = $wgTitle;
84 }
85 return $this->mTitle;
86 }
87
88 /**
89 * Get the OutputPage object
90 *
91 * @return OutputPage object
92 */
93 public function getOutput() {
94 if ( !isset( $this->mOutput ) ) {
95 $this->mOutput = new OutputPage( $this );
96 }
97 return $this->mOutput;
98 }
99
100 /**
101 * Set the User object
102 *
103 * @param $u User
104 */
105 public function setUser( User $u ) {
106 $this->mUser = $u;
107 }
108
109 /**
110 * Get the User object
111 *
112 * @return User
113 */
114 public function getUser() {
115 if ( !isset( $this->mUser ) ) {
116 global $wgCommandLineMode;
117 $this->mUser = $wgCommandLineMode
118 ? new User
119 : User::newFromSession( $this->getRequest() );
120 }
121 return $this->mUser;
122 }
123
124 /**
125 * Get the Language object
126 *
127 * @return Language
128 */
129 public function getLang() {
130 if ( !isset( $this->mLang ) ) {
131 global $wgLanguageCode, $wgContLang;
132 $code = $this->getRequest()->getVal(
133 'uselang',
134 $this->getUser()->getOption( 'language' )
135 );
136 // BCP 47 - letter case MUST NOT carry meaning
137 $code = strtolower( $code );
138
139 # Validate $code
140 if ( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
141 wfDebug( "Invalid user language code\n" );
142 $code = $wgLanguageCode;
143 }
144
145 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
146
147 if ( $code === $wgLanguageCode ) {
148 $this->mLang = $wgContLang;
149 } else {
150 $obj = Language::factory( $code );
151 $this->mLang = $obj;
152 }
153 }
154 return $this->mLang;
155 }
156
157 /**
158 * Get the Skin object
159 *
160 * @return Skin
161 */
162 public function getSkin() {
163 if ( !isset( $this->mSkin ) ) {
164 wfProfileIn( __METHOD__ . '-createskin' );
165
166 global $wgHiddenPrefs;
167 if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
168 # get the user skin
169 $userSkin = $this->getUser()->getOption( 'skin' );
170 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
171 } else {
172 # if we're not allowing users to override, then use the default
173 global $wgDefaultSkin;
174 $userSkin = $wgDefaultSkin;
175 }
176
177 $this->mSkin = Skin::newFromKey( $userSkin );
178 $this->mSkin->setContext( $this );
179 wfProfileOut( __METHOD__ . '-createskin' );
180 }
181 return $this->mSkin;
182 }
183
184 /** Helpful methods **/
185
186 /**
187 * Get a Message object with context set
188 * Parameters are the same as wfMessage()
189 *
190 * @return Message object
191 */
192 public function msg() {
193 $args = func_get_args();
194 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
195 }
196
197 /** Static methods **/
198
199 /**
200 * Get the RequestContext object associated with the main request
201 *
202 * @return RequestContext object
203 */
204 public static function getMain() {
205 static $instance = null;
206 if ( !isset( $instance ) ) {
207 $instance = new self;
208 }
209 return $instance;
210 }
211
212 /**
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()
216 *
217 * @param $name string
218 *
219 * @return string
220 */
221 public function __get( $name ) {
222 wfDeprecated( 'RequestContext::__get() is deprecated; use $context->getFoo() instead' );
223 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
224 $fname = 'get' . ucfirst( $name );
225 return $this->$fname();
226 }
227 trigger_error( "Undefined property {$name}", E_NOTICE );
228 }
229
230 /**
231 * @param $name string
232 * @param $value
233 * @return string
234 */
235 public function __set( $name, $value ) {
236 wfDeprecated( 'RequestContext::__set() is deprecated; use $context->setFoo() instead' );
237 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
238 $fname = 'set' . ucfirst( $name );
239 return $this->$fname( $value );
240 }
241 trigger_error( "Undefined property {$name}", E_NOTICE );
242 }
243 }
244