A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mediawiki.org/wiki/Special:MyLanguage/RequestContext below:

Manual:RequestContext.php - MediaWiki

MediaWiki version:

1.18

The RequestContext class is used to encapsulate all the pieces that are relevant to the context of a request. It implements the IContextSource and MutableContext interfaces.

Extensions should call getContext() and then getSomeObject() rather than rely on global state variables.

Access a RequestContext[edit]

Most places where you need to do something with RequestContext data should provide access to an IContextData source (?) and you should use that, not the main RequestContext instance or $wg globals.

You can also access the main request context using RequestContext::getMain(); however this should be a last resort.

When writing a SpecialPage[edit]

When writing a SpecialPage , you have access to the context through $this->getContext(); SpecialPage also implements a number of helper methods that are the equivalent of methods provided by RequestContext, for which see below.

SpecialPages are meant to be executable in alternate contexts, extensions should start moving away from the use of $wg's. We may drop support for includable special pages using $wg RequestContext related variables around MW 1.20.

When writing skin code[edit]

You have access to the context through $this->getContext();. Skin similarly provides a few helper methods, for which see below.

When writing parser functions and hooks[edit] Create a new RequestContext[edit]

There is still code using the global $wgOut , $wgTitle , $wgUser variables. Until those are eliminated we cannot expect custom contexts to work perfectly and will need to keep the same workarounds, however if we fix code to stop using those globals then something like this should be possible:

$context = new RequestContext();
$context->setRequest( new FauxRequest( [...] ) );
$context->setTitle( Title::newFromText( [...] ) );
$context->setUser( User::newFromName( 'Dantman' ) );
$context->setSkin( new OfflineDummySkin() );

// [...]
$html = $context->getOutput()->capture();

A RequestContext or IContextSource provides the following accessor methods:

The output and language are read-only, the rest of the RequestContext may be set using methods such as $context->setTitle( Title::newMainPage() )).

When writing SpecialPage[edit]

SpecialPage also implements a number of helpers:

When writing skin code[edit]

Like SpecialPage, Skin also implements a number of helpers:

The skin context is entered by Skin::outputPage( $out ); which is called by OutputPage::output(); external access to context sensitive method calls should be avoided.

Alternative classes to consider[edit]

The base of a RequestContext is the IContextSource interface. It defines the API of something from which you can get pieces of request context.

If you are writing an API which uses type hinting in the arguments or makes instanceof checks you should use IContextSource, not RequestContext.

ContextSource is an abstract helper class which like RequestContext, implements IContextSource. By making your new class extend ContextSource, your class will be provided with the various helper methods (getOutput(), getSkin(), getLanguage(), etc.) and it will implement IContextSource.

Like ContextSource, DerivativeContext can be used to inherit the context from another source, but it also allows for the individual pieces of the context, such as a Title instance, to be changed locally.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4