| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
namespace MediaWiki\Context; |
| 26 |
|
| 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\MessageSpecifier; |
| 56 |
use Wikimedia\NonSerializable\NonSerializableTrait; |
| 57 |
use Wikimedia\ScopedCallback; |
| 58 |
use WikiPage; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
class RequestContext implements IContextSource, MutableContext { |
| 68 |
use NonSerializableTrait; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
private $request; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private $title; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
private $wikipage; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
private $action; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
private $output; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
private $user; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
private $authority; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private $lang; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
private $skin; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
private $timing; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
private $config; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
private static $instance = null; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
private $languageRecursion = false; |
| 135 |
|
| 136 |
|
| 137 |
private $skinFromHook; |
| 138 |
|
| 139 |
|
| 140 |
private $skinHookCalled = false; |
| 141 |
|
| 142 |
|
| 143 |
private $skinName; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function setConfig( Config $config ) { |
| 149 |
$this->config = $config; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function getConfig() { |
| 156 |
|
| 157 |
|
| 158 |
$this->config ??= MediaWikiServices::getInstance()->getMainConfig(); |
| 159 |
|
| 160 |
return $this->config; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public function setRequest( WebRequest $request ) { |
| 167 |
$this->request = $request; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function getRequest() { |
| 174 |
if ( $this->request === null ) { |
| 175 |
|
| 176 |
if ( MW_ENTRY_POINT === 'cli' ) { |
| 177 |
|
| 178 |
|
| 179 |
$this->request = new FauxRequest( [] ); |
| 180 |
} else { |
| 181 |
$this->request = new WebRequest(); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return $this->request; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function getTiming() { |
| 192 |
$this->timing ??= new Timing( [ |
| 193 |
'logger' => LoggerFactory::getInstance( 'Timing' ) |
| 194 |
] ); |
| 195 |
return $this->timing; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public function setTitle( ?Title $title = null ) { |
| 202 |
$this->title = $title; |
| 203 |
|
| 204 |
$this->wikipage = null; |
| 205 |
$this->clearActionName(); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public function getTitle() { |
| 212 |
if ( $this->title === null ) { |
| 213 |
|
| 214 |
global $wgTitle; |
| 215 |
$this->title = $wgTitle; |
| 216 |
$logger = LoggerFactory::getInstance( 'GlobalTitleFail' ); |
| 217 |
$logger->info( |
| 218 |
__METHOD__ . ' called with no title set.', |
| 219 |
[ 'exception' => new RuntimeException ] |
| 220 |
); |
| 221 |
} |
| 222 |
|
| 223 |
return $this->title; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public function hasTitle() { |
| 233 |
return $this->title !== null; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public function canUseWikiPage() { |
| 245 |
if ( $this->wikipage ) { |
| 246 |
|
| 247 |
return true; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
$title = $this->getTitle(); |
| 252 |
return $title && $title->canExist(); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public function setWikiPage( WikiPage $wikiPage ) { |
| 260 |
$pageTitle = $wikiPage->getTitle(); |
| 261 |
if ( !$this->hasTitle() || !$pageTitle->equals( $this->getTitle() ) ) { |
| 262 |
$this->setTitle( $pageTitle ); |
| 263 |
} |
| 264 |
|
| 265 |
$this->wikipage = $wikiPage; |
| 266 |
|
| 267 |
$this->clearActionName(); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public function getWikiPage() { |
| 280 |
if ( $this->wikipage === null ) { |
| 281 |
$title = $this->getTitle(); |
| 282 |
if ( $title === null ) { |
| 283 |
throw new BadMethodCallException( __METHOD__ . ' called without Title object set' ); |
| 284 |
} |
| 285 |
$this->wikipage = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title ); |
| 286 |
} |
| 287 |
|
| 288 |
return $this->wikipage; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public function setActionName( string $action ): void { |
| 296 |
$this->action = $action; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public function getActionName(): string { |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
$this->action ??= MediaWikiServices::getInstance() |
| 322 |
->getActionFactory() |
| 323 |
->getActionName( $this ); |
| 324 |
|
| 325 |
return $this->action; |
| 326 |
} |
| 327 |
|
| 328 |
private function clearActionName(): void { |
| 329 |
if ( $this->action !== null ) { |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
trigger_error( 'Unexpected clearActionName after getActionName already called' ); |
| 339 |
$this->action = null; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public function setOutput( OutputPage $output ) { |
| 347 |
$this->output = $output; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public function getOutput() { |
| 354 |
$this->output ??= new OutputPage( $this ); |
| 355 |
|
| 356 |
return $this->output; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public function setUser( User $user ) { |
| 363 |
$this->user = $user; |
| 364 |
|
| 365 |
$this->authority = $user; |
| 366 |
|
| 367 |
$this->lang = null; |
| 368 |
$this->skin = null; |
| 369 |
$this->skinName = null; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public function getUser() { |
| 376 |
if ( $this->user === null ) { |
| 377 |
if ( $this->authority !== null ) { |
| 378 |
|
| 379 |
$this->user = MediaWikiServices::getInstance() |
| 380 |
->getUserFactory() |
| 381 |
->newFromAuthority( $this->authority ); |
| 382 |
} else { |
| 383 |
$this->user = User::newFromSession( $this->getRequest() ); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
return $this->user; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public function setAuthority( Authority $authority ) { |
| 394 |
$this->authority = $authority; |
| 395 |
|
| 396 |
$this->user = null; |
| 397 |
|
| 398 |
$this->lang = null; |
| 399 |
$this->skin = null; |
| 400 |
$this->skinName = null; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public function getAuthority(): Authority { |
| 408 |
return $this->authority ?: $this->getUser(); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
public static function sanitizeLangCode( $code ) { |
| 418 |
global $wgLanguageCode; |
| 419 |
|
| 420 |
if ( !$code ) { |
| 421 |
return $wgLanguageCode; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
$code = strtolower( $code ); |
| 426 |
|
| 427 |
|
| 428 |
if ( !MediaWikiServices::getInstance()->getLanguageNameUtils() |
| 429 |
->isValidCode( $code ) |
| 430 |
|| $code === 'qqq' |
| 431 |
) { |
| 432 |
$code = $wgLanguageCode; |
| 433 |
} |
| 434 |
|
| 435 |
return $code; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public function setLanguage( $language ) { |
| 443 |
Assert::parameterType( [ Language::class, 'string' ], $language, '$language' ); |
| 444 |
if ( $language instanceof Language ) { |
| 445 |
$this->lang = $language; |
| 446 |
} else { |
| 447 |
$language = self::sanitizeLangCode( $language ); |
| 448 |
$obj = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $language ); |
| 449 |
$this->lang = $obj; |
| 450 |
} |
| 451 |
OutputPage::resetOOUI(); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public function getLanguage() { |
| 462 |
if ( $this->languageRecursion === true ) { |
| 463 |
throw new LogicException( 'Recursion detected' ); |
| 464 |
} |
| 465 |
|
| 466 |
if ( $this->lang === null ) { |
| 467 |
$this->languageRecursion = true; |
| 468 |
|
| 469 |
try { |
| 470 |
$request = $this->getRequest(); |
| 471 |
$user = $this->getUser(); |
| 472 |
$services = MediaWikiServices::getInstance(); |
| 473 |
|
| 474 |
|
| 475 |
$code = $request->getRawVal( 'uselang' ) ?? 'user'; |
| 476 |
if ( $code === 'user' ) { |
| 477 |
$userOptionsLookup = $services->getUserOptionsLookup(); |
| 478 |
$code = $userOptionsLookup->getOption( $user, 'language' ); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
$code = self::sanitizeLangCode( $code ); |
| 493 |
|
| 494 |
( new HookRunner( $services->getHookContainer() ) )->onUserGetLanguageObject( $user, $code, $this ); |
| 495 |
|
| 496 |
if ( $code === $this->getConfig()->get( MainConfigNames::LanguageCode ) ) { |
| 497 |
$this->lang = $services->getContentLanguage(); |
| 498 |
} else { |
| 499 |
$obj = $services->getLanguageFactory() |
| 500 |
->getLanguage( $code ); |
| 501 |
$this->lang = $obj; |
| 502 |
} |
| 503 |
} finally { |
| 504 |
$this->languageRecursion = false; |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
return $this->lang; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
public function getLanguageCode() { |
| 516 |
return $this->getLanguage(); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
public function setSkin( Skin $skin ) { |
| 523 |
$this->skin = clone $skin; |
| 524 |
$this->skin->setContext( $this ); |
| 525 |
$this->skinName = $skin->getSkinName(); |
| 526 |
OutputPage::resetOOUI(); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
public function getSkinName() { |
| 536 |
if ( $this->skinName === null ) { |
| 537 |
$this->skinName = $this->fetchSkinName(); |
| 538 |
} |
| 539 |
return $this->skinName; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
private function fetchSkinName() { |
| 548 |
$skinFromHook = $this->getSkinFromHook(); |
| 549 |
if ( $skinFromHook instanceof Skin ) { |
| 550 |
|
| 551 |
return $skinFromHook->getSkinName(); |
| 552 |
} elseif ( is_string( $skinFromHook ) ) { |
| 553 |
|
| 554 |
$skinName = $skinFromHook; |
| 555 |
} elseif ( !in_array( 'skin', $this->getConfig()->get( MainConfigNames::HiddenPrefs ) ) ) { |
| 556 |
|
| 557 |
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup(); |
| 558 |
$userSkin = $userOptionsLookup->getOption( $this->getUser(), 'skin' ); |
| 559 |
|
| 560 |
$skinName = $this->getRequest()->getRawVal( 'useskin' ) ?? $userSkin; |
| 561 |
} else { |
| 562 |
|
| 563 |
$skinName = $this->getConfig()->get( MainConfigNames::DefaultSkin ); |
| 564 |
} |
| 565 |
return Skin::normalizeKey( $skinName ); |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
private function getSkinFromHook() { |
| 574 |
if ( !$this->skinHookCalled ) { |
| 575 |
$this->skinHookCalled = true; |
| 576 |
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) ) |
| 577 |
->onRequestContextCreateSkin( $this, $this->skinFromHook ); |
| 578 |
} |
| 579 |
return $this->skinFromHook; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
public function getSkin() { |
| 586 |
if ( $this->skin === null ) { |
| 587 |
$skinFromHook = $this->getSkinFromHook(); |
| 588 |
if ( $skinFromHook instanceof Skin ) { |
| 589 |
$this->skin = $skinFromHook; |
| 590 |
} else { |
| 591 |
$skinName = is_string( $skinFromHook ) |
| 592 |
? Skin::normalizeKey( $skinFromHook ) |
| 593 |
: $this->getSkinName(); |
| 594 |
$factory = MediaWikiServices::getInstance()->getSkinFactory(); |
| 595 |
$this->skin = $factory->makeSkin( $skinName ); |
| 596 |
} |
| 597 |
$this->skin->setContext( $this ); |
| 598 |
} |
| 599 |
return $this->skin; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
public function msg( $key, ...$params ) { |
| 612 |
return wfMessage( $key, ...$params )->setContext( $this ); |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
public static function getMain(): RequestContext { |
| 621 |
self::$instance ??= new self; |
| 622 |
|
| 623 |
return self::$instance; |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
public static function getMainAndWarn( $func = __METHOD__ ) { |
| 635 |
wfDebug( $func . ' called without context. ' . |
| 636 |
"Using RequestContext::getMain()" ); |
| 637 |
|
| 638 |
return self::getMain(); |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
public static function resetMain() { |
| 645 |
if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) { |
| 646 |
throw new LogicException( __METHOD__ . '() should be called only from unit tests!' ); |
| 647 |
} |
| 648 |
self::$instance = null; |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
public function exportSession() { |
| 659 |
$session = SessionManager::getGlobalSession(); |
| 660 |
return [ |
| 661 |
'ip' => $this->getRequest()->getIP(), |
| 662 |
'headers' => $this->getRequest()->getAllHeaders(), |
| 663 |
'sessionId' => $session->isPersistent() ? $session->getId() : '', |
| 664 |
'userId' => $this->getUser()->getId() |
| 665 |
]; |
| 666 |
} |
| 667 |
|
| 668 |
public function getCsrfTokenSet(): CsrfTokenSet { |
| 669 |
return new CsrfTokenSet( $this->getRequest() ); |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
public static function importScopedSession( array $params ) { |
| 692 |
if ( strlen( $params['sessionId'] ) && |
| 693 |
SessionManager::getGlobalSession()->isPersistent() |
| 694 |
) { |
| 695 |
|
| 696 |
|
| 697 |
throw new BadMethodCallException( "Sessions can only be imported when none is active." ); |
| 698 |
} elseif ( !IPUtils::isValid( $params['ip'] ) ) { |
| 699 |
throw new InvalidArgumentException( "Invalid client IP address '{$params['ip']}'." ); |
| 700 |
} |
| 701 |
|
| 702 |
$userFactory = MediaWikiServices::getInstance()->getUserFactory(); |
| 703 |
|
| 704 |
if ( $params['userId'] ) { |
| 705 |
$user = $userFactory->newFromId( (int)$params['userId'] ); |
| 706 |
$user->load(); |
| 707 |
if ( !$user->isRegistered() ) { |
| 708 |
throw new InvalidArgumentException( "No user with ID '{$params['userId']}'." ); |
| 709 |
} |
| 710 |
} else { |
| 711 |
$user = $userFactory->newFromName( $params['ip'], UserRigorOptions::RIGOR_NONE ); |
| 712 |
} |
| 713 |
|
| 714 |
$importSessionFunc = static function ( User $user, array $params ) { |
| 715 |
global $wgRequest; |
| 716 |
|
| 717 |
$context = RequestContext::getMain(); |
| 718 |
|
| 719 |
|
| 720 |
if ( PHPSessionHandler::isEnabled() ) { |
| 721 |
session_write_close(); |
| 722 |
session_id( '' ); |
| 723 |
$_SESSION = []; |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
$session = null; |
| 728 |
if ( strlen( $params['sessionId'] ) ) { |
| 729 |
$manager = SessionManager::singleton(); |
| 730 |
$session = $manager->getSessionById( $params['sessionId'], true ) |
| 731 |
?: $manager->getEmptySession(); |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
$context->setRequest( new FauxRequest( [], false, $session ) ); |
| 737 |
$wgRequest = $context->getRequest(); |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
$context->setUser( $user ); |
| 744 |
StubGlobalUser::setUser( $context->getUser() ); |
| 745 |
if ( $session && PHPSessionHandler::isEnabled() ) { |
| 746 |
session_id( $session->getId() ); |
| 747 |
AtEase::quietCall( 'session_start' ); |
| 748 |
} |
| 749 |
$request = new FauxRequest( [], false, $session ); |
| 750 |
$request->setIP( $params['ip'] ); |
| 751 |
foreach ( $params['headers'] as $name => $value ) { |
| 752 |
$request->setHeader( $name, $value ); |
| 753 |
} |
| 754 |
|
| 755 |
$context->setRequest( $request ); |
| 756 |
$wgRequest = $context->getRequest(); |
| 757 |
}; |
| 758 |
|
| 759 |
|
| 760 |
$oUser = self::getMain()->getUser(); |
| 761 |
$oParams = self::getMain()->exportSession(); |
| 762 |
$oRequest = self::getMain()->getRequest(); |
| 763 |
|
| 764 |
$importSessionFunc( $user, $params ); |
| 765 |
|
| 766 |
|
| 767 |
return new ScopedCallback( |
| 768 |
static function () use ( $importSessionFunc, $oUser, $oParams, $oRequest ) { |
| 769 |
global $wgRequest; |
| 770 |
$importSessionFunc( $oUser, $oParams ); |
| 771 |
|
| 772 |
RequestContext::getMain()->setRequest( $oRequest ); |
| 773 |
$wgRequest = RequestContext::getMain()->getRequest(); |
| 774 |
} |
| 775 |
); |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
public static function newExtraneousContext( Title $title, $request = [] ) { |
| 793 |
$context = new self; |
| 794 |
$context->setTitle( $title ); |
| 795 |
if ( $request instanceof WebRequest ) { |
| 796 |
$context->setRequest( $request ); |
| 797 |
} else { |
| 798 |
$context->setRequest( new FauxRequest( $request ) ); |
| 799 |
} |
| 800 |
$context->user = MediaWikiServices::getInstance()->getUserFactory()->newFromName( |
| 801 |
'127.0.0.1', |
| 802 |
UserRigorOptions::RIGOR_NONE |
| 803 |
); |
| 804 |
|
| 805 |
return $context; |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
public function __clone() { |
| 810 |
throw new LogicException( |
| 811 |
__CLASS__ . ' should not be cloned, use DerivativeContext instead.' |
| 812 |
); |
| 813 |
} |
| 814 |
|
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
class_alias( RequestContext::class, 'RequestContext' ); |