Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
65.32% covered (warning)
65.32%
145 / 222
48.72% covered (danger)
48.72%
19 / 39
CRAP
0.00% covered (danger)
0.00%
0 / 1
RequestContext
65.61% covered (warning)
65.61%
145 / 221
48.72% covered (danger)
48.72%
19 / 39
363.17
0.00% covered (danger)
0.00%
0 / 1
 setConfig
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getConfig
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setRequest
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRequest
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 getTiming
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 setTitle
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getTitle
25.00% covered (danger)
25.00%
2 / 8
0.00% covered (danger)
0.00%
0 / 1
3.69
 hasTitle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 canUseWikiPage
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 setWikiPage
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getWikiPage
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 setActionName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActionName
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 clearActionName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setOutput
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOutput
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setUser
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getUser
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 hasUser
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 setAuthority
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 getAuthority
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 sanitizeLangCode
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 setLanguage
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getLanguage
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
30
 getLanguageCode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setSkin
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getSkinName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 fetchSkinName
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
4.10
 getSkinFromHook
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getSkin
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 msg
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMain
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getMainAndWarn
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 resetMain
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 exportSession
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getCsrfTokenSet
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 importScopedSession
89.80% covered (warning)
89.80%
44 / 49
0.00% covered (danger)
0.00%
0 / 1
12.15
 newExtraneousContext
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 __clone
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @since 1.18
5 *
6 * @author Alexandre Emsenhuber
7 * @author Daniel Friesen
8 * @file
9 */
10
11namespace MediaWiki\Context;
12
13use BadMethodCallException;
14use InvalidArgumentException;
15use LogicException;
16use MediaWiki\Config\Config;
17use MediaWiki\HookContainer\HookRunner;
18use MediaWiki\Language\Language;
19use MediaWiki\Logger\LoggerFactory;
20use MediaWiki\MainConfigNames;
21use MediaWiki\MediaWikiServices;
22use MediaWiki\Message\Message;
23use MediaWiki\Output\OutputPage;
24use MediaWiki\Page\WikiPage;
25use MediaWiki\Permissions\Authority;
26use MediaWiki\Request\FauxRequest;
27use MediaWiki\Request\WebRequest;
28use MediaWiki\Session\CsrfTokenSet;
29use MediaWiki\Session\PHPSessionHandler;
30use MediaWiki\Skin\Skin;
31use MediaWiki\StubObject\StubGlobalUser;
32use MediaWiki\Title\Title;
33use MediaWiki\User\User;
34use MediaWiki\User\UserRigorOptions;
35use RuntimeException;
36use Wikimedia\Assert\Assert;
37use Wikimedia\AtEase\AtEase;
38use Wikimedia\Bcp47Code\Bcp47Code;
39use Wikimedia\IPUtils;
40use Wikimedia\Message\MessageParam;
41use Wikimedia\Message\MessageSpecifier;
42use Wikimedia\NonSerializable\NonSerializableTrait;
43use Wikimedia\ScopedCallback;
44use Wikimedia\Timing\Timing;
45
46/**
47 * Group all the pieces relevant to the context of a request into one instance
48 * @newable
49 * @note marked as newable in 1.35 for lack of a better alternative,
50 *       but should use a factory in the future and should be narrowed
51 *       down to not expose heavy weight objects.
52 */
53class RequestContext implements IContextSource, MutableContext {
54    use NonSerializableTrait;
55
56    /**
57     * @var WebRequest
58     */
59    private $request;
60
61    /**
62     * @var Title
63     */
64    private $title;
65
66    /**
67     * @var WikiPage|null
68     */
69    private $wikipage;
70
71    /**
72     * @var null|string
73     */
74    private $action;
75
76    /**
77     * @var OutputPage
78     */
79    private $output;
80
81    /**
82     * @var User|null
83     */
84    private $user;
85
86    /**
87     * @var Authority
88     */
89    private $authority;
90
91    /**
92     * @var Language|null
93     */
94    private $lang;
95
96    /**
97     * @var Skin|null
98     */
99    private $skin;
100
101    /**
102     * @var Timing
103     */
104    private $timing;
105
106    /**
107     * @var Config
108     */
109    private $config;
110
111    /**
112     * @var RequestContext|null
113     */
114    private static $instance = null;
115
116    /**
117     * Boolean flag to guard against recursion in getLanguage
118     * @var bool
119     */
120    private $languageRecursion = false;
121
122    /** @var Skin|string|null */
123    private $skinFromHook;
124
125    /** @var bool */
126    private $skinHookCalled = false;
127
128    /** @var string|null */
129    private $skinName;
130
131    public function setConfig( Config $config ) {
132        $this->config = $config;
133    }
134
135    /**
136     * @return Config
137     */
138    public function getConfig() {
139        // @todo In the future, we could move this to WebStart.php so
140        // the Config object is ready for when initialization happens
141        $this->config ??= MediaWikiServices::getInstance()->getMainConfig();
142
143        return $this->config;
144    }
145
146    public function setRequest( WebRequest $request ) {
147        $this->request = $request;
148    }
149
150    /**
151     * @return WebRequest
152     */
153    public function getRequest() {
154        if ( $this->request === null ) {
155            // create the WebRequest object on the fly
156            if ( MW_ENTRY_POINT === 'cli' ) {
157                // Don't use real WebRequest in CLI mode, it throws errors when trying to access
158                // things that don't exist, e.g. "Unable to determine IP".
159                $this->request = new FauxRequest( [] );
160            } else {
161                $this->request = new WebRequest();
162            }
163        }
164
165        return $this->request;
166    }
167
168    /**
169     * @return Timing
170     */
171    public function getTiming() {
172        $this->timing ??= new Timing( [
173            'logger' => LoggerFactory::getInstance( 'Timing' )
174        ] );
175        return $this->timing;
176    }
177
178    /**
179     * @param Title|null $title
180     */
181    public function setTitle( ?Title $title = null ) {
182        $this->title = $title;
183        // Clear cache of derived getters
184        $this->wikipage = null;
185        $this->clearActionName();
186    }
187
188    /**
189     * @return Title|null
190     */
191    public function getTitle() {
192        if ( $this->title === null ) {
193            // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgTitle
194            global $wgTitle; # fallback to $wg till we can improve this
195            $this->title = $wgTitle;
196            $logger = LoggerFactory::getInstance( 'GlobalTitleFail' );
197            $logger->info(
198                __METHOD__ . ' called with no title set.',
199                [ 'exception' => new RuntimeException ]
200            );
201        }
202
203        return $this->title;
204    }
205
206    /**
207     * Check, if a Title object is set
208     *
209     * @since 1.25
210     * @return bool
211     */
212    public function hasTitle() {
213        return $this->title !== null;
214    }
215
216    /**
217     * Check whether a WikiPage object can be get with getWikiPage().
218     * Callers should expect that an exception is thrown from getWikiPage()
219     * if this method returns false.
220     *
221     * @since 1.19
222     * @return bool
223     */
224    public function canUseWikiPage() {
225        if ( $this->wikipage ) {
226            // If there's a WikiPage object set, we can for sure get it
227            return true;
228        }
229        // Only pages with legitimate titles can have WikiPages.
230        // That usually means pages in non-virtual namespaces.
231        $title = $this->getTitle();
232        return $title && $title->canExist();
233    }
234
235    /**
236     * @since 1.19
237     * @param WikiPage $wikiPage
238     */
239    public function setWikiPage( WikiPage $wikiPage ) {
240        $pageTitle = $wikiPage->getTitle();
241        if ( !$this->hasTitle() || !$pageTitle->equals( $this->getTitle() ) ) {
242            $this->setTitle( $pageTitle );
243        }
244        // Defer this to the end since setTitle sets it to null.
245        $this->wikipage = $wikiPage;
246        // Clear cache of derived getter
247        $this->clearActionName();
248    }
249
250    /**
251     * Get the WikiPage object.
252     * May throw an exception if there's no Title object set or the Title object
253     * belongs to a special namespace that doesn't have WikiPage, so use first
254     * canUseWikiPage() to check whether this method can be called safely.
255     *
256     * @since 1.19
257     * @return WikiPage
258     */
259    public function getWikiPage() {
260        if ( $this->wikipage === null ) {
261            $title = $this->getTitle();
262            if ( $title === null ) {
263                throw new BadMethodCallException( __METHOD__ . ' called without Title object set' );
264            }
265            $this->wikipage = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
266        }
267
268        return $this->wikipage;
269    }
270
271    /**
272     * @since 1.38
273     * @param string $action
274     */
275    public function setActionName( string $action ): void {
276        $this->action = $action;
277    }
278
279    /**
280     * Get the action name for the current web request.
281     *
282     * This generally returns "view" if the current request or process is
283     * not for a skinned index.php web request (e.g. load.php, thumb.php,
284     * job runner, CLI, API).
285     *
286     * @warning This must not be called before or during the Setup.php phase,
287     * and may cause an error or warning if called too early.
288     *
289     * @since 1.38
290     * @return string Action
291     */
292    public function getActionName(): string {
293        // Optimisation: This is cached to avoid repeated running of the
294        // expensive operations to compute this. The computation involves creation
295        // of Article, WikiPage, and ContentHandler objects (and the various
296        // database queries these classes require to be instantiated), as well
297        // as potentially slow extension hooks in these classes.
298        //
299        // This value is frequently needed in OutputPage and in various
300        // Skin-related methods and classes.
301        $this->action ??= MediaWikiServices::getInstance()
302            ->getActionFactory()
303            ->getActionName( $this );
304
305        return $this->action;
306    }
307
308    private function clearActionName(): void {
309        if ( $this->action !== null ) {
310            // If we're clearing after something else has actually already computed the action,
311            // emit a warning.
312            //
313            // Doing so is unstable, given the first caller got something that turns out to be
314            // incomplete or incorrect. Even if we end up re-creating an instance of the same
315            // class, we may now be acting on a different title/skin/user etc.
316            //
317            // Re-computing the action is expensive and can be a performance problem (T302623).
318            trigger_error( 'Unexpected clearActionName after getActionName already called' );
319            $this->action = null;
320        }
321    }
322
323    public function setOutput( OutputPage $output ) {
324        $this->output = $output;
325    }
326
327    /**
328     * @return OutputPage
329     */
330    public function getOutput() {
331        $this->output ??= new OutputPage( $this );
332
333        return $this->output;
334    }
335
336    public function setUser( User $user ) {
337        $this->user = $user;
338        // Keep authority consistent
339        $this->authority = $user;
340        // Invalidate cached user interface language and skin
341        $this->lang = null;
342        $this->skin = null;
343        $this->skinName = null;
344    }
345
346    /**
347     * @return User
348     */
349    public function getUser() {
350        if ( $this->user === null ) {
351            if ( $this->authority !== null ) {
352                // Keep user consistent by using a possible set authority
353                $this->user = MediaWikiServices::getInstance()
354                    ->getUserFactory()
355                    ->newFromAuthority( $this->authority );
356            } else {
357                $this->user = User::newFromSession( $this->getRequest() );
358            }
359        }
360
361        return $this->user;
362    }
363
364    public function hasUser(): bool {
365        if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
366            throw new LogicException( __METHOD__ . '() should be called only from tests!' );
367        }
368        return $this->user !== null;
369    }
370
371    public function setAuthority( Authority $authority ) {
372        $this->authority = $authority;
373        // If needed, a User object is constructed from this authority
374        $this->user = null;
375        // Invalidate cached user interface language and skin
376        $this->lang = null;
377        $this->skin = null;
378        $this->skinName = null;
379    }
380
381    /**
382     * @since 1.36
383     * @return Authority
384     */
385    public function getAuthority(): Authority {
386        return $this->authority ?: $this->getUser();
387    }
388
389    /**
390     * Accepts a language code and ensures it's sensible. Outputs a cleaned up language
391     * code and replaces with $wgLanguageCode if not sensible.
392     * @param ?string $code Language code
393     * @return string
394     */
395    public static function sanitizeLangCode( $code ) {
396        global $wgLanguageCode;
397
398        if ( !$code ) {
399            return $wgLanguageCode;
400        }
401
402        // BCP 47 - letter case MUST NOT carry meaning
403        $code = strtolower( $code );
404
405        # Validate $code
406        if ( !MediaWikiServices::getInstance()->getLanguageNameUtils()
407                ->isValidCode( $code )
408            || $code === 'qqq'
409        ) {
410            $code = $wgLanguageCode;
411        }
412
413        return $code;
414    }
415
416    /**
417     * @param Language|string $language Language instance or language code
418     * @since 1.19
419     */
420    public function setLanguage( $language ) {
421        Assert::parameterType( [ Language::class, 'string' ], $language, '$language' );
422        if ( $language instanceof Language ) {
423            $this->lang = $language;
424        } else {
425            $language = self::sanitizeLangCode( $language );
426            $obj = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $language );
427            $this->lang = $obj;
428        }
429        OutputPage::resetOOUI();
430    }
431
432    /**
433     * Get the Language object.
434     * Initialization of user or request objects can depend on this.
435     * @return Language
436     * @throws LogicException
437     * @since 1.19
438     */
439    public function getLanguage() {
440        if ( $this->languageRecursion === true ) {
441            throw new LogicException( 'Recursion detected' );
442        }
443
444        if ( $this->lang === null ) {
445            $this->languageRecursion = true;
446
447            try {
448                $request = $this->getRequest();
449                $user = $this->getUser();
450                $services = MediaWikiServices::getInstance();
451
452                // Optimisation: Avoid slow getVal(), this isn't user-generated content.
453                $code = $request->getRawVal( 'uselang' ) ?? 'user';
454                if ( $code === 'user' ) {
455                    $userOptionsLookup = $services->getUserOptionsLookup();
456                    $code = $userOptionsLookup->getOption( $user, 'language' );
457                }
458
459                // There are certain characters we don't allow in language code strings,
460                // but by and large almost any valid UTF-8 string will makes it past
461                // this check and the LanguageNameUtils::isValidCode method it uses.
462                // This is to support on-wiki interface message overrides for
463                // non-existent language codes. Also known as "Uselang hacks".
464                // See <https://www.mediawiki.org/wiki/Manual:Uselang_hack>
465                // For something like "en-whatever" or "de-whatever" it will end up
466                // with a mostly "en" or "de" interface, but with an extra layer of
467                // possible MessageCache overrides from `MediaWiki:*/<code>` titles.
468                // While non-ASCII works here, it is required that they are in
469                // NFC form given this will not convert to normalised form.
470                $code = self::sanitizeLangCode( $code );
471
472                ( new HookRunner( $services->getHookContainer() ) )->onUserGetLanguageObject( $user, $code, $this );
473
474                if ( $code === $this->getConfig()->get( MainConfigNames::LanguageCode ) ) {
475                    $this->lang = $services->getContentLanguage();
476                } else {
477                    $obj = $services->getLanguageFactory()
478                        ->getLanguage( $code );
479                    $this->lang = $obj;
480                }
481            } finally {
482                $this->languageRecursion = false;
483            }
484        }
485
486        return $this->lang;
487    }
488
489    /**
490     * @since 1.42
491     * @return Bcp47Code
492     */
493    public function getLanguageCode() {
494        return $this->getLanguage();
495    }
496
497    public function setSkin( Skin $skin ) {
498        $this->skin = clone $skin;
499        $this->skin->setContext( $this );
500        $this->skinName = $skin->getSkinName();
501        OutputPage::resetOOUI();
502    }
503
504    /**
505     * Get the name of the skin
506     *
507     * @since 1.41
508     * @return string
509     */
510    public function getSkinName() {
511        if ( $this->skinName === null ) {
512            $this->skinName = $this->fetchSkinName();
513        }
514        return $this->skinName;
515    }
516
517    /**
518     * Get the name of the skin, without caching
519     *
520     * @return string
521     */
522    private function fetchSkinName() {
523        $skinFromHook = $this->getSkinFromHook();
524        if ( $skinFromHook instanceof Skin ) {
525            // The hook provided a skin object
526            return $skinFromHook->getSkinName();
527        } elseif ( is_string( $skinFromHook ) ) {
528            // The hook provided a skin name
529            $skinName = $skinFromHook;
530        } elseif ( !in_array( 'skin', $this->getConfig()->get( MainConfigNames::HiddenPrefs ) ) ) {
531            // The normal case
532            $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
533            $userSkin = $userOptionsLookup->getOption( $this->getUser(), 'skin' );
534            // Optimisation: Avoid slow getVal(), this isn't user-generated content.
535            $skinName = $this->getRequest()->getRawVal( 'useskin' ) ?? $userSkin;
536        } else {
537            // User preference disabled
538            $skinName = $this->getConfig()->get( MainConfigNames::DefaultSkin );
539        }
540        return Skin::normalizeKey( $skinName );
541    }
542
543    /**
544     * Get the skin set by the RequestContextCreateSkin hook, if there is any.
545     *
546     * @return Skin|string|null
547     */
548    private function getSkinFromHook() {
549        if ( !$this->skinHookCalled ) {
550            $this->skinHookCalled = true;
551            ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
552                ->onRequestContextCreateSkin( $this, $this->skinFromHook );
553        }
554        return $this->skinFromHook;
555    }
556
557    /**
558     * @return Skin
559     */
560    public function getSkin() {
561        if ( $this->skin === null ) {
562            $skinFromHook = $this->getSkinFromHook();
563            if ( $skinFromHook instanceof Skin ) {
564                $this->skin = $skinFromHook;
565            } else {
566                $skinName = is_string( $skinFromHook )
567                    ? Skin::normalizeKey( $skinFromHook )
568                    : $this->getSkinName();
569                $factory = MediaWikiServices::getInstance()->getSkinFactory();
570                $this->skin = $factory->makeSkin( $skinName );
571            }
572            $this->skin->setContext( $this );
573        }
574        return $this->skin;
575    }
576
577    /**
578     * Get a Message object with context set
579     * Parameters are the same as wfMessage()
580     *
581     * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
582     *   or a MessageSpecifier.
583     * @phpcs:ignore Generic.Files.LineLength
584     * @param MessageParam|MessageSpecifier|string|int|float|list<MessageParam|MessageSpecifier|string|int|float> ...$params
585     *   See Message::params()
586     * @return Message
587     */
588    public function msg( $key, ...$params ) {
589        return wfMessage( $key, ...$params )->setContext( $this );
590    }
591
592    /**
593     * Get the RequestContext object associated with the main request
594     */
595    public static function getMain(): RequestContext {
596        self::$instance ??= new self;
597
598        return self::$instance;
599    }
600
601    /**
602     * Get the RequestContext object associated with the main request
603     * and gives a warning to the log, to find places, where a context maybe is missing.
604     *
605     * @param string $func @phan-mandatory-param
606     * @return RequestContext
607     * @since 1.24
608     */
609    public static function getMainAndWarn( $func = __METHOD__ ) {
610        wfDebug( $func . ' called without context. ' .
611            "Using RequestContext::getMain()" );
612
613        return self::getMain();
614    }
615
616    /**
617     * Resets singleton returned by getMain(). Should be called only from unit tests.
618     */
619    public static function resetMain() {
620        if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
621            throw new LogicException( __METHOD__ . '() should be called only from unit tests!' );
622        }
623        self::$instance = null;
624    }
625
626    /**
627     * Export the resolved user IP, HTTP headers, user ID, and session ID.
628     * The result will be reasonably sized to allow for serialization.
629     *
630     * @return array
631     * @since 1.21
632     */
633    public function exportSession() {
634        $session = $this->getRequest()->getSession();
635        return [
636            'ip' => $this->getRequest()->getIP(),
637            'headers' => $this->getRequest()->getAllHeaders(),
638            'sessionId' => $session->isPersistent() ? $session->getId() : '',
639            'userId' => $this->getUser()->getId()
640        ];
641    }
642
643    public function getCsrfTokenSet(): CsrfTokenSet {
644        return new CsrfTokenSet( $this->getRequest() );
645    }
646
647    /**
648     * Import a client IP address, HTTP headers, user ID, and session ID
649     *
650     * This sets the current session, $wgUser, and $wgRequest from $params.
651     * Once the return value falls out of scope, the old context is restored.
652     * This method should only be called in contexts where there is no session
653     * ID or end user receiving the response (CLI or HTTP job runners). This
654     * is partly enforced, and is done so to avoid leaking cookies if certain
655     * error conditions arise.
656     *
657     * This is useful when background scripts inherit context when acting on
658     * behalf of a user. In general, the 'sessionId' parameter should be set
659     * to an empty string unless session importing is *truly* needed. This
660     * feature is somewhat deprecated.
661     *
662     * @param array $params Result of RequestContext::exportSession()
663     * @return ScopedCallback
664     * @since 1.21
665     */
666    public static function importScopedSession( array $params ) {
667        if ( $params['sessionId'] !== '' &&
668            self::getMain()->getRequest()->getSession()->isPersistent()
669        ) {
670            // Check to avoid sending random cookies for the wrong users.
671            // This method should only be called by CLI scripts or by HTTP job runners.
672            throw new BadMethodCallException( "Sessions can only be imported when none is active." );
673        } elseif ( !IPUtils::isValid( $params['ip'] ) ) {
674            throw new InvalidArgumentException( "Invalid client IP address '{$params['ip']}'." );
675        }
676
677        $services = MediaWikiServices::getInstance();
678        $userFactory = $services->getUserFactory();
679
680        if ( $params['userId'] ) { // logged-in user
681            $user = $userFactory->newFromId( (int)$params['userId'] );
682            $user->load();
683            if ( !$user->isRegistered() ) {
684                throw new InvalidArgumentException( "No user with ID '{$params['userId']}'." );
685            }
686        } else { // anon user
687            $user = $userFactory->newFromName( $params['ip'], UserRigorOptions::RIGOR_NONE );
688        }
689
690        $importSessionFunc = static function ( User $user, array $params ) use ( $services ) {
691            global $wgRequest;
692
693            $context = RequestContext::getMain();
694
695            // Commit and close any current session
696            if ( PHPSessionHandler::isEnabled() ) {
697                session_write_close(); // persist
698                session_id( '' ); // detach
699                $_SESSION = []; // clear in-memory array
700            }
701
702            // Get new session, if applicable
703            $session = null;
704            if ( $params['sessionId'] !== '' ) { // don't make a new random ID
705                $manager = $services->getSessionManager();
706                $session = $manager->getSessionById( $params['sessionId'], true )
707                    ?: $manager->getEmptySession();
708            }
709
710            // Remove any user IP or agent information, and attach the request
711            // with the new session.
712            $context->setRequest( new FauxRequest( [], false, $session ) );
713            $wgRequest = $context->getRequest(); // b/c
714
715            // Now that all private information is detached from the user, it should
716            // be safe to load the new user. If errors occur or an exception is thrown
717            // and caught (leaving the main context in a mixed state), there is no risk
718            // of the User object being attached to the wrong IP, headers, or session.
719            $context->setUser( $user );
720            StubGlobalUser::setUser( $context->getUser() ); // b/c
721            if ( $session && PHPSessionHandler::isEnabled() ) {
722                session_id( $session->getId() );
723                AtEase::quietCall( 'session_start' );
724            }
725            $request = new FauxRequest( [], false, $session );
726            $request->setIP( $params['ip'] );
727            foreach ( $params['headers'] as $name => $value ) {
728                $request->setHeader( $name, $value );
729            }
730            // Set the current context to use the new WebRequest
731            $context->setRequest( $request );
732            $wgRequest = $context->getRequest(); // b/c
733        };
734
735        // Stash the old session and load in the new one
736        $oUser = self::getMain()->getUser();
737        $oParams = self::getMain()->exportSession();
738        $oRequest = self::getMain()->getRequest();
739        // @phan-suppress-next-line PhanTypeMismatchArgumentNullable exceptions triggered above prevent the null case
740        $importSessionFunc( $user, $params );
741
742        // Set callback to save and close the new session and reload the old one
743        return new ScopedCallback(
744            static function () use ( $importSessionFunc, $oUser, $oParams, $oRequest ) {
745                global $wgRequest;
746                $importSessionFunc( $oUser, $oParams );
747                // Restore the exact previous Request object (instead of leaving MediaWiki\Request\FauxRequest)
748                RequestContext::getMain()->setRequest( $oRequest );
749                $wgRequest = RequestContext::getMain()->getRequest(); // b/c
750            }
751        );
752    }
753
754    /**
755     * Create a new extraneous context. The context is filled with information
756     * external to the current session.
757     * - Title is specified by argument
758     * - Request is a MediaWiki\Request\FauxRequest, or a MediaWiki\Request\FauxRequest can be specified by argument
759     * - User is an anonymous user, for separation IPv4 localhost is used
760     * - Language will be based on the anonymous user and request, may be content
761     *   language or a uselang param in the fauxrequest data may change the lang
762     * - Skin will be based on the anonymous user, should be the wiki's default skin
763     *
764     * @param Title $title Title to use for the extraneous request
765     * @param WebRequest|array $request A WebRequest or data to use for a MediaWiki\Request\FauxRequest
766     * @return RequestContext
767     */
768    public static function newExtraneousContext( Title $title, $request = [] ) {
769        $context = new self;
770        $context->setTitle( $title );
771        if ( $request instanceof WebRequest ) {
772            $context->setRequest( $request );
773        } else {
774            $context->setRequest( new FauxRequest( $request ) );
775        }
776        $context->user = MediaWikiServices::getInstance()->getUserFactory()->newFromName(
777            '127.0.0.1',
778            UserRigorOptions::RIGOR_NONE
779        );
780
781        return $context;
782    }
783
784    /** @return never */
785    public function __clone() {
786        throw new LogicException(
787            __CLASS__ . ' should not be cloned, use DerivativeContext instead.'
788        );
789    }
790
791}
792
793/** @deprecated class alias since 1.42 */
794class_alias( RequestContext::class, 'RequestContext' );