Deprecate WikiRevision::$importer
[mediawiki.git] / includes / context / DerivativeContext.php
blobe77a058d1d8f9a1b011decbfcfd47ea321efef7f
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
21 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
22 use MediaWiki\MediaWikiServices;
24 /**
25 * An IContextSource implementation which will inherit context from another source
26 * but allow individual pieces of context to be changed locally
27 * eg: A ContextSource that can inherit from the main RequestContext but have
28 * a different Title instance set on it.
29 * @since 1.19
31 class DerivativeContext extends ContextSource implements MutableContext {
32 /**
33 * @var WebRequest
35 private $request;
37 /**
38 * @var Title
40 private $title;
42 /**
43 * @var WikiPage
45 private $wikipage;
47 /**
48 * @var OutputPage
50 private $output;
52 /**
53 * @var User
55 private $user;
57 /**
58 * @var Language
60 private $lang;
62 /**
63 * @var Skin
65 private $skin;
67 /**
68 * @var Config
70 private $config;
72 /**
73 * @var Timing
75 private $timing;
77 /**
78 * Constructor
79 * @param IContextSource $context Context to inherit from
81 public function __construct( IContextSource $context ) {
82 $this->setContext( $context );
85 /**
86 * Set the SiteConfiguration object
88 * @param Config $s
90 public function setConfig( Config $s ) {
91 $this->config = $s;
94 /**
95 * Get the Config object
97 * @return Config
99 public function getConfig() {
100 if ( !is_null( $this->config ) ) {
101 return $this->config;
102 } else {
103 return $this->getContext()->getConfig();
108 * Get the stats object
110 * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
112 * @return StatsdDataFactory
114 public function getStats() {
115 return MediaWikiServices::getInstance()->getStatsdDataFactory();
119 * Get the timing object
121 * @return Timing
123 public function getTiming() {
124 if ( !is_null( $this->timing ) ) {
125 return $this->timing;
126 } else {
127 return $this->getContext()->getTiming();
132 * Set the WebRequest object
134 * @param WebRequest $r
136 public function setRequest( WebRequest $r ) {
137 $this->request = $r;
141 * Get the WebRequest object
143 * @return WebRequest
145 public function getRequest() {
146 if ( !is_null( $this->request ) ) {
147 return $this->request;
148 } else {
149 return $this->getContext()->getRequest();
154 * Set the Title object
156 * @param Title $t
158 public function setTitle( Title $t ) {
159 $this->title = $t;
163 * Get the Title object
165 * @return Title|null
167 public function getTitle() {
168 if ( !is_null( $this->title ) ) {
169 return $this->title;
170 } else {
171 return $this->getContext()->getTitle();
176 * Check whether a WikiPage object can be get with getWikiPage().
177 * Callers should expect that an exception is thrown from getWikiPage()
178 * if this method returns false.
180 * @since 1.19
181 * @return bool
183 public function canUseWikiPage() {
184 if ( $this->wikipage !== null ) {
185 return true;
186 } elseif ( $this->title !== null ) {
187 return $this->title->canExist();
188 } else {
189 return $this->getContext()->canUseWikiPage();
194 * Set the WikiPage object
196 * @since 1.19
197 * @param WikiPage $p
199 public function setWikiPage( WikiPage $p ) {
200 $this->wikipage = $p;
204 * Get the WikiPage object.
205 * May throw an exception if there's no Title object set or the Title object
206 * belongs to a special namespace that doesn't have WikiPage, so use first
207 * canUseWikiPage() to check whether this method can be called safely.
209 * @since 1.19
210 * @return WikiPage
212 public function getWikiPage() {
213 if ( !is_null( $this->wikipage ) ) {
214 return $this->wikipage;
215 } else {
216 return $this->getContext()->getWikiPage();
221 * Set the OutputPage object
223 * @param OutputPage $o
225 public function setOutput( OutputPage $o ) {
226 $this->output = $o;
230 * Get the OutputPage object
232 * @return OutputPage
234 public function getOutput() {
235 if ( !is_null( $this->output ) ) {
236 return $this->output;
237 } else {
238 return $this->getContext()->getOutput();
243 * Set the User object
245 * @param User $u
247 public function setUser( User $u ) {
248 $this->user = $u;
252 * Get the User object
254 * @return User
256 public function getUser() {
257 if ( !is_null( $this->user ) ) {
258 return $this->user;
259 } else {
260 return $this->getContext()->getUser();
265 * Set the Language object
267 * @param Language|string $l Language instance or language code
268 * @throws MWException
269 * @since 1.19
271 public function setLanguage( $l ) {
272 if ( $l instanceof Language ) {
273 $this->lang = $l;
274 } elseif ( is_string( $l ) ) {
275 $l = RequestContext::sanitizeLangCode( $l );
276 $obj = Language::factory( $l );
277 $this->lang = $obj;
278 } else {
279 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
284 * Get the Language object
286 * @return Language
287 * @since 1.19
289 public function getLanguage() {
290 if ( !is_null( $this->lang ) ) {
291 return $this->lang;
292 } else {
293 return $this->getContext()->getLanguage();
298 * Set the Skin object
300 * @param Skin $s
302 public function setSkin( Skin $s ) {
303 $this->skin = clone $s;
304 $this->skin->setContext( $this );
308 * Get the Skin object
310 * @return Skin
312 public function getSkin() {
313 if ( !is_null( $this->skin ) ) {
314 return $this->skin;
315 } else {
316 return $this->getContext()->getSkin();
321 * Get a message using the current context.
323 * This can't just inherit from ContextSource, since then
324 * it would set only the original context, and not take
325 * into account any changes.
327 * @param mixed $args,... Arguments to wfMessage
328 * @return Message
330 public function msg() {
331 $args = func_get_args();
333 return call_user_func_array( 'wfMessage', $args )->setContext( $this );