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