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