MediaWiki master
MediaWiki\Context\RequestContext Class Reference

Group all the pieces relevant to the context of a request into one instance. More...

Inherits MediaWiki\Context\IContextSource, and MediaWiki\Context\MutableContext.

Collaboration diagram for MediaWiki\Context\RequestContext:

Public Member Functions

 __clone ()
 
 canUseWikiPage ()
 Check whether a WikiPage object can be get with getWikiPage().
 
 exportSession ()
 Export the resolved user IP, HTTP headers, user ID, and session ID.
 
 getActionName ()
 Get the action name for the current web request.
 
 getAuthority ()
 
 getConfig ()
 
 getCsrfTokenSet ()
 Get a set of CSRF tokens to obtain and match specific tokens.
 
 getLanguage ()
 Get the Language object.
 
 getLanguageCode ()
 
 getOutput ()
 
 getRequest ()
 
 getSkin ()
 
 getSkinName ()
 Get the name of the skin.
 
 getTiming ()
 
 getTitle ()
 
 getUser ()
 
 getWikiPage ()
 Get the WikiPage object.
 
 hasTitle ()
 Check, if a Title object is set.
 
 hasUser ()
 
 msg ( $key,... $params)
 Get a Message object with context set Parameters are the same as wfMessage()
 
 setActionName (string $action)
 
 setAuthority (Authority $authority)
 
 setConfig (Config $config)
 
 setLanguage ( $language)
 
 setOutput (OutputPage $output)
 
 setRequest (WebRequest $request)
 
 setSkin (Skin $skin)
 
 setTitle (?Title $title=null)
 
 setUser (User $user)
 
 setWikiPage (WikiPage $wikiPage)
 
- Public Member Functions inherited from MediaWiki\Context\MutableContext
 setTitle (Title $title)
 

Static Public Member Functions

static getMain ()
 Get the RequestContext object associated with the main request.
 
static getMainAndWarn ( $func=__METHOD__)
 Get the RequestContext object associated with the main request and gives a warning to the log, to find places, where a context maybe is missing.
 
static importScopedSession (array $params)
 Import a client IP address, HTTP headers, user ID, and session ID.
 
static newExtraneousContext (Title $title, $request=[])
 Create a new extraneous context.
 
static resetMain ()
 Resets singleton returned by getMain().
 
static sanitizeLangCode ( $code)
 Accepts a language code and ensures it's sensible.
 

Detailed Description

Group all the pieces relevant to the context of a request into one instance.

Stability: newable
Note
marked as newable in 1.35 for lack of a better alternative, but should use a factory in the future and should be narrowed down to not expose heavy weight objects.

Definition at line 51 of file RequestContext.php.

Member Function Documentation

◆ __clone()

MediaWiki\Context\RequestContext::__clone ( )
Returns
never

Definition at line 804 of file RequestContext.php.

◆ canUseWikiPage()

MediaWiki\Context\RequestContext::canUseWikiPage ( )

Check whether a WikiPage object can be get with getWikiPage().

Callers should expect that an exception is thrown from getWikiPage() if this method returns false.

Since
1.19
Returns
bool

Implements MediaWiki\Context\IContextSource.

Definition at line 226 of file RequestContext.php.

References MediaWiki\Title\Title\canExist(), and MediaWiki\Context\RequestContext\getTitle().

◆ exportSession()

MediaWiki\Context\RequestContext::exportSession ( )

Export the resolved user IP, HTTP headers, user ID, and session ID.

The result will be reasonably sized to allow for serialization.

Returns
array
Since
1.21

Implements MediaWiki\Context\IContextSource.

Definition at line 652 of file RequestContext.php.

◆ getActionName()

MediaWiki\Context\RequestContext::getActionName ( )

Get the action name for the current web request.

This generally returns "view" if the current request or process is not for a skinned index.php web request (e.g. load.php, thumb.php, job runner, CLI, API).

Warning
This must not be called before or during the Setup.php phase, and may cause an error or warning if called too early.
Since
1.38
Returns
string Action

Implements MediaWiki\Context\IContextSource.

Definition at line 294 of file RequestContext.php.

◆ getAuthority()

MediaWiki\Context\RequestContext::getAuthority ( )
Since
1.36
Returns
Authority

Implements MediaWiki\Context\IContextSource.

Definition at line 387 of file RequestContext.php.

◆ getConfig()

MediaWiki\Context\RequestContext::getConfig ( )
Returns
Config

Implements MediaWiki\Context\IContextSource.

Definition at line 140 of file RequestContext.php.

References MediaWiki\MediaWikiServices\getInstance().

◆ getCsrfTokenSet()

MediaWiki\Context\RequestContext::getCsrfTokenSet ( )

Get a set of CSRF tokens to obtain and match specific tokens.

Returns
CsrfTokenSet

Implements MediaWiki\Session\CsrfTokenSetProvider.

Definition at line 662 of file RequestContext.php.

◆ getLanguage()

MediaWiki\Context\RequestContext::getLanguage ( )

Get the Language object.

Initialization of user or request objects can depend on this.

If self::getUser has User::isSafeToLoad return false, then the language will be recached when User::isSafeToLoad returns true. This behaviour will not occur if the language was set via self::setLanguage.

Returns
Language
Exceptions
LogicException
Since
1.19

Implements MediaWiki\Context\IContextSource.

Definition at line 447 of file RequestContext.php.

References $request, and MediaWiki\User\Options\UserOptionsLookup\getOption().

◆ getLanguageCode()

MediaWiki\Context\RequestContext::getLanguageCode ( )
Since
1.42
Returns
Bcp47Code

Implements MediaWiki\Language\LocalizationContext.

Definition at line 513 of file RequestContext.php.

◆ getMain()

static MediaWiki\Context\RequestContext::getMain ( )
static

Get the RequestContext object associated with the main request.

Definition at line 615 of file RequestContext.php.

Referenced by MediaWiki\Context\ContextSource\getContext().

◆ getMainAndWarn()

static MediaWiki\Context\RequestContext::getMainAndWarn ( $func = __METHOD__)
static

Get the RequestContext object associated with the main request and gives a warning to the log, to find places, where a context maybe is missing.

Parameters
string$func
Returns
self
Since
1.24

Definition at line 628 of file RequestContext.php.

References wfDebug().

◆ getOutput()

MediaWiki\Context\RequestContext::getOutput ( )
Returns
OutputPage

Implements MediaWiki\Context\IContextSource.

Definition at line 332 of file RequestContext.php.

◆ getRequest()

MediaWiki\Context\RequestContext::getRequest ( )
Returns
WebRequest

Implements MediaWiki\Context\IContextSource.

Definition at line 155 of file RequestContext.php.

References MW_ENTRY_POINT.

◆ getSkin()

MediaWiki\Context\RequestContext::getSkin ( )
Returns
Skin

Implements MediaWiki\Context\IContextSource.

Definition at line 580 of file RequestContext.php.

◆ getSkinName()

MediaWiki\Context\RequestContext::getSkinName ( )

Get the name of the skin.

Since
1.41
Returns
string

Definition at line 530 of file RequestContext.php.

◆ getTiming()

MediaWiki\Context\RequestContext::getTiming ( )
Returns
Timing

Implements MediaWiki\Context\IContextSource.

Definition at line 173 of file RequestContext.php.

◆ getTitle()

MediaWiki\Context\RequestContext::getTitle ( )

◆ getUser()

MediaWiki\Context\RequestContext::getUser ( )
Returns
User

Implements MediaWiki\Context\IContextSource.

Definition at line 351 of file RequestContext.php.

◆ getWikiPage()

MediaWiki\Context\RequestContext::getWikiPage ( )

Get the WikiPage object.

May throw an exception if there's no Title object set or the Title object belongs to a special namespace that doesn't have WikiPage, so use first canUseWikiPage() to check whether this method can be called safely.

Since
1.19
Returns
WikiPage

Implements MediaWiki\Context\IContextSource.

Definition at line 261 of file RequestContext.php.

References MediaWiki\MediaWikiServices\getInstance(), and MediaWiki\Context\RequestContext\getTitle().

◆ hasTitle()

MediaWiki\Context\RequestContext::hasTitle ( )

Check, if a Title object is set.

Since
1.25
Returns
bool

Definition at line 214 of file RequestContext.php.

Referenced by MediaWiki\Context\RequestContext\setWikiPage().

◆ hasUser()

MediaWiki\Context\RequestContext::hasUser ( )

Definition at line 366 of file RequestContext.php.

◆ importScopedSession()

static MediaWiki\Context\RequestContext::importScopedSession ( array $params)
static

Import a client IP address, HTTP headers, user ID, and session ID.

This sets the current session and $wgRequest from $params. Once the return value falls out of scope, the old context is restored. This method should only be called in contexts where there is no session ID or end user receiving the response (CLI or HTTP job runners). This is partly enforced, and is done so to avoid leaking cookies if certain error conditions arise.

This is useful when background scripts inherit context when acting on behalf of a user. In general, the 'sessionId' parameter should be set to an empty string unless session importing is truly needed. This feature is somewhat deprecated.

Parameters
array$paramsResult of RequestContext::exportSession()
Since
1.21

Definition at line 685 of file RequestContext.php.

◆ msg()

MediaWiki\Context\RequestContext::msg ( $key,
$params )

Get a Message object with context set Parameters are the same as wfMessage()

Parameters
string | string[] | MessageSpecifier$keyMessage key, or array of keys, or a MessageSpecifier.
MessageParam|MessageSpecifier|string|int|float|list<MessageParam|MessageSpecifier|string|int|float>...$params See Message::params()
Returns
Message

Implements MediaWiki\Language\MessageLocalizer.

Definition at line 608 of file RequestContext.php.

References wfMessage().

◆ newExtraneousContext()

static MediaWiki\Context\RequestContext::newExtraneousContext ( Title $title,
$request = [] )
static

Create a new extraneous context.

The context is filled with information external to the current session.

  • Title is specified by argument
  • Request is a MediaWiki\Request\FauxRequest, or a MediaWiki\Request\FauxRequest can be specified by argument
  • User is an anonymous user, for separation IPv4 localhost is used
  • Language will be based on the anonymous user and request, may be content language or a uselang param in the fauxrequest data may change the lang
  • Skin will be based on the anonymous user, should be the wiki's default skin
Parameters
Title$titleTitle to use for the extraneous request
WebRequest | array$requestA WebRequest or data to use for a MediaWiki\Request\FauxRequest
Returns
self

Definition at line 787 of file RequestContext.php.

References $request.

◆ resetMain()

static MediaWiki\Context\RequestContext::resetMain ( )
static

Resets singleton returned by getMain().

Should be called only from unit tests.

Definition at line 638 of file RequestContext.php.

◆ sanitizeLangCode()

static MediaWiki\Context\RequestContext::sanitizeLangCode ( $code)
static

Accepts a language code and ensures it's sensible.

Outputs a cleaned up language code and replaces with $wgLanguageCode if not sensible.

Parameters
?string$codeLanguage code
Returns
string

Definition at line 397 of file RequestContext.php.

References $wgLanguageCode.

◆ setActionName()

MediaWiki\Context\RequestContext::setActionName ( string $action)
Since
1.38
Parameters
string$action

Implements MediaWiki\Context\MutableContext.

Definition at line 277 of file RequestContext.php.

◆ setAuthority()

MediaWiki\Context\RequestContext::setAuthority ( Authority $authority)
Stability: unstable
Parameters
Authority$authority

Implements MediaWiki\Context\MutableContext.

Definition at line 373 of file RequestContext.php.

◆ setConfig()

MediaWiki\Context\RequestContext::setConfig ( Config $config)
Returns
void

Implements MediaWiki\Context\MutableContext.

Definition at line 133 of file RequestContext.php.

◆ setLanguage()

MediaWiki\Context\RequestContext::setLanguage ( $language)
Parameters
Language | string$languageLanguage instance or language code
Since
1.19

Implements MediaWiki\Context\MutableContext.

Definition at line 422 of file RequestContext.php.

◆ setOutput()

MediaWiki\Context\RequestContext::setOutput ( OutputPage $output)
Returns
void

Implements MediaWiki\Context\MutableContext.

Definition at line 325 of file RequestContext.php.

◆ setRequest()

MediaWiki\Context\RequestContext::setRequest ( WebRequest $request)
Returns
void

Implements MediaWiki\Context\MutableContext.

Definition at line 148 of file RequestContext.php.

References $request.

Referenced by MediaWiki\Api\ApiEditPage\execute().

◆ setSkin()

MediaWiki\Context\RequestContext::setSkin ( Skin $skin)

◆ setTitle()

MediaWiki\Context\RequestContext::setTitle ( ?Title $title = null)
Parameters
Title | null$title

Definition at line 183 of file RequestContext.php.

Referenced by MediaWiki\Context\RequestContext\setWikiPage().

◆ setUser()

MediaWiki\Context\RequestContext::setUser ( User $user)
Returns
void

Implements MediaWiki\Context\MutableContext.

Definition at line 338 of file RequestContext.php.

Referenced by MediaWiki\Api\ApiParamInfo\execute().

◆ setWikiPage()

MediaWiki\Context\RequestContext::setWikiPage ( WikiPage $wikiPage)

The documentation for this class was generated from the following file: