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...
 
 getConfig ()
 
 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)
 Get a Message object with context set Parameters are the same as wfMessage() More...
 
 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 sane. More...
 

Private Attributes

Config $config
 
Language $lang
 
OutputPage $output
 
WebRequest $request
 
Skin $skin
 
Timing $timing
 
Title $title
 
User $user
 
WikiPage $wikipage
 

Static Private Attributes

static RequestContext $instance = null
 

Detailed Description

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

Definition at line 32 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 191 of file RequestContext.php.

References Title\canExist(), 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 474 of file RequestContext.php.

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

◆ getConfig()

RequestContext::getConfig ( )
Returns
Config

Implements IContextSource.

Definition at line 93 of file RequestContext.php.

References $config, and null.

Referenced by getLanguage(), and getSkin().

◆ getLanguage()

RequestContext::getLanguage ( )

Get the Language object.

Initialization of user or request objects can depend on this.

Returns
Language
Exceptions
Exception
Since
1.19

Implements IContextSource.

Definition at line 319 of file RequestContext.php.

References $code, $e, $lang, Language\factory(), getConfig(), User\getOption(), getRequest(), getUser(), WebRequest\getVal(), null, Hooks\run(), and wfDebugLog().

◆ getMain()

static RequestContext::getMain ( )
static

Get the RequestContext object associated with the main request.

Returns
RequestContext

Definition at line 434 of file RequestContext.php.

References null.

Referenced by UserBlockedError\__construct(), MediaWiki\Widget\ExpiryInputWidget\__construct(), PermissionsError\__construct(), MediaWiki\__construct(), CliInstaller\__construct(), Pingback\__construct(), MediaWiki\Session\PHPSessionHandler\__construct(), LogFormatter\__construct(), MediaWiki\Session\SessionBackend\__construct(), ApiMain\__construct(), Installer\__construct(), StubUserLang\_newObject(), ApiComparePagesTest\addDBDataOnce(), Block\appliesToRight(), Block\appliesToUsertalk(), SpecialBlock\blockLogFlags(), Linker\buildRollbackLink(), ChangeTags\buildTagFilterSelector(), Preprocessor\cacheGetTree(), Preprocessor\cacheSetTree(), MediaWiki\Special\SpecialPageFactory\capturePath(), Title\checkUserBlock(), TextSlotDiffRenderer\diff(), ApiTestCase\doApiRequest(), ApiTestCase\doLogin(), Undelete\execute(), SpecialUserLogout\execute(), RebuildFileCache\execute(), UpdateMediaWiki\execute(), DeferredUpdates\execute(), LoginSignupSpecialPage\execute(), ChangesList\flag(), FeedUtils\formatDiffRow(), ChangeTags\formatSummaryRow(), Linker\generateRollback(), MediaWiki\Auth\AuthManager\getAuthenticationRequestsInternal(), User\getBlockedStatus(), User\getBlockFromCookieValue(), ActionTest\getContext(), ContextSource\getContext(), Profiler\getContext(), Action\getContext(), SpecialPage\getContext(), Article\getContext(), Block\getCookieValue(), MediaWiki\Session\SessionManager\getGlobalSession(), MWTimestamp\getHumanTimestamp(), Language\getHumanTimestamp(), Block\getIdFromCookieValue(), Message\getLanguage(), ApiStructureTest\getMain(), ApiContinuationManagerTest\getManager(), MediaWiki\Session\BotPasswordSessionProviderTest\getProvider(), MWTimestamp\getRelativeTimestamp(), User\getRights(), ParserOutput\getText(), MediaWiki\Special\SpecialPageFactory\getUsablePages(), ApiMain\handleApiBeforeMainException(), importScopedSession(), Html\inlineScript(), HTMLButtonField\isBadIE(), WikitextContent\isCountable(), Status\languageFromParam(), Html\linkedScript(), PageDataRequestHandlerTest\makeOutputPage(), ViewActionTest\makeViewActionClassFactory(), SpecialPageExecutor\newContext(), User\newFatalPermissionDeniedStatus(), EmailNotification\notifyOnPageChange(), CategoryMembershipChangeJob\notifyUpdatesForRevision(), ParserTestRunner\perTestSetup(), Block\prevents(), ResourceLoaderFileModuleTest\providerGetScriptPackageFiles(), MessageCache\refreshAndReplaceInternal(), UserNotLoggedIn\report(), User\resetOptions(), ResourceLoader\respond(), ChangeTags\restrictedTagError(), MediaWikiTestCase\revisionDelete(), AssembleUploadChunksJob\run(), PublishStashedFileJob\run(), ParserTestRunner\runTest(), MediaWiki\Auth\AuthManager\setDefaultUserOptions(), MediaWiki\Session\TestBagOStuff\setRawSession(), LoginSignupSpecialPage\setSessionUserForCurrentRequest(), ContentSecurityPolicyTest\setUp(), UploadFromUrlTestSuite\setUp(), WatchActionTest\setUp(), LogFormatterTest\setUp(), WebInstaller\setupLanguage(), MediaWikiTestCase\setUserLang(), ChangesList\showCharacterDifference(), LogEventsList\showLogExtract(), MediaWiki\Auth\AuthManager\singleton(), MediaWiki\Auth\ConfirmLinkSecondaryAuthenticationProviderTest\testBeginLinkAttempt(), MediaWiki\Session\SessionManagerTest\testConstructor(), ContentSecurityPolicyTest\testCSPIsEnabled(), ApiStructureTest\testDocumentationExists(), ApiBaseTest\testExtractRequestParams(), HTMLRestrictionsFieldTest\testForm(), ImageListPagerTest\testFormatValuesThrowException(), LinkerTest\testGenerateRollback(), ActionTest\testGetActionName_whenCanNotUseWikiPage_defaultsToView(), MediaWiki\Session\SessionManagerTest\testGetGlobalSession(), ContentHandlerTest\testGetSlotDiffRenderer_bc(), ContentHandlerTest\testGetSlotDiffRenderer_default(), ContentHandlerTest\testGetSlotDiffRenderer_hook(), ContentHandlerTest\testGetSlotDiffRenderer_nobc(), RequestContextTest\testImportScopedSession(), BotPasswordTest\testLogin(), ParserOptionsTest\testNewCanonical(), SpecialBlockTest\testProcessForm(), SpecialBlockTest\testProcessFormExisting(), SpecialBlockTest\testProcessFormRestrictions(), SpecialBlockTest\testProcessFormRestrictionsChange(), MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProviderTest\testRangeBlock(), DifferenceEngineTest\testRecursion(), MediaWiki\Session\SessionBackendTest\testResetIdOfGlobalSession(), MediaWiki\Auth\AuthManagerTest\testSetDefaultUserOptions(), MediaWiki\Auth\AuthManagerTest\testSingleton(), UserTest\testSoftBlockRanges(), ExtraParserTest\testSpecialPageTransclusionRestoresGlobalState(), MediaWiki\Session\SessionBackendTest\testTakeOverGlobalSession(), MediaWikiTest\testTryNormaliseRedirect(), ApiEditPageTest\testUndoAfterToHiddenRev(), ArticleViewTest\testUnhiddenViewOfDeletedRevision(), MediaWiki\Session\SessionBackendTest\testUnpersistOfGlobalSession(), MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProvider\testUserForCreation(), ArticleViewTest\testViewNonText(), ArticleViewTest\testViewOfDeletedRevision(), Linker\tocList(), User\trackBlockWithCookie(), WikiPage\triggerOpportunisticLinksUpdate(), MWExceptionRenderer\useOutputPage(), wfInstallerMain(), wfLogProfilingData(), 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 450 of file RequestContext.php.

References wfDebug().

Referenced by ImageGalleryBase\factory().

◆ getOutput()

RequestContext::getOutput ( )
Returns
OutputPage

Implements IContextSource.

Definition at line 247 of file RequestContext.php.

References $output, and null.

◆ getRequest()

RequestContext::getRequest ( )
Returns
WebRequest

Implements IContextSource.

Definition at line 113 of file RequestContext.php.

References $request, $wgCommandLineMode, null, and request.

Referenced by exportSession(), getLanguage(), getSkin(), and getUser().

◆ getSkin()

RequestContext::getSkin ( )

◆ getStats()

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

Implements IContextSource.

Definition at line 132 of file RequestContext.php.

◆ getTiming()

RequestContext::getTiming ( )
Returns
Timing

Implements IContextSource.

Definition at line 139 of file RequestContext.php.

References $timing, and null.

◆ getTitle()

RequestContext::getTitle ( )
Returns
Title|null

Implements IContextSource.

Definition at line 160 of file RequestContext.php.

References $title, $wgTitle, null, title, wfDebugLog(), and wfGetAllCallers().

Referenced by canUseWikiPage(), and getWikiPage().

◆ getUser()

RequestContext::getUser ( )
Returns
User

Implements IContextSource.

Definition at line 267 of file RequestContext.php.

References $user, getRequest(), User\newFromSession(), null, and user.

Referenced by exportSession(), getLanguage(), and getSkin().

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

References $wikipage, WikiPage\factory(), getTitle(), and null.

◆ hasTitle()

RequestContext::hasTitle ( )

Check, if a Title object is set.

Since
1.25
Returns
bool

Definition at line 179 of file RequestContext.php.

References null, and title.

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

References $context, $name, $params, $user, $value, $wgRequest, array(), as, User\getId(), getMain(), IP\isValid(), User\load(), User\newFromId(), User\newFromName(), null, WebRequest\setIP(), MediaWiki\Session\SessionManager\singleton(), and use.

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

◆ msg()

RequestContext::msg (   $key)

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

Parameters
string|string[]|MessageSpecifier$key Message key, or array of keys, or a MessageSpecifier.
mixed$args,...
Returns
Message

Implements MessageLocalizer.

Definition at line 423 of file RequestContext.php.

References $args, and 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 604 of file RequestContext.php.

References $context, and User\newFromName().

Referenced by LogPage\addEntry(), ChangesListSpecialPage\getChangeTagList(), ManualLogEntry\getRecentChange(), MovePage\moveToInternal(), SpecialChangeContentModel\onSubmit(), LocalFile\recordUpload2(), and RecentChangeTest\setUp().

◆ resetMain()

static RequestContext::resetMain ( )
static

Resets singleton returned by getMain().

Should be called only from unit tests.

Definition at line 460 of file RequestContext.php.

References null.

Referenced by LogFormatterTestCase\doTestLogFormatter(), MediaWikiTestCase\resetNonServiceCaches(), UploadFromUrlTestSuite\setUp(), TitlePermissionTest\setUp(), and MediaWikiTestCase\tearDown().

◆ sanitizeLangCode()

static RequestContext::sanitizeLangCode (   $code)
static

Accepts a language code and ensures it's sane.

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

Parameters
string$codeLanguage code
Returns
string

Definition at line 281 of file RequestContext.php.

References $code, $wgLanguageCode, and Language\isValidCode().

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

◆ setConfig()

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

Implements MutableContext.

Definition at line 86 of file RequestContext.php.

References $config.

◆ setLanguage()

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

Implements MutableContext.

Definition at line 300 of file RequestContext.php.

References Language\factory().

◆ setOutput()

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

Implements MutableContext.

Definition at line 240 of file RequestContext.php.

References $output.

◆ setRequest()

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

Implements MutableContext.

Definition at line 106 of file RequestContext.php.

References $request, and request.

◆ setSkin()

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

Implements MutableContext.

Definition at line 363 of file RequestContext.php.

References $skin, and ContextSource\setContext().

◆ setTitle()

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

Implements MutableContext.

Definition at line 151 of file RequestContext.php.

References $title, null, and title.

Referenced by setWikiPage().

◆ setUser()

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

Implements MutableContext.

Definition at line 258 of file RequestContext.php.

References $user, null, and user.

◆ setWikiPage()

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

Implements MutableContext.

Definition at line 206 of file RequestContext.php.

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

Member Data Documentation

◆ $config

Config RequestContext::$config
private

Definition at line 76 of file RequestContext.php.

Referenced by getConfig(), and setConfig().

◆ $instance

RequestContext RequestContext::$instance = null
staticprivate

Definition at line 81 of file RequestContext.php.

◆ $lang

Language RequestContext::$lang
private

Definition at line 61 of file RequestContext.php.

Referenced by getLanguage().

◆ $output

OutputPage RequestContext::$output
private

Definition at line 51 of file RequestContext.php.

Referenced by getOutput(), and setOutput().

◆ $request

WebRequest RequestContext::$request
private

Definition at line 36 of file RequestContext.php.

Referenced by getRequest(), and setRequest().

◆ $skin

Skin RequestContext::$skin
private

Definition at line 66 of file RequestContext.php.

Referenced by getSkin(), and setSkin().

◆ $timing

Timing RequestContext::$timing
private

Definition at line 71 of file RequestContext.php.

Referenced by getTiming().

◆ $title

Title RequestContext::$title
private

Definition at line 41 of file RequestContext.php.

Referenced by getTitle(), and setTitle().

◆ $user

User RequestContext::$user
private

◆ $wikipage

WikiPage RequestContext::$wikipage
private

Definition at line 46 of file RequestContext.php.

Referenced by getWikiPage().


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