Merge "objectcache: Add BagOStuff::READ_VERIFIED flag to get()"
[mediawiki.git] / includes / context / DerivativeContext.php
blob09c39396966220e03001428f0ec182b1b9b96eac
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 * Constructor
77 * @param IContextSource $context Context to inherit from
79 public function __construct( IContextSource $context ) {
80 $this->setContext( $context );
83 /**
84 * Set the SiteConfiguration object
86 * @param Config $s
88 public function setConfig( Config $s ) {
89 $this->config = $s;
92 /**
93 * Get the Config object
95 * @return Config
97 public function getConfig() {
98 if ( !is_null( $this->config ) ) {
99 return $this->config;
100 } else {
101 return $this->getContext()->getConfig();
106 * Get the stats object
108 * @return BufferingStatsdDataFactory
110 public function getStats() {
111 if ( !is_null( $this->stats ) ) {
112 return $this->stats;
113 } else {
114 return $this->getContext()->getStats();
119 * Set the WebRequest object
121 * @param WebRequest $r
123 public function setRequest( WebRequest $r ) {
124 $this->request = $r;
128 * Get the WebRequest object
130 * @return WebRequest
132 public function getRequest() {
133 if ( !is_null( $this->request ) ) {
134 return $this->request;
135 } else {
136 return $this->getContext()->getRequest();
141 * Set the Title object
143 * @param Title $t
145 public function setTitle( Title $t ) {
146 $this->title = $t;
150 * Get the Title object
152 * @return Title|null
154 public function getTitle() {
155 if ( !is_null( $this->title ) ) {
156 return $this->title;
157 } else {
158 return $this->getContext()->getTitle();
163 * Check whether a WikiPage object can be get with getWikiPage().
164 * Callers should expect that an exception is thrown from getWikiPage()
165 * if this method returns false.
167 * @since 1.19
168 * @return bool
170 public function canUseWikiPage() {
171 if ( $this->wikipage !== null ) {
172 return true;
173 } elseif ( $this->title !== null ) {
174 return $this->title->canExist();
175 } else {
176 return $this->getContext()->canUseWikiPage();
181 * Set the WikiPage object
183 * @since 1.19
184 * @param WikiPage $p
186 public function setWikiPage( WikiPage $p ) {
187 $this->wikipage = $p;
191 * Get the WikiPage object.
192 * May throw an exception if there's no Title object set or the Title object
193 * belongs to a special namespace that doesn't have WikiPage, so use first
194 * canUseWikiPage() to check whether this method can be called safely.
196 * @since 1.19
197 * @return WikiPage
199 public function getWikiPage() {
200 if ( !is_null( $this->wikipage ) ) {
201 return $this->wikipage;
202 } else {
203 return $this->getContext()->getWikiPage();
208 * Set the OutputPage object
210 * @param OutputPage $o
212 public function setOutput( OutputPage $o ) {
213 $this->output = $o;
217 * Get the OutputPage object
219 * @return OutputPage
221 public function getOutput() {
222 if ( !is_null( $this->output ) ) {
223 return $this->output;
224 } else {
225 return $this->getContext()->getOutput();
230 * Set the User object
232 * @param User $u
234 public function setUser( User $u ) {
235 $this->user = $u;
239 * Get the User object
241 * @return User
243 public function getUser() {
244 if ( !is_null( $this->user ) ) {
245 return $this->user;
246 } else {
247 return $this->getContext()->getUser();
252 * Set the Language object
254 * @param Language|string $l Language instance or language code
255 * @throws MWException
256 * @since 1.19
258 public function setLanguage( $l ) {
259 if ( $l instanceof Language ) {
260 $this->lang = $l;
261 } elseif ( is_string( $l ) ) {
262 $l = RequestContext::sanitizeLangCode( $l );
263 $obj = Language::factory( $l );
264 $this->lang = $obj;
265 } else {
266 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
271 * Get the Language object
273 * @return Language
274 * @since 1.19
276 public function getLanguage() {
277 if ( !is_null( $this->lang ) ) {
278 return $this->lang;
279 } else {
280 return $this->getContext()->getLanguage();
285 * Set the Skin object
287 * @param Skin $s
289 public function setSkin( Skin $s ) {
290 $this->skin = clone $s;
291 $this->skin->setContext( $this );
295 * Get the Skin object
297 * @return Skin
299 public function getSkin() {
300 if ( !is_null( $this->skin ) ) {
301 return $this->skin;
302 } else {
303 return $this->getContext()->getSkin();
308 * Get a message using the current context.
310 * This can't just inherit from ContextSource, since then
311 * it would set only the original context, and not take
312 * into account any changes.
314 * @param mixed $args,... Arguments to wfMessage
315 * @return Message
317 public function msg() {
318 $args = func_get_args();
320 return call_user_func_array( 'wfMessage', $args )->setContext( $this );