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