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