Merge "mw.ui: button: Update focus state"
[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 * Get the stats object
102 *
103 * @return BufferingStatsdDataFactory
104 */
105 public function getStats() {
106 if ( !is_null( $this->stats ) ) {
107 return $this->stats;
108 } else {
109 return $this->getContext()->getStats();
110 }
111 }
112
113 /**
114 * Set the WebRequest object
115 *
116 * @param WebRequest $r
117 */
118 public function setRequest( WebRequest $r ) {
119 $this->request = $r;
120 }
121
122 /**
123 * Get the WebRequest object
124 *
125 * @return WebRequest
126 */
127 public function getRequest() {
128 if ( !is_null( $this->request ) ) {
129 return $this->request;
130 } else {
131 return $this->getContext()->getRequest();
132 }
133 }
134
135 /**
136 * Set the Title object
137 *
138 * @param Title $t
139 */
140 public function setTitle( Title $t ) {
141 $this->title = $t;
142 }
143
144 /**
145 * Get the Title object
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 * Set the WikiPage object
177 *
178 * @since 1.19
179 * @param WikiPage $p
180 */
181 public function setWikiPage( WikiPage $p ) {
182 $this->wikipage = $p;
183 }
184
185 /**
186 * Get the WikiPage object.
187 * May throw an exception if there's no Title object set or the Title object
188 * belongs to a special namespace that doesn't have WikiPage, so use first
189 * canUseWikiPage() to check whether this method can be called safely.
190 *
191 * @since 1.19
192 * @return WikiPage
193 */
194 public function getWikiPage() {
195 if ( !is_null( $this->wikipage ) ) {
196 return $this->wikipage;
197 } else {
198 return $this->getContext()->getWikiPage();
199 }
200 }
201
202 /**
203 * Set the OutputPage object
204 *
205 * @param OutputPage $o
206 */
207 public function setOutput( OutputPage $o ) {
208 $this->output = $o;
209 }
210
211 /**
212 * Get the OutputPage object
213 *
214 * @return OutputPage
215 */
216 public function getOutput() {
217 if ( !is_null( $this->output ) ) {
218 return $this->output;
219 } else {
220 return $this->getContext()->getOutput();
221 }
222 }
223
224 /**
225 * Set the User object
226 *
227 * @param User $u
228 */
229 public function setUser( User $u ) {
230 $this->user = $u;
231 }
232
233 /**
234 * Get the User object
235 *
236 * @return User
237 */
238 public function getUser() {
239 if ( !is_null( $this->user ) ) {
240 return $this->user;
241 } else {
242 return $this->getContext()->getUser();
243 }
244 }
245
246 /**
247 * Set the Language object
248 *
249 * @param Language|string $l Language instance or language code
250 * @throws MWException
251 * @since 1.19
252 */
253 public function setLanguage( $l ) {
254 if ( $l instanceof Language ) {
255 $this->lang = $l;
256 } elseif ( is_string( $l ) ) {
257 $l = RequestContext::sanitizeLangCode( $l );
258 $obj = Language::factory( $l );
259 $this->lang = $obj;
260 } else {
261 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
262 }
263 }
264
265 /**
266 * Get the Language object
267 *
268 * @return Language
269 * @since 1.19
270 */
271 public function getLanguage() {
272 if ( !is_null( $this->lang ) ) {
273 return $this->lang;
274 } else {
275 return $this->getContext()->getLanguage();
276 }
277 }
278
279 /**
280 * Set the Skin object
281 *
282 * @param Skin $s
283 */
284 public function setSkin( Skin $s ) {
285 $this->skin = clone $s;
286 $this->skin->setContext( $this );
287 }
288
289 /**
290 * Get the Skin object
291 *
292 * @return Skin
293 */
294 public function getSkin() {
295 if ( !is_null( $this->skin ) ) {
296 return $this->skin;
297 } else {
298 return $this->getContext()->getSkin();
299 }
300 }
301
302 /**
303 * Get a message using the current context.
304 *
305 * This can't just inherit from ContextSource, since then
306 * it would set only the original context, and not take
307 * into account any changes.
308 *
309 * @param mixed $args,... Arguments to wfMessage
310 * @return Message
311 */
312 public function msg() {
313 $args = func_get_args();
314
315 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
316 }
317 }