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