Simplify the check to make it more understandable
[mediawiki.git] / includes / context / DerivativeContext.php
blobee817dbd265329277fd545496e79a99ba92607cd
1 <?php
2 /**
3 * Request-dependant objects containers.
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.
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.
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
20 * @since 1.19
22 * @author Daniel Friesen
23 * @file
26 /**
27 * An IContextSource implementation which will inherit context from another source
28 * but allow individual pieces of context to be changed locally
29 * eg: A ContextSource that can inherit from the main RequestContext but have
30 * a different Title instance set on it.
32 class DerivativeContext extends ContextSource {
34 /**
35 * @var WebRequest
37 private $request;
39 /**
40 * @var Title
42 private $title;
44 /**
45 * @var WikiPage
47 private $wikipage;
49 /**
50 * @var OutputPage
52 private $output;
54 /**
55 * @var User
57 private $user;
59 /**
60 * @var Language
62 private $lang;
64 /**
65 * @var Skin
67 private $skin;
69 /**
70 * Constructor
71 * @param $context IContextSource Context to inherit from
73 public function __construct( IContextSource $context ) {
74 $this->setContext( $context );
77 /**
78 * Set the WebRequest object
80 * @param $r WebRequest object
82 public function setRequest( WebRequest $r ) {
83 $this->request = $r;
86 /**
87 * Get the WebRequest object
89 * @return WebRequest
91 public function getRequest() {
92 if ( !is_null( $this->request ) ) {
93 return $this->request;
94 } else {
95 return $this->getContext()->getRequest();
99 /**
100 * Set the Title object
102 * @param $t Title object
104 public function setTitle( Title $t ) {
105 $this->title = $t;
109 * Get the Title object
111 * @return Title
113 public function getTitle() {
114 if ( !is_null( $this->title ) ) {
115 return $this->title;
116 } else {
117 return $this->getContext()->getTitle();
122 * Set the WikiPage object
124 * @since 1.19
125 * @param $p WikiPage object
127 public function setWikiPage( WikiPage $p ) {
128 $this->wikipage = $p;
132 * Get the WikiPage object
134 * @since 1.19
135 * @return WikiPage
137 public function getWikiPage() {
138 if ( !is_null( $this->wikipage ) ) {
139 return $this->wikipage;
140 } else {
141 return $this->getContext()->getWikiPage();
146 * Set the OutputPage object
148 * @param $o OutputPage
150 public function setOutput( OutputPage $o ) {
151 $this->output = $o;
155 * Get the OutputPage object
157 * @return OutputPage object
159 public function getOutput() {
160 if ( !is_null( $this->output ) ) {
161 return $this->output;
162 } else {
163 return $this->getContext()->getOutput();
168 * Set the User object
170 * @param $u User
172 public function setUser( User $u ) {
173 $this->user = $u;
177 * Get the User object
179 * @return User
181 public function getUser() {
182 if ( !is_null( $this->user ) ) {
183 return $this->user;
184 } else {
185 return $this->getContext()->getUser();
190 * Set the Language object
192 * @deprecated 1.19 Use setLanguage instead
193 * @param $l Mixed Language instance or language code
195 public function setLang( $l ) {
196 wfDeprecated( __METHOD__, '1.19' );
197 $this->setLanguage( $l );
201 * Set the Language object
203 * @param $l Mixed Language instance or language code
204 * @since 1.19
206 public function setLanguage( $l ) {
207 if ( $l instanceof Language ) {
208 $this->lang = $l;
209 } elseif ( is_string( $l ) ) {
210 $l = RequestContext::sanitizeLangCode( $l );
211 $obj = Language::factory( $l );
212 $this->lang = $obj;
213 } else {
214 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
219 * @deprecated 1.19 Use getLanguage instead
220 * @return Language
222 public function getLang() {
223 wfDeprecated( __METHOD__, '1.19' );
224 $this->getLanguage();
228 * Get the Language object
230 * @return Language
231 * @since 1.19
233 public function getLanguage() {
234 if ( !is_null( $this->lang ) ) {
235 return $this->lang;
236 } else {
237 return $this->getContext()->getLanguage();
242 * Set the Skin object
244 * @param $s Skin
246 public function setSkin( Skin $s ) {
247 $this->skin = clone $s;
248 $this->skin->setContext( $this );
252 * Get the Skin object
254 * @return Skin
256 public function getSkin() {
257 if ( !is_null( $this->skin ) ) {
258 return $this->skin;
259 } else {
260 return $this->getContext()->getSkin();