MediaWiki  master
RequestContext Class Reference

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

Inheritance diagram for RequestContext:
Collaboration diagram for RequestContext:

Public Member Functions

 canUseWikiPage ()
 Check whether a WikiPage object can be get with getWikiPage(). More...
 
 exportSession ()
 Export the resolved user IP, HTTP headers, user ID, and session ID. More...
 
 getActionName ()
 Get the action name for the current web request. More...
 
 getAuthority ()
 
 getConfig ()
 
 getCsrfTokenSet ()
 Get a set of CSRF tokens to obtain and match specific tokens. More...
 
 getLanguage ()
 Get the Language object. More...
 
 getOutput ()
 
 getRequest ()
 
 getSkin ()
 
 getStats ()
 
 getTiming ()
 
 getTitle ()
 
 getUser ()
 
 getWikiPage ()
 Get the WikiPage object. More...
 
 hasTitle ()
 Check, if a Title object is set. More...
 
 msg ( $key,... $params)
 Get a Message object with context set Parameters are the same as wfMessage() More...
 
 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)
 

Static Public Member Functions

static getMain ()
 Get the RequestContext object associated with the main request. More...
 
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. More...
 
static importScopedSession (array $params)
 Import an client IP address, HTTP headers, user ID, and session ID. More...
 
static newExtraneousContext (Title $title, $request=[])
 Create a new extraneous context. More...
 
static resetMain ()
 Resets singleton returned by getMain(). More...
 
static sanitizeLangCode ( $code)
 Accepts a language code and ensures it's sensible. More...
 

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 42 of file RequestContext.php.

Member Function Documentation

◆ canUseWikiPage()

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 IContextSource.

Definition at line 223 of file RequestContext.php.

References $title, and getTitle().

◆ exportSession()

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 IContextSource.

Definition at line 603 of file RequestContext.php.

References MediaWiki\Session\SessionManager\getGlobalSession(), and getUser().

◆ getActionName()

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 IContextSource.

Definition at line 292 of file RequestContext.php.

◆ getAuthority()

RequestContext::getAuthority ( )
Since
1.36
Returns
Authority

Implements IContextSource.

Definition at line 391 of file RequestContext.php.

◆ getConfig()

RequestContext::getConfig ( )
Returns
Config

Implements IContextSource.

Definition at line 121 of file RequestContext.php.

◆ getCsrfTokenSet()

RequestContext::getCsrfTokenSet ( )

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

Returns
CsrfTokenSet

Implements MediaWiki\Session\CsrfTokenSetProvider.

Definition at line 613 of file RequestContext.php.

◆ getLanguage()

RequestContext::getLanguage ( )

Get the Language object.

Initialization of user or request objects can depend on this.

Returns
Language
Exceptions
LogicException
Since
1.19

Implements IContextSource.

Definition at line 443 of file RequestContext.php.

References getUser().

◆ getMain()

static RequestContext::getMain ( )
static

Get the RequestContext object associated with the main request.

Returns
RequestContext

Definition at line 563 of file RequestContext.php.

Referenced by ApiMain\__construct(), HTMLTimezoneField\__construct(), PermissionsError\__construct(), CliInstaller\__construct(), UserBlockedError\__construct(), UploadStash\__construct(), MediaWiki\__construct(), LogFormatter\__construct(), StubUserLang\_newObject(), Linker\buildRollbackLink(), ChangeTags\buildTagFilterSelector(), MediaWiki\SpecialPage\SpecialPageFactory\capturePath(), UploadBase\checkWarnings(), Parser\cleanSig(), Language\dateFormat(), TextSlotDiffRenderer\diff(), DeferredUpdates\doUpdates(), RebuildFileCache\execute(), UpdateMediaWiki\execute(), FileRepo\findFiles(), LocalRepo\findFiles(), WikiImporter\finishImportPage(), ChangesList\flag(), FeedUtils\formatDiffRow2(), ChangeTags\formatSummaryRow(), Linker\generateRollback(), MediaWiki\Rest\Handler\ActionModuleBasedHandler\getApiMain(), ContextSource\getContext(), Article\getContext(), Profiler\getContext(), SpecialPage\getContext(), EditPage\getCopyrightWarning(), LocalFile\getDescriptionText(), Parser\getFlatSectionInfo(), GenderCache\getGenderOf(), UserGroupMembership\getGroupMemberName(), UserGroupMembership\getGroupName(), MWTimestamp\getHumanTimestamp(), Language\getHumanTimestamp(), Message\getLanguage(), LanguageConverter\getPreferredVariant(), MediaWiki\Block\AbstractBlock\getReason(), MWTimestamp\getRelativeTimestamp(), User\getRequest(), ParserOutput\getText(), MediaWiki\SpecialPage\SpecialPageFactory\getUsablePages(), ApiMain\handleApiBeforeMainException(), ImportableOldRevisionImporter\import(), importScopedSession(), WikitextContent\isCountable(), UploadFromUrl\isValidRequest(), MediaWiki\Rest\EntryPoint\main(), User\newFatalPermissionDeniedStatus(), UserNotLoggedIn\report(), Installer\resetMediaWikiServices(), ChangeTags\restrictedTagError(), Linker\revComment(), Linker\revUserLink(), Linker\revUserTools(), AssembleUploadChunksJob\run(), PublishStashedFileJob\run(), LoginSignupSpecialPage\setSessionUserForCurrentRequest(), WebInstaller\setupLanguage(), ChangesList\showCharacterDifference(), LogEventsList\showLogExtract(), MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProvider\testUserForCreation(), Linker\tocList(), LocalFile\upload(), Language\userAdjust(), ChangesList\userCan(), Linker\userToolLinks(), ContentHandler\validateSave(), wfApiMain(), wfImageAuthMain(), wfInstallerMain(), and wfStreamThumb().

◆ getMainAndWarn()

static 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
RequestContext
Since
1.24

Definition at line 579 of file RequestContext.php.

References wfDebug().

Referenced by ImageGalleryBase\factory().

◆ getOutput()

RequestContext::getOutput ( )
Returns
OutputPage

Implements IContextSource.

Definition at line 339 of file RequestContext.php.

◆ getRequest()

RequestContext::getRequest ( )
Returns
WebRequest

Implements IContextSource.

Definition at line 141 of file RequestContext.php.

References $wgCommandLineMode.

◆ getSkin()

RequestContext::getSkin ( )
Returns
Skin

Implements IContextSource.

Definition at line 504 of file RequestContext.php.

References Skin\normalizeKey(), and Hooks\runner().

◆ getStats()

RequestContext::getStats ( )
Deprecated:
since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected).

Hard deprecated since 1.39.

Returns
IBufferingStatsdDataFactory

Implements IContextSource.

Definition at line 161 of file RequestContext.php.

References wfDeprecated().

◆ getTiming()

RequestContext::getTiming ( )
Returns
Timing

Implements IContextSource.

Definition at line 169 of file RequestContext.php.

◆ getTitle()

RequestContext::getTitle ( )
Returns
Title|null

Implements IContextSource.

Definition at line 191 of file RequestContext.php.

References $wgTitle.

Referenced by canUseWikiPage(), and getWikiPage().

◆ getUser()

RequestContext::getUser ( )
Returns
User

Implements IContextSource.

Definition at line 361 of file RequestContext.php.

References User\newFromSession().

◆ getWikiPage()

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
Exceptions
MWException
Returns
WikiPage

Implements IContextSource.

Definition at line 259 of file RequestContext.php.

References $title, and getTitle().

◆ hasTitle()

RequestContext::hasTitle ( )

Check, if a Title object is set.

Since
1.25
Returns
bool

Definition at line 211 of file RequestContext.php.

Referenced by setWikiPage().

◆ importScopedSession()

static RequestContext::importScopedSession ( array  $params)
static

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

This sets the current session, $wgUser, 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.

Note
suhosin.session.encrypt may interfere with this method.
Parameters
array$paramsResult of RequestContext::exportSession()
Returns
ScopedCallback
Exceptions
MWException
Since
1.21

Definition at line 639 of file RequestContext.php.

References $wgRequest, getMain(), User\newFromId(), User\newFromName(), StubGlobalUser\setUser(), and MediaWiki\Session\SessionManager\singleton().

Referenced by AssembleUploadChunksJob\run(), and PublishStashedFileJob\run().

◆ msg()

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.
mixed...$params
Returns
Message

Implements MessageLocalizer.

Definition at line 554 of file RequestContext.php.

References wfMessage().

◆ newExtraneousContext()

static 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 FauxRequest, or a 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 FauxRequest
Returns
RequestContext

Definition at line 737 of file RequestContext.php.

References $title, and User\newFromName().

Referenced by LogPage\addEntry(), ContentModelChange\doContentModelChange(), and ManualLogEntry\getRecentChange().

◆ resetMain()

static RequestContext::resetMain ( )
static

Resets singleton returned by getMain().

Should be called only from unit tests.

Definition at line 589 of file RequestContext.php.

◆ sanitizeLangCode()

static 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 401 of file RequestContext.php.

References $wgLanguageCode.

Referenced by ApiMain\__construct(), and DerivativeContext\setLanguage().

◆ setActionName()

RequestContext::setActionName ( string  $action)
Since
1.38
Parameters
string$action

Implements MutableContext.

Definition at line 275 of file RequestContext.php.

◆ setAuthority()

RequestContext::setAuthority ( Authority  $authority)
Parameters
Authority$authority

Implements MutableContext.

Definition at line 379 of file RequestContext.php.

◆ setConfig()

RequestContext::setConfig ( Config  $config)
Parameters
Config$config

Implements MutableContext.

Definition at line 114 of file RequestContext.php.

◆ setLanguage()

RequestContext::setLanguage (   $language)
Parameters
Language | string$languageLanguage instance or language code
Exceptions
MWException
Since
1.19

Implements MutableContext.

Definition at line 424 of file RequestContext.php.

◆ setOutput()

RequestContext::setOutput ( OutputPage  $output)
Parameters
OutputPage$output

Implements MutableContext.

Definition at line 332 of file RequestContext.php.

◆ setRequest()

RequestContext::setRequest ( WebRequest  $request)
Parameters
WebRequest$request

Implements MutableContext.

Definition at line 134 of file RequestContext.php.

Referenced by ApiEditPage\execute().

◆ setSkin()

RequestContext::setSkin ( Skin  $skin)
Parameters
Skin$skin

Implements MutableContext.

Definition at line 496 of file RequestContext.php.

References ContextSource\setContext().

◆ setTitle()

RequestContext::setTitle ( Title  $title = null)
Parameters
Title | null$title

Implements MutableContext.

Definition at line 181 of file RequestContext.php.

References $title.

Referenced by Parser\braceSubstitution(), and setWikiPage().

◆ setUser()

RequestContext::setUser ( User  $user)
Parameters
User$user

Implements MutableContext.

Definition at line 350 of file RequestContext.php.

Referenced by ApiParamInfo\execute().

◆ setWikiPage()

RequestContext::setWikiPage ( WikiPage  $wikiPage)
Since
1.19
Parameters
WikiPage$wikiPage

Implements MutableContext.

Definition at line 238 of file RequestContext.php.

References WikiPage\getTitle(), hasTitle(), and setTitle().


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