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