r97179 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97178‎ | r97179 | r97180 >
Date:17:42, 15 September 2011
Author:dantman
Status:resolved
Tags:
Comment:
Separate RequestContext.php into separate files inside of context/
Update the AutoLoader too. This is also a follow up to r97161 since I forgot to add the AutoLoader line.
Modified paths:
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/RequestContext.php (deleted) (history)
  • /trunk/phase3/includes/context (added) (history)
  • /trunk/phase3/includes/context/ContextSource.php (added) (history)
  • /trunk/phase3/includes/context/DerivativeContext.php (added) (history)
  • /trunk/phase3/includes/context/IContextSource.php (added) (history)
  • /trunk/phase3/includes/context/RequestContext.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/RequestContext.php
@@ -1,634 +0,0 @@
2 -<?php
3 -/**
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 - *
21 - * @since 1.18
22 - *
23 - * @author Alexandre Emsenhuber
24 - * @author Daniel Friesen
25 - * @file
26 - */
27 -
28 -/**
29 - * Interface for objects which can provide a context on request.
30 - */
31 -interface IContextSource {
32 -
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 - /**
82 - * @var WebRequest
83 - */
84 - private $request;
85 -
86 - /**
87 - * @var Title
88 - */
89 - private $title;
90 -
91 - /**
92 - * @var OutputPage
93 - */
94 - private $output;
95 -
96 - /**
97 - * @var User
98 - */
99 - private $user;
100 -
101 - /**
102 - * @var Language
103 - */
104 - private $lang;
105 -
106 - /**
107 - * @var Skin
108 - */
109 - private $skin;
110 -
111 - /**
112 - * Set the WebRequest object
113 - *
114 - * @param $r WebRequest object
115 - */
116 - public function setRequest( WebRequest $r ) {
117 - $this->request = $r;
118 - }
119 -
120 - /**
121 - * Get the WebRequest object
122 - *
123 - * @return WebRequest
124 - */
125 - public function getRequest() {
126 - if ( $this->request === null ) {
127 - global $wgRequest; # fallback to $wg till we can improve this
128 - $this->request = $wgRequest;
129 - }
130 - return $this->request;
131 - }
132 -
133 - /**
134 - * Set the Title object
135 - *
136 - * @param $t Title object
137 - */
138 - public function setTitle( Title $t ) {
139 - $this->title = $t;
140 - }
141 -
142 - /**
143 - * Get the Title object
144 - *
145 - * @return Title
146 - */
147 - public function getTitle() {
148 - if ( $this->title === null ) {
149 - global $wgTitle; # fallback to $wg till we can improve this
150 - $this->title = $wgTitle;
151 - }
152 - return $this->title;
153 - }
154 -
155 - /**
156 - * @param $o OutputPage
157 - */
158 - public function setOutput( OutputPage $o ) {
159 - $this->output = $o;
160 - }
161 -
162 - /**
163 - * Get the OutputPage object
164 - *
165 - * @return OutputPage object
166 - */
167 - public function getOutput() {
168 - if ( $this->output === null ) {
169 - $this->output = new OutputPage( $this );
170 - }
171 - return $this->output;
172 - }
173 -
174 - /**
175 - * Set the User object
176 - *
177 - * @param $u User
178 - */
179 - public function setUser( User $u ) {
180 - $this->user = $u;
181 - }
182 -
183 - /**
184 - * Get the User object
185 - *
186 - * @return User
187 - */
188 - public function getUser() {
189 - if ( $this->user === null ) {
190 - $this->user = User::newFromSession( $this->getRequest() );
191 - }
192 - return $this->user;
193 - }
194 -
195 - /**
196 - * Accepts a language code and ensures it's sane. Outputs a cleaned up language
197 - * code and replaces with $wgLanguageCode if not sane.
198 - */
199 - private static function sanitizeLangCode( $code ) {
200 - global $wgLanguageCode;
201 -
202 - // BCP 47 - letter case MUST NOT carry meaning
203 - $code = strtolower( $code );
204 -
205 - # Validate $code
206 - if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
207 - wfDebug( "Invalid user language code\n" );
208 - $code = $wgLanguageCode;
209 - }
210 -
211 - return $code;
212 - }
213 -
214 - /**
215 - * Set the Language object
216 - *
217 - * @param $l Mixed Language instance or language code
218 - */
219 - public function setLang( $l ) {
220 - if ( $l instanceof Language ) {
221 - $this->lang = $l;
222 - } elseif ( is_string( $l ) ) {
223 - $l = self::sanitizeLangCode( $l );
224 - $obj = Language::factory( $l );
225 - $this->lang = $obj;
226 - } else {
227 - throw new MWException( __METHOD__ . " was passed an invalid type of data." );
228 - }
229 - }
230 -
231 - /**
232 - * Get the Language object
233 - *
234 - * @return Language
235 - */
236 - public function getLang() {
237 - if ( $this->lang === null ) {
238 - global $wgLanguageCode, $wgContLang;
239 - $code = $this->getRequest()->getVal(
240 - 'uselang',
241 - $this->getUser()->getOption( 'language' )
242 - );
243 - $code = self::sanitizeLangCode( $code );
244 -
245 - wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
246 -
247 - if( $code === $wgLanguageCode ) {
248 - $this->lang = $wgContLang;
249 - } else {
250 - $obj = Language::factory( $code );
251 - $this->lang = $obj;
252 - }
253 - }
254 - return $this->lang;
255 - }
256 -
257 - /**
258 - * Set the Skin object
259 - *
260 - * @param $s Skin
261 - */
262 - public function setSkin( Skin $s ) {
263 - $this->skin = clone $s;
264 - $this->skin->setContext( $this );
265 - }
266 -
267 - /**
268 - * Get the Skin object
269 - *
270 - * @return Skin
271 - */
272 - public function getSkin() {
273 - if ( $this->skin === null ) {
274 - wfProfileIn( __METHOD__ . '-createskin' );
275 -
276 - global $wgHiddenPrefs;
277 - if( !in_array( 'skin', $wgHiddenPrefs ) ) {
278 - # get the user skin
279 - $userSkin = $this->getUser()->getOption( 'skin' );
280 - $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
281 - } else {
282 - # if we're not allowing users to override, then use the default
283 - global $wgDefaultSkin;
284 - $userSkin = $wgDefaultSkin;
285 - }
286 -
287 - $this->skin = Skin::newFromKey( $userSkin );
288 - $this->skin->setContext( $this );
289 - wfProfileOut( __METHOD__ . '-createskin' );
290 - }
291 - return $this->skin;
292 - }
293 -
294 - /** Helpful methods **/
295 -
296 - /**
297 - * Get a Message object with context set
298 - * Parameters are the same as wfMessage()
299 - *
300 - * @return Message object
301 - */
302 - public function msg() {
303 - $args = func_get_args();
304 - return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->title( $this->getTitle() );
305 - }
306 -
307 - /** Static methods **/
308 -
309 - /**
310 - * Get the RequestContext object associated with the main request
311 - *
312 - * @return RequestContext object
313 - */
314 - public static function getMain() {
315 - static $instance = null;
316 - if ( $instance === null ) {
317 - $instance = new self;
318 - }
319 - return $instance;
320 - }
321 -
322 - /**
323 - * Create a new extraneous context. The context is filled with information
324 - * external to the current session.
325 - * - Title is specified by argument
326 - * - Request is a FauxRequest, or a FauxRequest can be specified by argument
327 - * - User is an anonymous user, for separation IPv4 localhost is used
328 - * - Language will be based on the anonymous user and request, may be content
329 - * language or a uselang param in the fauxrequest data may change the lang
330 - * - Skin will be based on the anonymous user, should be the wiki's default skin
331 - *
332 - * @param $title Title Title to use for the extraneous request
333 - * @param $request Mixed A WebRequest or data to use for a FauxRequest
334 - * @return RequestContext
335 - */
336 - public static function newExtraneousContext( Title $title, $request=array() ) {
337 - $context = new self;
338 - $context->setTitle( $title );
339 - if ( $request instanceof WebRequest ) {
340 - $context->setRequest( $request );
341 - } else {
342 - $context->setRequest( new FauxRequest( $request ) );
343 - }
344 - $context->user = User::newFromName( '127.0.0.1', false );
345 - return $context;
346 - }
347 -
348 -}
349 -
350 -/**
351 - * The simplest way of implementing IContextSource is to hold a RequestContext as a
352 - * member variable and provide accessors to it.
353 - */
354 -abstract class ContextSource implements IContextSource {
355 -
356 - /**
357 - * @var IContextSource
358 - */
359 - private $context;
360 -
361 - /**
362 - * Get the IContextSource object
363 - *
364 - * @return IContextSource
365 - */
366 - public function getContext() {
367 - if ( $this->context === null ) {
368 - $class = get_class( $this );
369 - wfDebug( __METHOD__ . " ($class): called and \$context is null. Using RequestContext::getMain() for sanity\n" );
370 - $this->context = RequestContext::getMain();
371 - }
372 - return $this->context;
373 - }
374 -
375 - /**
376 - * Set the IContextSource object
377 - *
378 - * @param $context IContextSource
379 - */
380 - public function setContext( IContextSource $context ) {
381 - $this->context = $context;
382 - }
383 -
384 - /**
385 - * Get the WebRequest object
386 - *
387 - * @return WebRequest
388 - */
389 - public function getRequest() {
390 - return $this->getContext()->getRequest();
391 - }
392 -
393 - /**
394 - * Get the Title object
395 - *
396 - * @return Title
397 - */
398 - public function getTitle() {
399 - return $this->getContext()->getTitle();
400 - }
401 -
402 - /**
403 - * Get the OutputPage object
404 - *
405 - * @return OutputPage object
406 - */
407 - public function getOutput() {
408 - return $this->getContext()->getOutput();
409 - }
410 -
411 - /**
412 - * Get the User object
413 - *
414 - * @return User
415 - */
416 - public function getUser() {
417 - return $this->getContext()->getUser();
418 - }
419 -
420 - /**
421 - * Get the Language object
422 - *
423 - * @return Language
424 - */
425 - public function getLang() {
426 - return $this->getContext()->getLang();
427 - }
428 -
429 - /**
430 - * Get the Skin object
431 - *
432 - * @return Skin
433 - */
434 - public function getSkin() {
435 - return $this->getContext()->getSkin();
436 - }
437 -
438 - /**
439 - * Get a Message object with context set
440 - * Parameters are the same as wfMessage()
441 - *
442 - * @return Message object
443 - */
444 - public function msg( /* $args */ ) {
445 - return call_user_func_array( array( $this->getContext(), 'msg' ), func_get_args() );
446 - }
447 -}
448 -
449 -/**
450 - * An IContextSource implementation which will inherit context from another source
451 - * but allow individual pieces of context to be changed locally
452 - * eg: A ContextSource that can inherit from the main RequestContext but have
453 - * a different Title instance set on it.
454 - */
455 -class DerivativeContext extends ContextSource {
456 -
457 - /**
458 - * @var WebRequest
459 - */
460 - private $request;
461 -
462 - /**
463 - * @var Title
464 - */
465 - private $title;
466 -
467 - /**
468 - * @var OutputPage
469 - */
470 - private $output;
471 -
472 - /**
473 - * @var User
474 - */
475 - private $user;
476 -
477 - /**
478 - * @var Language
479 - */
480 - private $lang;
481 -
482 - /**
483 - * @var Skin
484 - */
485 - private $skin;
486 -
487 - /**
488 - * Constructor
489 - * @param $context IContextSource Context to inherit from
490 - */
491 - public function __construct( IContextSource $context ) {
492 - $this->setContext( $context );
493 - }
494 -
495 - /**
496 - * Set the WebRequest object
497 - *
498 - * @param $r WebRequest object
499 - */
500 - public function setRequest( WebRequest $r ) {
501 - $this->request = $r;
502 - }
503 -
504 - /**
505 - * Get the WebRequest object
506 - *
507 - * @return WebRequest
508 - */
509 - public function getRequest() {
510 - if ( !is_null( $this->request ) ) {
511 - return $this->request;
512 - } else {
513 - return $this->getContext()->getRequest();
514 - }
515 - }
516 -
517 - /**
518 - * Set the Title object
519 - *
520 - * @param $t Title object
521 - */
522 - public function setTitle( Title $t ) {
523 - $this->title = $t;
524 - }
525 -
526 - /**
527 - * Get the Title object
528 - *
529 - * @return Title
530 - */
531 - public function getTitle() {
532 - if ( !is_null( $this->title ) ) {
533 - return $this->title;
534 - } else {
535 - return $this->getContext()->getTitle();
536 - }
537 - }
538 -
539 - /**
540 - * @param $o OutputPage
541 - */
542 - public function setOutput( OutputPage $o ) {
543 - $this->output = $o;
544 - }
545 -
546 - /**
547 - * Get the OutputPage object
548 - *
549 - * @return OutputPage object
550 - */
551 - public function getOutput() {
552 - if ( !is_null( $this->output ) ) {
553 - return $this->output;
554 - } else {
555 - return $this->getContext()->getOutput();
556 - }
557 - }
558 -
559 - /**
560 - * Set the User object
561 - *
562 - * @param $u User
563 - */
564 - public function setUser( User $u ) {
565 - $this->user = $u;
566 - }
567 -
568 - /**
569 - * Get the User object
570 - *
571 - * @return User
572 - */
573 - public function getUser() {
574 - if ( !is_null( $this->user ) ) {
575 - return $this->user;
576 - } else {
577 - return $this->getContext()->getUser();
578 - }
579 - }
580 -
581 - /**
582 - * Set the Language object
583 - *
584 - * @param $l Mixed Language instance or language code
585 - */
586 - public function setLang( $l ) {
587 - if ( $l instanceof Language ) {
588 - $this->lang = $l;
589 - } elseif ( is_string( $l ) ) {
590 - $l = self::sanitizeLangCode( $l );
591 - $obj = Language::factory( $l );
592 - $this->lang = $obj;
593 - } else {
594 - throw new MWException( __METHOD__ . " was passed an invalid type of data." );
595 - }
596 - }
597 -
598 - /**
599 - * Get the Language object
600 - *
601 - * @return Language
602 - */
603 - public function getLang() {
604 - if ( !is_null( $this->lang ) ) {
605 - return $this->lang;
606 - } else {
607 - return $this->getContext()->getLang();
608 - }
609 - }
610 -
611 - /**
612 - * Set the Skin object
613 - *
614 - * @param $s Skin
615 - */
616 - public function setSkin( Skin $s ) {
617 - $this->skin = clone $s;
618 - $this->skin->setContext( $this );
619 - }
620 -
621 - /**
622 - * Get the Skin object
623 - *
624 - * @return Skin
625 - */
626 - public function getSkin() {
627 - if ( !is_null( $this->skin ) ) {
628 - return $this->skin;
629 - } else {
630 - return $this->getContext()->getSkin();
631 - }
632 - }
633 -
634 -}
635 -
Index: trunk/phase3/includes/context/RequestContext.php
@@ -0,0 +1,300 @@
 2+<?php
 3+/**
 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+ *
 21+ * @since 1.18
 22+ *
 23+ * @author Alexandre Emsenhuber
 24+ * @author Daniel Friesen
 25+ * @file
 26+ */
 27+
 28+/**
 29+ * Group all the pieces relevant to the context of a request into one instance
 30+ */
 31+class RequestContext implements IContextSource {
 32+
 33+ /**
 34+ * @var WebRequest
 35+ */
 36+ private $request;
 37+
 38+ /**
 39+ * @var Title
 40+ */
 41+ private $title;
 42+
 43+ /**
 44+ * @var OutputPage
 45+ */
 46+ private $output;
 47+
 48+ /**
 49+ * @var User
 50+ */
 51+ private $user;
 52+
 53+ /**
 54+ * @var Language
 55+ */
 56+ private $lang;
 57+
 58+ /**
 59+ * @var Skin
 60+ */
 61+ private $skin;
 62+
 63+ /**
 64+ * Set the WebRequest object
 65+ *
 66+ * @param $r WebRequest object
 67+ */
 68+ public function setRequest( WebRequest $r ) {
 69+ $this->request = $r;
 70+ }
 71+
 72+ /**
 73+ * Get the WebRequest object
 74+ *
 75+ * @return WebRequest
 76+ */
 77+ public function getRequest() {
 78+ if ( $this->request === null ) {
 79+ global $wgRequest; # fallback to $wg till we can improve this
 80+ $this->request = $wgRequest;
 81+ }
 82+ return $this->request;
 83+ }
 84+
 85+ /**
 86+ * Set the Title object
 87+ *
 88+ * @param $t Title object
 89+ */
 90+ public function setTitle( Title $t ) {
 91+ $this->title = $t;
 92+ }
 93+
 94+ /**
 95+ * Get the Title object
 96+ *
 97+ * @return Title
 98+ */
 99+ public function getTitle() {
 100+ if ( $this->title === null ) {
 101+ global $wgTitle; # fallback to $wg till we can improve this
 102+ $this->title = $wgTitle;
 103+ }
 104+ return $this->title;
 105+ }
 106+
 107+ /**
 108+ * @param $o OutputPage
 109+ */
 110+ public function setOutput( OutputPage $o ) {
 111+ $this->output = $o;
 112+ }
 113+
 114+ /**
 115+ * Get the OutputPage object
 116+ *
 117+ * @return OutputPage object
 118+ */
 119+ public function getOutput() {
 120+ if ( $this->output === null ) {
 121+ $this->output = new OutputPage( $this );
 122+ }
 123+ return $this->output;
 124+ }
 125+
 126+ /**
 127+ * Set the User object
 128+ *
 129+ * @param $u User
 130+ */
 131+ public function setUser( User $u ) {
 132+ $this->user = $u;
 133+ }
 134+
 135+ /**
 136+ * Get the User object
 137+ *
 138+ * @return User
 139+ */
 140+ public function getUser() {
 141+ if ( $this->user === null ) {
 142+ $this->user = User::newFromSession( $this->getRequest() );
 143+ }
 144+ return $this->user;
 145+ }
 146+
 147+ /**
 148+ * Accepts a language code and ensures it's sane. Outputs a cleaned up language
 149+ * code and replaces with $wgLanguageCode if not sane.
 150+ */
 151+ private static function sanitizeLangCode( $code ) {
 152+ global $wgLanguageCode;
 153+
 154+ // BCP 47 - letter case MUST NOT carry meaning
 155+ $code = strtolower( $code );
 156+
 157+ # Validate $code
 158+ if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
 159+ wfDebug( "Invalid user language code\n" );
 160+ $code = $wgLanguageCode;
 161+ }
 162+
 163+ return $code;
 164+ }
 165+
 166+ /**
 167+ * Set the Language object
 168+ *
 169+ * @param $l Mixed Language instance or language code
 170+ */
 171+ public function setLang( $l ) {
 172+ if ( $l instanceof Language ) {
 173+ $this->lang = $l;
 174+ } elseif ( is_string( $l ) ) {
 175+ $l = self::sanitizeLangCode( $l );
 176+ $obj = Language::factory( $l );
 177+ $this->lang = $obj;
 178+ } else {
 179+ throw new MWException( __METHOD__ . " was passed an invalid type of data." );
 180+ }
 181+ }
 182+
 183+ /**
 184+ * Get the Language object
 185+ *
 186+ * @return Language
 187+ */
 188+ public function getLang() {
 189+ if ( $this->lang === null ) {
 190+ global $wgLanguageCode, $wgContLang;
 191+ $code = $this->getRequest()->getVal(
 192+ 'uselang',
 193+ $this->getUser()->getOption( 'language' )
 194+ );
 195+ $code = self::sanitizeLangCode( $code );
 196+
 197+ wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
 198+
 199+ if( $code === $wgLanguageCode ) {
 200+ $this->lang = $wgContLang;
 201+ } else {
 202+ $obj = Language::factory( $code );
 203+ $this->lang = $obj;
 204+ }
 205+ }
 206+ return $this->lang;
 207+ }
 208+
 209+ /**
 210+ * Set the Skin object
 211+ *
 212+ * @param $s Skin
 213+ */
 214+ public function setSkin( Skin $s ) {
 215+ $this->skin = clone $s;
 216+ $this->skin->setContext( $this );
 217+ }
 218+
 219+ /**
 220+ * Get the Skin object
 221+ *
 222+ * @return Skin
 223+ */
 224+ public function getSkin() {
 225+ if ( $this->skin === null ) {
 226+ wfProfileIn( __METHOD__ . '-createskin' );
 227+
 228+ global $wgHiddenPrefs;
 229+ if( !in_array( 'skin', $wgHiddenPrefs ) ) {
 230+ # get the user skin
 231+ $userSkin = $this->getUser()->getOption( 'skin' );
 232+ $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
 233+ } else {
 234+ # if we're not allowing users to override, then use the default
 235+ global $wgDefaultSkin;
 236+ $userSkin = $wgDefaultSkin;
 237+ }
 238+
 239+ $this->skin = Skin::newFromKey( $userSkin );
 240+ $this->skin->setContext( $this );
 241+ wfProfileOut( __METHOD__ . '-createskin' );
 242+ }
 243+ return $this->skin;
 244+ }
 245+
 246+ /** Helpful methods **/
 247+
 248+ /**
 249+ * Get a Message object with context set
 250+ * Parameters are the same as wfMessage()
 251+ *
 252+ * @return Message object
 253+ */
 254+ public function msg() {
 255+ $args = func_get_args();
 256+ return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->title( $this->getTitle() );
 257+ }
 258+
 259+ /** Static methods **/
 260+
 261+ /**
 262+ * Get the RequestContext object associated with the main request
 263+ *
 264+ * @return RequestContext object
 265+ */
 266+ public static function getMain() {
 267+ static $instance = null;
 268+ if ( $instance === null ) {
 269+ $instance = new self;
 270+ }
 271+ return $instance;
 272+ }
 273+
 274+ /**
 275+ * Create a new extraneous context. The context is filled with information
 276+ * external to the current session.
 277+ * - Title is specified by argument
 278+ * - Request is a FauxRequest, or a FauxRequest can be specified by argument
 279+ * - User is an anonymous user, for separation IPv4 localhost is used
 280+ * - Language will be based on the anonymous user and request, may be content
 281+ * language or a uselang param in the fauxrequest data may change the lang
 282+ * - Skin will be based on the anonymous user, should be the wiki's default skin
 283+ *
 284+ * @param $title Title Title to use for the extraneous request
 285+ * @param $request Mixed A WebRequest or data to use for a FauxRequest
 286+ * @return RequestContext
 287+ */
 288+ public static function newExtraneousContext( Title $title, $request=array() ) {
 289+ $context = new self;
 290+ $context->setTitle( $title );
 291+ if ( $request instanceof WebRequest ) {
 292+ $context->setRequest( $request );
 293+ } else {
 294+ $context->setRequest( new FauxRequest( $request ) );
 295+ }
 296+ $context->user = User::newFromName( '127.0.0.1', false );
 297+ return $context;
 298+ }
 299+
 300+}
 301+
Property changes on: trunk/phase3/includes/context/RequestContext.php
___________________________________________________________________
Added: svn:eol-style
1302 + native
Index: trunk/phase3/includes/context/IContextSource.php
@@ -0,0 +1,73 @@
 2+<?php
 3+/**
 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+ *
 21+ * @since 1.18
 22+ *
 23+ * @author Happy-melon
 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+
Property changes on: trunk/phase3/includes/context/IContextSource.php
___________________________________________________________________
Added: svn:eol-style
175 + native
Index: trunk/phase3/includes/context/ContextSource.php
@@ -0,0 +1,124 @@
 2+<?php
 3+/**
 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+ *
 21+ * @since 1.18
 22+ *
 23+ * @author Happy-melon
 24+ * @file
 25+ */
 26+
 27+/**
 28+ * The simplest way of implementing IContextSource is to hold a RequestContext as a
 29+ * member variable and provide accessors to it.
 30+ */
 31+abstract class ContextSource implements IContextSource {
 32+
 33+ /**
 34+ * @var IContextSource
 35+ */
 36+ private $context;
 37+
 38+ /**
 39+ * Get the IContextSource object
 40+ *
 41+ * @return IContextSource
 42+ */
 43+ public function getContext() {
 44+ if ( $this->context === null ) {
 45+ $class = get_class( $this );
 46+ wfDebug( __METHOD__ . " ($class): called and \$context is null. Using RequestContext::getMain() for sanity\n" );
 47+ $this->context = RequestContext::getMain();
 48+ }
 49+ return $this->context;
 50+ }
 51+
 52+ /**
 53+ * Set the IContextSource object
 54+ *
 55+ * @param $context IContextSource
 56+ */
 57+ public function setContext( IContextSource $context ) {
 58+ $this->context = $context;
 59+ }
 60+
 61+ /**
 62+ * Get the WebRequest object
 63+ *
 64+ * @return WebRequest
 65+ */
 66+ public function getRequest() {
 67+ return $this->getContext()->getRequest();
 68+ }
 69+
 70+ /**
 71+ * Get the Title object
 72+ *
 73+ * @return Title
 74+ */
 75+ public function getTitle() {
 76+ return $this->getContext()->getTitle();
 77+ }
 78+
 79+ /**
 80+ * Get the OutputPage object
 81+ *
 82+ * @return OutputPage object
 83+ */
 84+ public function getOutput() {
 85+ return $this->getContext()->getOutput();
 86+ }
 87+
 88+ /**
 89+ * Get the User object
 90+ *
 91+ * @return User
 92+ */
 93+ public function getUser() {
 94+ return $this->getContext()->getUser();
 95+ }
 96+
 97+ /**
 98+ * Get the Language object
 99+ *
 100+ * @return Language
 101+ */
 102+ public function getLang() {
 103+ return $this->getContext()->getLang();
 104+ }
 105+
 106+ /**
 107+ * Get the Skin object
 108+ *
 109+ * @return Skin
 110+ */
 111+ public function getSkin() {
 112+ return $this->getContext()->getSkin();
 113+ }
 114+
 115+ /**
 116+ * Get a Message object with context set
 117+ * Parameters are the same as wfMessage()
 118+ *
 119+ * @return Message object
 120+ */
 121+ public function msg( /* $args */ ) {
 122+ return call_user_func_array( array( $this->getContext(), 'msg' ), func_get_args() );
 123+ }
 124+}
 125+
Property changes on: trunk/phase3/includes/context/ContextSource.php
___________________________________________________________________
Added: svn:eol-style
1126 + native
Index: trunk/phase3/includes/context/DerivativeContext.php
@@ -0,0 +1,212 @@
 2+<?php
 3+/**
 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+ *
 21+ * @since 1.19
 22+ *
 23+ * @author Daniel Friesen
 24+ * @file
 25+ */
 26+
 27+/**
 28+ * An IContextSource implementation which will inherit context from another source
 29+ * but allow individual pieces of context to be changed locally
 30+ * eg: A ContextSource that can inherit from the main RequestContext but have
 31+ * a different Title instance set on it.
 32+ */
 33+class DerivativeContext extends ContextSource {
 34+
 35+ /**
 36+ * @var WebRequest
 37+ */
 38+ private $request;
 39+
 40+ /**
 41+ * @var Title
 42+ */
 43+ private $title;
 44+
 45+ /**
 46+ * @var OutputPage
 47+ */
 48+ private $output;
 49+
 50+ /**
 51+ * @var User
 52+ */
 53+ private $user;
 54+
 55+ /**
 56+ * @var Language
 57+ */
 58+ private $lang;
 59+
 60+ /**
 61+ * @var Skin
 62+ */
 63+ private $skin;
 64+
 65+ /**
 66+ * Constructor
 67+ * @param $context IContextSource Context to inherit from
 68+ */
 69+ public function __construct( IContextSource $context ) {
 70+ $this->setContext( $context );
 71+ }
 72+
 73+ /**
 74+ * Set the WebRequest object
 75+ *
 76+ * @param $r WebRequest object
 77+ */
 78+ public function setRequest( WebRequest $r ) {
 79+ $this->request = $r;
 80+ }
 81+
 82+ /**
 83+ * Get the WebRequest object
 84+ *
 85+ * @return WebRequest
 86+ */
 87+ public function getRequest() {
 88+ if ( !is_null( $this->request ) ) {
 89+ return $this->request;
 90+ } else {
 91+ return $this->getContext()->getRequest();
 92+ }
 93+ }
 94+
 95+ /**
 96+ * Set the Title object
 97+ *
 98+ * @param $t Title object
 99+ */
 100+ public function setTitle( Title $t ) {
 101+ $this->title = $t;
 102+ }
 103+
 104+ /**
 105+ * Get the Title object
 106+ *
 107+ * @return Title
 108+ */
 109+ public function getTitle() {
 110+ if ( !is_null( $this->title ) ) {
 111+ return $this->title;
 112+ } else {
 113+ return $this->getContext()->getTitle();
 114+ }
 115+ }
 116+
 117+ /**
 118+ * @param $o OutputPage
 119+ */
 120+ public function setOutput( OutputPage $o ) {
 121+ $this->output = $o;
 122+ }
 123+
 124+ /**
 125+ * Get the OutputPage object
 126+ *
 127+ * @return OutputPage object
 128+ */
 129+ public function getOutput() {
 130+ if ( !is_null( $this->output ) ) {
 131+ return $this->output;
 132+ } else {
 133+ return $this->getContext()->getOutput();
 134+ }
 135+ }
 136+
 137+ /**
 138+ * Set the User object
 139+ *
 140+ * @param $u User
 141+ */
 142+ public function setUser( User $u ) {
 143+ $this->user = $u;
 144+ }
 145+
 146+ /**
 147+ * Get the User object
 148+ *
 149+ * @return User
 150+ */
 151+ public function getUser() {
 152+ if ( !is_null( $this->user ) ) {
 153+ return $this->user;
 154+ } else {
 155+ return $this->getContext()->getUser();
 156+ }
 157+ }
 158+
 159+ /**
 160+ * Set the Language object
 161+ *
 162+ * @param $l Mixed Language instance or language code
 163+ */
 164+ public function setLang( $l ) {
 165+ if ( $l instanceof Language ) {
 166+ $this->lang = $l;
 167+ } elseif ( is_string( $l ) ) {
 168+ $l = self::sanitizeLangCode( $l );
 169+ $obj = Language::factory( $l );
 170+ $this->lang = $obj;
 171+ } else {
 172+ throw new MWException( __METHOD__ . " was passed an invalid type of data." );
 173+ }
 174+ }
 175+
 176+ /**
 177+ * Get the Language object
 178+ *
 179+ * @return Language
 180+ */
 181+ public function getLang() {
 182+ if ( !is_null( $this->lang ) ) {
 183+ return $this->lang;
 184+ } else {
 185+ return $this->getContext()->getLang();
 186+ }
 187+ }
 188+
 189+ /**
 190+ * Set the Skin object
 191+ *
 192+ * @param $s Skin
 193+ */
 194+ public function setSkin( Skin $s ) {
 195+ $this->skin = clone $s;
 196+ $this->skin->setContext( $this );
 197+ }
 198+
 199+ /**
 200+ * Get the Skin object
 201+ *
 202+ * @return Skin
 203+ */
 204+ public function getSkin() {
 205+ if ( !is_null( $this->skin ) ) {
 206+ return $this->skin;
 207+ } else {
 208+ return $this->getContext()->getSkin();
 209+ }
 210+ }
 211+
 212+}
 213+
Property changes on: trunk/phase3/includes/context/DerivativeContext.php
___________________________________________________________________
Added: svn:eol-style
1214 + native
Index: trunk/phase3/includes/AutoLoader.php
@@ -46,7 +46,6 @@
4747 'ConfEditor' => 'includes/ConfEditor.php',
4848 'ConfEditorParseError' => 'includes/ConfEditor.php',
4949 'ConfEditorToken' => 'includes/ConfEditor.php',
50 - 'ContextSource' => 'includes/RequestContext.php',
5150 'Cookie' => 'includes/Cookie.php',
5251 'CookieJar' => 'includes/Cookie.php',
5352 'DeferrableUpdate' => 'includes/DeferredUpdates.php',
@@ -116,7 +115,6 @@
117116 'HTMLTextField' => 'includes/HTMLForm.php',
118117 'Http' => 'includes/HttpFunctions.php',
119118 'HttpRequest' => 'includes/HttpFunctions.old.php',
120 - 'IContextSource' => 'includes/RequestContext.php',
121119 'IcuCollation' => 'includes/Collation.php',
122120 'IdentityCollation' => 'includes/Collation.php',
123121 'ImageGallery' => 'includes/ImageGallery.php',
@@ -139,6 +137,7 @@
140138 'Linker' => 'includes/Linker.php',
141139 'LinkFilter' => 'includes/LinkFilter.php',
142140 'LinksUpdate' => 'includes/LinksUpdate.php',
 141+ 'ListMessageParser' => 'includes/ListMessageParser.php',
143142 'LocalisationCache' => 'includes/LocalisationCache.php',
144143 'LocalisationCache_BulkLoad' => 'includes/LocalisationCache.php',
145144 'LogEventsList' => 'includes/LogEventsList.php',
@@ -187,7 +186,6 @@
188187 'RegexlikeReplacer' => 'includes/StringUtils.php',
189188 'ReplacementArray' => 'includes/StringUtils.php',
190189 'Replacer' => 'includes/StringUtils.php',
191 - 'RequestContext' => 'includes/RequestContext.php',
192190 'ReverseChronologicalPager' => 'includes/Pager.php',
193191 'RevisionItemBase' => 'includes/RevisionList.php',
194192 'RevisionListBase' => 'includes/RevisionList.php',
@@ -200,6 +198,7 @@
201199 'SiteStatsInit' => 'includes/SiteStats.php',
202200 'SiteStatsUpdate' => 'includes/SiteStats.php',
203201 'Skin' => 'includes/Skin.php',
 202+ 'Skin2' => 'includes/Skin2.php',
204203 'SkinLegacy' => 'includes/SkinLegacy.php',
205204 'SkinTemplate' => 'includes/SkinTemplate.php',
206205 'SpecialCreateAccount' => 'includes/SpecialPage.php',
@@ -384,6 +383,12 @@
385384 'DatabaseConf' => 'includes/conf/DatabaseConf.php',
386385 'DefaultSettings' => 'includes/conf/DefaultSettings.php',
387386
 387+ # includes/context
 388+ 'ContextSource' => 'includes/context/ContextSource.php',
 389+ 'DerivativeContext' => 'includes/context/DerivativeContext.php',
 390+ 'IContextSource' => 'includes/context/IContextSource.php',
 391+ 'RequestContext' => 'includes/context/RequestContext.php',
 392+
388393 # includes/db
389394 'Blob' => 'includes/db/DatabaseUtility.php',
390395 'ChronologyProtector' => 'includes/db/LBFactory.php',
@@ -533,6 +538,7 @@
534539 'JavaScriptMinifier' => 'includes/libs/JavaScriptMinifier.php',
535540 'JSMinPlus' => 'includes/libs/jsminplus.php',
536541 'JSParser' => 'includes/libs/jsminplus.php',
 542+ 'simple_html_dom' => 'includes/libs/simple_html_dom.php',
537543
538544 # includes/logging
539545 'LogEntry' => 'includes/logging/LogEntry.php',

Follow-up revisions

RevisionCommit summaryAuthorDate
r97180Follow up r97179; Err... ;) you didn't see AutoLoader lines for half-finished...dantman17:47, 15 September 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r97161Implement DerivativeContext. Can be used to inherit context from another cont...dantman15:47, 15 September 2011

Status & tagging log