Merge "mediawiki.Title: Add 'params' parameter to #getUrl"
[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 * Constructor
70 * @param IContextSource $context Context to inherit from
71 */
72 public function __construct( IContextSource $context ) {
73 $this->setContext( $context );
74 }
75
76 /**
77 * Set the WebRequest object
78 *
79 * @param WebRequest $r
80 */
81 public function setRequest( WebRequest $r ) {
82 $this->request = $r;
83 }
84
85 /**
86 * Get the WebRequest object
87 *
88 * @return WebRequest
89 */
90 public function getRequest() {
91 if ( !is_null( $this->request ) ) {
92 return $this->request;
93 } else {
94 return $this->getContext()->getRequest();
95 }
96 }
97
98 /**
99 * Set the Title object
100 *
101 * @param Title $t
102 * @throws MWException
103 */
104 public function setTitle( $t ) {
105 if ( $t !== null && !$t instanceof Title ) {
106 throw new MWException( __METHOD__ . " expects an instance of Title" );
107 }
108 $this->title = $t;
109 }
110
111 /**
112 * Get the Title object
113 *
114 * @return Title
115 */
116 public function getTitle() {
117 if ( !is_null( $this->title ) ) {
118 return $this->title;
119 } else {
120 return $this->getContext()->getTitle();
121 }
122 }
123
124 /**
125 * Check whether a WikiPage object can be get with getWikiPage().
126 * Callers should expect that an exception is thrown from getWikiPage()
127 * if this method returns false.
128 *
129 * @since 1.19
130 * @return bool
131 */
132 public function canUseWikiPage() {
133 if ( $this->wikipage !== null ) {
134 return true;
135 } elseif ( $this->title !== null ) {
136 return $this->title->canExist();
137 } else {
138 return $this->getContext()->canUseWikiPage();
139 }
140 }
141
142 /**
143 * Set the WikiPage object
144 *
145 * @since 1.19
146 * @param WikiPage $p
147 */
148 public function setWikiPage( WikiPage $p ) {
149 $this->wikipage = $p;
150 }
151
152 /**
153 * Get the WikiPage object.
154 * May throw an exception if there's no Title object set or the Title object
155 * belongs to a special namespace that doesn't have WikiPage, so use first
156 * canUseWikiPage() to check whether this method can be called safely.
157 *
158 * @since 1.19
159 * @return WikiPage
160 */
161 public function getWikiPage() {
162 if ( !is_null( $this->wikipage ) ) {
163 return $this->wikipage;
164 } else {
165 return $this->getContext()->getWikiPage();
166 }
167 }
168
169 /**
170 * Set the OutputPage object
171 *
172 * @param OutputPage $o
173 */
174 public function setOutput( OutputPage $o ) {
175 $this->output = $o;
176 }
177
178 /**
179 * Get the OutputPage object
180 *
181 * @return OutputPage
182 */
183 public function getOutput() {
184 if ( !is_null( $this->output ) ) {
185 return $this->output;
186 } else {
187 return $this->getContext()->getOutput();
188 }
189 }
190
191 /**
192 * Set the User object
193 *
194 * @param User $u
195 */
196 public function setUser( User $u ) {
197 $this->user = $u;
198 }
199
200 /**
201 * Get the User object
202 *
203 * @return User
204 */
205 public function getUser() {
206 if ( !is_null( $this->user ) ) {
207 return $this->user;
208 } else {
209 return $this->getContext()->getUser();
210 }
211 }
212
213 /**
214 * Set the Language object
215 *
216 * @deprecated since 1.19 Use setLanguage instead
217 * @param Language|string $l Language instance or language code
218 */
219 public function setLang( $l ) {
220 wfDeprecated( __METHOD__, '1.19' );
221 $this->setLanguage( $l );
222 }
223
224 /**
225 * Set the Language object
226 *
227 * @param Language|string $l Language instance or language code
228 * @throws MWException
229 * @since 1.19
230 */
231 public function setLanguage( $l ) {
232 if ( $l instanceof Language ) {
233 $this->lang = $l;
234 } elseif ( is_string( $l ) ) {
235 $l = RequestContext::sanitizeLangCode( $l );
236 $obj = Language::factory( $l );
237 $this->lang = $obj;
238 } else {
239 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
240 }
241 }
242
243 /**
244 * @deprecated since 1.19 Use getLanguage instead
245 * @return Language
246 */
247 public function getLang() {
248 wfDeprecated( __METHOD__, '1.19' );
249 $this->getLanguage();
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 String Message name
297 * @param Variable number of message arguments
298 * @return Message
299 */
300 public function msg() {
301 $args = func_get_args();
302
303 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
304 }
305 }