Merge "Begin exposing SiteConfiguration via site contexts"
[lhc/web/wiklou.git] / includes / context / DerivativeContext.php
1 <?php
2 /**
3 * Request-dependant objects containers.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @since 1.19
21 *
22 * @author Daniel Friesen
23 * @file
24 */
25
26 /**
27 * An IContextSource implementation which will inherit context from another source
28 * but allow individual pieces of context to be changed locally
29 * eg: A ContextSource that can inherit from the main RequestContext but have
30 * a different Title instance set on it.
31 */
32 class DerivativeContext extends ContextSource {
33 /**
34 * @var WebRequest
35 */
36 private $request;
37
38 /**
39 * @var Title
40 */
41 private $title;
42
43 /**
44 * @var WikiPage
45 */
46 private $wikipage;
47
48 /**
49 * @var OutputPage
50 */
51 private $output;
52
53 /**
54 * @var User
55 */
56 private $user;
57
58 /**
59 * @var Language
60 */
61 private $lang;
62
63 /**
64 * @var Skin
65 */
66 private $skin;
67
68 /**
69 * @var SiteConfiguration
70 */
71 private $config;
72
73 /**
74 * Constructor
75 * @param IContextSource $context Context to inherit from
76 */
77 public function __construct( IContextSource $context ) {
78 $this->setContext( $context );
79 }
80
81 /**
82 * Set the SiteConfiguration object
83 *
84 * @param SiteConfiguration $c
85 */
86 public function setConfig( SiteConfiguration $s ) {
87 $this->config = $s;
88 }
89
90 /**
91 * Get the SiteConfiguration object
92 *
93 * @return SiteConfiguration
94 */
95 public function getConfig() {
96 if ( !is_null( $this->config ) ) {
97 return $this->config;
98 } else {
99 return $this->getContext()->getConfig();
100 }
101 }
102
103 /**
104 * Set the WebRequest object
105 *
106 * @param WebRequest $r
107 */
108 public function setRequest( WebRequest $r ) {
109 $this->request = $r;
110 }
111
112 /**
113 * Get the WebRequest object
114 *
115 * @return WebRequest
116 */
117 public function getRequest() {
118 if ( !is_null( $this->request ) ) {
119 return $this->request;
120 } else {
121 return $this->getContext()->getRequest();
122 }
123 }
124
125 /**
126 * Set the Title object
127 *
128 * @param Title $t
129 * @throws MWException
130 */
131 public function setTitle( $t ) {
132 if ( $t !== null && !$t instanceof Title ) {
133 throw new MWException( __METHOD__ . " expects an instance of Title" );
134 }
135 $this->title = $t;
136 }
137
138 /**
139 * Get the Title object
140 *
141 * @return Title
142 */
143 public function getTitle() {
144 if ( !is_null( $this->title ) ) {
145 return $this->title;
146 } else {
147 return $this->getContext()->getTitle();
148 }
149 }
150
151 /**
152 * Check whether a WikiPage object can be get with getWikiPage().
153 * Callers should expect that an exception is thrown from getWikiPage()
154 * if this method returns false.
155 *
156 * @since 1.19
157 * @return bool
158 */
159 public function canUseWikiPage() {
160 if ( $this->wikipage !== null ) {
161 return true;
162 } elseif ( $this->title !== null ) {
163 return $this->title->canExist();
164 } else {
165 return $this->getContext()->canUseWikiPage();
166 }
167 }
168
169 /**
170 * Set the WikiPage object
171 *
172 * @since 1.19
173 * @param WikiPage $p
174 */
175 public function setWikiPage( WikiPage $p ) {
176 $this->wikipage = $p;
177 }
178
179 /**
180 * Get the WikiPage object.
181 * May throw an exception if there's no Title object set or the Title object
182 * belongs to a special namespace that doesn't have WikiPage, so use first
183 * canUseWikiPage() to check whether this method can be called safely.
184 *
185 * @since 1.19
186 * @return WikiPage
187 */
188 public function getWikiPage() {
189 if ( !is_null( $this->wikipage ) ) {
190 return $this->wikipage;
191 } else {
192 return $this->getContext()->getWikiPage();
193 }
194 }
195
196 /**
197 * Set the OutputPage object
198 *
199 * @param OutputPage $o
200 */
201 public function setOutput( OutputPage $o ) {
202 $this->output = $o;
203 }
204
205 /**
206 * Get the OutputPage object
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 * Set the User object
220 *
221 * @param User $u
222 */
223 public function setUser( User $u ) {
224 $this->user = $u;
225 }
226
227 /**
228 * Get the User object
229 *
230 * @return User
231 */
232 public function getUser() {
233 if ( !is_null( $this->user ) ) {
234 return $this->user;
235 } else {
236 return $this->getContext()->getUser();
237 }
238 }
239
240 /**
241 * Set the Language object
242 *
243 * @deprecated since 1.19 Use setLanguage instead
244 * @param Language|string $l Language instance or language code
245 */
246 public function setLang( $l ) {
247 wfDeprecated( __METHOD__, '1.19' );
248 $this->setLanguage( $l );
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 * @deprecated since 1.19 Use getLanguage instead
272 * @return Language
273 */
274 public function getLang() {
275 wfDeprecated( __METHOD__, '1.19' );
276 $this->getLanguage();
277 }
278
279 /**
280 * Get the Language object
281 *
282 * @return Language
283 * @since 1.19
284 */
285 public function getLanguage() {
286 if ( !is_null( $this->lang ) ) {
287 return $this->lang;
288 } else {
289 return $this->getContext()->getLanguage();
290 }
291 }
292
293 /**
294 * Set the Skin object
295 *
296 * @param Skin $s
297 */
298 public function setSkin( Skin $s ) {
299 $this->skin = clone $s;
300 $this->skin->setContext( $this );
301 }
302
303 /**
304 * Get the Skin object
305 *
306 * @return Skin
307 */
308 public function getSkin() {
309 if ( !is_null( $this->skin ) ) {
310 return $this->skin;
311 } else {
312 return $this->getContext()->getSkin();
313 }
314 }
315
316 /**
317 * Get a message using the current context.
318 *
319 * This can't just inherit from ContextSource, since then
320 * it would set only the original context, and not take
321 * into account any changes.
322 *
323 * @param String Message name
324 * @param Variable number of message arguments
325 * @return Message
326 */
327 public function msg() {
328 $args = func_get_args();
329
330 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
331 }
332 }