25use Wikimedia\AtEase\AtEase;
28use Wikimedia\ScopedCallback;
94 $this->config = $config;
101 if ( $this->config ===
null ) {
104 $this->config = MediaWikiServices::getInstance()->getMainConfig();
107 return $this->config;
114 $this->request = $request;
121 if ( $this->request ===
null ) {
131 return $this->request;
140 return MediaWikiServices::getInstance()->getStatsdDataFactory();
147 if ( $this->timing ===
null ) {
148 $this->timing =
new Timing( [
149 'logger' => LoggerFactory::getInstance(
'Timing' )
152 return $this->timing;
161 $this->wikipage =
null;
168 if ( $this->title ===
null ) {
169 global
$wgTitle; # fallback to $wg till we can improve
this
173 __METHOD__ .
' called by ' .
wfGetAllCallers( 5 ) .
' with no title set.'
187 return $this->title !==
null;
199 if ( $this->wikipage ) {
215 if ( !$this->
hasTitle() || !$pageTitle->equals( $this->getTitle() ) ) {
219 $this->wikipage = $wikiPage;
233 if ( $this->wikipage ===
null ) {
236 throw new MWException( __METHOD__ .
' called without Title object set' );
238 $this->wikipage = WikiPage::factory(
$title );
241 return $this->wikipage;
248 $this->output = $output;
255 if ( $this->output ===
null ) {
259 return $this->output;
275 if ( $this->user ===
null ) {
276 $this->user = User::newFromSession( $this->
getRequest() );
292 $code = strtolower( $code );
295 if ( !$code || !Language::isValidCode( $code ) || $code ===
'qqq' ) {
308 if ( $language instanceof
Language ) {
309 $this->lang = $language;
310 } elseif ( is_string( $language ) ) {
311 $language = self::sanitizeLangCode( $language );
312 $obj = Language::factory( $language );
315 throw new MWException( __METHOD__ .
" was passed an invalid type of data." );
327 if ( $this->languageRecursion ===
true ) {
328 trigger_error(
"Recursion detected in " . __METHOD__, E_USER_WARNING );
330 wfDebugLog(
'recursion-guard',
"Recursion detected:\n" . $e->getTraceAsString() );
332 $code = $this->
getConfig()->get(
'LanguageCode' ) ?:
'en';
333 $this->lang = Language::factory( $code );
334 } elseif ( $this->lang ===
null ) {
335 $this->languageRecursion =
true;
343 if ( $code ===
'user' ) {
346 $code = self::sanitizeLangCode( $code );
348 Hooks::run(
'UserGetLanguageObject', [
$user, &$code, $this ] );
350 if ( $code === $this->
getConfig()->
get(
'LanguageCode' ) ) {
351 $this->lang = MediaWikiServices::getInstance()->getContentLanguage();
353 $obj = Language::factory( $code );
357 $this->languageRecursion =
false;
368 $this->skin = clone $skin;
376 if ( $this->skin ===
null ) {
378 Hooks::run(
'RequestContextCreateSkin', [ $this, &
$skin ] );
379 $factory = MediaWikiServices::getInstance()->getSkinFactory();
384 } elseif ( is_string(
$skin ) ) {
387 $normalized = Skin::normalizeKey(
$skin );
388 $this->skin = $factory->makeSkin( $normalized );
391 if ( !in_array(
'skin', $this->
getConfig()->
get(
'HiddenPrefs' ) ) ) {
392 $userSkin = $this->
getUser()->getOption(
'skin' );
394 $userSkin = $this->
getRequest()->getRawVal(
'useskin', $userSkin );
396 $userSkin = $this->
getConfig()->get(
'DefaultSkin' );
402 $normalized = Skin::normalizeKey( $userSkin );
403 $this->skin = $factory->makeSkin( $normalized );
407 $this->skin->setContext( $this );
422 public function msg( $key, ...$params ) {
423 return wfMessage( $key, ...$params )->setContext( $this );
432 if ( self::$instance ===
null ) {
433 self::$instance =
new self;
436 return self::$instance;
448 wfDebug( $func .
' called without context. ' .
449 "Using RequestContext::getMain() for sanity\n" );
451 return self::getMain();
458 if ( !( defined(
'MW_PHPUNIT_TEST' ) || defined(
'MW_PARSER_TEST' ) ) ) {
459 throw new MWException( __METHOD__ .
'() should be called only from unit tests!' );
461 self::$instance =
null;
472 $session = MediaWiki\Session\SessionManager::getGlobalSession();
475 'headers' => $this->
getRequest()->getAllHeaders(),
476 'sessionId' => $session->isPersistent() ? $session->getId() :
'',
477 'userId' => $this->
getUser()->getId()
504 if ( strlen( $params[
'sessionId'] ) &&
505 MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent()
509 throw new MWException(
"Sessions can only be imported when none is active." );
510 } elseif ( !IP::isValid( $params[
'ip'] ) ) {
511 throw new MWException(
"Invalid client IP address '{$params['ip']}'." );
514 if ( $params[
'userId'] ) {
515 $user = User::newFromId( $params[
'userId'] );
518 throw new MWException(
"No user with ID '{$params['userId']}'." );
521 $user = User::newFromName( $params[
'ip'],
false );
524 $importSessionFunc =
function (
User $user, array $params ) {
530 if (
MediaWiki\Session\PHPSessionHandler::isEnabled() ) {
531 session_write_close();
538 if ( strlen( $params[
'sessionId'] ) ) {
539 $manager = MediaWiki\Session\SessionManager::singleton();
540 $session = $manager->getSessionById( $params[
'sessionId'],
true )
541 ?: $manager->getEmptySession();
555 if ( $session &&
MediaWiki\Session\PHPSessionHandler::isEnabled() ) {
556 session_id( $session->getId() );
557 AtEase::quietCall(
'session_start' );
561 foreach ( $params[
'headers'] as $name => $value ) {
562 $request->setHeader( $name, $value );
570 $oUser = self::getMain()->getUser();
571 $oParams = self::getMain()->exportSession();
572 $oRequest = self::getMain()->getRequest();
573 $importSessionFunc( $user, $params );
576 return new ScopedCallback(
577 function () use ( $importSessionFunc, $oUser, $oParams, $oRequest ) {
579 $importSessionFunc( $oUser, $oParams );
609 $context->user = User::newFromName(
'127.0.0.1',
false );
$wgLanguageCode
Site language code.
global $wgCommandLineMode
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfGetAllCallers( $limit=3)
Return a string consisting of callers in the stack.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(! $wgDBerrorLogTZ) $wgRequest
if(! $wgRequest->checkUrlExtension()) if(isset( $_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] !='') $wgTitle
setContext(IContextSource $context)
WebRequest clone which takes values from a provided array.
Internationalisation code.
This is one of the Core classes and should be read at least once by any new developers.
Group all the pieces relevant to the context of a request into one instance.
static getMainAndWarn( $func=__METHOD__)
Get the RequestContext object associated with the main request and gives a warning to the log,...
canUseWikiPage()
Check whether a WikiPage object can be get with getWikiPage().
static importScopedSession(array $params)
Import an client IP address, HTTP headers, user ID, and session ID.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
static RequestContext $instance
static sanitizeLangCode( $code)
Accepts a language code and ensures it's sane.
setConfig(Config $config)
hasTitle()
Check, if a Title object is set.
bool $languageRecursion
Boolean flag to guard against recursion in getLanguage.
setTitle(Title $title=null)
static newExtraneousContext(Title $title, $request=[])
Create a new extraneous context.
exportSession()
Export the resolved user IP, HTTP headers, user ID, and session ID.
setOutput(OutputPage $output)
setWikiPage(WikiPage $wikiPage)
static resetMain()
Resets singleton returned by getMain().
static getMain()
Get the RequestContext object associated with the main request.
getLanguage()
Get the Language object.
setRequest(WebRequest $request)
getWikiPage()
Get the WikiPage object.
The main skin class which provides methods and properties for all other skins.
An interface to help developers measure the performance of their applications.
Represents a title within MediaWiki.
canExist()
Is this in a namespace that allows actual pages?
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
getId()
Get the user's ID.
getOption( $oname, $defaultOverride=null, $ignoreHidden=false)
Get the user's current setting for a given option.
load( $flags=self::READ_NORMAL)
Load the user table data for this object from the source given by mFrom.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
getRawVal( $name, $default=null)
Fetch a scalar from the input without normalization, or return $default if it's not set.
Class representing a MediaWiki article and history.
getTitle()
Get the title object of the article.
Interface for configuration instances.
Interface for objects which can provide a MediaWiki context on request.
if(!isset( $args[0])) $lang