Add a new RequestContext::newExtraneousContext() to create a context filled with...
[lhc/web/wiklou.git] / includes / RequestContext.php
1 <?php
2 /**
3 * Request-dependant objects containers.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @since 1.18
21 *
22 * @author Alexandre Emsenhuber
23 * @author Daniel Friesen
24 * @file
25 */
26
27 /**
28 * Interface for objects which can provide a context on request.
29 */
30 interface IContextSource {
31
32 /**
33 * Get the WebRequest object
34 *
35 * @return WebRequest
36 */
37 public function getRequest();
38
39 /**
40 * Get the Title object
41 *
42 * @return Title
43 */
44 public function getTitle();
45
46 /**
47 * Get the OutputPage object
48 *
49 * @return OutputPage object
50 */
51 public function getOutput();
52
53 /**
54 * Get the User object
55 *
56 * @return User
57 */
58 public function getUser();
59
60 /**
61 * Get the Language object
62 *
63 * @return Language
64 */
65 public function getLang();
66
67 /**
68 * Get the Skin object
69 *
70 * @return Skin
71 */
72 public function getSkin();
73 }
74
75 /**
76 * Group all the pieces relevant to the context of a request into one instance
77 */
78 class RequestContext implements IContextSource {
79
80 /**
81 * @var WebRequest
82 */
83 private $request;
84
85 /**
86 * @var Title
87 */
88 private $title;
89
90 /**
91 * @var OutputPage
92 */
93 private $output;
94
95 /**
96 * @var User
97 */
98 private $user;
99
100 /**
101 * @var Language
102 */
103 private $lang;
104
105 /**
106 * @var Skin
107 */
108 private $skin;
109
110 /**
111 * Set the WebRequest object
112 *
113 * @param $r WebRequest object
114 */
115 public function setRequest( WebRequest $r ) {
116 $this->request = $r;
117 }
118
119 /**
120 * Get the WebRequest object
121 *
122 * @return WebRequest
123 */
124 public function getRequest() {
125 if ( $this->request === null ) {
126 global $wgRequest; # fallback to $wg till we can improve this
127 $this->request = $wgRequest;
128 }
129 return $this->request;
130 }
131
132 /**
133 * Set the Title object
134 *
135 * @param $t Title object
136 */
137 public function setTitle( Title $t ) {
138 $this->title = $t;
139 }
140
141 /**
142 * Get the Title object
143 *
144 * @return Title
145 */
146 public function getTitle() {
147 if ( $this->title === null ) {
148 global $wgTitle; # fallback to $wg till we can improve this
149 $this->title = $wgTitle;
150 }
151 return $this->title;
152 }
153
154 /**
155 * @param $o OutputPage
156 */
157 public function setOutput( OutputPage $o ) {
158 $this->output = $o;
159 }
160
161 /**
162 * Get the OutputPage object
163 *
164 * @return OutputPage object
165 */
166 public function getOutput() {
167 if ( $this->output === null ) {
168 $this->output = new OutputPage( $this );
169 }
170 return $this->output;
171 }
172
173 /**
174 * Set the User object
175 *
176 * @param $u User
177 */
178 public function setUser( User $u ) {
179 $this->user = $u;
180 }
181
182 /**
183 * Get the User object
184 *
185 * @return User
186 */
187 public function getUser() {
188 if ( $this->user === null ) {
189 $this->user = User::newFromSession( $this->getRequest() );
190 }
191 return $this->user;
192 }
193
194 /**
195 * Accepts a language code and ensures it's sane. Outputs a cleaned up language
196 * code and replaces with $wgLanguageCode if not sane.
197 */
198 private static function sanitizeLangCode( $code ) {
199 global $wgLanguageCode;
200
201 // BCP 47 - letter case MUST NOT carry meaning
202 $code = strtolower( $code );
203
204 # Validate $code
205 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
206 wfDebug( "Invalid user language code\n" );
207 $code = $wgLanguageCode;
208 }
209
210 return $code;
211 }
212
213 /**
214 * Set the Language object
215 *
216 * @param $l Mixed Language instance or language code
217 */
218 public function setLang( $l ) {
219 if ( $l instanceof Language ) {
220 $this->lang = $l;
221 } elseif ( is_string( $l ) ) {
222 $l = self::sanitizeLangCode( $l );
223 $obj = Language::factory( $l );
224 $this->lang = $obj;
225 } else {
226 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
227 }
228 }
229
230 /**
231 * Get the Language object
232 *
233 * @return Language
234 */
235 public function getLang() {
236 if ( $this->lang === null ) {
237 global $wgLanguageCode, $wgContLang;
238 $code = $this->getRequest()->getVal(
239 'uselang',
240 $this->getUser()->getOption( 'language' )
241 );
242 $code = self::sanitizeLangCode( $code );
243
244 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
245
246 if( $code === $wgLanguageCode ) {
247 $this->lang = $wgContLang;
248 } else {
249 $obj = Language::factory( $code );
250 $this->lang = $obj;
251 }
252 }
253 return $this->lang;
254 }
255
256 /**
257 * Set the Skin object
258 *
259 * @param $s Skin
260 */
261 public function setSkin( Skin $s ) {
262 $this->skin = clone $s;
263 $this->skin->setContext( $this );
264 }
265
266 /**
267 * Get the Skin object
268 *
269 * @return Skin
270 */
271 public function getSkin() {
272 if ( $this->skin === null ) {
273 wfProfileIn( __METHOD__ . '-createskin' );
274
275 global $wgHiddenPrefs;
276 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
277 # get the user skin
278 $userSkin = $this->getUser()->getOption( 'skin' );
279 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
280 } else {
281 # if we're not allowing users to override, then use the default
282 global $wgDefaultSkin;
283 $userSkin = $wgDefaultSkin;
284 }
285
286 $this->skin = Skin::newFromKey( $userSkin );
287 $this->skin->setContext( $this );
288 wfProfileOut( __METHOD__ . '-createskin' );
289 }
290 return $this->skin;
291 }
292
293 /** Helpful methods **/
294
295 /**
296 * Get a Message object with context set
297 * Parameters are the same as wfMessage()
298 *
299 * @return Message object
300 */
301 public function msg() {
302 $args = func_get_args();
303 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->title( $this->getTitle() );
304 }
305
306 /** Static methods **/
307
308 /**
309 * Get the RequestContext object associated with the main request
310 *
311 * @return RequestContext object
312 */
313 public static function getMain() {
314 static $instance = null;
315 if ( $instance === null ) {
316 $instance = new self;
317 }
318 return $instance;
319 }
320
321 /**
322 * Create a new extraneous context. The context is filled with information
323 * external to the current session.
324 * - Title is specified by argument
325 * - Request is a FauxRequest, or a FauxRequest can be specified by argument
326 * - User is an anonymous user, for separation IPv4 localhost is used
327 * - Language will be based on the anonymous user and request, may be content
328 * language or a uselang param in the fauxrequest data may change the lang
329 * - Skin will be based on the anonymous user, should be the wiki's default skin
330 *
331 * @param $title Title Title to use for the extraneous request
332 * @param $request Mixed A WebRequest or data to use for a FauxRequest
333 * @return RequestContext
334 */
335 public static function newExtraneousContext( Title $title, $request=array() ) {
336 $context = new self;
337 $context->setTitle( $title );
338 if ( $request instanceof WebRequest ) {
339 $context->setRequest( $request );
340 } else {
341 $context->setRequest( new FauxRequest( $request ) );
342 }
343 $context->user = User::newFromName( '127.0.0.1', false );
344 return $context;
345 }
346
347 }
348
349 /**
350 * The simplest way of implementing IContextSource is to hold a RequestContext as a
351 * member variable and provide accessors to it.
352 */
353 abstract class ContextSource implements IContextSource {
354
355 /**
356 * @var RequestContext
357 */
358 private $context;
359
360 /**
361 * Get the RequestContext object
362 *
363 * @return RequestContext
364 */
365 public function getContext() {
366 if ( $this->context === null ) {
367 $class = get_class( $this );
368 wfDebug( __METHOD__ . " ($class): called and \$context is null. Using RequestContext::getMain() for sanity\n" );
369 $this->context = RequestContext::getMain();
370 }
371 return $this->context;
372 }
373
374 /**
375 * Set the RequestContext object
376 *
377 * @param $context RequestContext
378 */
379 public function setContext( RequestContext $context ) {
380 $this->context = $context;
381 }
382
383 /**
384 * Get the WebRequest object
385 *
386 * @return WebRequest
387 */
388 public function getRequest() {
389 return $this->getContext()->getRequest();
390 }
391
392 /**
393 * Get the Title object
394 *
395 * @return Title
396 */
397 public function getTitle() {
398 return $this->getContext()->getTitle();
399 }
400
401 /**
402 * Get the OutputPage object
403 *
404 * @return OutputPage object
405 */
406 public function getOutput() {
407 return $this->getContext()->getOutput();
408 }
409
410 /**
411 * Get the User object
412 *
413 * @return User
414 */
415 public function getUser() {
416 return $this->getContext()->getUser();
417 }
418
419 /**
420 * Get the Language object
421 *
422 * @return Language
423 */
424 public function getLang() {
425 return $this->getContext()->getLang();
426 }
427
428 /**
429 * Get the Skin object
430 *
431 * @return Skin
432 */
433 public function getSkin() {
434 return $this->getContext()->getSkin();
435 }
436
437 /**
438 * Get a Message object with context set
439 * Parameters are the same as wfMessage()
440 *
441 * @return Message object
442 */
443 public function msg( /* $args */ ) {
444 return call_user_func_array( array( $this->getContext(), 'msg' ), func_get_args() );
445 }
446 }