Merge "Made upload stash cleanup script scan the temp dir for old files."
[lhc/web/wiklou.git] / includes / context / RequestContext.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.18
21 *
22 * @author Alexandre Emsenhuber
23 * @author Daniel Friesen
24 * @file
25 */
26
27 /**
28 * Group all the pieces relevant to the context of a request into one instance
29 */
30 class RequestContext implements IContextSource {
31 /**
32 * @var WebRequest
33 */
34 private $request;
35
36 /**
37 * @var Title
38 */
39 private $title;
40
41 /**
42 * @var WikiPage
43 */
44 private $wikipage;
45
46 /**
47 * @var OutputPage
48 */
49 private $output;
50
51 /**
52 * @var User
53 */
54 private $user;
55
56 /**
57 * @var Language
58 */
59 private $lang;
60
61 /**
62 * @var Skin
63 */
64 private $skin;
65
66 /**
67 * Set the WebRequest object
68 *
69 * @param WebRequest $r
70 */
71 public function setRequest( WebRequest $r ) {
72 $this->request = $r;
73 }
74
75 /**
76 * Get the WebRequest object
77 *
78 * @return WebRequest
79 */
80 public function getRequest() {
81 if ( $this->request === null ) {
82 global $wgRequest; # fallback to $wg till we can improve this
83 $this->request = $wgRequest;
84 }
85 return $this->request;
86 }
87
88 /**
89 * Set the Title object
90 *
91 * @param Title $t
92 */
93 public function setTitle( Title $t ) {
94 $this->title = $t;
95 // Erase the WikiPage so a new one with the new title gets created.
96 $this->wikipage = null;
97 }
98
99 /**
100 * Get the Title object
101 *
102 * @return Title
103 */
104 public function getTitle() {
105 if ( $this->title === null ) {
106 global $wgTitle; # fallback to $wg till we can improve this
107 $this->title = $wgTitle;
108 }
109 return $this->title;
110 }
111
112 /**
113 * Check whether a WikiPage object can be get with getWikiPage().
114 * Callers should expect that an exception is thrown from getWikiPage()
115 * if this method returns false.
116 *
117 * @since 1.19
118 * @return bool
119 */
120 public function canUseWikiPage() {
121 if ( $this->wikipage !== null ) {
122 # If there's a WikiPage object set, we can for sure get it
123 return true;
124 }
125 $title = $this->getTitle();
126 if ( $title === null ) {
127 # No Title, no WikiPage
128 return false;
129 } else {
130 # Only namespaces whose pages are stored in the database can have WikiPage
131 return $title->canExist();
132 }
133 }
134
135 /**
136 * Set the WikiPage object
137 *
138 * @since 1.19
139 * @param WikiPage $p
140 */
141 public function setWikiPage( WikiPage $p ) {
142 $contextTitle = $this->getTitle();
143 $pageTitle = $p->getTitle();
144 if ( !$contextTitle || !$pageTitle->equals( $contextTitle ) ) {
145 $this->setTitle( $pageTitle );
146 }
147 // Defer this to the end since setTitle sets it to null.
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 * @throws MWException
159 * @return WikiPage
160 */
161 public function getWikiPage() {
162 if ( $this->wikipage === null ) {
163 $title = $this->getTitle();
164 if ( $title === null ) {
165 throw new MWException( __METHOD__ . ' called without Title object set' );
166 }
167 $this->wikipage = WikiPage::factory( $title );
168 }
169 return $this->wikipage;
170 }
171
172 /**
173 * @param $o OutputPage
174 */
175 public function setOutput( OutputPage $o ) {
176 $this->output = $o;
177 }
178
179 /**
180 * Get the OutputPage object
181 *
182 * @return OutputPage
183 */
184 public function getOutput() {
185 if ( $this->output === null ) {
186 $this->output = new OutputPage( $this );
187 }
188 return $this->output;
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 ( $this->user === null ) {
207 $this->user = User::newFromSession( $this->getRequest() );
208 }
209 return $this->user;
210 }
211
212 /**
213 * Accepts a language code and ensures it's sane. Outputs a cleaned up language
214 * code and replaces with $wgLanguageCode if not sane.
215 * @param string $code Language code
216 * @return string
217 */
218 public static function sanitizeLangCode( $code ) {
219 global $wgLanguageCode;
220
221 // BCP 47 - letter case MUST NOT carry meaning
222 $code = strtolower( $code );
223
224 # Validate $code
225 if ( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
226 wfDebug( "Invalid user language code\n" );
227 $code = $wgLanguageCode;
228 }
229
230 return $code;
231 }
232
233 /**
234 * Set the Language object
235 *
236 * @deprecated 1.19 Use setLanguage instead
237 * @param Language|string $l Language instance or language code
238 */
239 public function setLang( $l ) {
240 wfDeprecated( __METHOD__, '1.19' );
241 $this->setLanguage( $l );
242 }
243
244 /**
245 * Set the Language object
246 *
247 * @param Language|string $l Language instance or language code
248 * @throws MWException
249 * @since 1.19
250 */
251 public function setLanguage( $l ) {
252 if ( $l instanceof Language ) {
253 $this->lang = $l;
254 } elseif ( is_string( $l ) ) {
255 $l = self::sanitizeLangCode( $l );
256 $obj = Language::factory( $l );
257 $this->lang = $obj;
258 } else {
259 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
260 }
261 }
262
263 /**
264 * @deprecated 1.19 Use getLanguage instead
265 * @return Language
266 */
267 public function getLang() {
268 wfDeprecated( __METHOD__, '1.19' );
269 return $this->getLanguage();
270 }
271
272 /**
273 * Get the Language object.
274 * Initialization of user or request objects can depend on this.
275 *
276 * @return Language
277 * @since 1.19
278 */
279 public function getLanguage() {
280 if ( isset( $this->recursion ) ) {
281 throw new MWException( 'Recursion detected' );
282 }
283
284 if ( $this->lang === null ) {
285 $this->recursion = true;
286
287 global $wgLanguageCode, $wgContLang;
288
289 $request = $this->getRequest();
290 $user = $this->getUser();
291
292 $code = $request->getVal( 'uselang', $user->getOption( 'language' ) );
293 $code = self::sanitizeLangCode( $code );
294
295 wfRunHooks( 'UserGetLanguageObject', array( $user, &$code, $this ) );
296
297 if ( $code === $wgLanguageCode ) {
298 $this->lang = $wgContLang;
299 } else {
300 $obj = Language::factory( $code );
301 $this->lang = $obj;
302 }
303
304 unset( $this->recursion );
305 }
306
307 return $this->lang;
308 }
309
310 /**
311 * Set the Skin object
312 *
313 * @param Skin $s
314 */
315 public function setSkin( Skin $s ) {
316 $this->skin = clone $s;
317 $this->skin->setContext( $this );
318 }
319
320 /**
321 * Get the Skin object
322 *
323 * @return Skin
324 */
325 public function getSkin() {
326 if ( $this->skin === null ) {
327 wfProfileIn( __METHOD__ . '-createskin' );
328
329 $skin = null;
330 wfRunHooks( 'RequestContextCreateSkin', array( $this, &$skin ) );
331
332 // If the hook worked try to set a skin from it
333 if ( $skin instanceof Skin ) {
334 $this->skin = $skin;
335 } elseif ( is_string( $skin ) ) {
336 $this->skin = Skin::newFromKey( $skin );
337 }
338
339 // If this is still null (the hook didn't run or didn't work)
340 // then go through the normal processing to load a skin
341 if ( $this->skin === null ) {
342 global $wgHiddenPrefs;
343 if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
344 # get the user skin
345 $userSkin = $this->getUser()->getOption( 'skin' );
346 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
347 } else {
348 # if we're not allowing users to override, then use the default
349 global $wgDefaultSkin;
350 $userSkin = $wgDefaultSkin;
351 }
352
353 $this->skin = Skin::newFromKey( $userSkin );
354 }
355
356 // After all that set a context on whatever skin got created
357 $this->skin->setContext( $this );
358 wfProfileOut( __METHOD__ . '-createskin' );
359 }
360 return $this->skin;
361 }
362
363 /** Helpful methods **/
364
365 /**
366 * Get a Message object with context set
367 * Parameters are the same as wfMessage()
368 *
369 * @return Message
370 */
371 public function msg() {
372 $args = func_get_args();
373 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
374 }
375
376 /** Static methods **/
377
378 /**
379 * Get the RequestContext object associated with the main request
380 *
381 * @return RequestContext
382 */
383 public static function getMain() {
384 static $instance = null;
385 if ( $instance === null ) {
386 $instance = new self;
387 }
388 return $instance;
389 }
390
391 /**
392 * Create a new extraneous context. The context is filled with information
393 * external to the current session.
394 * - Title is specified by argument
395 * - Request is a FauxRequest, or a FauxRequest can be specified by argument
396 * - User is an anonymous user, for separation IPv4 localhost is used
397 * - Language will be based on the anonymous user and request, may be content
398 * language or a uselang param in the fauxrequest data may change the lang
399 * - Skin will be based on the anonymous user, should be the wiki's default skin
400 *
401 * @param Title $title Title to use for the extraneous request
402 * @param WebRequest|array $request A WebRequest or data to use for a FauxRequest
403 * @return RequestContext
404 */
405 public static function newExtraneousContext( Title $title, $request = array() ) {
406 $context = new self;
407 $context->setTitle( $title );
408 if ( $request instanceof WebRequest ) {
409 $context->setRequest( $request );
410 } else {
411 $context->setRequest( new FauxRequest( $request ) );
412 }
413 $context->user = User::newFromName( '127.0.0.1', false );
414 return $context;
415 }
416 }