Index: trunk/phase3/includes/RequestContext.php |
— | — | @@ -1,17 +1,83 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | | - * Group all the pieces relevant to the context of a request into one instance |
5 | | - * |
| 4 | + * Request-dependant objects containers. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License along |
| 17 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + * http://www.gnu.org/copyleft/gpl.html |
| 20 | + * |
6 | 21 | * @since 1.18 |
7 | 22 | * |
8 | | - * @author IAlex |
| 23 | + * @author Alexandre Emsenhuber |
9 | 24 | * @author Daniel Friesen |
10 | 25 | * @file |
11 | 26 | */ |
12 | 27 | |
13 | | -class RequestContext { |
| 28 | +/** |
| 29 | + * Interface for objects which can provide a context on request. |
| 30 | + */ |
| 31 | +interface IContextSource { |
14 | 32 | |
15 | 33 | /** |
| 34 | + * Get the WebRequest object |
| 35 | + * |
| 36 | + * @return WebRequest |
| 37 | + */ |
| 38 | + public function getRequest(); |
| 39 | + |
| 40 | + /** |
| 41 | + * Get the Title object |
| 42 | + * |
| 43 | + * @return Title |
| 44 | + */ |
| 45 | + public function getTitle(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the OutputPage object |
| 49 | + * |
| 50 | + * @return OutputPage object |
| 51 | + */ |
| 52 | + public function getOutput(); |
| 53 | + |
| 54 | + /** |
| 55 | + * Get the User object |
| 56 | + * |
| 57 | + * @return User |
| 58 | + */ |
| 59 | + public function getUser(); |
| 60 | + |
| 61 | + /** |
| 62 | + * Get the Language object |
| 63 | + * |
| 64 | + * @return Language |
| 65 | + */ |
| 66 | + public function getLang(); |
| 67 | + |
| 68 | + /** |
| 69 | + * Get the Skin object |
| 70 | + * |
| 71 | + * @return Skin |
| 72 | + */ |
| 73 | + public function getSkin(); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Group all the pieces relevant to the context of a request into one instance |
| 78 | + */ |
| 79 | +class RequestContext implements IContextSource { |
| 80 | + |
| 81 | + /** |
16 | 82 | * @var WebRequest |
17 | 83 | */ |
18 | 84 | private $request; |
— | — | @@ -234,54 +300,6 @@ |
235 | 301 | } |
236 | 302 | |
237 | 303 | /** |
238 | | - * Interface for objects which can provide a context on request. |
239 | | - */ |
240 | | -interface IContextSource { |
241 | | - |
242 | | - /** |
243 | | - * Get the WebRequest object |
244 | | - * |
245 | | - * @return WebRequest |
246 | | - */ |
247 | | - public function getRequest(); |
248 | | - |
249 | | - /** |
250 | | - * Get the Title object |
251 | | - * |
252 | | - * @return Title |
253 | | - */ |
254 | | - public function getTitle(); |
255 | | - |
256 | | - /** |
257 | | - * Get the OutputPage object |
258 | | - * |
259 | | - * @return OutputPage object |
260 | | - */ |
261 | | - public function getOutput(); |
262 | | - |
263 | | - /** |
264 | | - * Get the User object |
265 | | - * |
266 | | - * @return User |
267 | | - */ |
268 | | - public function getUser(); |
269 | | - |
270 | | - /** |
271 | | - * Get the Language object |
272 | | - * |
273 | | - * @return Language |
274 | | - */ |
275 | | - public function getLang(); |
276 | | - |
277 | | - /** |
278 | | - * Get the Skin object |
279 | | - * |
280 | | - * @return Skin |
281 | | - */ |
282 | | - public function getSkin(); |
283 | | -} |
284 | | - |
285 | | -/** |
286 | 304 | * The simplest way of implementing IContextSource is to hold a RequestContext as a |
287 | 305 | * member variable and provide accessors to it. |
288 | 306 | */ |
— | — | @@ -298,6 +316,11 @@ |
299 | 317 | * @return RequestContext |
300 | 318 | */ |
301 | 319 | public function getContext() { |
| 320 | + if ( $this->context === null ) { |
| 321 | + $class = get_class( $this ); |
| 322 | + wfDebug( __METHOD__ . " ($class): called and \$context is null. Using RequestContext::getMain() for sanity\n" ); |
| 323 | + $this->context = RequestContext::getMain(); |
| 324 | + } |
302 | 325 | return $this->context; |
303 | 326 | } |
304 | 327 | |
— | — | @@ -316,7 +339,7 @@ |
317 | 340 | * @return WebRequest |
318 | 341 | */ |
319 | 342 | public function getRequest() { |
320 | | - return $this->context->getRequest(); |
| 343 | + return $this->getContext()->getRequest(); |
321 | 344 | } |
322 | 345 | |
323 | 346 | /** |
— | — | @@ -325,7 +348,7 @@ |
326 | 349 | * @return Title |
327 | 350 | */ |
328 | 351 | public function getTitle() { |
329 | | - return $this->context->getTitle(); |
| 352 | + return $this->getContext()->getTitle(); |
330 | 353 | } |
331 | 354 | |
332 | 355 | /** |
— | — | @@ -334,7 +357,7 @@ |
335 | 358 | * @return OutputPage object |
336 | 359 | */ |
337 | 360 | public function getOutput() { |
338 | | - return $this->context->getOutput(); |
| 361 | + return $this->getContext()->getOutput(); |
339 | 362 | } |
340 | 363 | |
341 | 364 | /** |
— | — | @@ -343,7 +366,7 @@ |
344 | 367 | * @return User |
345 | 368 | */ |
346 | 369 | public function getUser() { |
347 | | - return $this->context->getUser(); |
| 370 | + return $this->getContext()->getUser(); |
348 | 371 | } |
349 | 372 | |
350 | 373 | /** |
— | — | @@ -352,7 +375,7 @@ |
353 | 376 | * @return Language |
354 | 377 | */ |
355 | 378 | public function getLang() { |
356 | | - return $this->context->getLang(); |
| 379 | + return $this->getContext()->getLang(); |
357 | 380 | } |
358 | 381 | |
359 | 382 | /** |
— | — | @@ -361,6 +384,16 @@ |
362 | 385 | * @return Skin |
363 | 386 | */ |
364 | 387 | public function getSkin() { |
365 | | - return $this->context->getSkin(); |
| 388 | + return $this->getContext()->getSkin(); |
366 | 389 | } |
| 390 | + |
| 391 | + /** |
| 392 | + * Get a Message object with context set |
| 393 | + * Parameters are the same as wfMessage() |
| 394 | + * |
| 395 | + * @return Message object |
| 396 | + */ |
| 397 | + public function msg( /* $args */ ) { |
| 398 | + return call_user_func_array( array( $this->getContext(), 'msg' ), func_get_args() ); |
| 399 | + } |
367 | 400 | } |