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