Merge "Revert "Avoid doNotifyQueueEmpty() race conditions for Redis""
[mediawiki.git] / includes / context / DerivativeContext.php
blob1b881e49ddb56083cb3dadac6217907078f8b108
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @author Daniel Friesen
19 * @file
22 /**
23 * An IContextSource implementation which will inherit context from another source
24 * but allow individual pieces of context to be changed locally
25 * eg: A ContextSource that can inherit from the main RequestContext but have
26 * a different Title instance set on it.
27 * @since 1.19
29 class DerivativeContext extends ContextSource implements MutableContext {
30 /**
31 * @var WebRequest
33 private $request;
35 /**
36 * @var Title
38 private $title;
40 /**
41 * @var WikiPage
43 private $wikipage;
45 /**
46 * @var OutputPage
48 private $output;
50 /**
51 * @var User
53 private $user;
55 /**
56 * @var Language
58 private $lang;
60 /**
61 * @var Skin
63 private $skin;
65 /**
66 * @var Config
68 private $config;
70 /**
71 * @var Stats
73 private $stats;
75 /**
76 * @var Timing
78 private $timing;
80 /**
81 * Constructor
82 * @param IContextSource $context Context to inherit from
84 public function __construct( IContextSource $context ) {
85 $this->setContext( $context );
88 /**
89 * Set the SiteConfiguration object
91 * @param Config $s
93 public function setConfig( Config $s ) {
94 $this->config = $s;
97 /**
98 * Get the Config object
100 * @return Config
102 public function getConfig() {
103 if ( !is_null( $this->config ) ) {
104 return $this->config;
105 } else {
106 return $this->getContext()->getConfig();
111 * Get the stats object
113 * @return BufferingStatsdDataFactory
115 public function getStats() {
116 if ( !is_null( $this->stats ) ) {
117 return $this->stats;
118 } else {
119 return $this->getContext()->getStats();
124 * Get the timing object
126 * @return Timing
128 public function getTiming() {
129 if ( !is_null( $this->timing ) ) {
130 return $this->timing;
131 } else {
132 return $this->getContext()->getTiming();
137 * Set the WebRequest object
139 * @param WebRequest $r
141 public function setRequest( WebRequest $r ) {
142 $this->request = $r;
146 * Get the WebRequest object
148 * @return WebRequest
150 public function getRequest() {
151 if ( !is_null( $this->request ) ) {
152 return $this->request;
153 } else {
154 return $this->getContext()->getRequest();
159 * Set the Title object
161 * @param Title $t
163 public function setTitle( Title $t ) {
164 $this->title = $t;
168 * Get the Title object
170 * @return Title|null
172 public function getTitle() {
173 if ( !is_null( $this->title ) ) {
174 return $this->title;
175 } else {
176 return $this->getContext()->getTitle();
181 * Check whether a WikiPage object can be get with getWikiPage().
182 * Callers should expect that an exception is thrown from getWikiPage()
183 * if this method returns false.
185 * @since 1.19
186 * @return bool
188 public function canUseWikiPage() {
189 if ( $this->wikipage !== null ) {
190 return true;
191 } elseif ( $this->title !== null ) {
192 return $this->title->canExist();
193 } else {
194 return $this->getContext()->canUseWikiPage();
199 * Set the WikiPage object
201 * @since 1.19
202 * @param WikiPage $p
204 public function setWikiPage( WikiPage $p ) {
205 $this->wikipage = $p;
209 * Get the WikiPage object.
210 * May throw an exception if there's no Title object set or the Title object
211 * belongs to a special namespace that doesn't have WikiPage, so use first
212 * canUseWikiPage() to check whether this method can be called safely.
214 * @since 1.19
215 * @return WikiPage
217 public function getWikiPage() {
218 if ( !is_null( $this->wikipage ) ) {
219 return $this->wikipage;
220 } else {
221 return $this->getContext()->getWikiPage();
226 * Set the OutputPage object
228 * @param OutputPage $o
230 public function setOutput( OutputPage $o ) {
231 $this->output = $o;
235 * Get the OutputPage object
237 * @return OutputPage
239 public function getOutput() {
240 if ( !is_null( $this->output ) ) {
241 return $this->output;
242 } else {
243 return $this->getContext()->getOutput();
248 * Set the User object
250 * @param User $u
252 public function setUser( User $u ) {
253 $this->user = $u;
257 * Get the User object
259 * @return User
261 public function getUser() {
262 if ( !is_null( $this->user ) ) {
263 return $this->user;
264 } else {
265 return $this->getContext()->getUser();
270 * Set the Language object
272 * @param Language|string $l Language instance or language code
273 * @throws MWException
274 * @since 1.19
276 public function setLanguage( $l ) {
277 if ( $l instanceof Language ) {
278 $this->lang = $l;
279 } elseif ( is_string( $l ) ) {
280 $l = RequestContext::sanitizeLangCode( $l );
281 $obj = Language::factory( $l );
282 $this->lang = $obj;
283 } else {
284 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
289 * Get the Language object
291 * @return Language
292 * @since 1.19
294 public function getLanguage() {
295 if ( !is_null( $this->lang ) ) {
296 return $this->lang;
297 } else {
298 return $this->getContext()->getLanguage();
303 * Set the Skin object
305 * @param Skin $s
307 public function setSkin( Skin $s ) {
308 $this->skin = clone $s;
309 $this->skin->setContext( $this );
313 * Get the Skin object
315 * @return Skin
317 public function getSkin() {
318 if ( !is_null( $this->skin ) ) {
319 return $this->skin;
320 } else {
321 return $this->getContext()->getSkin();
326 * Get a message using the current context.
328 * This can't just inherit from ContextSource, since then
329 * it would set only the original context, and not take
330 * into account any changes.
332 * @param mixed $args,... Arguments to wfMessage
333 * @return Message
335 public function msg() {
336 $args = func_get_args();
338 return call_user_func_array( 'wfMessage', $args )->setContext( $this );