* Expanded documentation, added GPL header and changed the @author tag to use my...
[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 * Set the Language object
196 *
197 * @param $l Language
198 */
199 public function setLang( Language $l ) {
200 $this->lang = $l;
201 }
202
203 /**
204 * Get the Language object
205 *
206 * @return Language
207 */
208 public function getLang() {
209 if ( $this->lang === null ) {
210 global $wgLanguageCode, $wgContLang;
211 $code = $this->getRequest()->getVal(
212 'uselang',
213 $this->getUser()->getOption( 'language' )
214 );
215 // BCP 47 - letter case MUST NOT carry meaning
216 $code = strtolower( $code );
217
218 # Validate $code
219 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
220 wfDebug( "Invalid user language code\n" );
221 $code = $wgLanguageCode;
222 }
223
224 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
225
226 if( $code === $wgLanguageCode ) {
227 $this->lang = $wgContLang;
228 } else {
229 $obj = Language::factory( $code );
230 $this->lang = $obj;
231 }
232 }
233 return $this->lang;
234 }
235
236 /**
237 * Set the Skin object
238 *
239 * @param $s Skin
240 */
241 public function setSkin( Skin $s ) {
242 $this->skin = clone $s;
243 $this->skin->setContext( $this );
244 }
245
246 /**
247 * Get the Skin object
248 *
249 * @return Skin
250 */
251 public function getSkin() {
252 if ( $this->skin === null ) {
253 wfProfileIn( __METHOD__ . '-createskin' );
254
255 global $wgHiddenPrefs;
256 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
257 # get the user skin
258 $userSkin = $this->getUser()->getOption( 'skin' );
259 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
260 } else {
261 # if we're not allowing users to override, then use the default
262 global $wgDefaultSkin;
263 $userSkin = $wgDefaultSkin;
264 }
265
266 $this->skin = Skin::newFromKey( $userSkin );
267 $this->skin->setContext( $this );
268 wfProfileOut( __METHOD__ . '-createskin' );
269 }
270 return $this->skin;
271 }
272
273 /** Helpful methods **/
274
275 /**
276 * Get a Message object with context set
277 * Parameters are the same as wfMessage()
278 *
279 * @return Message object
280 */
281 public function msg() {
282 $args = func_get_args();
283 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->title( $this->getTitle() );
284 }
285
286 /** Static methods **/
287
288 /**
289 * Get the RequestContext object associated with the main request
290 *
291 * @return RequestContext object
292 */
293 public static function getMain() {
294 static $instance = null;
295 if ( $instance === null ) {
296 $instance = new self;
297 }
298 return $instance;
299 }
300 }
301
302 /**
303 * The simplest way of implementing IContextSource is to hold a RequestContext as a
304 * member variable and provide accessors to it.
305 */
306 abstract class ContextSource implements IContextSource {
307
308 /**
309 * @var RequestContext
310 */
311 private $context;
312
313 /**
314 * Get the RequestContext object
315 *
316 * @return RequestContext
317 */
318 public function getContext() {
319 if ( $this->context === null ) {
320 $class = get_class( $this );
321 wfDebug( __METHOD__ . " ($class): called and \$context is null. Using RequestContext::getMain() for sanity\n" );
322 $this->context = RequestContext::getMain();
323 }
324 return $this->context;
325 }
326
327 /**
328 * Set the RequestContext object
329 *
330 * @param $context RequestContext
331 */
332 public function setContext( RequestContext $context ) {
333 $this->context = $context;
334 }
335
336 /**
337 * Get the WebRequest object
338 *
339 * @return WebRequest
340 */
341 public function getRequest() {
342 return $this->getContext()->getRequest();
343 }
344
345 /**
346 * Get the Title object
347 *
348 * @return Title
349 */
350 public function getTitle() {
351 return $this->getContext()->getTitle();
352 }
353
354 /**
355 * Get the OutputPage object
356 *
357 * @return OutputPage object
358 */
359 public function getOutput() {
360 return $this->getContext()->getOutput();
361 }
362
363 /**
364 * Get the User object
365 *
366 * @return User
367 */
368 public function getUser() {
369 return $this->getContext()->getUser();
370 }
371
372 /**
373 * Get the Language object
374 *
375 * @return Language
376 */
377 public function getLang() {
378 return $this->getContext()->getLang();
379 }
380
381 /**
382 * Get the Skin object
383 *
384 * @return Skin
385 */
386 public function getSkin() {
387 return $this->getContext()->getSkin();
388 }
389
390 /**
391 * Get a Message object with context set
392 * Parameters are the same as wfMessage()
393 *
394 * @return Message object
395 */
396 public function msg( /* $args */ ) {
397 return call_user_func_array( array( $this->getContext(), 'msg' ), func_get_args() );
398 }
399 }