r92387 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92386‎ | r92387 | r92388 >
Date:07:47, 17 July 2011
Author:ialex
Status:ok
Tags:
Comment:
* Expanded documentation, added GPL header and changed the @author tag to use my real name
* Made RequestContext implement IContextSource
* Added ContextSource::msg()
* Made ContextSource fall back to RequestContext::getMain() when there's no context set
Modified paths:
  • /trunk/phase3/includes/RequestContext.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/RequestContext.php
@@ -1,17 +1,83 @@
22 <?php
33 /**
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+ *
621 * @since 1.18
722 *
8 - * @author IAlex
 23+ * @author Alexandre Emsenhuber
924 * @author Daniel Friesen
1025 * @file
1126 */
1227
13 -class RequestContext {
 28+/**
 29+ * Interface for objects which can provide a context on request.
 30+ */
 31+interface IContextSource {
1432
1533 /**
 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+ /**
1682 * @var WebRequest
1783 */
1884 private $request;
@@ -234,54 +300,6 @@
235301 }
236302
237303 /**
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 -/**
286304 * The simplest way of implementing IContextSource is to hold a RequestContext as a
287305 * member variable and provide accessors to it.
288306 */
@@ -298,6 +316,11 @@
299317 * @return RequestContext
300318 */
301319 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+ }
302325 return $this->context;
303326 }
304327
@@ -316,7 +339,7 @@
317340 * @return WebRequest
318341 */
319342 public function getRequest() {
320 - return $this->context->getRequest();
 343+ return $this->getContext()->getRequest();
321344 }
322345
323346 /**
@@ -325,7 +348,7 @@
326349 * @return Title
327350 */
328351 public function getTitle() {
329 - return $this->context->getTitle();
 352+ return $this->getContext()->getTitle();
330353 }
331354
332355 /**
@@ -334,7 +357,7 @@
335358 * @return OutputPage object
336359 */
337360 public function getOutput() {
338 - return $this->context->getOutput();
 361+ return $this->getContext()->getOutput();
339362 }
340363
341364 /**
@@ -343,7 +366,7 @@
344367 * @return User
345368 */
346369 public function getUser() {
347 - return $this->context->getUser();
 370+ return $this->getContext()->getUser();
348371 }
349372
350373 /**
@@ -352,7 +375,7 @@
353376 * @return Language
354377 */
355378 public function getLang() {
356 - return $this->context->getLang();
 379+ return $this->getContext()->getLang();
357380 }
358381
359382 /**
@@ -361,6 +384,16 @@
362385 * @return Skin
363386 */
364387 public function getSkin() {
365 - return $this->context->getSkin();
 388+ return $this->getContext()->getSkin();
366389 }
 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+ }
367400 }

Status & tagging log