| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\ResourceLoader; |
| 24 |
|
| 25 |
use MediaWiki\Logger\LoggerFactory; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use MediaWiki\Message\Message; |
| 28 |
use MediaWiki\Page\PageReferenceValue; |
| 29 |
use MediaWiki\Request\FauxRequest; |
| 30 |
use MediaWiki\Request\WebRequest; |
| 31 |
use MediaWiki\User\User; |
| 32 |
use MediaWiki\User\UserIdentity; |
| 33 |
use MediaWiki\User\UserRigorOptions; |
| 34 |
use MessageLocalizer; |
| 35 |
use Psr\Log\LoggerInterface; |
| 36 |
use Wikimedia\Message\MessageSpecifier; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class Context implements MessageLocalizer { |
| 46 |
public const DEFAULT_LANG = 'qqx'; |
| 47 |
public const DEFAULT_SKIN = 'fallback'; |
| 48 |
|
| 49 |
|
| 50 |
public const DEBUG_OFF = 0; |
| 51 |
|
| 52 |
public const DEBUG_LEGACY = 1; |
| 53 |
|
| 54 |
public const DEBUG_MAIN = 2; |
| 55 |
|
| 56 |
|
| 57 |
protected $resourceLoader; |
| 58 |
|
| 59 |
protected $request; |
| 60 |
|
| 61 |
protected $logger; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
protected $skin; |
| 66 |
|
| 67 |
protected $language; |
| 68 |
|
| 69 |
protected $debug; |
| 70 |
|
| 71 |
protected $user; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
protected $modules; |
| 76 |
|
| 77 |
protected $only; |
| 78 |
|
| 79 |
protected $version; |
| 80 |
|
| 81 |
protected $raw; |
| 82 |
|
| 83 |
protected $sourcemap; |
| 84 |
|
| 85 |
protected $image; |
| 86 |
|
| 87 |
protected $variant; |
| 88 |
|
| 89 |
protected $format; |
| 90 |
|
| 91 |
|
| 92 |
protected $direction; |
| 93 |
|
| 94 |
protected $hash; |
| 95 |
|
| 96 |
protected $userObj; |
| 97 |
|
| 98 |
protected $userIdentity = false; |
| 99 |
|
| 100 |
protected $imageObj; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function __construct( |
| 109 |
ResourceLoader $resourceLoader, WebRequest $request, $validSkins = null |
| 110 |
) { |
| 111 |
$this->resourceLoader = $resourceLoader; |
| 112 |
$this->request = $request; |
| 113 |
$this->logger = $resourceLoader->getLogger(); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
$modules = $request->getRawVal( 'modules' ); |
| 120 |
$this->modules = $modules ? ResourceLoader::expandModuleNames( $modules ) : []; |
| 121 |
|
| 122 |
|
| 123 |
$this->user = $request->getRawVal( 'user' ); |
| 124 |
$this->debug = self::debugFromString( $request->getRawVal( 'debug' ) ); |
| 125 |
$this->only = $request->getRawVal( 'only' ); |
| 126 |
$this->version = $request->getRawVal( 'version' ); |
| 127 |
$this->raw = $request->getFuzzyBool( 'raw' ); |
| 128 |
$this->sourcemap = $request->getFuzzyBool( 'sourcemap' ); |
| 129 |
|
| 130 |
|
| 131 |
$this->image = $request->getRawVal( 'image' ); |
| 132 |
$this->variant = $request->getRawVal( 'variant' ); |
| 133 |
$this->format = $request->getRawVal( 'format' ); |
| 134 |
|
| 135 |
$skin = $request->getRawVal( 'skin' ); |
| 136 |
if ( |
| 137 |
$skin === null |
| 138 |
|| ( is_array( $validSkins ) && !in_array( $skin, $validSkins ) ) |
| 139 |
) { |
| 140 |
|
| 141 |
|
| 142 |
$skin = self::DEFAULT_SKIN; |
| 143 |
} |
| 144 |
$this->skin = $skin; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public static function debugFromString( ?string $debug ): int { |
| 153 |
|
| 154 |
|
| 155 |
if ( $debug === 'true' || $debug === '1' ) { |
| 156 |
$ret = self::DEBUG_LEGACY; |
| 157 |
} elseif ( $debug === '2' ) { |
| 158 |
$ret = self::DEBUG_MAIN; |
| 159 |
} else { |
| 160 |
$ret = self::DEBUG_OFF; |
| 161 |
} |
| 162 |
|
| 163 |
return $ret; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public static function newDummyContext(): Context { |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
return new self( new ResourceLoader( |
| 182 |
MediaWikiServices::getInstance()->getMainConfig(), |
| 183 |
LoggerFactory::getInstance( 'resourceloader' ) |
| 184 |
), new FauxRequest( [] ) ); |
| 185 |
} |
| 186 |
|
| 187 |
public function getResourceLoader(): ResourceLoader { |
| 188 |
return $this->resourceLoader; |
| 189 |
} |
| 190 |
|
| 191 |
public function getRequest(): WebRequest { |
| 192 |
return $this->request; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public function getLogger() { |
| 202 |
return $this->logger; |
| 203 |
} |
| 204 |
|
| 205 |
public function getModules(): array { |
| 206 |
return $this->modules; |
| 207 |
} |
| 208 |
|
| 209 |
public function getLanguage(): string { |
| 210 |
if ( $this->language === null ) { |
| 211 |
|
| 212 |
|
| 213 |
$lang = $this->getRequest()->getRawVal( 'lang' ) ?? ''; |
| 214 |
|
| 215 |
$validBuiltinCode = MediaWikiServices::getInstance()->getLanguageNameUtils() |
| 216 |
->isValidBuiltInCode( $lang ); |
| 217 |
if ( !$validBuiltinCode ) { |
| 218 |
|
| 219 |
|
| 220 |
$lang = self::DEFAULT_LANG; |
| 221 |
} |
| 222 |
$this->language = $lang; |
| 223 |
} |
| 224 |
return $this->language; |
| 225 |
} |
| 226 |
|
| 227 |
public function getDirection(): string { |
| 228 |
if ( $this->direction === null ) { |
| 229 |
$direction = $this->getRequest()->getRawVal( 'dir' ); |
| 230 |
if ( $direction === 'ltr' || $direction === 'rtl' ) { |
| 231 |
$this->direction = $direction; |
| 232 |
} else { |
| 233 |
|
| 234 |
$this->direction = MediaWikiServices::getInstance()->getLanguageFactory() |
| 235 |
->getLanguage( $this->getLanguage() )->getDir(); |
| 236 |
} |
| 237 |
} |
| 238 |
return $this->direction; |
| 239 |
} |
| 240 |
|
| 241 |
public function getSkin(): string { |
| 242 |
return $this->skin; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public function getUser(): ?string { |
| 249 |
return $this->user; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public function msg( $key, ...$params ): Message { |
| 262 |
return wfMessage( $key, ...$params ) |
| 263 |
|
| 264 |
->inLanguage( $this->getLanguage() ) |
| 265 |
|
| 266 |
->setInterfaceMessageFlag( true ) |
| 267 |
|
| 268 |
|
| 269 |
->page( PageReferenceValue::localReference( NS_SPECIAL, 'Badtitle/ResourceLoaderContext' ) ); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public function getUserIdentity(): ?UserIdentity { |
| 282 |
if ( $this->userIdentity === false ) { |
| 283 |
$username = $this->getUser(); |
| 284 |
if ( $username === null ) { |
| 285 |
|
| 286 |
$this->userIdentity = null; |
| 287 |
} else { |
| 288 |
|
| 289 |
$this->userIdentity = MediaWikiServices::getInstance() |
| 290 |
->getUserFactory() |
| 291 |
->newFromName( $username, UserRigorOptions::RIGOR_VALID ); |
| 292 |
} |
| 293 |
} |
| 294 |
return $this->userIdentity; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public function getUserObj(): User { |
| 304 |
if ( $this->userObj === null ) { |
| 305 |
$username = $this->getUser(); |
| 306 |
$userFactory = MediaWikiServices::getInstance()->getUserFactory(); |
| 307 |
if ( $username ) { |
| 308 |
|
| 309 |
$this->userObj = $userFactory->newFromName( $username, UserRigorOptions::RIGOR_VALID ); |
| 310 |
} |
| 311 |
|
| 312 |
$this->userObj ??= $userFactory->newAnonymous(); |
| 313 |
} |
| 314 |
|
| 315 |
return $this->userObj; |
| 316 |
} |
| 317 |
|
| 318 |
public function getDebug(): int { |
| 319 |
return $this->debug; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public function getOnly(): ?string { |
| 326 |
return $this->only; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public function getVersion(): ?string { |
| 335 |
return $this->version; |
| 336 |
} |
| 337 |
|
| 338 |
public function getRaw(): bool { |
| 339 |
return $this->raw; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
public function isSourceMap(): bool { |
| 347 |
return $this->sourcemap; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public function getImage(): ?string { |
| 354 |
return $this->image; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
public function getVariant(): ?string { |
| 361 |
return $this->variant; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public function getFormat(): ?string { |
| 368 |
return $this->format; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public function getImageObj() { |
| 378 |
if ( $this->imageObj === null ) { |
| 379 |
$this->imageObj = false; |
| 380 |
|
| 381 |
if ( !$this->image ) { |
| 382 |
return $this->imageObj; |
| 383 |
} |
| 384 |
|
| 385 |
$modules = $this->getModules(); |
| 386 |
if ( count( $modules ) !== 1 ) { |
| 387 |
return $this->imageObj; |
| 388 |
} |
| 389 |
|
| 390 |
$module = $this->getResourceLoader()->getModule( $modules[0] ); |
| 391 |
if ( !$module || !$module instanceof ImageModule ) { |
| 392 |
return $this->imageObj; |
| 393 |
} |
| 394 |
|
| 395 |
$image = $module->getImage( $this->image, $this ); |
| 396 |
if ( !$image ) { |
| 397 |
return $this->imageObj; |
| 398 |
} |
| 399 |
|
| 400 |
$this->imageObj = $image; |
| 401 |
} |
| 402 |
|
| 403 |
return $this->imageObj; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public function getContentOverrideCallback() { |
| 419 |
return null; |
| 420 |
} |
| 421 |
|
| 422 |
public function shouldIncludeScripts(): bool { |
| 423 |
return $this->getOnly() === null || $this->getOnly() === 'scripts'; |
| 424 |
} |
| 425 |
|
| 426 |
public function shouldIncludeStyles(): bool { |
| 427 |
return $this->getOnly() === null || $this->getOnly() === 'styles'; |
| 428 |
} |
| 429 |
|
| 430 |
public function shouldIncludeMessages(): bool { |
| 431 |
return $this->getOnly() === null; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
public function getHash(): string { |
| 446 |
if ( $this->hash === null ) { |
| 447 |
$this->hash = implode( '|', [ |
| 448 |
|
| 449 |
$this->getLanguage(), |
| 450 |
$this->getSkin(), |
| 451 |
(string)$this->getDebug(), |
| 452 |
$this->getUser() ?? '', |
| 453 |
|
| 454 |
$this->getOnly() ?? '', |
| 455 |
$this->getVersion() ?? '', |
| 456 |
(string)$this->getRaw(), |
| 457 |
$this->getImage() ?? '', |
| 458 |
$this->getVariant() ?? '', |
| 459 |
$this->getFormat() ?? '', |
| 460 |
] ); |
| 461 |
} |
| 462 |
return $this->hash; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
public function getReqBase(): array { |
| 472 |
$reqBase = []; |
| 473 |
$lang = $this->getLanguage(); |
| 474 |
if ( $lang !== self::DEFAULT_LANG ) { |
| 475 |
$reqBase['lang'] = $lang; |
| 476 |
} |
| 477 |
$skin = $this->getSkin(); |
| 478 |
if ( $skin !== self::DEFAULT_SKIN ) { |
| 479 |
$reqBase['skin'] = $skin; |
| 480 |
} |
| 481 |
$debug = $this->getDebug(); |
| 482 |
if ( $debug !== self::DEBUG_OFF ) { |
| 483 |
$reqBase['debug'] = strval( $debug ); |
| 484 |
} |
| 485 |
return $reqBase; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
public function encodeJson( $data ) { |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
$jsonFlags = JSON_UNESCAPED_SLASHES | |
| 507 |
JSON_UNESCAPED_UNICODE | |
| 508 |
JSON_PARTIAL_OUTPUT_ON_ERROR | |
| 509 |
JSON_HEX_TAG | |
| 510 |
JSON_HEX_AMP; |
| 511 |
if ( $this->getDebug() ) { |
| 512 |
$jsonFlags |= JSON_PRETTY_PRINT; |
| 513 |
} |
| 514 |
$json = json_encode( $data, $jsonFlags ); |
| 515 |
if ( json_last_error() !== JSON_ERROR_NONE ) { |
| 516 |
trigger_error( __METHOD__ . ' partially failed: ' . json_last_error_msg(), E_USER_WARNING ); |
| 517 |
} |
| 518 |
return $json; |
| 519 |
} |
| 520 |
} |