r97161 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97160‎ | r97161 | r97162 >
Date:15:47, 15 September 2011
Author:dantman
Status:resolved (Comments)
Tags:
Comment:
Implement DerivativeContext. Can be used to inherit context from another context while overriding only parts of the context.
Modified paths:
  • /trunk/phase3/includes/RequestContext.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/RequestContext.php
@@ -444,3 +444,191 @@
445445 return call_user_func_array( array( $this->getContext(), 'msg' ), func_get_args() );
446446 }
447447 }
 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+

Follow-up revisions

RevisionCommit summaryAuthorDate
r97173Merged revisions 97087,97091-97092,97094,97096-97098,97100-97101,97103,97136,...dantman16:19, 15 September 2011
r97179Separate RequestContext.php into separate files inside of context/...dantman17:42, 15 September 2011

Comments

#Comment by Nikerabbit (talk | contribs)   16:58, 15 September 2011

The file is getting longish, maybe move this class to own file?

#Comment by Nikerabbit (talk | contribs)   04:46, 16 September 2011

And how about a @since tag? :)

#Comment by Dantman (talk | contribs)   04:48, 16 September 2011

Implicitly added by r97179 when I adapted the headers to be class specific.

Status & tagging log