3 * Request-dependant objects containers.
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.
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.
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
22 * @author Alexandre Emsenhuber
23 * @author Daniel Friesen
28 * Group all the pieces relevant to the context of a request into one instance
30 class RequestContext
implements IContextSource
{
74 private static $instance = null;
77 * Set the Config object
81 public function setConfig( Config
$c ) {
86 * Get the Config object
90 public function getConfig() {
91 if ( $this->config
=== null ) {
92 // @todo In the future, we could move this to WebStart.php so
93 // the Config object is ready for when initialization happens
94 $this->config
= ConfigFactory
::getDefaultInstance()->makeConfig( 'main' );
101 * Set the WebRequest object
103 * @param WebRequest $r
105 public function setRequest( WebRequest
$r ) {
110 * Get the WebRequest object
114 public function getRequest() {
115 if ( $this->request
=== null ) {
116 global $wgRequest; # fallback to $wg till we can improve this
117 $this->request
= $wgRequest;
120 return $this->request
;
124 * Set the Title object
126 * @param Title $title
128 public function setTitle( Title
$title ) {
129 $this->title
= $title;
130 // Erase the WikiPage so a new one with the new title gets created.
131 $this->wikipage
= null;
135 * Get the Title object
139 public function getTitle() {
140 if ( $this->title
=== null ) {
141 global $wgTitle; # fallback to $wg till we can improve this
142 $this->title
= $wgTitle;
149 * Check whether a WikiPage object can be get with getWikiPage().
150 * Callers should expect that an exception is thrown from getWikiPage()
151 * if this method returns false.
156 public function canUseWikiPage() {
157 if ( $this->wikipage
) {
158 // If there's a WikiPage object set, we can for sure get it
161 // Only pages with legitimate titles can have WikiPages.
162 // That usually means pages in non-virtual namespaces.
163 $title = $this->getTitle();
164 return $title ?
$title->canExist() : false;
168 * Set the WikiPage object
173 public function setWikiPage( WikiPage
$p ) {
174 $contextTitle = $this->getTitle();
175 $pageTitle = $p->getTitle();
176 if ( !$contextTitle ||
!$pageTitle->equals( $contextTitle ) ) {
177 $this->setTitle( $pageTitle );
179 // Defer this to the end since setTitle sets it to null.
180 $this->wikipage
= $p;
184 * Get the WikiPage object.
185 * May throw an exception if there's no Title object set or the Title object
186 * belongs to a special namespace that doesn't have WikiPage, so use first
187 * canUseWikiPage() to check whether this method can be called safely.
190 * @throws MWException
193 public function getWikiPage() {
194 if ( $this->wikipage
=== null ) {
195 $title = $this->getTitle();
196 if ( $title === null ) {
197 throw new MWException( __METHOD__
. ' called without Title object set' );
199 $this->wikipage
= WikiPage
::factory( $title );
202 return $this->wikipage
;
206 * @param OutputPage $o
208 public function setOutput( OutputPage
$o ) {
213 * Get the OutputPage object
217 public function getOutput() {
218 if ( $this->output
=== null ) {
219 $this->output
= new OutputPage( $this );
222 return $this->output
;
226 * Set the User object
230 public function setUser( User
$u ) {
235 * Get the User object
239 public function getUser() {
240 if ( $this->user
=== null ) {
241 $this->user
= User
::newFromSession( $this->getRequest() );
248 * Accepts a language code and ensures it's sane. Outputs a cleaned up language
249 * code and replaces with $wgLanguageCode if not sane.
250 * @param string $code Language code
253 public static function sanitizeLangCode( $code ) {
254 global $wgLanguageCode;
256 // BCP 47 - letter case MUST NOT carry meaning
257 $code = strtolower( $code );
260 if ( !$code ||
!Language
::isValidCode( $code ) ||
$code === 'qqq' ) {
261 wfDebug( "Invalid user language code\n" );
262 $code = $wgLanguageCode;
269 * Set the Language object
271 * @param Language|string $l Language instance or language code
272 * @throws MWException
275 public function setLanguage( $l ) {
276 if ( $l instanceof Language
) {
278 } elseif ( is_string( $l ) ) {
279 $l = self
::sanitizeLangCode( $l );
280 $obj = Language
::factory( $l );
283 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
288 * Get the Language object.
289 * Initialization of user or request objects can depend on this.
294 public function getLanguage() {
295 if ( isset( $this->recursion
) ) {
296 trigger_error( "Recursion detected in " . __METHOD__
, E_USER_WARNING
);
298 wfDebugLog( 'recursion-guard', "Recursion detected:\n" . $e->getTraceAsString() );
300 $code = $this->getConfig()->get( 'LanguageCode' ) ?
: 'en';
301 $this->lang
= Language
::factory( $code );
302 } elseif ( $this->lang
=== null ) {
303 $this->recursion
= true;
308 $request = $this->getRequest();
309 $user = $this->getUser();
311 $code = $request->getVal( 'uselang', $user->getOption( 'language' ) );
312 $code = self
::sanitizeLangCode( $code );
314 wfRunHooks( 'UserGetLanguageObject', array( $user, &$code, $this ) );
316 if ( $code === $this->getConfig()->get( 'LanguageCode' ) ) {
317 $this->lang
= $wgContLang;
319 $obj = Language
::factory( $code );
323 unset( $this->recursion
);
325 catch ( Exception
$ex ) {
326 unset( $this->recursion
);
335 * Set the Skin object
339 public function setSkin( Skin
$s ) {
340 $this->skin
= clone $s;
341 $this->skin
->setContext( $this );
345 * Get the Skin object
349 public function getSkin() {
350 if ( $this->skin
=== null ) {
351 wfProfileIn( __METHOD__
. '-createskin' );
354 wfRunHooks( 'RequestContextCreateSkin', array( $this, &$skin ) );
355 $factory = SkinFactory
::getDefaultInstance();
357 // If the hook worked try to set a skin from it
358 if ( $skin instanceof Skin
) {
360 } elseif ( is_string( $skin ) ) {
361 // Normalize the key, just in case the hook did something weird.
362 $normalized = Skin
::normalizeKey( $skin );
363 $this->skin
= $factory->makeSkin( $normalized );
366 // If this is still null (the hook didn't run or didn't work)
367 // then go through the normal processing to load a skin
368 if ( $this->skin
=== null ) {
369 if ( !in_array( 'skin', $this->getConfig()->get( 'HiddenPrefs' ) ) ) {
371 $userSkin = $this->getUser()->getOption( 'skin' );
372 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
374 # if we're not allowing users to override, then use the default
375 $userSkin = $this->getConfig()->get( 'DefaultSkin' );
378 // Normalize the key in case the user is passing gibberish
379 // or has old preferences (bug 69566).
380 $normalized = Skin
::normalizeKey( $userSkin );
382 // Skin::normalizeKey will also validate it, so
383 // this won't throw an exception
384 $this->skin
= $factory->makeSkin( $normalized );
387 // After all that set a context on whatever skin got created
388 $this->skin
->setContext( $this );
389 wfProfileOut( __METHOD__
. '-createskin' );
395 /** Helpful methods **/
398 * Get a Message object with context set
399 * Parameters are the same as wfMessage()
403 public function msg() {
404 $args = func_get_args();
406 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
409 /** Static methods **/
412 * Get the RequestContext object associated with the main request
414 * @return RequestContext
416 public static function getMain() {
417 if ( self
::$instance === null ) {
418 self
::$instance = new self
;
421 return self
::$instance;
425 * Get the RequestContext object associated with the main request
426 * and gives a warning to the log, to find places, where a context maybe is missing.
428 * @param string $func
429 * @return RequestContext
432 public static function getMainAndWarn( $func = __METHOD__
) {
433 wfDebug( $func . ' called without context. ' .
434 "Using RequestContext::getMain() for sanity\n" );
436 return self
::getMain();
440 * Resets singleton returned by getMain(). Should be called only from unit tests.
442 public static function resetMain() {
443 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
444 throw new MWException( __METHOD__
. '() should be called only from unit tests!' );
446 self
::$instance = null;
450 * Export the resolved user IP, HTTP headers, user ID, and session ID.
451 * The result will be reasonably sized to allow for serialization.
456 public function exportSession() {
458 'ip' => $this->getRequest()->getIP(),
459 'headers' => $this->getRequest()->getAllHeaders(),
460 'sessionId' => session_id(),
461 'userId' => $this->getUser()->getId()
466 * Import the resolved user IP, HTTP headers, user ID, and session ID.
467 * This sets the current session and sets $wgUser and $wgRequest.
468 * Once the return value falls out of scope, the old context is restored.
469 * This function can only be called within CLI mode scripts.
471 * This will setup the session from the given ID. This is useful when
472 * background scripts inherit context when acting on behalf of a user.
474 * @note suhosin.session.encrypt may interfere with this method.
476 * @param array $params Result of RequestContext::exportSession()
477 * @return ScopedCallback
478 * @throws MWException
481 public static function importScopedSession( array $params ) {
482 if ( PHP_SAPI
!== 'cli' ) {
483 // Don't send random private cookies or turn $wgRequest into FauxRequest
484 throw new MWException( "Sessions can only be imported in cli mode." );
485 } elseif ( !strlen( $params['sessionId'] ) ) {
486 throw new MWException( "No session ID was specified." );
489 if ( $params['userId'] ) { // logged-in user
490 $user = User
::newFromId( $params['userId'] );
492 if ( !$user->getId() ) {
493 throw new MWException( "No user with ID '{$params['userId']}'." );
495 } elseif ( !IP
::isValid( $params['ip'] ) ) {
496 throw new MWException( "Could not load user '{$params['ip']}'." );
497 } else { // anon user
498 $user = User
::newFromName( $params['ip'], false );
501 $importSessionFunction = function ( User
$user, array $params ) {
502 global $wgRequest, $wgUser;
504 $context = RequestContext
::getMain();
505 // Commit and close any current session
506 session_write_close(); // persist
507 session_id( '' ); // detach
508 $_SESSION = array(); // clear in-memory array
509 // Remove any user IP or agent information
510 $context->setRequest( new FauxRequest() );
511 $wgRequest = $context->getRequest(); // b/c
512 // Now that all private information is detached from the user, it should
513 // be safe to load the new user. If errors occur or an exception is thrown
514 // and caught (leaving the main context in a mixed state), there is no risk
515 // of the User object being attached to the wrong IP, headers, or session.
516 $context->setUser( $user );
517 $wgUser = $context->getUser(); // b/c
518 if ( strlen( $params['sessionId'] ) ) { // don't make a new random ID
519 wfSetupSession( $params['sessionId'] ); // sets $_SESSION
521 $request = new FauxRequest( array(), false, $_SESSION );
522 $request->setIP( $params['ip'] );
523 foreach ( $params['headers'] as $name => $value ) {
524 $request->setHeader( $name, $value );
526 // Set the current context to use the new WebRequest
527 $context->setRequest( $request );
528 $wgRequest = $context->getRequest(); // b/c
531 // Stash the old session and load in the new one
532 $oUser = self
::getMain()->getUser();
533 $oParams = self
::getMain()->exportSession();
534 $importSessionFunction( $user, $params );
536 // Set callback to save and close the new session and reload the old one
537 return new ScopedCallback( function () use ( $importSessionFunction, $oUser, $oParams ) {
538 $importSessionFunction( $oUser, $oParams );
543 * Create a new extraneous context. The context is filled with information
544 * external to the current session.
545 * - Title is specified by argument
546 * - Request is a FauxRequest, or a FauxRequest can be specified by argument
547 * - User is an anonymous user, for separation IPv4 localhost is used
548 * - Language will be based on the anonymous user and request, may be content
549 * language or a uselang param in the fauxrequest data may change the lang
550 * - Skin will be based on the anonymous user, should be the wiki's default skin
552 * @param Title $title Title to use for the extraneous request
553 * @param WebRequest|array $request A WebRequest or data to use for a FauxRequest
554 * @return RequestContext
556 public static function newExtraneousContext( Title
$title, $request = array() ) {
558 $context->setTitle( $title );
559 if ( $request instanceof WebRequest
) {
560 $context->setRequest( $request );
562 $context->setRequest( new FauxRequest( $request ) );
564 $context->user
= User
::newFromName( '127.0.0.1', false );