r92054 : Remove leftover space
[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 $request;
18
19 /**
20 * @var Title
21 */
22 private $title;
23
24 /**
25 * @var OutputPage
26 */
27 private $output;
28
29 /**
30 * @var User
31 */
32 private $user;
33
34 /**
35 * @var Language
36 */
37 private $lang;
38
39 /**
40 * @var Skin
41 */
42 private $skin;
43
44 /**
45 * Set the WebRequest object
46 *
47 * @param $r WebRequest object
48 */
49 public function setRequest( WebRequest $r ) {
50 $this->request = $r;
51 }
52
53 /**
54 * Get the WebRequest object
55 *
56 * @return WebRequest
57 */
58 public function getRequest() {
59 if ( $this->request === null ) {
60 global $wgRequest; # fallback to $wg till we can improve this
61 $this->request = $wgRequest;
62 }
63 return $this->request;
64 }
65
66 /**
67 * Set the Title object
68 *
69 * @param $t Title object
70 */
71 public function setTitle( Title $t ) {
72 $this->title = $t;
73 }
74
75 /**
76 * Get the Title object
77 *
78 * @return Title
79 */
80 public function getTitle() {
81 if ( $this->title === null ) {
82 global $wgTitle; # fallback to $wg till we can improve this
83 $this->title = $wgTitle;
84 }
85 return $this->title;
86 }
87
88 /**
89 * @param $o OutputPage
90 */
91 public function setOutput( OutputPage $o ) {
92 $this->output = $o;
93 }
94
95 /**
96 * Get the OutputPage object
97 *
98 * @return OutputPage object
99 */
100 public function getOutput() {
101 if ( $this->output === null ) {
102 $this->output = new OutputPage( $this );
103 }
104 return $this->output;
105 }
106
107 /**
108 * Set the User object
109 *
110 * @param $u User
111 */
112 public function setUser( User $u ) {
113 $this->user = $u;
114 }
115
116 /**
117 * Get the User object
118 *
119 * @return User
120 */
121 public function getUser() {
122 if ( $this->user === null ) {
123 $this->user = User::newFromSession( $this->getRequest() );
124 }
125 return $this->user;
126 }
127
128 /**
129 * Set the Language object
130 *
131 * @param $l Language
132 */
133 public function setLang( Language $l ) {
134 $this->lang = $l;
135 }
136
137 /**
138 * Get the Language object
139 *
140 * @return Language
141 */
142 public function getLang() {
143 if ( $this->lang === null ) {
144 global $wgLanguageCode, $wgContLang;
145 $code = $this->getRequest()->getVal(
146 'uselang',
147 $this->getUser()->getOption( 'language' )
148 );
149 // BCP 47 - letter case MUST NOT carry meaning
150 $code = strtolower( $code );
151
152 # Validate $code
153 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
154 wfDebug( "Invalid user language code\n" );
155 $code = $wgLanguageCode;
156 }
157
158 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
159
160 if( $code === $wgLanguageCode ) {
161 $this->lang = $wgContLang;
162 } else {
163 $obj = Language::factory( $code );
164 $this->lang = $obj;
165 }
166 }
167 return $this->lang;
168 }
169
170 /**
171 * Set the Skin object
172 *
173 * @param $s Skin
174 */
175 public function setSkin( Skin $s ) {
176 $this->skin = clone $s;
177 $this->skin->setContext( $this );
178 }
179
180 /**
181 * Get the Skin object
182 *
183 * @return Skin
184 */
185 public function getSkin() {
186 if ( $this->skin === null ) {
187 wfProfileIn( __METHOD__ . '-createskin' );
188
189 global $wgHiddenPrefs;
190 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
191 # get the user skin
192 $userSkin = $this->getUser()->getOption( 'skin' );
193 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
194 } else {
195 # if we're not allowing users to override, then use the default
196 global $wgDefaultSkin;
197 $userSkin = $wgDefaultSkin;
198 }
199
200 $this->skin = Skin::newFromKey( $userSkin );
201 $this->skin->setContext( $this );
202 wfProfileOut( __METHOD__ . '-createskin' );
203 }
204 return $this->skin;
205 }
206
207 /** Helpful methods **/
208
209 /**
210 * Get a Message object with context set
211 * Parameters are the same as wfMessage()
212 *
213 * @return Message object
214 */
215 public function msg() {
216 $args = func_get_args();
217 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->title( $this->getTitle() );
218 }
219
220 /** Static methods **/
221
222 /**
223 * Get the RequestContext object associated with the main request
224 *
225 * @return RequestContext object
226 */
227 public static function getMain() {
228 static $instance = null;
229 if ( $instance === null ) {
230 $instance = new self;
231 }
232 return $instance;
233 }
234 }
235
236 /**
237 * Interface for objects which can provide a context on request.
238 */
239 interface IContextSource {
240
241 /**
242 * Get the WebRequest object
243 *
244 * @return WebRequest
245 */
246 public function getRequest();
247
248 /**
249 * Get the Title object
250 *
251 * @return Title
252 */
253 public function getTitle();
254
255 /**
256 * Get the OutputPage object
257 *
258 * @return OutputPage object
259 */
260 public function getOutput();
261
262 /**
263 * Get the User object
264 *
265 * @return User
266 */
267 public function getUser();
268
269 /**
270 * Get the Language object
271 *
272 * @return Language
273 */
274 public function getLang();
275
276 /**
277 * Get the Skin object
278 *
279 * @return Skin
280 */
281 public function getSkin();
282 }
283
284 /**
285 * The simplest way of implementing IContextSource is to hold a RequestContext as a
286 * member variable and provide accessors to it.
287 */
288 abstract class ContextSource implements IContextSource {
289
290 /**
291 * @var RequestContext
292 */
293 private $context;
294
295 /**
296 * Get the RequestContext object
297 *
298 * @return RequestContext
299 */
300 public function getContext() {
301 return $this->context;
302 }
303
304 /**
305 * Set the RequestContext object
306 *
307 * @param $context RequestContext
308 */
309 public function setContext( RequestContext $context ) {
310 $this->context = $context;
311 }
312
313 /**
314 * Get the WebRequest object
315 *
316 * @return WebRequest
317 */
318 public function getRequest() {
319 return $this->context->getRequest();
320 }
321
322 /**
323 * Get the Title object
324 *
325 * @return Title
326 */
327 public function getTitle() {
328 return $this->context->getTitle();
329 }
330
331 /**
332 * Get the OutputPage object
333 *
334 * @return OutputPage object
335 */
336 public function getOutput() {
337 return $this->context->getOutput();
338 }
339
340 /**
341 * Get the User object
342 *
343 * @return User
344 */
345 public function getUser() {
346 return $this->context->getUser();
347 }
348
349 /**
350 * Get the Language object
351 *
352 * @return Language
353 */
354 public function getLang() {
355 return $this->context->getLang();
356 }
357
358 /**
359 * Get the Skin object
360 *
361 * @return Skin
362 */
363 public function getSkin() {
364 return $this->context->getSkin();
365 }
366 }