Merge "Don't check namespace in SpecialWantedtemplates"
[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
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
28 */
29 class DerivativeContext extends ContextSource implements MutableContext {
30 /**
31 * @var WebRequest
32 */
33 private $request;
34
35 /**
36 * @var Title
37 */
38 private $title;
39
40 /**
41 * @var WikiPage
42 */
43 private $wikipage;
44
45 /**
46 * @var OutputPage
47 */
48 private $output;
49
50 /**
51 * @var User
52 */
53 private $user;
54
55 /**
56 * @var Language
57 */
58 private $lang;
59
60 /**
61 * @var Skin
62 */
63 private $skin;
64
65 /**
66 * @var Config
67 */
68 private $config;
69
70 /**
71 * @var Stats
72 */
73 private $stats;
74
75 /**
76 * Constructor
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 * @return BufferingStatsdDataFactory
109 */
110 public function getStats() {
111 if ( !is_null( $this->stats ) ) {
112 return $this->stats;
113 } else {
114 return $this->getContext()->getStats();
115 }
116 }
117
118 /**
119 * Set the WebRequest object
120 *
121 * @param WebRequest $r
122 */
123 public function setRequest( WebRequest $r ) {
124 $this->request = $r;
125 }
126
127 /**
128 * Get the WebRequest object
129 *
130 * @return WebRequest
131 */
132 public function getRequest() {
133 if ( !is_null( $this->request ) ) {
134 return $this->request;
135 } else {
136 return $this->getContext()->getRequest();
137 }
138 }
139
140 /**
141 * Set the Title object
142 *
143 * @param Title $t
144 */
145 public function setTitle( Title $t ) {
146 $this->title = $t;
147 }
148
149 /**
150 * Get the Title object
151 *
152 * @return Title|null
153 */
154 public function getTitle() {
155 if ( !is_null( $this->title ) ) {
156 return $this->title;
157 } else {
158 return $this->getContext()->getTitle();
159 }
160 }
161
162 /**
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.
166 *
167 * @since 1.19
168 * @return bool
169 */
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();
177 }
178 }
179
180 /**
181 * Set the WikiPage object
182 *
183 * @since 1.19
184 * @param WikiPage $p
185 */
186 public function setWikiPage( WikiPage $p ) {
187 $this->wikipage = $p;
188 }
189
190 /**
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.
195 *
196 * @since 1.19
197 * @return WikiPage
198 */
199 public function getWikiPage() {
200 if ( !is_null( $this->wikipage ) ) {
201 return $this->wikipage;
202 } else {
203 return $this->getContext()->getWikiPage();
204 }
205 }
206
207 /**
208 * Set the OutputPage object
209 *
210 * @param OutputPage $o
211 */
212 public function setOutput( OutputPage $o ) {
213 $this->output = $o;
214 }
215
216 /**
217 * Get the OutputPage object
218 *
219 * @return OutputPage
220 */
221 public function getOutput() {
222 if ( !is_null( $this->output ) ) {
223 return $this->output;
224 } else {
225 return $this->getContext()->getOutput();
226 }
227 }
228
229 /**
230 * Set the User object
231 *
232 * @param User $u
233 */
234 public function setUser( User $u ) {
235 $this->user = $u;
236 }
237
238 /**
239 * Get the User object
240 *
241 * @return User
242 */
243 public function getUser() {
244 if ( !is_null( $this->user ) ) {
245 return $this->user;
246 } else {
247 return $this->getContext()->getUser();
248 }
249 }
250
251 /**
252 * Set the Language object
253 *
254 * @param Language|string $l Language instance or language code
255 * @throws MWException
256 * @since 1.19
257 */
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." );
267 }
268 }
269
270 /**
271 * Get the Language object
272 *
273 * @return Language
274 * @since 1.19
275 */
276 public function getLanguage() {
277 if ( !is_null( $this->lang ) ) {
278 return $this->lang;
279 } else {
280 return $this->getContext()->getLanguage();
281 }
282 }
283
284 /**
285 * Set the Skin object
286 *
287 * @param Skin $s
288 */
289 public function setSkin( Skin $s ) {
290 $this->skin = clone $s;
291 $this->skin->setContext( $this );
292 }
293
294 /**
295 * Get the Skin object
296 *
297 * @return Skin
298 */
299 public function getSkin() {
300 if ( !is_null( $this->skin ) ) {
301 return $this->skin;
302 } else {
303 return $this->getContext()->getSkin();
304 }
305 }
306
307 /**
308 * Get a message using the current context.
309 *
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.
313 *
314 * @param mixed $args,... Arguments to wfMessage
315 * @return Message
316 */
317 public function msg() {
318 $args = func_get_args();
319
320 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
321 }
322 }