27use Wikimedia\AtEase\AtEase;
29use Wikimedia\ScopedCallback;
99 $this->config = $config;
106 if ( $this->config ===
null ) {
109 $this->config = MediaWikiServices::getInstance()->getMainConfig();
112 return $this->config;
119 $this->request = $request;
126 if ( $this->request ===
null ) {
136 return $this->request;
145 return MediaWikiServices::getInstance()->getStatsdDataFactory();
152 if ( $this->timing ===
null ) {
153 $this->timing =
new Timing( [
154 'logger' => LoggerFactory::getInstance(
'Timing' )
157 return $this->timing;
166 $this->wikipage =
null;
173 if ( $this->title ===
null ) {
174 global
$wgTitle; # fallback to $wg till we can improve
this
176 $logger = LoggerFactory::getInstance(
'GlobalTitleFail' );
178 __METHOD__ .
' called with no title set.',
179 [
'exception' =>
new Exception ]
193 return $this->title !==
null;
205 if ( $this->wikipage ) {
221 if ( !$this->
hasTitle() || !$pageTitle->equals( $this->getTitle() ) ) {
225 $this->wikipage = $wikiPage;
239 if ( $this->wikipage ===
null ) {
242 throw new MWException( __METHOD__ .
' called without Title object set' );
244 $this->wikipage = WikiPage::factory(
$title );
247 return $this->wikipage;
254 $this->output = $output;
261 if ( $this->output ===
null ) {
265 return $this->output;
281 if ( $this->user ===
null ) {
298 $code = strtolower( $code );
302 || !MediaWikiServices::getInstance()->getLanguageNameUtils()
303 ->isValidCode( $code )
318 if ( $language instanceof
Language ) {
319 $this->lang = $language;
320 } elseif ( is_string( $language ) ) {
321 $language = self::sanitizeLangCode( $language );
322 $obj = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $language );
325 throw new MWException( __METHOD__ .
" was passed an invalid type of data." );
337 if ( $this->languageRecursion ===
true ) {
338 trigger_error(
"Recursion detected in " . __METHOD__, E_USER_WARNING );
340 wfDebugLog(
'recursion-guard',
"Recursion detected:\n" . $e->getTraceAsString() );
342 $code = $this->
getConfig()->get(
'LanguageCode' ) ?:
'en';
343 $this->lang = MediaWikiServices::getInstance()->getLanguageFactory()
344 ->getLanguage( $code );
345 } elseif ( $this->lang ===
null ) {
346 $this->languageRecursion =
true;
354 if ( $code ===
'user' ) {
369 $code = self::sanitizeLangCode( $code );
371 Hooks::runner()->onUserGetLanguageObject(
$user, $code, $this );
373 if ( $code === $this->
getConfig()->
get(
'LanguageCode' ) ) {
374 $this->lang = MediaWikiServices::getInstance()->getContentLanguage();
376 $obj = MediaWikiServices::getInstance()->getLanguageFactory()
377 ->getLanguage( $code );
381 $this->languageRecursion =
false;
392 $this->skin = clone $skin;
400 if ( $this->skin ===
null ) {
402 Hooks::runner()->onRequestContextCreateSkin( $this,
$skin );
403 $factory = MediaWikiServices::getInstance()->getSkinFactory();
408 } elseif ( is_string(
$skin ) ) {
411 $normalized = Skin::normalizeKey(
$skin );
412 $this->skin = $factory->makeSkin( $normalized );
415 if ( !in_array(
'skin', $this->
getConfig()->
get(
'HiddenPrefs' ) ) ) {
416 $userSkin = $this->
getUser()->getOption(
'skin' );
418 $userSkin = $this->
getRequest()->getRawVal(
'useskin', $userSkin );
420 $userSkin = $this->
getConfig()->get(
'DefaultSkin' );
426 $normalized = Skin::normalizeKey( $userSkin );
427 $this->skin = $factory->makeSkin( $normalized );
431 $this->skin->setContext( $this );
446 public function msg( $key, ...$params ) {
447 return wfMessage( $key, ...$params )->setContext( $this );
456 if ( self::$instance ===
null ) {
457 self::$instance =
new self;
460 return self::$instance;
472 wfDebug( $func .
' called without context. ' .
473 "Using RequestContext::getMain() for sanity" );
475 return self::getMain();
482 if ( !( defined(
'MW_PHPUNIT_TEST' ) || defined(
'MW_PARSER_TEST' ) ) ) {
483 throw new MWException( __METHOD__ .
'() should be called only from unit tests!' );
485 self::$instance =
null;
496 $session = MediaWiki\Session\SessionManager::getGlobalSession();
499 'headers' => $this->
getRequest()->getAllHeaders(),
500 'sessionId' => $session->isPersistent() ? $session->getId() :
'',
501 'userId' => $this->
getUser()->getId()
528 if ( strlen( $params[
'sessionId'] ) &&
529 MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent()
533 throw new MWException(
"Sessions can only be imported when none is active." );
534 } elseif ( !IPUtils::isValid( $params[
'ip'] ) ) {
535 throw new MWException(
"Invalid client IP address '{$params['ip']}'." );
538 if ( $params[
'userId'] ) {
542 throw new MWException(
"No user with ID '{$params['userId']}'." );
548 $importSessionFunc =
function (
User $user, array $params ) {
554 if (
MediaWiki\Session\PHPSessionHandler::isEnabled() ) {
555 session_write_close();
562 if ( strlen( $params[
'sessionId'] ) ) {
563 $manager = MediaWiki\Session\SessionManager::singleton();
564 $session = $manager->getSessionById( $params[
'sessionId'],
true )
565 ?: $manager->getEmptySession();
570 $context->setRequest(
new FauxRequest( [],
false, $session ) );
577 $context->setUser( $user );
578 $wgUser = $context->getUser();
579 if ( $session &&
MediaWiki\Session\PHPSessionHandler::isEnabled() ) {
580 session_id( $session->getId() );
581 AtEase::quietCall(
'session_start' );
585 foreach ( $params[
'headers'] as $name => $value ) {
586 $request->setHeader( $name, $value );
594 $oUser = self::getMain()->getUser();
595 $oParams = self::getMain()->exportSession();
596 $oRequest = self::getMain()->getRequest();
597 $importSessionFunc( $user, $params );
600 return new ScopedCallback(
601 function () use ( $importSessionFunc, $oUser, $oParams, $oRequest ) {
603 $importSessionFunc( $oUser, $oParams );
627 $context->setTitle(
$title );
$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.
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
setContext(IContextSource $context)
WebRequest clone which takes values from a provided array.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
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 @newable.
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()
Can this title represent a page in the wiki's database?
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
getId()
Get the user's ID.
getOption( $oname, $defaultOverride=null, $ignoreHidden=false)
Get the user's current setting for a given option.
static newFromId( $id)
Static factory method for creation from a given user ID.
static newFromSession(WebRequest $request=null)
Create a new user object using data from session.
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