Merge "Improve "selfmove" message's wording"
[lhc/web/wiklou.git] / includes / context / DerivativeContext.php
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.
7 *
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.
12 *
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
17 *
18 * @author Daniel Friesen
19 * @file
20 */
21 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
22 use MediaWiki\MediaWikiServices;
23
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
30 */
31 class DerivativeContext extends ContextSource implements MutableContext {
32 /**
33 * @var WebRequest
34 */
35 private $request;
36
37 /**
38 * @var Title
39 */
40 private $title;
41
42 /**
43 * @var WikiPage
44 */
45 private $wikipage;
46
47 /**
48 * @var OutputPage
49 */
50 private $output;
51
52 /**
53 * @var User
54 */
55 private $user;
56
57 /**
58 * @var Language
59 */
60 private $lang;
61
62 /**
63 * @var Skin
64 */
65 private $skin;
66
67 /**
68 * @var Config
69 */
70 private $config;
71
72 /**
73 * @var Timing
74 */
75 private $timing;
76
77 /**
78 * @param IContextSource $context Context to inherit from
79 */
80 public function __construct( IContextSource $context ) {
81 $this->setContext( $context );
82 }
83
84 /**
85 * Set the SiteConfiguration object
86 *
87 * @param Config $s
88 */
89 public function setConfig( Config $s ) {
90 $this->config = $s;
91 }
92
93 /**
94 * Get the Config object
95 *
96 * @return Config
97 */
98 public function getConfig() {
99 if ( !is_null( $this->config ) ) {
100 return $this->config;
101 } else {
102 return $this->getContext()->getConfig();
103 }
104 }
105
106 /**
107 * Get the stats object
108 *
109 * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
110 *
111 * @return IBufferingStatsdDataFactory
112 */
113 public function getStats() {
114 return MediaWikiServices::getInstance()->getStatsdDataFactory();
115 }
116
117 /**
118 * Get the timing object
119 *
120 * @return Timing
121 */
122 public function getTiming() {
123 if ( !is_null( $this->timing ) ) {
124 return $this->timing;
125 } else {
126 return $this->getContext()->getTiming();
127 }
128 }
129
130 /**
131 * Set the WebRequest object
132 *
133 * @param WebRequest $r
134 */
135 public function setRequest( WebRequest $r ) {
136 $this->request = $r;
137 }
138
139 /**
140 * Get the WebRequest object
141 *
142 * @return WebRequest
143 */
144 public function getRequest() {
145 if ( !is_null( $this->request ) ) {
146 return $this->request;
147 } else {
148 return $this->getContext()->getRequest();
149 }
150 }
151
152 /**
153 * Set the Title object
154 *
155 * @param Title $t
156 */
157 public function setTitle( Title $t ) {
158 $this->title = $t;
159 }
160
161 /**
162 * Get the Title object
163 *
164 * @return Title|null
165 */
166 public function getTitle() {
167 if ( !is_null( $this->title ) ) {
168 return $this->title;
169 } else {
170 return $this->getContext()->getTitle();
171 }
172 }
173
174 /**
175 * Check whether a WikiPage object can be get with getWikiPage().
176 * Callers should expect that an exception is thrown from getWikiPage()
177 * if this method returns false.
178 *
179 * @since 1.19
180 * @return bool
181 */
182 public function canUseWikiPage() {
183 if ( $this->wikipage !== null ) {
184 return true;
185 } elseif ( $this->title !== null ) {
186 return $this->title->canExist();
187 } else {
188 return $this->getContext()->canUseWikiPage();
189 }
190 }
191
192 /**
193 * Set the WikiPage object
194 *
195 * @since 1.19
196 * @param WikiPage $p
197 */
198 public function setWikiPage( WikiPage $p ) {
199 $this->wikipage = $p;
200 }
201
202 /**
203 * Get the WikiPage object.
204 * May throw an exception if there's no Title object set or the Title object
205 * belongs to a special namespace that doesn't have WikiPage, so use first
206 * canUseWikiPage() to check whether this method can be called safely.
207 *
208 * @since 1.19
209 * @return WikiPage
210 */
211 public function getWikiPage() {
212 if ( !is_null( $this->wikipage ) ) {
213 return $this->wikipage;
214 } else {
215 return $this->getContext()->getWikiPage();
216 }
217 }
218
219 /**
220 * Set the OutputPage object
221 *
222 * @param OutputPage $o
223 */
224 public function setOutput( OutputPage $o ) {
225 $this->output = $o;
226 }
227
228 /**
229 * Get the OutputPage object
230 *
231 * @return OutputPage
232 */
233 public function getOutput() {
234 if ( !is_null( $this->output ) ) {
235 return $this->output;
236 } else {
237 return $this->getContext()->getOutput();
238 }
239 }
240
241 /**
242 * Set the User object
243 *
244 * @param User $u
245 */
246 public function setUser( User $u ) {
247 $this->user = $u;
248 }
249
250 /**
251 * Get the User object
252 *
253 * @return User
254 */
255 public function getUser() {
256 if ( !is_null( $this->user ) ) {
257 return $this->user;
258 } else {
259 return $this->getContext()->getUser();
260 }
261 }
262
263 /**
264 * Set the Language object
265 *
266 * @param Language|string $l Language instance or language code
267 * @throws MWException
268 * @since 1.19
269 */
270 public function setLanguage( $l ) {
271 if ( $l instanceof Language ) {
272 $this->lang = $l;
273 } elseif ( is_string( $l ) ) {
274 $l = RequestContext::sanitizeLangCode( $l );
275 $obj = Language::factory( $l );
276 $this->lang = $obj;
277 } else {
278 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
279 }
280 }
281
282 /**
283 * Get the Language object
284 *
285 * @return Language
286 * @since 1.19
287 */
288 public function getLanguage() {
289 if ( !is_null( $this->lang ) ) {
290 return $this->lang;
291 } else {
292 return $this->getContext()->getLanguage();
293 }
294 }
295
296 /**
297 * Set the Skin object
298 *
299 * @param Skin $s
300 */
301 public function setSkin( Skin $s ) {
302 $this->skin = clone $s;
303 $this->skin->setContext( $this );
304 }
305
306 /**
307 * Get the Skin object
308 *
309 * @return Skin
310 */
311 public function getSkin() {
312 if ( !is_null( $this->skin ) ) {
313 return $this->skin;
314 } else {
315 return $this->getContext()->getSkin();
316 }
317 }
318
319 /**
320 * Get a message using the current context.
321 *
322 * This can't just inherit from ContextSource, since then
323 * it would set only the original context, and not take
324 * into account any changes.
325 *
326 * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
327 * or a MessageSpecifier.
328 * @param mixed $args,... Arguments to wfMessage
329 * @return Message
330 */
331 public function msg( $key ) {
332 $args = func_get_args();
333
334 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
335 }
336 }