Thanks link and other links should be styled consistently with other links
[mediawiki.git] / includes / context / RequestContext.php
blob2ea1f29b8337d012747f1e66cdb285053f639428
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @since 1.18
20 * @author Alexandre Emsenhuber
21 * @author Daniel Friesen
22 * @file
25 namespace MediaWiki\Context;
27 use BadMethodCallException;
28 use InvalidArgumentException;
29 use LogicException;
30 use MediaWiki\Config\Config;
31 use MediaWiki\HookContainer\HookRunner;
32 use MediaWiki\Language\Language;
33 use MediaWiki\Logger\LoggerFactory;
34 use MediaWiki\MainConfigNames;
35 use MediaWiki\MediaWikiServices;
36 use MediaWiki\Message\Message;
37 use MediaWiki\Output\OutputPage;
38 use MediaWiki\Permissions\Authority;
39 use MediaWiki\Request\FauxRequest;
40 use MediaWiki\Request\WebRequest;
41 use MediaWiki\Session\CsrfTokenSet;
42 use MediaWiki\Session\PHPSessionHandler;
43 use MediaWiki\Session\SessionManager;
44 use MediaWiki\StubObject\StubGlobalUser;
45 use MediaWiki\Title\Title;
46 use MediaWiki\User\User;
47 use MediaWiki\User\UserRigorOptions;
48 use RuntimeException;
49 use Skin;
50 use Timing;
51 use Wikimedia\Assert\Assert;
52 use Wikimedia\AtEase\AtEase;
53 use Wikimedia\Bcp47Code\Bcp47Code;
54 use Wikimedia\IPUtils;
55 use Wikimedia\Message\MessageParam;
56 use Wikimedia\Message\MessageSpecifier;
57 use Wikimedia\NonSerializable\NonSerializableTrait;
58 use Wikimedia\ScopedCallback;
59 use WikiPage;
61 /**
62 * Group all the pieces relevant to the context of a request into one instance
63 * @newable
64 * @note marked as newable in 1.35 for lack of a better alternative,
65 * but should use a factory in the future and should be narrowed
66 * down to not expose heavy weight objects.
68 class RequestContext implements IContextSource, MutableContext {
69 use NonSerializableTrait;
71 /**
72 * @var WebRequest
74 private $request;
76 /**
77 * @var Title
79 private $title;
81 /**
82 * @var WikiPage|null
84 private $wikipage;
86 /**
87 * @var null|string
89 private $action;
91 /**
92 * @var OutputPage
94 private $output;
96 /**
97 * @var User|null
99 private $user;
102 * @var Authority
104 private $authority;
107 * @var Language|null
109 private $lang;
112 * @var Skin|null
114 private $skin;
117 * @var Timing
119 private $timing;
122 * @var Config
124 private $config;
127 * @var RequestContext|null
129 private static $instance = null;
132 * Boolean flag to guard against recursion in getLanguage
133 * @var bool
135 private $languageRecursion = false;
137 /** @var Skin|string|null */
138 private $skinFromHook;
140 /** @var bool */
141 private $skinHookCalled = false;
143 /** @var string|null */
144 private $skinName;
146 public function setConfig( Config $config ) {
147 $this->config = $config;
151 * @return Config
153 public function getConfig() {
154 // @todo In the future, we could move this to WebStart.php so
155 // the Config object is ready for when initialization happens
156 $this->config ??= MediaWikiServices::getInstance()->getMainConfig();
158 return $this->config;
161 public function setRequest( WebRequest $request ) {
162 $this->request = $request;
166 * @return WebRequest
168 public function getRequest() {
169 if ( $this->request === null ) {
170 // create the WebRequest object on the fly
171 if ( MW_ENTRY_POINT === 'cli' ) {
172 // Don't use real WebRequest in CLI mode, it throws errors when trying to access
173 // things that don't exist, e.g. "Unable to determine IP".
174 $this->request = new FauxRequest( [] );
175 } else {
176 $this->request = new WebRequest();
180 return $this->request;
184 * @return Timing
186 public function getTiming() {
187 $this->timing ??= new Timing( [
188 'logger' => LoggerFactory::getInstance( 'Timing' )
189 ] );
190 return $this->timing;
194 * @param Title|null $title
196 public function setTitle( ?Title $title = null ) {
197 $this->title = $title;
198 // Clear cache of derived getters
199 $this->wikipage = null;
200 $this->clearActionName();
204 * @return Title|null
206 public function getTitle() {
207 if ( $this->title === null ) {
208 // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgTitle
209 global $wgTitle; # fallback to $wg till we can improve this
210 $this->title = $wgTitle;
211 $logger = LoggerFactory::getInstance( 'GlobalTitleFail' );
212 $logger->info(
213 __METHOD__ . ' called with no title set.',
214 [ 'exception' => new RuntimeException ]
218 return $this->title;
222 * Check, if a Title object is set
224 * @since 1.25
225 * @return bool
227 public function hasTitle() {
228 return $this->title !== null;
232 * Check whether a WikiPage object can be get with getWikiPage().
233 * Callers should expect that an exception is thrown from getWikiPage()
234 * if this method returns false.
236 * @since 1.19
237 * @return bool
239 public function canUseWikiPage() {
240 if ( $this->wikipage ) {
241 // If there's a WikiPage object set, we can for sure get it
242 return true;
244 // Only pages with legitimate titles can have WikiPages.
245 // That usually means pages in non-virtual namespaces.
246 $title = $this->getTitle();
247 return $title && $title->canExist();
251 * @since 1.19
252 * @param WikiPage $wikiPage
254 public function setWikiPage( WikiPage $wikiPage ) {
255 $pageTitle = $wikiPage->getTitle();
256 if ( !$this->hasTitle() || !$pageTitle->equals( $this->getTitle() ) ) {
257 $this->setTitle( $pageTitle );
259 // Defer this to the end since setTitle sets it to null.
260 $this->wikipage = $wikiPage;
261 // Clear cache of derived getter
262 $this->clearActionName();
266 * Get the WikiPage object.
267 * May throw an exception if there's no Title object set or the Title object
268 * belongs to a special namespace that doesn't have WikiPage, so use first
269 * canUseWikiPage() to check whether this method can be called safely.
271 * @since 1.19
272 * @return WikiPage
274 public function getWikiPage() {
275 if ( $this->wikipage === null ) {
276 $title = $this->getTitle();
277 if ( $title === null ) {
278 throw new BadMethodCallException( __METHOD__ . ' called without Title object set' );
280 $this->wikipage = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
283 return $this->wikipage;
287 * @since 1.38
288 * @param string $action
290 public function setActionName( string $action ): void {
291 $this->action = $action;
295 * Get the action name for the current web request.
297 * This generally returns "view" if the current request or process is
298 * not for a skinned index.php web request (e.g. load.php, thumb.php,
299 * job runner, CLI, API).
301 * @warning This must not be called before or during the Setup.php phase,
302 * and may cause an error or warning if called too early.
304 * @since 1.38
305 * @return string Action
307 public function getActionName(): string {
308 // Optimisation: This is cached to avoid repeated running of the
309 // expensive operations to compute this. The computation involves creation
310 // of Article, WikiPage, and ContentHandler objects (and the various
311 // database queries these classes require to be instantiated), as well
312 // as potentially slow extension hooks in these classes.
314 // This value is frequently needed in OutputPage and in various
315 // Skin-related methods and classes.
316 $this->action ??= MediaWikiServices::getInstance()
317 ->getActionFactory()
318 ->getActionName( $this );
320 return $this->action;
323 private function clearActionName(): void {
324 if ( $this->action !== null ) {
325 // If we're clearing after something else has actually already computed the action,
326 // emit a warning.
328 // Doing so is unstable, given the first caller got something that turns out to be
329 // incomplete or incorrect. Even if we end up re-creating an instance of the same
330 // class, we may now be acting on a different title/skin/user etc.
332 // Re-computing the action is expensive and can be a performance problem (T302623).
333 trigger_error( 'Unexpected clearActionName after getActionName already called' );
334 $this->action = null;
338 public function setOutput( OutputPage $output ) {
339 $this->output = $output;
343 * @return OutputPage
345 public function getOutput() {
346 $this->output ??= new OutputPage( $this );
348 return $this->output;
351 public function setUser( User $user ) {
352 $this->user = $user;
353 // Keep authority consistent
354 $this->authority = $user;
355 // Invalidate cached user interface language and skin
356 $this->lang = null;
357 $this->skin = null;
358 $this->skinName = null;
362 * @return User
364 public function getUser() {
365 if ( $this->user === null ) {
366 if ( $this->authority !== null ) {
367 // Keep user consistent by using a possible set authority
368 $this->user = MediaWikiServices::getInstance()
369 ->getUserFactory()
370 ->newFromAuthority( $this->authority );
371 } else {
372 $this->user = User::newFromSession( $this->getRequest() );
376 return $this->user;
379 public function hasUser(): bool {
380 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
381 throw new LogicException( __METHOD__ . '() should be called only from tests!' );
383 return $this->user !== null;
386 public function setAuthority( Authority $authority ) {
387 $this->authority = $authority;
388 // If needed, a User object is constructed from this authority
389 $this->user = null;
390 // Invalidate cached user interface language and skin
391 $this->lang = null;
392 $this->skin = null;
393 $this->skinName = null;
397 * @since 1.36
398 * @return Authority
400 public function getAuthority(): Authority {
401 return $this->authority ?: $this->getUser();
405 * Accepts a language code and ensures it's sensible. Outputs a cleaned up language
406 * code and replaces with $wgLanguageCode if not sensible.
407 * @param ?string $code Language code
408 * @return string
410 public static function sanitizeLangCode( $code ) {
411 global $wgLanguageCode;
413 if ( !$code ) {
414 return $wgLanguageCode;
417 // BCP 47 - letter case MUST NOT carry meaning
418 $code = strtolower( $code );
420 # Validate $code
421 if ( !MediaWikiServices::getInstance()->getLanguageNameUtils()
422 ->isValidCode( $code )
423 || $code === 'qqq'
425 $code = $wgLanguageCode;
428 return $code;
432 * @param Language|string $language Language instance or language code
433 * @since 1.19
435 public function setLanguage( $language ) {
436 Assert::parameterType( [ Language::class, 'string' ], $language, '$language' );
437 if ( $language instanceof Language ) {
438 $this->lang = $language;
439 } else {
440 $language = self::sanitizeLangCode( $language );
441 $obj = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $language );
442 $this->lang = $obj;
444 OutputPage::resetOOUI();
448 * Get the Language object.
449 * Initialization of user or request objects can depend on this.
450 * @return Language
451 * @throws LogicException
452 * @since 1.19
454 public function getLanguage() {
455 if ( $this->languageRecursion === true ) {
456 throw new LogicException( 'Recursion detected' );
459 if ( $this->lang === null ) {
460 $this->languageRecursion = true;
462 try {
463 $request = $this->getRequest();
464 $user = $this->getUser();
465 $services = MediaWikiServices::getInstance();
467 // Optimisation: Avoid slow getVal(), this isn't user-generated content.
468 $code = $request->getRawVal( 'uselang' ) ?? 'user';
469 if ( $code === 'user' ) {
470 $userOptionsLookup = $services->getUserOptionsLookup();
471 $code = $userOptionsLookup->getOption( $user, 'language' );
474 // There are certain characters we don't allow in language code strings,
475 // but by and large almost any valid UTF-8 string will makes it past
476 // this check and the LanguageNameUtils::isValidCode method it uses.
477 // This is to support on-wiki interface message overrides for
478 // non-existent language codes. Also known as "Uselang hacks".
479 // See <https://www.mediawiki.org/wiki/Manual:Uselang_hack>
480 // For something like "en-whatever" or "de-whatever" it will end up
481 // with a mostly "en" or "de" interface, but with an extra layer of
482 // possible MessageCache overrides from `MediaWiki:*/<code>` titles.
483 // While non-ASCII works here, it is required that they are in
484 // NFC form given this will not convert to normalised form.
485 $code = self::sanitizeLangCode( $code );
487 ( new HookRunner( $services->getHookContainer() ) )->onUserGetLanguageObject( $user, $code, $this );
489 if ( $code === $this->getConfig()->get( MainConfigNames::LanguageCode ) ) {
490 $this->lang = $services->getContentLanguage();
491 } else {
492 $obj = $services->getLanguageFactory()
493 ->getLanguage( $code );
494 $this->lang = $obj;
496 } finally {
497 $this->languageRecursion = false;
501 return $this->lang;
505 * @since 1.42
506 * @return Bcp47Code
508 public function getLanguageCode() {
509 return $this->getLanguage();
512 public function setSkin( Skin $skin ) {
513 $this->skin = clone $skin;
514 $this->skin->setContext( $this );
515 $this->skinName = $skin->getSkinName();
516 OutputPage::resetOOUI();
520 * Get the name of the skin
522 * @since 1.41
523 * @return string
525 public function getSkinName() {
526 if ( $this->skinName === null ) {
527 $this->skinName = $this->fetchSkinName();
529 return $this->skinName;
533 * Get the name of the skin, without caching
535 * @return string
537 private function fetchSkinName() {
538 $skinFromHook = $this->getSkinFromHook();
539 if ( $skinFromHook instanceof Skin ) {
540 // The hook provided a skin object
541 return $skinFromHook->getSkinName();
542 } elseif ( is_string( $skinFromHook ) ) {
543 // The hook provided a skin name
544 $skinName = $skinFromHook;
545 } elseif ( !in_array( 'skin', $this->getConfig()->get( MainConfigNames::HiddenPrefs ) ) ) {
546 // The normal case
547 $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
548 $userSkin = $userOptionsLookup->getOption( $this->getUser(), 'skin' );
549 // Optimisation: Avoid slow getVal(), this isn't user-generated content.
550 $skinName = $this->getRequest()->getRawVal( 'useskin' ) ?? $userSkin;
551 } else {
552 // User preference disabled
553 $skinName = $this->getConfig()->get( MainConfigNames::DefaultSkin );
555 return Skin::normalizeKey( $skinName );
559 * Get the skin set by the RequestContextCreateSkin hook, if there is any.
561 * @return Skin|string|null
563 private function getSkinFromHook() {
564 if ( !$this->skinHookCalled ) {
565 $this->skinHookCalled = true;
566 ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
567 ->onRequestContextCreateSkin( $this, $this->skinFromHook );
569 return $this->skinFromHook;
573 * @return Skin
575 public function getSkin() {
576 if ( $this->skin === null ) {
577 $skinFromHook = $this->getSkinFromHook();
578 if ( $skinFromHook instanceof Skin ) {
579 $this->skin = $skinFromHook;
580 } else {
581 $skinName = is_string( $skinFromHook )
582 ? Skin::normalizeKey( $skinFromHook )
583 : $this->getSkinName();
584 $factory = MediaWikiServices::getInstance()->getSkinFactory();
585 $this->skin = $factory->makeSkin( $skinName );
587 $this->skin->setContext( $this );
589 return $this->skin;
593 * Get a Message object with context set
594 * Parameters are the same as wfMessage()
596 * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
597 * or a MessageSpecifier.
598 * @phpcs:ignore Generic.Files.LineLength
599 * @param MessageParam|MessageSpecifier|string|int|float|list<MessageParam|MessageSpecifier|string|int|float> ...$params
600 * See Message::params()
601 * @return Message
603 public function msg( $key, ...$params ) {
604 return wfMessage( $key, ...$params )->setContext( $this );
608 * Get the RequestContext object associated with the main request
610 public static function getMain(): RequestContext {
611 self::$instance ??= new self;
613 return self::$instance;
617 * Get the RequestContext object associated with the main request
618 * and gives a warning to the log, to find places, where a context maybe is missing.
620 * @param string $func @phan-mandatory-param
621 * @return RequestContext
622 * @since 1.24
624 public static function getMainAndWarn( $func = __METHOD__ ) {
625 wfDebug( $func . ' called without context. ' .
626 "Using RequestContext::getMain()" );
628 return self::getMain();
632 * Resets singleton returned by getMain(). Should be called only from unit tests.
634 public static function resetMain() {
635 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
636 throw new LogicException( __METHOD__ . '() should be called only from unit tests!' );
638 self::$instance = null;
642 * Export the resolved user IP, HTTP headers, user ID, and session ID.
643 * The result will be reasonably sized to allow for serialization.
645 * @return array
646 * @since 1.21
648 public function exportSession() {
649 $session = SessionManager::getGlobalSession();
650 return [
651 'ip' => $this->getRequest()->getIP(),
652 'headers' => $this->getRequest()->getAllHeaders(),
653 'sessionId' => $session->isPersistent() ? $session->getId() : '',
654 'userId' => $this->getUser()->getId()
658 public function getCsrfTokenSet(): CsrfTokenSet {
659 return new CsrfTokenSet( $this->getRequest() );
663 * Import a client IP address, HTTP headers, user ID, and session ID
665 * This sets the current session, $wgUser, and $wgRequest from $params.
666 * Once the return value falls out of scope, the old context is restored.
667 * This method should only be called in contexts where there is no session
668 * ID or end user receiving the response (CLI or HTTP job runners). This
669 * is partly enforced, and is done so to avoid leaking cookies if certain
670 * error conditions arise.
672 * This is useful when background scripts inherit context when acting on
673 * behalf of a user. In general the 'sessionId' parameter should be set
674 * to an empty string unless session importing is *truly* needed. This
675 * feature is somewhat deprecated.
677 * @param array $params Result of RequestContext::exportSession()
678 * @return ScopedCallback
679 * @since 1.21
681 public static function importScopedSession( array $params ) {
682 if ( strlen( $params['sessionId'] ) &&
683 SessionManager::getGlobalSession()->isPersistent()
685 // Check to avoid sending random cookies for the wrong users.
686 // This method should only called by CLI scripts or by HTTP job runners.
687 throw new BadMethodCallException( "Sessions can only be imported when none is active." );
688 } elseif ( !IPUtils::isValid( $params['ip'] ) ) {
689 throw new InvalidArgumentException( "Invalid client IP address '{$params['ip']}'." );
692 $userFactory = MediaWikiServices::getInstance()->getUserFactory();
694 if ( $params['userId'] ) { // logged-in user
695 $user = $userFactory->newFromId( (int)$params['userId'] );
696 $user->load();
697 if ( !$user->isRegistered() ) {
698 throw new InvalidArgumentException( "No user with ID '{$params['userId']}'." );
700 } else { // anon user
701 $user = $userFactory->newFromName( $params['ip'], UserRigorOptions::RIGOR_NONE );
704 $importSessionFunc = static function ( User $user, array $params ) {
705 global $wgRequest;
707 $context = RequestContext::getMain();
709 // Commit and close any current session
710 if ( PHPSessionHandler::isEnabled() ) {
711 session_write_close(); // persist
712 session_id( '' ); // detach
713 $_SESSION = []; // clear in-memory array
716 // Get new session, if applicable
717 $session = null;
718 if ( strlen( $params['sessionId'] ) ) { // don't make a new random ID
719 $manager = SessionManager::singleton();
720 $session = $manager->getSessionById( $params['sessionId'], true )
721 ?: $manager->getEmptySession();
724 // Remove any user IP or agent information, and attach the request
725 // with the new session.
726 $context->setRequest( new FauxRequest( [], false, $session ) );
727 $wgRequest = $context->getRequest(); // b/c
729 // Now that all private information is detached from the user, it should
730 // be safe to load the new user. If errors occur or an exception is thrown
731 // and caught (leaving the main context in a mixed state), there is no risk
732 // of the User object being attached to the wrong IP, headers, or session.
733 $context->setUser( $user );
734 StubGlobalUser::setUser( $context->getUser() ); // b/c
735 if ( $session && PHPSessionHandler::isEnabled() ) {
736 session_id( $session->getId() );
737 AtEase::quietCall( 'session_start' );
739 $request = new FauxRequest( [], false, $session );
740 $request->setIP( $params['ip'] );
741 foreach ( $params['headers'] as $name => $value ) {
742 $request->setHeader( $name, $value );
744 // Set the current context to use the new WebRequest
745 $context->setRequest( $request );
746 $wgRequest = $context->getRequest(); // b/c
749 // Stash the old session and load in the new one
750 $oUser = self::getMain()->getUser();
751 $oParams = self::getMain()->exportSession();
752 $oRequest = self::getMain()->getRequest();
753 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable exceptions triggered above prevent the null case
754 $importSessionFunc( $user, $params );
756 // Set callback to save and close the new session and reload the old one
757 return new ScopedCallback(
758 static function () use ( $importSessionFunc, $oUser, $oParams, $oRequest ) {
759 global $wgRequest;
760 $importSessionFunc( $oUser, $oParams );
761 // Restore the exact previous Request object (instead of leaving MediaWiki\Request\FauxRequest)
762 RequestContext::getMain()->setRequest( $oRequest );
763 $wgRequest = RequestContext::getMain()->getRequest(); // b/c
769 * Create a new extraneous context. The context is filled with information
770 * external to the current session.
771 * - Title is specified by argument
772 * - Request is a MediaWiki\Request\FauxRequest, or a MediaWiki\Request\FauxRequest can be specified by argument
773 * - User is an anonymous user, for separation IPv4 localhost is used
774 * - Language will be based on the anonymous user and request, may be content
775 * language or a uselang param in the fauxrequest data may change the lang
776 * - Skin will be based on the anonymous user, should be the wiki's default skin
778 * @param Title $title Title to use for the extraneous request
779 * @param WebRequest|array $request A WebRequest or data to use for a MediaWiki\Request\FauxRequest
780 * @return RequestContext
782 public static function newExtraneousContext( Title $title, $request = [] ) {
783 $context = new self;
784 $context->setTitle( $title );
785 if ( $request instanceof WebRequest ) {
786 $context->setRequest( $request );
787 } else {
788 $context->setRequest( new FauxRequest( $request ) );
790 $context->user = MediaWikiServices::getInstance()->getUserFactory()->newFromName(
791 '127.0.0.1',
792 UserRigorOptions::RIGOR_NONE
795 return $context;
798 /** @return never */
799 public function __clone() {
800 throw new LogicException(
801 __CLASS__ . ' should not be cloned, use DerivativeContext instead.'
807 /** @deprecated class alias since 1.42 */
808 class_alias( RequestContext::class, 'RequestContext' );