Merge "Rewrite pref cleanup script"
[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 * @param Config $config
85 */
86 public function setConfig( Config $config ) {
87 $this->config = $config;
88 }
89
90 /**
91 * @return Config
92 */
93 public function getConfig() {
94 if ( !is_null( $this->config ) ) {
95 return $this->config;
96 } else {
97 return $this->getContext()->getConfig();
98 }
99 }
100
101 /**
102 * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
103 *
104 * @return IBufferingStatsdDataFactory
105 */
106 public function getStats() {
107 return MediaWikiServices::getInstance()->getStatsdDataFactory();
108 }
109
110 /**
111 * @return Timing
112 */
113 public function getTiming() {
114 if ( !is_null( $this->timing ) ) {
115 return $this->timing;
116 } else {
117 return $this->getContext()->getTiming();
118 }
119 }
120
121 /**
122 * @param WebRequest $request
123 */
124 public function setRequest( WebRequest $request ) {
125 $this->request = $request;
126 }
127
128 /**
129 * @return WebRequest
130 */
131 public function getRequest() {
132 if ( !is_null( $this->request ) ) {
133 return $this->request;
134 } else {
135 return $this->getContext()->getRequest();
136 }
137 }
138
139 /**
140 * @param Title $title
141 */
142 public function setTitle( Title $title ) {
143 $this->title = $title;
144 }
145
146 /**
147 * @return Title|null
148 */
149 public function getTitle() {
150 if ( !is_null( $this->title ) ) {
151 return $this->title;
152 } else {
153 return $this->getContext()->getTitle();
154 }
155 }
156
157 /**
158 * Check whether a WikiPage object can be get with getWikiPage().
159 * Callers should expect that an exception is thrown from getWikiPage()
160 * if this method returns false.
161 *
162 * @since 1.19
163 * @return bool
164 */
165 public function canUseWikiPage() {
166 if ( $this->wikipage !== null ) {
167 return true;
168 } elseif ( $this->title !== null ) {
169 return $this->title->canExist();
170 } else {
171 return $this->getContext()->canUseWikiPage();
172 }
173 }
174
175 /**
176 * @since 1.19
177 * @param WikiPage $wikiPage
178 */
179 public function setWikiPage( WikiPage $wikiPage ) {
180 $this->wikipage = $wikiPage;
181 }
182
183 /**
184 * Get the WikiPage object.
185 * May throw an exception if there's no Title object set or the Title object
186 * belongs to a special namespace that doesn't have WikiPage, so use first
187 * canUseWikiPage() to check whether this method can be called safely.
188 *
189 * @since 1.19
190 * @return WikiPage
191 */
192 public function getWikiPage() {
193 if ( !is_null( $this->wikipage ) ) {
194 return $this->wikipage;
195 } else {
196 return $this->getContext()->getWikiPage();
197 }
198 }
199
200 /**
201 * @param OutputPage $output
202 */
203 public function setOutput( OutputPage $output ) {
204 $this->output = $output;
205 }
206
207 /**
208 * @return OutputPage
209 */
210 public function getOutput() {
211 if ( !is_null( $this->output ) ) {
212 return $this->output;
213 } else {
214 return $this->getContext()->getOutput();
215 }
216 }
217
218 /**
219 * @param User $user
220 */
221 public function setUser( User $user ) {
222 $this->user = $user;
223 }
224
225 /**
226 * @return User
227 */
228 public function getUser() {
229 if ( !is_null( $this->user ) ) {
230 return $this->user;
231 } else {
232 return $this->getContext()->getUser();
233 }
234 }
235
236 /**
237 * @param Language|string $language Language instance or language code
238 * @throws MWException
239 * @since 1.19
240 */
241 public function setLanguage( $language ) {
242 if ( $language instanceof Language ) {
243 $this->lang = $language;
244 } elseif ( is_string( $language ) ) {
245 $language = RequestContext::sanitizeLangCode( $language );
246 $obj = Language::factory( $language );
247 $this->lang = $obj;
248 } else {
249 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
250 }
251 }
252
253 /**
254 * @return Language
255 * @since 1.19
256 */
257 public function getLanguage() {
258 if ( !is_null( $this->lang ) ) {
259 return $this->lang;
260 } else {
261 return $this->getContext()->getLanguage();
262 }
263 }
264
265 /**
266 * @param Skin $skin
267 */
268 public function setSkin( Skin $skin ) {
269 $this->skin = clone $skin;
270 $this->skin->setContext( $this );
271 }
272
273 /**
274 * @return Skin
275 */
276 public function getSkin() {
277 if ( !is_null( $this->skin ) ) {
278 return $this->skin;
279 } else {
280 return $this->getContext()->getSkin();
281 }
282 }
283
284 /**
285 * Get a message using the current context.
286 *
287 * This can't just inherit from ContextSource, since then
288 * it would set only the original context, and not take
289 * into account any changes.
290 *
291 * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
292 * or a MessageSpecifier.
293 * @param mixed $args,... Arguments to wfMessage
294 * @return Message
295 */
296 public function msg( $key ) {
297 $args = func_get_args();
298
299 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
300 }
301 }