| 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 Exception; |
| 26 |
use HttpStatus; |
| 27 |
use InvalidArgumentException; |
| 28 |
use Less_Environment; |
| 29 |
use Less_Parser; |
| 30 |
use LogicException; |
| 31 |
use MediaWiki\CommentStore\CommentStore; |
| 32 |
use MediaWiki\Config\Config; |
| 33 |
use MediaWiki\Deferred\DeferredUpdates; |
| 34 |
use MediaWiki\HookContainer\HookContainer; |
| 35 |
use MediaWiki\Html\Html; |
| 36 |
use MediaWiki\Html\HtmlJsCode; |
| 37 |
use MediaWiki\MainConfigNames; |
| 38 |
use MediaWiki\MediaWikiServices; |
| 39 |
use MediaWiki\Output\OutputPage; |
| 40 |
use MediaWiki\Profiler\ProfilingContext; |
| 41 |
use MediaWiki\Registration\ExtensionRegistry; |
| 42 |
use MediaWiki\Request\HeaderCallback; |
| 43 |
use MediaWiki\Request\WebRequest; |
| 44 |
use MediaWiki\Title\Title; |
| 45 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 46 |
use MediaWiki\WikiMap\WikiMap; |
| 47 |
use MWExceptionHandler; |
| 48 |
use MWExceptionRenderer; |
| 49 |
use Net_URL2; |
| 50 |
use Psr\Log\LoggerAwareInterface; |
| 51 |
use Psr\Log\LoggerInterface; |
| 52 |
use Psr\Log\NullLogger; |
| 53 |
use RuntimeException; |
| 54 |
use stdClass; |
| 55 |
use Throwable; |
| 56 |
use UnexpectedValueException; |
| 57 |
use Wikimedia\DependencyStore\DependencyStore; |
| 58 |
use Wikimedia\DependencyStore\KeyValueDependencyStore; |
| 59 |
use Wikimedia\Minify\CSSMin; |
| 60 |
use Wikimedia\Minify\IdentityMinifierState; |
| 61 |
use Wikimedia\Minify\IndexMap; |
| 62 |
use Wikimedia\Minify\IndexMapOffset; |
| 63 |
use Wikimedia\Minify\JavaScriptMapperState; |
| 64 |
use Wikimedia\Minify\JavaScriptMinifier; |
| 65 |
use Wikimedia\Minify\JavaScriptMinifierState; |
| 66 |
use Wikimedia\Minify\MinifierState; |
| 67 |
use Wikimedia\ObjectCache\BagOStuff; |
| 68 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 69 |
use Wikimedia\RequestTimeout\TimeoutException; |
| 70 |
use Wikimedia\ScopedCallback; |
| 71 |
use Wikimedia\Stats\StatsFactory; |
| 72 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 73 |
use Wikimedia\WrappedString; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
class ResourceLoader implements LoggerAwareInterface { |
| 96 |
|
| 97 |
public const CACHE_VERSION = 9; |
| 98 |
|
| 99 |
public const FILTER_NOMIN = '/*@nomin*/'; |
| 100 |
|
| 101 |
|
| 102 |
private const RL_DEP_STORE_PREFIX = 'ResourceLoaderModule'; |
| 103 |
|
| 104 |
private const RL_MODULE_DEP_TTL = BagOStuff::TTL_YEAR; |
| 105 |
|
| 106 |
private const MAXAGE_RECOVER = 60; |
| 107 |
|
| 108 |
|
| 109 |
protected static $debugMode = null; |
| 110 |
|
| 111 |
|
| 112 |
private $config; |
| 113 |
|
| 114 |
private $blobStore; |
| 115 |
|
| 116 |
private $depStore; |
| 117 |
|
| 118 |
private $logger; |
| 119 |
|
| 120 |
private $hookContainer; |
| 121 |
|
| 122 |
private $srvCache; |
| 123 |
|
| 124 |
private $statsFactory; |
| 125 |
|
| 126 |
private $maxageVersioned; |
| 127 |
|
| 128 |
private $maxageUnversioned; |
| 129 |
|
| 130 |
|
| 131 |
private $modules = []; |
| 132 |
|
| 133 |
private $moduleInfos = []; |
| 134 |
|
| 135 |
private $testModuleNames = []; |
| 136 |
|
| 137 |
private $sources = []; |
| 138 |
|
| 139 |
protected $errors = []; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
protected $extraHeaders = []; |
| 145 |
|
| 146 |
private $depStoreUpdateBuffer = []; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
private $moduleSkinStyles = []; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function __construct( |
| 174 |
Config $config, |
| 175 |
?LoggerInterface $logger = null, |
| 176 |
?DependencyStore $tracker = null, |
| 177 |
array $params = [] |
| 178 |
) { |
| 179 |
$this->maxageVersioned = $params['maxageVersioned'] ?? 30 * 24 * 60 * 60; |
| 180 |
$this->maxageUnversioned = $params['maxageUnversioned'] ?? 5 * 60; |
| 181 |
|
| 182 |
$this->config = $config; |
| 183 |
$this->logger = $logger ?: new NullLogger(); |
| 184 |
|
| 185 |
$services = MediaWikiServices::getInstance(); |
| 186 |
$this->hookContainer = $services->getHookContainer(); |
| 187 |
|
| 188 |
$this->srvCache = $services->getLocalServerObjectCache(); |
| 189 |
$this->statsFactory = $services->getStatsFactory(); |
| 190 |
|
| 191 |
|
| 192 |
$this->addSource( 'local', $params['loadScript'] ?? '/load.php' ); |
| 193 |
|
| 194 |
|
| 195 |
$this->register( 'startup', [ 'class' => StartUpModule::class ] ); |
| 196 |
|
| 197 |
$this->setMessageBlobStore( |
| 198 |
new MessageBlobStore( $this, $this->logger, $services->getMainWANObjectCache() ) |
| 199 |
); |
| 200 |
|
| 201 |
$tracker = $tracker ?: new KeyValueDependencyStore( new HashBagOStuff() ); |
| 202 |
$this->setDependencyStore( $tracker ); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public function getConfig() { |
| 209 |
return $this->config; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public function setLogger( LoggerInterface $logger ) { |
| 217 |
$this->logger = $logger; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public function getLogger() { |
| 225 |
return $this->logger; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public function getMessageBlobStore() { |
| 233 |
return $this->blobStore; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public function setMessageBlobStore( MessageBlobStore $blobStore ) { |
| 241 |
$this->blobStore = $blobStore; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public function setDependencyStore( DependencyStore $tracker ) { |
| 249 |
$this->depStore = $tracker; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public function setModuleSkinStyles( array $moduleSkinStyles ) { |
| 257 |
$this->moduleSkinStyles = $moduleSkinStyles; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function register( $name, ?array $info = null ) { |
| 272 |
|
| 273 |
$registrations = is_array( $name ) ? $name : [ $name => $info ]; |
| 274 |
foreach ( $registrations as $name => $info ) { |
| 275 |
|
| 276 |
if ( isset( $this->moduleInfos[$name] ) ) { |
| 277 |
|
| 278 |
$this->logger->warning( |
| 279 |
'ResourceLoader duplicate registration warning. ' . |
| 280 |
'Another module has already been registered as ' . $name |
| 281 |
); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
if ( !self::isValidModuleName( $name ) ) { |
| 286 |
throw new InvalidArgumentException( "ResourceLoader module name '$name' is invalid, " |
| 287 |
. "see ResourceLoader::isValidModuleName()" ); |
| 288 |
} |
| 289 |
if ( !is_array( $info ) ) { |
| 290 |
throw new InvalidArgumentException( |
| 291 |
'Invalid module info for "' . $name . '": expected array, got ' . get_debug_type( $info ) |
| 292 |
); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
$this->moduleInfos[$name] = $info; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
public function registerTestModules(): void { |
| 305 |
$extRegistry = ExtensionRegistry::getInstance(); |
| 306 |
$testModules = $extRegistry->getAttribute( 'QUnitTestModules' ); |
| 307 |
|
| 308 |
$testModuleNames = []; |
| 309 |
foreach ( $testModules as $name => &$module ) { |
| 310 |
|
| 311 |
if ( isset( $module['dependencies'] ) && is_string( $module['dependencies'] ) ) { |
| 312 |
$module['dependencies'] = [ $module['dependencies'] ]; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
$module['dependencies'][] = 'mediawiki.qunit-testrunner'; |
| 317 |
|
| 318 |
|
| 319 |
$testModuleNames[] = $name; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
$testModules = ( include MW_INSTALL_PATH . '/tests/qunit/QUnitTestResources.php' ) + $testModules; |
| 324 |
$testModuleNames[] = 'test.MediaWiki'; |
| 325 |
|
| 326 |
$this->register( $testModules ); |
| 327 |
$this->testModuleNames = $testModuleNames; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
public function addSource( $sources, $loadUrl = null ) { |
| 341 |
if ( !is_array( $sources ) ) { |
| 342 |
$sources = [ $sources => $loadUrl ]; |
| 343 |
} |
| 344 |
foreach ( $sources as $id => $source ) { |
| 345 |
|
| 346 |
if ( isset( $this->sources[$id] ) ) { |
| 347 |
throw new RuntimeException( 'Cannot register source ' . $id . ' twice' ); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
if ( is_array( $source ) ) { |
| 352 |
if ( !isset( $source['loadScript'] ) ) { |
| 353 |
throw new InvalidArgumentException( 'Each source must have a "loadScript" key' ); |
| 354 |
} |
| 355 |
$source = $source['loadScript']; |
| 356 |
} |
| 357 |
|
| 358 |
$this->sources[$id] = $source; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
public function getModuleNames() { |
| 366 |
return array_keys( $this->moduleInfos ); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public function getTestSuiteModuleNames() { |
| 377 |
return $this->testModuleNames; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public function isModuleRegistered( $name ) { |
| 388 |
return isset( $this->moduleInfos[$name] ); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public function getModule( $name ) { |
| 403 |
if ( !isset( $this->modules[$name] ) ) { |
| 404 |
if ( !isset( $this->moduleInfos[$name] ) ) { |
| 405 |
|
| 406 |
return null; |
| 407 |
} |
| 408 |
|
| 409 |
$info = $this->moduleInfos[$name]; |
| 410 |
if ( isset( $info['factory'] ) ) { |
| 411 |
|
| 412 |
$object = call_user_func( $info['factory'], $info ); |
| 413 |
} else { |
| 414 |
$class = $info['class'] ?? FileModule::class; |
| 415 |
|
| 416 |
$object = new $class( $info ); |
| 417 |
} |
| 418 |
$object->setConfig( $this->getConfig() ); |
| 419 |
$object->setLogger( $this->logger ); |
| 420 |
$object->setHookContainer( $this->hookContainer ); |
| 421 |
$object->setName( $name ); |
| 422 |
$object->setDependencyAccessCallbacks( |
| 423 |
[ $this, 'loadModuleDependenciesInternal' ], |
| 424 |
[ $this, 'saveModuleDependenciesInternal' ] |
| 425 |
); |
| 426 |
$object->setSkinStylesOverride( $this->moduleSkinStyles ); |
| 427 |
$this->modules[$name] = $object; |
| 428 |
} |
| 429 |
|
| 430 |
return $this->modules[$name]; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
public function preloadModuleInfo( array $moduleNames, Context $context ) { |
| 440 |
|
| 441 |
$vary = Module::getVary( $context ); |
| 442 |
$entitiesByModule = []; |
| 443 |
foreach ( $moduleNames as $moduleName ) { |
| 444 |
$entitiesByModule[$moduleName] = "$moduleName|$vary"; |
| 445 |
} |
| 446 |
$depsByEntity = $this->depStore->retrieveMulti( |
| 447 |
self::RL_DEP_STORE_PREFIX, |
| 448 |
$entitiesByModule |
| 449 |
); |
| 450 |
|
| 451 |
foreach ( $moduleNames as $moduleName ) { |
| 452 |
$module = $this->getModule( $moduleName ); |
| 453 |
if ( $module ) { |
| 454 |
$entity = $entitiesByModule[$moduleName]; |
| 455 |
$deps = $depsByEntity[$entity]; |
| 456 |
$paths = Module::expandRelativePaths( $deps['paths'] ); |
| 457 |
$module->setFileDependencies( $context, $paths ); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
WikiModule::preloadTitleInfo( $context, $moduleNames ); |
| 462 |
|
| 463 |
|
| 464 |
$modulesWithMessages = []; |
| 465 |
foreach ( $moduleNames as $moduleName ) { |
| 466 |
$module = $this->getModule( $moduleName ); |
| 467 |
if ( $module && $module->getMessages() ) { |
| 468 |
$modulesWithMessages[$moduleName] = $module; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
$lang = $context->getLanguage(); |
| 473 |
$store = $this->getMessageBlobStore(); |
| 474 |
$blobs = $store->getBlobs( $modulesWithMessages, $lang ); |
| 475 |
foreach ( $blobs as $moduleName => $blob ) { |
| 476 |
$modulesWithMessages[$moduleName]->setMessageBlob( $blob, $lang ); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
public function loadModuleDependenciesInternal( $moduleName, $variant ) { |
| 487 |
$deps = $this->depStore->retrieve( self::RL_DEP_STORE_PREFIX, "$moduleName|$variant" ); |
| 488 |
|
| 489 |
return Module::expandRelativePaths( $deps['paths'] ); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
public function saveModuleDependenciesInternal( $moduleName, $variant, $paths, $priorPaths ) { |
| 500 |
$hasPendingUpdate = (bool)$this->depStoreUpdateBuffer; |
| 501 |
$entity = "$moduleName|$variant"; |
| 502 |
|
| 503 |
if ( array_diff( $paths, $priorPaths ) || array_diff( $priorPaths, $paths ) ) { |
| 504 |
|
| 505 |
if ( $paths ) { |
| 506 |
$deps = $this->depStore->newEntityDependencies( $paths, time() ); |
| 507 |
$this->depStoreUpdateBuffer[$entity] = $deps; |
| 508 |
} else { |
| 509 |
$this->depStoreUpdateBuffer[$entity] = null; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
if ( !$hasPendingUpdate ) { |
| 520 |
DeferredUpdates::addCallableUpdate( function () { |
| 521 |
$updatesByEntity = $this->depStoreUpdateBuffer; |
| 522 |
$this->depStoreUpdateBuffer = []; |
| 523 |
$cache = MediaWikiServices::getInstance() |
| 524 |
->getObjectCacheFactory()->getLocalClusterInstance(); |
| 525 |
|
| 526 |
$scopeLocks = []; |
| 527 |
$depsByEntity = []; |
| 528 |
$entitiesUnreg = []; |
| 529 |
foreach ( $updatesByEntity as $entity => $update ) { |
| 530 |
$lockKey = $cache->makeKey( 'rl-deps', $entity ); |
| 531 |
$scopeLocks[$entity] = $cache->getScopedLock( $lockKey, 0 ); |
| 532 |
if ( !$scopeLocks[$entity] ) { |
| 533 |
|
| 534 |
|
| 535 |
continue; |
| 536 |
} |
| 537 |
if ( $update === null ) { |
| 538 |
$entitiesUnreg[] = $entity; |
| 539 |
} else { |
| 540 |
$depsByEntity[$entity] = $update; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
$ttl = self::RL_MODULE_DEP_TTL; |
| 545 |
$this->depStore->storeMulti( self::RL_DEP_STORE_PREFIX, $depsByEntity, $ttl ); |
| 546 |
$this->depStore->remove( self::RL_DEP_STORE_PREFIX, $entitiesUnreg ); |
| 547 |
} ); |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
public function getSources() { |
| 557 |
return $this->sources; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
public function getLoadScript( $source ) { |
| 569 |
if ( !isset( $this->sources[$source] ) ) { |
| 570 |
throw new UnexpectedValueException( "Unknown source '$source'" ); |
| 571 |
} |
| 572 |
return $this->sources[$source]; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
public const HASH_LENGTH = 5; |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
public static function makeHash( $value ) { |
| 643 |
$hash = hash( 'fnv132', $value ); |
| 644 |
|
| 645 |
|
| 646 |
return substr( |
| 647 |
\Wikimedia\base_convert( $hash, 16, 36, self::HASH_LENGTH ), |
| 648 |
0, |
| 649 |
self::HASH_LENGTH |
| 650 |
); |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
public function outputErrorAndLog( Exception $e, $msg, array $context = [] ) { |
| 663 |
MWExceptionHandler::logException( $e ); |
| 664 |
$this->logger->warning( |
| 665 |
$msg, |
| 666 |
$context + [ 'exception' => $e ] |
| 667 |
); |
| 668 |
$this->errors[] = self::formatExceptionNoComment( $e ); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
public function getCombinedVersion( Context $context, array $moduleNames ) { |
| 680 |
if ( !$moduleNames ) { |
| 681 |
return ''; |
| 682 |
} |
| 683 |
$hashes = []; |
| 684 |
foreach ( $moduleNames as $module ) { |
| 685 |
try { |
| 686 |
$hash = $this->getModule( $module )->getVersionHash( $context ); |
| 687 |
} catch ( TimeoutException $e ) { |
| 688 |
throw $e; |
| 689 |
} catch ( Exception $e ) { |
| 690 |
|
| 691 |
|
| 692 |
$this->outputErrorAndLog( $e, |
| 693 |
'Calculating version for "{module}" failed: {exception}', |
| 694 |
[ |
| 695 |
'module' => $module, |
| 696 |
] |
| 697 |
); |
| 698 |
$hash = ''; |
| 699 |
} |
| 700 |
$hashes[] = $hash; |
| 701 |
} |
| 702 |
return self::makeHash( implode( '', $hashes ) ); |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
public function makeVersionQuery( Context $context, array $modules ) { |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
$filtered = []; |
| 726 |
foreach ( $modules as $name ) { |
| 727 |
if ( !$this->getModule( $name ) ) { |
| 728 |
|
| 729 |
|
| 730 |
return ''; |
| 731 |
} |
| 732 |
$filtered[] = $name; |
| 733 |
} |
| 734 |
return $this->getCombinedVersion( $context, $filtered ); |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
public function respond( Context $context ) { |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
ob_start(); |
| 751 |
|
| 752 |
$this->errors = []; |
| 753 |
$responseTime = $this->measureResponseTime(); |
| 754 |
ProfilingContext::singleton()->init( MW_ENTRY_POINT, 'respond' ); |
| 755 |
|
| 756 |
|
| 757 |
$modules = []; |
| 758 |
$missing = []; |
| 759 |
foreach ( $context->getModules() as $name ) { |
| 760 |
$module = $this->getModule( $name ); |
| 761 |
if ( $module ) { |
| 762 |
|
| 763 |
|
| 764 |
if ( $module->getGroup() === Module::GROUP_PRIVATE ) { |
| 765 |
|
| 766 |
$this->logger->debug( "Request for private module '$name' denied" ); |
| 767 |
$this->errors[] = "Cannot build private module \"$name\""; |
| 768 |
continue; |
| 769 |
} |
| 770 |
$modules[$name] = $module; |
| 771 |
} else { |
| 772 |
$missing[] = $name; |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
try { |
| 777 |
|
| 778 |
$this->preloadModuleInfo( array_keys( $modules ), $context ); |
| 779 |
} catch ( TimeoutException $e ) { |
| 780 |
throw $e; |
| 781 |
} catch ( Exception $e ) { |
| 782 |
$this->outputErrorAndLog( $e, 'Preloading module info failed: {exception}' ); |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
$versionHash = $this->getCombinedVersion( $context, array_keys( $modules ) ); |
| 787 |
|
| 788 |
s |
| 789 |
|
| 790 |
$etag = 'W/"' . $versionHash . '"'; |
| 791 |
|
| 792 |
|
| 793 |
if ( $this->tryRespondNotModified( $context, $etag ) ) { |
| 794 |
return; |
| 795 |
} |
| 796 |
|
| 797 |
if ( $context->isSourceMap() ) { |
| 798 |
|
| 799 |
if ( $context->getVersion() !== null && $versionHash !== $context->getVersion() ) { |
| 800 |
ob_end_clean(); |
| 801 |
$this->sendSourceMapVersionMismatch( $versionHash ); |
| 802 |
return; |
| 803 |
} |
| 804 |
|
| 805 |
if ( $context->getImage() |
| 806 |
|| $context->getOnly() === 'styles' |
| 807 |
|| $context->getDebug() |
| 808 |
) { |
| 809 |
ob_end_clean(); |
| 810 |
$this->sendSourceMapTypeNotImplemented(); |
| 811 |
return; |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
if ( $this->config->get( MainConfigNames::ResourceLoaderEnableSourceMapLinks ) |
| 816 |
&& !$context->getImageObj() |
| 817 |
&& !$context->isSourceMap() |
| 818 |
&& $context->shouldIncludeScripts() |
| 819 |
&& !$context->getDebug() |
| 820 |
) { |
| 821 |
$this->extraHeaders[] = 'SourceMap: ' . $this->getSourceMapUrl( $context, $versionHash ); |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
$response = $this->makeModuleResponse( $context, $modules, $missing ); |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
if ( $context->getDebug() ) { |
| 830 |
$warnings = ob_get_contents(); |
| 831 |
if ( strlen( $warnings ) ) { |
| 832 |
$this->errors[] = $warnings; |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
$this->sendResponseHeaders( $context, $etag, (bool)$this->errors, $this->extraHeaders ); |
| 837 |
|
| 838 |
|
| 839 |
ob_end_clean(); |
| 840 |
|
| 841 |
if ( $context->getImageObj() && $this->errors ) { |
| 842 |
|
| 843 |
$response = implode( "\n\n", $this->errors ); |
| 844 |
} elseif ( $this->errors ) { |
| 845 |
$errorText = implode( "\n\n", $this->errors ); |
| 846 |
$errorResponse = self::makeComment( $errorText ); |
| 847 |
if ( $context->shouldIncludeScripts() ) { |
| 848 |
$errorResponse .= 'if (window.console && console.error) { console.error(' |
| 849 |
. $context->encodeJson( $errorText ) |
| 850 |
. "); }\n"; |
| 851 |
|
| 852 |
|
| 853 |
$response .= $errorResponse; |
| 854 |
} else { |
| 855 |
|
| 856 |
$response = $errorResponse . $response; |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
echo $response; |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
protected function measureResponseTime() { |
| 868 |
$statStart = $_SERVER['REQUEST_TIME_FLOAT']; |
| 869 |
return new ScopedCallback( function () use ( $statStart ) { |
| 870 |
$statTiming = microtime( true ) - $statStart; |
| 871 |
|
| 872 |
$this->statsFactory->getTiming( 'resourceloader_response_time_seconds' ) |
| 873 |
->copyToStatsdAt( 'resourceloader.responseTime' ) |
| 874 |
->observe( 1000 * $statTiming ); |
| 875 |
} ); |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
protected function sendResponseHeaders( |
| 889 |
Context $context, $etag, $errors, array $extra = [] |
| 890 |
): void { |
| 891 |
HeaderCallback::warnIfHeadersSent(); |
| 892 |
|
| 893 |
if ( $errors ) { |
| 894 |
$maxage = self::MAXAGE_RECOVER; |
| 895 |
} elseif ( |
| 896 |
$context->getVersion() !== null |
| 897 |
&& $context->getVersion() !== $this->makeVersionQuery( $context, $context->getModules() ) |
| 898 |
) { |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
$this->logger->debug( 'Client and server registry version out of sync' ); |
| 904 |
$maxage = self::MAXAGE_RECOVER; |
| 905 |
} elseif ( $context->getVersion() === null ) { |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
$maxage = $this->maxageUnversioned; |
| 910 |
} else { |
| 911 |
|
| 912 |
|
| 913 |
$maxage = $this->maxageVersioned; |
| 914 |
} |
| 915 |
if ( $context->getImageObj() ) { |
| 916 |
|
| 917 |
if ( $errors ) { |
| 918 |
header( 'Content-Type: text/plain; charset=utf-8' ); |
| 919 |
} else { |
| 920 |
$context->getImageObj()->sendResponseHeaders( $context ); |
| 921 |
} |
| 922 |
} elseif ( $context->isSourceMap() ) { |
| 923 |
header( 'Content-Type: application/json' ); |
| 924 |
} elseif ( $context->getOnly() === 'styles' ) { |
| 925 |
header( 'Content-Type: text/css; charset=utf-8' ); |
| 926 |
header( 'Access-Control-Allow-Origin: *' ); |
| 927 |
} else { |
| 928 |
header( 'Content-Type: text/javascript; charset=utf-8' ); |
| 929 |
} |
| 930 |
g |
| 931 |
|
| 932 |
header( 'ETag: ' . $etag ); |
| 933 |
if ( $context->getDebug() ) { |
| 934 |
|
| 935 |
header( 'Cache-Control: private, no-cache, must-revalidate' ); |
| 936 |
} else { |
| 937 |
|
| 938 |
|
| 939 |
$staleDirective = ( $maxage > self::MAXAGE_RECOVER |
| 940 |
? ", stale-while-revalidate=" . min( 60, intval( $maxage / 2 ) ) |
| 941 |
: '' |
| 942 |
); |
| 943 |
header( "Cache-Control: public, max-age=$maxage, s-maxage=$maxage" . $staleDirective ); |
| 944 |
header( 'Expires: ' . ConvertibleTimestamp::convert( TS_RFC2822, time() + $maxage ) ); |
| 945 |
} |
| 946 |
foreach ( $extra as $header ) { |
| 947 |
header( $header ); |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
protected function tryRespondNotModified( Context $context, $etag ) { |
| 962 |
h |
| 963 |
|
| 964 |
$clientKeys = $context->getRequest()->getHeader( 'If-None-Match', WebRequest::GETHEADER_LIST ); |
| 965 |
|
| 966 |
if ( $clientKeys !== false && !$context->getDebug() && in_array( $etag, $clientKeys ) ) { |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
wfResetOutputBuffers( true ); |
| 977 |
|
| 978 |
HttpStatus::header( 304 ); |
| 979 |
|
| 980 |
$this->sendResponseHeaders( $context, $etag, false ); |
| 981 |
return true; |
| 982 |
} |
| 983 |
return false; |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
private function getSourceMapUrl( Context $context, $version ) { |
| 994 |
return $this->createLoaderURL( 'local', $context, [ |
| 995 |
'sourcemap' => '1', |
| 996 |
'version' => $version |
| 997 |
] ); |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
private function sendSourceMapVersionMismatch( $currentVersion ) { |
| 1006 |
HttpStatus::header( 404 ); |
| 1007 |
header( 'Content-Type: text/plain; charset=utf-8' ); |
| 1008 |
header( 'X-Content-Type-Options: nosniff' ); |
| 1009 |
echo "Can't deliver a source map for the requested version " . |
| 1010 |
"since the version is now '$currentVersion'\n"; |
| 1011 |
} |
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
private function sendSourceMapTypeNotImplemented() { |
| 1018 |
HttpStatus::header( 404 ); |
| 1019 |
header( 'Content-Type: text/plain; charset=utf-8' ); |
| 1020 |
header( 'X-Content-Type-Options: nosniff' ); |
| 1021 |
echo "Can't make a source map for this content type\n"; |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
public static function makeComment( $text ) { |
| 1033 |
$encText = str_replace( '*/', '* /', $text ); |
| 1034 |
return "/*\n$encText\n*/\n"; |
| 1035 |
} |
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
protected static function formatExceptionNoComment( Throwable $e ) { |
| 1045 |
if ( !MWExceptionRenderer::shouldShowExceptionDetails() ) { |
| 1046 |
return MWExceptionHandler::getPublicLogMessage( $e ); |
| 1047 |
} |
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
$type = get_class( $e ); |
| 1055 |
$message = $e->getMessage(); |
| 1056 |
|
| 1057 |
return "$type: $message" . |
| 1058 |
"\nBacktrace:\n" . |
| 1059 |
MWExceptionHandler::getRedactedTraceAsString( $e ); |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
public function makeModuleResponse( Context $context, |
| 1074 |
array $modules, array $missing = [] |
| 1075 |
) { |
| 1076 |
if ( $modules === [] && $missing === [] ) { |
| 1077 |
return <<<MESSAGE |
| 1078 |
/* This file is the Web entry point for MediaWiki's ResourceLoader: |
| 1079 |
<https://www.mediawiki.org/wiki/ResourceLoader>. In this request, |
| 1080 |
no modules were requested. Max made me put this here. */ |
| 1081 |
MESSAGE; |
| 1082 |
} |
| 1083 |
|
| 1084 |
$image = $context->getImageObj(); |
| 1085 |
if ( $image ) { |
| 1086 |
$data = $image->getImageData( $context ); |
| 1087 |
if ( $data === false ) { |
| 1088 |
$data = ''; |
| 1089 |
$this->errors[] = 'Image generation failed'; |
| 1090 |
} |
| 1091 |
return $data; |
| 1092 |
} |
| 1093 |
|
| 1094 |
$states = []; |
| 1095 |
foreach ( $missing as $name ) { |
| 1096 |
$states[$name] = 'missing'; |
| 1097 |
} |
| 1098 |
|
| 1099 |
$only = $context->getOnly(); |
| 1100 |
$debug = (bool)$context->getDebug(); |
| 1101 |
if ( $context->isSourceMap() && count( $modules ) > 1 ) { |
| 1102 |
$indexMap = new IndexMap; |
| 1103 |
} else { |
| 1104 |
$indexMap = null; |
| 1105 |
} |
| 1106 |
|
| 1107 |
$out = ''; |
| 1108 |
foreach ( $modules as $name => $module ) { |
| 1109 |
try { |
| 1110 |
[ $response, $offset ] = $this->getOneModuleResponse( $context, $name, $module ); |
| 1111 |
if ( $indexMap ) { |
| 1112 |
$indexMap->addEncodedMap( $response, $offset ); |
| 1113 |
} else { |
| 1114 |
$out .= $response; |
| 1115 |
} |
| 1116 |
} catch ( TimeoutException $e ) { |
| 1117 |
throw $e; |
| 1118 |
} catch ( Exception $e ) { |
| 1119 |
$this->outputErrorAndLog( $e, 'Generating module package failed: {exception}' ); |
| 1120 |
|
| 1121 |
|
| 1122 |
$states[$name] = 'error'; |
| 1123 |
unset( $modules[$name] ); |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
|
| 1128 |
if ( $context->shouldIncludeScripts() && !$context->getRaw() ) { |
| 1129 |
if ( $modules && $only === 'scripts' ) { |
| 1130 |
|
| 1131 |
|
| 1132 |
foreach ( $modules as $name => $module ) { |
| 1133 |
$states[$name] = 'ready'; |
| 1134 |
} |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
if ( $states && !$context->isSourceMap() ) { |
| 1139 |
$stateScript = self::makeLoaderStateScript( $context, $states ); |
| 1140 |
if ( !$debug ) { |
| 1141 |
$stateScript = self::filter( 'minify-js', $stateScript ); |
| 1142 |
} |
| 1143 |
|
| 1144 |
$out = self::ensureNewline( $out ) . $stateScript; |
| 1145 |
} |
| 1146 |
} elseif ( $states ) { |
| 1147 |
$this->errors[] = 'Problematic modules: ' |
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
. @$context->encodeJson( $states ); |
| 1152 |
} |
| 1153 |
|
| 1154 |
if ( $indexMap ) { |
| 1155 |
return $indexMap->getMap(); |
| 1156 |
} else { |
| 1157 |
return $out; |
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
private function getOneModuleResponse( Context $context, $name, Module $module ) { |
| 1170 |
$only = $context->getOnly(); |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
$shouldCache = !$module->shouldEmbedModule( $context ); |
| 1175 |
if ( $only === 'styles' ) { |
| 1176 |
$minifier = new IdentityMinifierState; |
| 1177 |
$this->addOneModuleResponse( $context, $minifier, $name, $module, $this->extraHeaders ); |
| 1178 |
|
| 1179 |
|
| 1180 |
$styles = $minifier->getMinifiedOutput(); |
| 1181 |
if ( $context->getDebug() ) { |
| 1182 |
return [ $styles, null ]; |
| 1183 |
} |
| 1184 |
return [ |
| 1185 |
self::filter( 'minify-css', $styles, |
| 1186 |
[ 'cache' => $shouldCache ] ), |
| 1187 |
null |
| 1188 |
]; |
| 1189 |
} |
| 1190 |
|
| 1191 |
$minifier = new IdentityMinifierState; |
| 1192 |
$this->addOneModuleResponse( $context, $minifier, $name, $module, $this->extraHeaders ); |
| 1193 |
$plainContent = $minifier->getMinifiedOutput(); |
| 1194 |
if ( $context->getDebug() ) { |
| 1195 |
return [ $plainContent, null ]; |
| 1196 |
} |
| 1197 |
|
| 1198 |
$isHit = true; |
| 1199 |
$callback = function () use ( $context, $name, $module, &$isHit ) { |
| 1200 |
$isHit = false; |
| 1201 |
if ( $context->isSourceMap() ) { |
| 1202 |
$minifier = ( new JavaScriptMapperState ) |
| 1203 |
->outputFile( $this->createLoaderURL( 'local', $context, [ |
| 1204 |
'modules' => self::makePackedModulesString( $context->getModules() ), |
| 1205 |
'only' => $context->getOnly() |
| 1206 |
] ) ); |
| 1207 |
} else { |
| 1208 |
$minifier = new JavaScriptMinifierState; |
| 1209 |
} |
| 1210 |
|
| 1211 |
$discardedHeaders = null; |
| 1212 |
$this->addOneModuleResponse( $context, $minifier, $name, $module, $discardedHeaders ); |
| 1213 |
if ( $context->isSourceMap() ) { |
| 1214 |
$sourceMap = $minifier->getRawSourceMap(); |
| 1215 |
$generated = $minifier->getMinifiedOutput(); |
| 1216 |
$offset = IndexMapOffset::newFromText( $generated ); |
| 1217 |
return [ $sourceMap, $offset->toArray() ]; |
| 1218 |
} else { |
| 1219 |
return [ $minifier->getMinifiedOutput(), null ]; |
| 1220 |
} |
| 1221 |
}; |
| 1222 |
|
| 1223 |
if ( $shouldCache ) { |
| 1224 |
[ $response, $offsetArray ] = $this->srvCache->getWithSetCallback( |
| 1225 |
$this->srvCache->makeGlobalKey( |
| 1226 |
'resourceloader-mapped', |
| 1227 |
self::CACHE_VERSION, |
| 1228 |
$name, |
| 1229 |
$context->isSourceMap() ? '1' : '0', |
| 1230 |
md5( $plainContent ) |
| 1231 |
), |
| 1232 |
BagOStuff::TTL_DAY, |
| 1233 |
$callback |
| 1234 |
); |
| 1235 |
|
| 1236 |
$mapType = $context->isSourceMap() ? 'map-js' : 'minify-js'; |
| 1237 |
$statsdNamespace = implode( '.', [ |
| 1238 |
"resourceloader_cache", $mapType, $isHit ? 'hit' : 'miss' |
| 1239 |
] ); |
| 1240 |
$this->statsFactory->getCounter( 'resourceloader_cache_total' ) |
| 1241 |
->setLabel( 'type', $mapType ) |
| 1242 |
->setLabel( 'status', $isHit ? 'hit' : 'miss' ) |
| 1243 |
->copyToStatsdAt( [ $statsdNamespace ] ) |
| 1244 |
->increment(); |
| 1245 |
} else { |
| 1246 |
[ $response, $offsetArray ] = $callback(); |
| 1247 |
} |
| 1248 |
$offset = $offsetArray ? IndexMapOffset::newFromArray( $offsetArray ) : null; |
| 1249 |
|
| 1250 |
return [ $response, $offset ]; |
| 1251 |
} |
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
private function addOneModuleResponse( |
| 1264 |
Context $context, MinifierState $minifier, $name, Module $module, &$headers |
| 1265 |
) { |
| 1266 |
$only = $context->getOnly(); |
| 1267 |
$debug = (bool)$context->getDebug(); |
| 1268 |
$content = $module->getModuleContent( $context ); |
| 1269 |
$version = $module->getVersionHash( $context ); |
| 1270 |
|
| 1271 |
if ( $headers !== null && isset( $content['headers'] ) ) { |
| 1272 |
$headers = array_merge( $headers, $content['headers'] ); |
| 1273 |
} |
| 1274 |
|
| 1275 |
|
| 1276 |
switch ( $only ) { |
| 1277 |
case 'scripts': |
| 1278 |
$scripts = $content['scripts']; |
| 1279 |
if ( !is_array( $scripts ) ) { |
| 1280 |
|
| 1281 |
|
| 1282 |
throw new InvalidArgumentException( 'scripts must be an array' ); |
| 1283 |
} |
| 1284 |
if ( isset( $scripts['plainScripts'] ) ) { |
| 1285 |
|
| 1286 |
$this->addPlainScripts( $minifier, $name, $scripts['plainScripts'] ); |
| 1287 |
} elseif ( isset( $scripts['files'] ) ) { |
| 1288 |
|
| 1289 |
$this->addImplementScript( |
| 1290 |
$minifier, |
| 1291 |
$name, |
| 1292 |
$version, |
| 1293 |
$scripts, |
| 1294 |
[], |
| 1295 |
null, |
| 1296 |
[], |
| 1297 |
$content['deprecationWarning'] ?? null |
| 1298 |
); |
| 1299 |
} |
| 1300 |
break; |
| 1301 |
case 'styles': |
| 1302 |
$styles = $content['styles']; |
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
if ( isset( $styles['css'] ) ) { |
| 1307 |
$minifier->addOutput( implode( '', $styles['css'] ) ); |
| 1308 |
} |
| 1309 |
break; |
| 1310 |
default: |
| 1311 |
$scripts = $content['scripts'] ?? ''; |
| 1312 |
if ( ( $name === 'site' || $name === 'user' ) |
| 1313 |
&& isset( $scripts['plainScripts'] ) |
| 1314 |
) { |
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
$scripts = self::concatenatePlainScripts( $scripts['plainScripts'] ); |
| 1320 |
if ( !$debug ) { |
| 1321 |
$scripts = self::filter( 'minify-js', $scripts ); |
| 1322 |
} |
| 1323 |
} |
| 1324 |
$this->addImplementScript( |
| 1325 |
$minifier, |
| 1326 |
$name, |
| 1327 |
$version, |
| 1328 |
$scripts, |
| 1329 |
$content['styles'] ?? [], |
| 1330 |
isset( $content['messagesBlob'] ) ? new HtmlJsCode( $content['messagesBlob'] ) : null, |
| 1331 |
$content['templates'] ?? [], |
| 1332 |
$content['deprecationWarning'] ?? null |
| 1333 |
); |
| 1334 |
break; |
| 1335 |
} |
| 1336 |
$minifier->ensureNewline(); |
| 1337 |
} |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
public static function ensureNewline( $str ) { |
| 1346 |
$end = substr( $str, -1 ); |
| 1347 |
if ( $end === '' || $end === "\n" ) { |
| 1348 |
return $str; |
| 1349 |
} |
| 1350 |
return $str . "\n"; |
| 1351 |
} |
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
public function getModulesByMessage( $messageKey ) { |
| 1360 |
$moduleNames = []; |
| 1361 |
foreach ( $this->getModuleNames() as $moduleName ) { |
| 1362 |
$module = $this->getModule( $moduleName ); |
| 1363 |
if ( in_array( $messageKey, $module->getMessages() ) ) { |
| 1364 |
$moduleNames[] = $moduleName; |
| 1365 |
} |
| 1366 |
} |
| 1367 |
return $moduleNames; |
| 1368 |
} |
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
private function addImplementScript( MinifierState $minifier, |
| 1392 |
$moduleName, $version, $scripts, $styles, $messages, $templates, $deprecationWarning |
| 1393 |
) { |
| 1394 |
$implementKey = "$moduleName@$version"; |
| 1395 |
|
| 1396 |
|
| 1397 |
$minifier->addOutput( "mw.loader.impl(function(){return[" . |
| 1398 |
Html::encodeJsVar( $implementKey ) . "," ); |
| 1399 |
|
| 1400 |
|
| 1401 |
if ( is_string( $scripts ) ) { |
| 1402 |
|
| 1403 |
$minifier->addOutput( Html::encodeJsVar( $scripts ) ); |
| 1404 |
} elseif ( is_array( $scripts ) ) { |
| 1405 |
if ( isset( $scripts['files'] ) ) { |
| 1406 |
$minifier->addOutput( |
| 1407 |
"{\"main\":" . |
| 1408 |
Html::encodeJsVar( $scripts['main'] ) . |
| 1409 |
",\"files\":" ); |
| 1410 |
$this->addFiles( $minifier, $moduleName, $scripts['files'] ); |
| 1411 |
$minifier->addOutput( "}" ); |
| 1412 |
} elseif ( isset( $scripts['plainScripts'] ) ) { |
| 1413 |
if ( $this->isEmptyFileInfos( $scripts['plainScripts'] ) ) { |
| 1414 |
$minifier->addOutput( 'null' ); |
| 1415 |
} else { |
| 1416 |
$minifier->addOutput( "function($,jQuery,require,module){" ); |
| 1417 |
$this->addPlainScripts( $minifier, $moduleName, $scripts['plainScripts'] ); |
| 1418 |
$minifier->addOutput( "}" ); |
| 1419 |
} |
| 1420 |
} elseif ( $scripts === [] || isset( $scripts[0] ) ) { |
| 1421 |
|
| 1422 |
$minifier->addOutput( Html::encodeJsVar( $scripts ) ); |
| 1423 |
} else { |
| 1424 |
throw new InvalidArgumentException( 'Invalid script array: ' . |
| 1425 |
'must contain files, plainScripts or be an array of URLs' ); |
| 1426 |
} |
| 1427 |
} else { |
| 1428 |
throw new InvalidArgumentException( 'Script must be a string or array' ); |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
$extraArgs = [ |
| 1435 |
(object)$styles, |
| 1436 |
$messages ?? (object)[], |
| 1437 |
(object)$templates, |
| 1438 |
$deprecationWarning |
| 1439 |
]; |
| 1440 |
self::trimArray( $extraArgs ); |
| 1441 |
foreach ( $extraArgs as $arg ) { |
| 1442 |
$minifier->addOutput( ',' . Html::encodeJsVar( $arg ) ); |
| 1443 |
} |
| 1444 |
$minifier->addOutput( "];});" ); |
| 1445 |
} |
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
private function addFiles( MinifierState $minifier, $moduleName, $files ) { |
| 1458 |
$first = true; |
| 1459 |
$minifier->addOutput( "{" ); |
| 1460 |
foreach ( $files as $fileName => $file ) { |
| 1461 |
if ( $first ) { |
| 1462 |
$first = false; |
| 1463 |
} else { |
| 1464 |
$minifier->addOutput( "," ); |
| 1465 |
} |
| 1466 |
$minifier->addOutput( Html::encodeJsVar( $fileName ) . ':' ); |
| 1467 |
$this->addFileContent( $minifier, $moduleName, 'packageFile', $fileName, $file ); |
| 1468 |
} |
| 1469 |
$minifier->addOutput( "}" ); |
| 1470 |
} |
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
private function addFileContent( MinifierState $minifier, |
| 1482 |
$moduleName, $sourceType, $sourceIndex, array $file |
| 1483 |
) { |
| 1484 |
$isScript = ( $file['type'] ?? 'script' ) === 'script'; |
| 1485 |
|
| 1486 |
$filePath = $file['filePath'] ?? $file['virtualFilePath'] ?? null; |
| 1487 |
if ( $filePath !== null && $filePath->getRemoteBasePath() !== null ) { |
| 1488 |
$url = $filePath->getRemotePath(); |
| 1489 |
} else { |
| 1490 |
$ext = $isScript ? 'js' : 'json'; |
| 1491 |
$scriptPath = $this->config->has( MainConfigNames::ScriptPath ) |
| 1492 |
? $this->config->get( MainConfigNames::ScriptPath ) : ''; |
| 1493 |
$url = "$scriptPath/virtual-resource/$moduleName-$sourceType-$sourceIndex.$ext"; |
| 1494 |
} |
| 1495 |
$content = $file['content']; |
| 1496 |
if ( $isScript ) { |
| 1497 |
if ( $sourceType === 'packageFile' ) { |
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
$minifier->addOutput( "function(require,module,exports){" ); |
| 1502 |
$minifier->addSourceFile( $url, $content, true ); |
| 1503 |
$minifier->ensureNewline(); |
| 1504 |
$minifier->addOutput( "}" ); |
| 1505 |
} else { |
| 1506 |
$minifier->addSourceFile( $url, $content, true ); |
| 1507 |
$minifier->ensureNewline(); |
| 1508 |
} |
| 1509 |
} else { |
| 1510 |
$content = Html::encodeJsVar( $content, true ); |
| 1511 |
$minifier->addSourceFile( $url, $content, true ); |
| 1512 |
} |
| 1513 |
} |
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
private static function concatenatePlainScripts( $plainScripts ) { |
| 1523 |
$s = ''; |
| 1524 |
foreach ( $plainScripts as $script ) { |
| 1525 |
|
| 1526 |
|
| 1527 |
$s .= self::ensureNewline( $script['content'] ); |
| 1528 |
} |
| 1529 |
return $s; |
| 1530 |
} |
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
private function addPlainScripts( MinifierState $minifier, $moduleName, $plainScripts ) { |
| 1541 |
foreach ( $plainScripts as $index => $file ) { |
| 1542 |
$this->addFileContent( $minifier, $moduleName, 'script', $index, $file ); |
| 1543 |
} |
| 1544 |
} |
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
private function isEmptyFileInfos( $infos ) { |
| 1553 |
$len = 0; |
| 1554 |
foreach ( $infos as $info ) { |
| 1555 |
$len += strlen( $info['content'] ?? '' ); |
| 1556 |
} |
| 1557 |
return $len === 0; |
| 1558 |
} |
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
public static function makeCombinedStyles( array $stylePairs ) { |
| 1568 |
$out = []; |
| 1569 |
foreach ( $stylePairs as $media => $styles ) { |
| 1570 |
|
| 1571 |
|
| 1572 |
$styles = (array)$styles; |
| 1573 |
foreach ( $styles as $style ) { |
| 1574 |
$style = trim( $style ); |
| 1575 |
|
| 1576 |
if ( $style === '' ) { |
| 1577 |
continue; |
| 1578 |
} |
| 1579 |
|
| 1580 |
|
| 1581 |
$media = OutputPage::transformCssMedia( $media ); |
| 1582 |
|
| 1583 |
if ( $media === '' || $media == 'all' ) { |
| 1584 |
$out[] = $style; |
| 1585 |
} elseif ( is_string( $media ) ) { |
| 1586 |
$out[] = "@media $media {\n" . str_replace( "\n", "\n\t", "\t" . $style ) . "}"; |
| 1587 |
} |
| 1588 |
|
| 1589 |
} |
| 1590 |
} |
| 1591 |
return $out; |
| 1592 |
} |
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
private static function encodeJsonForScript( $data ) { |
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
$jsonFlags = JSON_UNESCAPED_SLASHES | |
| 1612 |
JSON_UNESCAPED_UNICODE | |
| 1613 |
JSON_HEX_TAG | |
| 1614 |
JSON_HEX_AMP; |
| 1615 |
if ( self::inDebugMode() ) { |
| 1616 |
$jsonFlags |= JSON_PRETTY_PRINT; |
| 1617 |
} |
| 1618 |
return json_encode( $data, $jsonFlags ); |
| 1619 |
} |
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
public static function makeLoaderStateScript( |
| 1630 |
Context $context, array $states |
| 1631 |
) { |
| 1632 |
return 'mw.loader.state(' |
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
. @$context->encodeJson( $states ) |
| 1637 |
. ');'; |
| 1638 |
} |
| 1639 |
|
| 1640 |
private static function isEmptyObject( stdClass $obj ) { |
| 1641 |
foreach ( $obj as $value ) { |
| 1642 |
return false; |
| 1643 |
} |
| 1644 |
return true; |
| 1645 |
} |
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
private static function trimArray( array &$array ): void { |
| 1661 |
$i = count( $array ); |
| 1662 |
while ( $i-- ) { |
| 1663 |
if ( $array[$i] === null |
| 1664 |
|| $array[$i] === [] |
| 1665 |
|| ( $array[$i] instanceof HtmlJsCode && $array[$i]->value === '{}' ) |
| 1666 |
|| ( $array[$i] instanceof stdClass && self::isEmptyObject( $array[$i] ) ) |
| 1667 |
) { |
| 1668 |
unset( $array[$i] ); |
| 1669 |
} else { |
| 1670 |
break; |
| 1671 |
} |
| 1672 |
} |
| 1673 |
} |
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
public static function makeLoaderRegisterScript( |
| 1701 |
Context $context, array $modules |
| 1702 |
) { |
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
$index = []; |
| 1707 |
foreach ( $modules as $i => $module ) { |
| 1708 |
|
| 1709 |
$index[$module[0]] = $i; |
| 1710 |
} |
| 1711 |
foreach ( $modules as &$module ) { |
| 1712 |
if ( isset( $module[2] ) ) { |
| 1713 |
foreach ( $module[2] as &$dependency ) { |
| 1714 |
if ( isset( $index[$dependency] ) ) { |
| 1715 |
|
| 1716 |
$dependency = $index[$dependency]; |
| 1717 |
} |
| 1718 |
} |
| 1719 |
} |
| 1720 |
self::trimArray( $module ); |
| 1721 |
} |
| 1722 |
|
| 1723 |
return 'mw.loader.register(' |
| 1724 |
. $context->encodeJson( $modules ) |
| 1725 |
. ');'; |
| 1726 |
} |
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
public static function makeLoaderSourcesScript( |
| 1742 |
Context $context, array $sources |
| 1743 |
) { |
| 1744 |
return 'mw.loader.addSource(' |
| 1745 |
. $context->encodeJson( $sources ) |
| 1746 |
. ');'; |
| 1747 |
} |
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
public static function makeLoaderConditionalScript( $script ) { |
| 1756 |
|
| 1757 |
return '(RLQ=window.RLQ||[]).push(function(){' . |
| 1758 |
trim( $script ) . '});'; |
| 1759 |
} |
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
public static function makeInlineCodeWithModule( $modules, $script ) { |
| 1770 |
|
| 1771 |
return '(RLQ=window.RLQ||[]).push([' |
| 1772 |
. self::encodeJsonForScript( $modules ) . ',' |
| 1773 |
. 'function(){' . trim( $script ) . '}' |
| 1774 |
. ']);'; |
| 1775 |
} |
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
public static function makeInlineScript( $script, $nonce = null ) { |
| 1788 |
$js = self::makeLoaderConditionalScript( $script ); |
| 1789 |
return new WrappedString( |
| 1790 |
Html::inlineScript( $js ), |
| 1791 |
"<script>(RLQ=window.RLQ||[]).push(function(){", |
| 1792 |
'});</script>' |
| 1793 |
); |
| 1794 |
} |
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
public static function makeConfigSetScript( array $configuration ) { |
| 1805 |
$json = self::encodeJsonForScript( $configuration ); |
| 1806 |
if ( $json === false ) { |
| 1807 |
$e = new LogicException( |
| 1808 |
'JSON serialization of config data failed. ' . |
| 1809 |
'This usually means the config data is not valid UTF-8.' |
| 1810 |
); |
| 1811 |
MWExceptionHandler::logException( $e ); |
| 1812 |
return 'mw.log.error(' . self::encodeJsonForScript( $e->__toString() ) . ');'; |
| 1813 |
} |
| 1814 |
return "mw.config.set($json);"; |
| 1815 |
} |
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
public static function makePackedModulesString( array $modules ) { |
| 1831 |
$moduleMap = []; |
| 1832 |
foreach ( $modules as $module ) { |
| 1833 |
$pos = strrpos( $module, '.' ); |
| 1834 |
$prefix = $pos === false ? '' : substr( $module, 0, $pos ); |
| 1835 |
$suffix = $pos === false ? $module : substr( $module, $pos + 1 ); |
| 1836 |
$moduleMap[$prefix][] = $suffix; |
| 1837 |
} |
| 1838 |
|
| 1839 |
$arr = []; |
| 1840 |
foreach ( $moduleMap as $prefix => $suffixes ) { |
| 1841 |
$p = $prefix === '' ? '' : $prefix . '.'; |
| 1842 |
$arr[] = $p . implode( ',', $suffixes ); |
| 1843 |
} |
| 1844 |
return implode( '|', $arr ); |
| 1845 |
} |
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
public static function expandModuleNames( $modules ) { |
| 1859 |
$retval = []; |
| 1860 |
$exploded = explode( '|', $modules ); |
| 1861 |
foreach ( $exploded as $group ) { |
| 1862 |
if ( strpos( $group, ',' ) === false ) { |
| 1863 |
|
| 1864 |
|
| 1865 |
$retval[] = $group; |
| 1866 |
continue; |
| 1867 |
} |
| 1868 |
|
| 1869 |
$pos = strrpos( $group, '.' ); |
| 1870 |
if ( $pos === false ) { |
| 1871 |
|
| 1872 |
$retval = array_merge( $retval, explode( ',', $group ) ); |
| 1873 |
continue; |
| 1874 |
} |
| 1875 |
|
| 1876 |
$prefix = substr( $group, 0, $pos ); |
| 1877 |
$suffixes = explode( ',', substr( $group, $pos + 1 ) ); |
| 1878 |
foreach ( $suffixes as $suffix ) { |
| 1879 |
$retval[] = "$prefix.$suffix"; |
| 1880 |
} |
| 1881 |
} |
| 1882 |
return $retval; |
| 1883 |
} |
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
public static function inDebugMode() { |
| 1896 |
if ( self::$debugMode === null ) { |
| 1897 |
global $wgRequest; |
| 1898 |
|
| 1899 |
$resourceLoaderDebug = MediaWikiServices::getInstance()->getMainConfig()->get( |
| 1900 |
MainConfigNames::ResourceLoaderDebug ); |
| 1901 |
$str = $wgRequest->getRawVal( 'debug' ) ?? |
| 1902 |
$wgRequest->getCookie( 'resourceLoaderDebug', '', $resourceLoaderDebug ? 'true' : '' ); |
| 1903 |
self::$debugMode = Context::debugFromString( $str ); |
| 1904 |
} |
| 1905 |
return self::$debugMode; |
| 1906 |
} |
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
public static function clearCache() { |
| 1919 |
self::$debugMode = null; |
| 1920 |
} |
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
public function createLoaderURL( $source, Context $context, |
| 1932 |
array $extraQuery = [] |
| 1933 |
) { |
| 1934 |
$query = self::createLoaderQuery( $context, $extraQuery ); |
| 1935 |
$script = $this->getLoadScript( $source ); |
| 1936 |
|
| 1937 |
return wfAppendQuery( $script, $query ); |
| 1938 |
} |
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
protected static function createLoaderQuery( |
| 1950 |
Context $context, array $extraQuery = [] |
| 1951 |
) { |
| 1952 |
return self::makeLoaderQuery( |
| 1953 |
$context->getModules(), |
| 1954 |
$context->getLanguage(), |
| 1955 |
$context->getSkin(), |
| 1956 |
$context->getUser(), |
| 1957 |
$context->getVersion(), |
| 1958 |
$context->getDebug(), |
| 1959 |
$context->getOnly(), |
| 1960 |
$context->getRequest()->getBool( 'printable' ), |
| 1961 |
null, |
| 1962 |
$extraQuery |
| 1963 |
); |
| 1964 |
} |
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
public static function makeLoaderQuery( array $modules, $lang, $skin, $user = null, |
| 1983 |
$version = null, $debug = Context::DEBUG_OFF, $only = null, |
| 1984 |
$printable = false, $handheld = null, array $extraQuery = [] |
| 1985 |
) { |
| 1986 |
$query = [ |
| 1987 |
'modules' => self::makePackedModulesString( $modules ), |
| 1988 |
]; |
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
if ( $lang !== Context::DEFAULT_LANG ) { |
| 1994 |
$query['lang'] = $lang; |
| 1995 |
} |
| 1996 |
if ( $skin !== Context::DEFAULT_SKIN ) { |
| 1997 |
$query['skin'] = $skin; |
| 1998 |
} |
| 1999 |
if ( $debug !== Context::DEBUG_OFF ) { |
| 2000 |
$query['debug'] = strval( $debug ); |
| 2001 |
} |
| 2002 |
if ( $user !== null ) { |
| 2003 |
$query['user'] = $user; |
| 2004 |
} |
| 2005 |
if ( $version !== null ) { |
| 2006 |
$query['version'] = $version; |
| 2007 |
} |
| 2008 |
if ( $only !== null ) { |
| 2009 |
$query['only'] = $only; |
| 2010 |
} |
| 2011 |
if ( $printable ) { |
| 2012 |
$query['printable'] = 1; |
| 2013 |
} |
| 2014 |
foreach ( $extraQuery as $name => $value ) { |
| 2015 |
$query[$name] = $value; |
| 2016 |
} |
| 2017 |
|
| 2018 |
|
| 2019 |
ksort( $query ); |
| 2020 |
return $query; |
| 2021 |
} |
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
|
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
public static function isValidModuleName( $moduleName ) { |
| 2033 |
$len = strlen( $moduleName ); |
| 2034 |
return $len <= 255 && strcspn( $moduleName, '!,|', 0, $len ) === $len; |
| 2035 |
} |
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
|
| 2046 |
|
| 2047 |
public function getLessCompiler( array $vars = [], array $importDirs = [] ) { |
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
if ( !class_exists( Less_Parser::class ) ) { |
| 2052 |
throw new RuntimeException( 'MediaWiki requires the less.php parser' ); |
| 2053 |
} |
| 2054 |
|
| 2055 |
$importDirs[] = MW_INSTALL_PATH . '/resources/src/mediawiki.less'; |
| 2056 |
|
| 2057 |
$parser = new Less_Parser; |
| 2058 |
$parser->ModifyVars( $vars ); |
| 2059 |
$parser->SetOption( 'relativeUrls', false ); |
| 2060 |
$parser->SetOption( 'math', 'always' ); |
| 2061 |
|
| 2062 |
|
| 2063 |
$formattedImportDirs = array_fill_keys( $importDirs, '' ); |
| 2064 |
|
| 2065 |
|
| 2066 |
$codexDevDir = $this->getConfig()->get( MainConfigNames::CodexDevelopmentDir ); |
| 2067 |
$formattedImportDirs[] = static function ( $path ) use ( $codexDevDir ) { |
| 2068 |
|
| 2069 |
$importMap = [ |
| 2070 |
'@wikimedia/codex-icons/' => $codexDevDir !== null ? |
| 2071 |
"$codexDevDir/packages/codex-icons/dist/" : |
| 2072 |
MW_INSTALL_PATH . '/resources/lib/codex-icons/', |
| 2073 |
'mediawiki.skin.codex/' => $codexDevDir !== null ? |
| 2074 |
"$codexDevDir/packages/codex/dist/" : |
| 2075 |
MW_INSTALL_PATH . '/resources/lib/codex/', |
| 2076 |
'mediawiki.skin.codex-design-tokens/' => $codexDevDir !== null ? |
| 2077 |
"$codexDevDir/packages/codex-design-tokens/dist/" : |
| 2078 |
MW_INSTALL_PATH . '/resources/lib/codex-design-tokens/', |
| 2079 |
'@wikimedia/codex-design-tokens/' => static function ( $unused_path ) { |
| 2080 |
throw new RuntimeException( |
| 2081 |
'Importing from @wikimedia/codex-design-tokens is not supported. ' . |
| 2082 |
"To use the Codex tokens, use `@import 'mediawiki.skin.variables.less';` instead." |
| 2083 |
); |
| 2084 |
} |
| 2085 |
]; |
| 2086 |
foreach ( $importMap as $importPath => $substPath ) { |
| 2087 |
if ( str_starts_with( $path, $importPath ) ) { |
| 2088 |
$restOfPath = substr( $path, strlen( $importPath ) ); |
| 2089 |
if ( is_callable( $substPath ) ) { |
| 2090 |
$resolvedPath = call_user_func( $substPath, $restOfPath ); |
| 2091 |
} else { |
| 2092 |
$filePath = $substPath . $restOfPath; |
| 2093 |
|
| 2094 |
$resolvedPath = null; |
| 2095 |
if ( file_exists( $filePath ) ) { |
| 2096 |
$resolvedPath = $filePath; |
| 2097 |
} elseif ( file_exists( "$filePath.less" ) ) { |
| 2098 |
$resolvedPath = "$filePath.less"; |
| 2099 |
} |
| 2100 |
} |
| 2101 |
|
| 2102 |
if ( $resolvedPath !== null ) { |
| 2103 |
return [ |
| 2104 |
Less_Environment::normalizePath( $resolvedPath ), |
| 2105 |
Less_Environment::normalizePath( dirname( $path ) ) |
| 2106 |
]; |
| 2107 |
} else { |
| 2108 |
break; |
| 2109 |
} |
| 2110 |
} |
| 2111 |
} |
| 2112 |
return [ null, null ]; |
| 2113 |
}; |
| 2114 |
$parser->SetImportDirs( $formattedImportDirs ); |
| 2115 |
|
| 2116 |
return $parser; |
| 2117 |
} |
| 2118 |
|
| 2119 |
|
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
|
| 2129 |
|
| 2130 |
|
| 2131 |
|
| 2132 |
public function expandUrl( string $base, string $url ): string { |
| 2133 |
|
| 2134 |
$isProtoRelative = strpos( $base, '//' ) === 0; |
| 2135 |
if ( $isProtoRelative ) { |
| 2136 |
$base = "https:$base"; |
| 2137 |
} |
| 2138 |
|
| 2139 |
$baseUrl = new Net_URL2( $base ); |
| 2140 |
$ret = $baseUrl->resolve( $url ); |
| 2141 |
if ( $isProtoRelative ) { |
| 2142 |
$ret->setScheme( false ); |
| 2143 |
} |
| 2144 |
return $ret->getURL(); |
| 2145 |
} |
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
|
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
public static function filter( $filter, $data, array $options = [] ) { |
| 2165 |
if ( strpos( $data, self::FILTER_NOMIN ) !== false ) { |
| 2166 |
return $data; |
| 2167 |
} |
| 2168 |
|
| 2169 |
if ( isset( $options['cache'] ) && $options['cache'] === false ) { |
| 2170 |
return self::applyFilter( $filter, $data ) ?? $data; |
| 2171 |
} |
| 2172 |
|
| 2173 |
$statsFactory = MediaWikiServices::getInstance()->getStatsFactory(); |
| 2174 |
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory() |
| 2175 |
->getLocalServerInstance( CACHE_ANYTHING ); |
| 2176 |
|
| 2177 |
$key = $cache->makeGlobalKey( |
| 2178 |
'resourceloader-filter', |
| 2179 |
$filter, |
| 2180 |
self::CACHE_VERSION, |
| 2181 |
md5( $data ) |
| 2182 |
); |
| 2183 |
|
| 2184 |
$status = 'hit'; |
| 2185 |
$incKey = "resourceloader_cache.$filter.$status"; |
| 2186 |
$result = $cache->getWithSetCallback( |
| 2187 |
$key, |
| 2188 |
BagOStuff::TTL_DAY, |
| 2189 |
static function () use ( $filter, $data, &$incKey, &$status ) { |
| 2190 |
$status = 'miss'; |
| 2191 |
$incKey = "resourceloader_cache.$filter.$status"; |
| 2192 |
return self::applyFilter( $filter, $data ); |
| 2193 |
} |
| 2194 |
); |
| 2195 |
$statsFactory->getCounter( 'resourceloader_cache_total' ) |
| 2196 |
->setLabel( 'type', $filter ) |
| 2197 |
->setLabel( 'status', $status ) |
| 2198 |
->copyToStatsdAt( [ $incKey ] ) |
| 2199 |
->increment(); |
| 2200 |
|
| 2201 |
|
| 2202 |
return $result ?? $data; |
| 2203 |
} |
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
private static function applyFilter( $filter, $data ) { |
| 2211 |
$data = trim( $data ); |
| 2212 |
if ( $data ) { |
| 2213 |
try { |
| 2214 |
$data = ( $filter === 'minify-css' ) |
| 2215 |
? CSSMin::minify( $data ) |
| 2216 |
: JavaScriptMinifier::minify( $data ); |
| 2217 |
} catch ( TimeoutException $e ) { |
| 2218 |
throw $e; |
| 2219 |
} catch ( Exception $e ) { |
| 2220 |
MWExceptionHandler::logException( $e ); |
| 2221 |
return null; |
| 2222 |
} |
| 2223 |
} |
| 2224 |
return $data; |
| 2225 |
} |
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
|
| 2232 |
|
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
public static function getUserDefaults( |
| 2239 |
Context $context, |
| 2240 |
HookContainer $hookContainer, |
| 2241 |
UserOptionsLookup $userOptionsLookup |
| 2242 |
): array { |
| 2243 |
$defaultOptions = $userOptionsLookup->getDefaultOptions(); |
| 2244 |
$keysToExclude = []; |
| 2245 |
$hookRunner = new HookRunner( $hookContainer ); |
| 2246 |
$hookRunner->onResourceLoaderExcludeUserOptions( $keysToExclude, $context ); |
| 2247 |
foreach ( $keysToExclude as $excludedKey ) { |
| 2248 |
unset( $defaultOptions[ $excludedKey ] ); |
| 2249 |
} |
| 2250 |
return $defaultOptions; |
| 2251 |
} |
| 2252 |
|
| 2253 |
|
| 2254 |
|
| 2255 |
|
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
|
| 2260 |
|
| 2261 |
public static function getSiteConfigSettings( |
| 2262 |
Context $context, Config $conf |
| 2263 |
): array { |
| 2264 |
$services = MediaWikiServices::getInstance(); |
| 2265 |
|
| 2266 |
|
| 2267 |
|
| 2268 |
$contLang = $services->getContentLanguage(); |
| 2269 |
$namespaceIds = $contLang->getNamespaceIds(); |
| 2270 |
$caseSensitiveNamespaces = []; |
| 2271 |
$nsInfo = $services->getNamespaceInfo(); |
| 2272 |
foreach ( $nsInfo->getCanonicalNamespaces() as $index => $name ) { |
| 2273 |
$namespaceIds[$contLang->lc( $name )] = $index; |
| 2274 |
if ( !$nsInfo->isCapitalized( $index ) ) { |
| 2275 |
$caseSensitiveNamespaces[] = $index; |
| 2276 |
} |
| 2277 |
} |
| 2278 |
|
| 2279 |
$illegalFileChars = $conf->get( MainConfigNames::IllegalFileChars ); |
| 2280 |
|
| 2281 |
|
| 2282 |
$skin = $context->getSkin(); |
| 2283 |
|
| 2284 |
|
| 2285 |
$vars = [ |
| 2286 |
'debug' => $context->getDebug(), |
| 2287 |
'skin' => $skin, |
| 2288 |
'stylepath' => $conf->get( MainConfigNames::StylePath ), |
| 2289 |
'wgArticlePath' => $conf->get( MainConfigNames::ArticlePath ), |
| 2290 |
'wgScriptPath' => $conf->get( MainConfigNames::ScriptPath ), |
| 2291 |
'wgScript' => $conf->get( MainConfigNames::Script ), |
| 2292 |
'wgSearchType' => $conf->get( MainConfigNames::SearchType ), |
| 2293 |
'wgVariantArticlePath' => $conf->get( MainConfigNames::VariantArticlePath ), |
| 2294 |
'wgServer' => $conf->get( MainConfigNames::Server ), |
| 2295 |
'wgServerName' => $conf->get( MainConfigNames::ServerName ), |
| 2296 |
'wgUserLanguage' => $context->getLanguage(), |
| 2297 |
'wgContentLanguage' => $contLang->getCode(), |
| 2298 |
'wgVersion' => MW_VERSION, |
| 2299 |
'wgFormattedNamespaces' => $contLang->getFormattedNamespaces(), |
| 2300 |
'wgNamespaceIds' => $namespaceIds, |
| 2301 |
'wgContentNamespaces' => $nsInfo->getContentNamespaces(), |
| 2302 |
'wgSiteName' => $conf->get( MainConfigNames::Sitename ), |
| 2303 |
'wgDBname' => $conf->get( MainConfigNames::DBname ), |
| 2304 |
'wgWikiID' => WikiMap::getCurrentWikiId(), |
| 2305 |
'wgCaseSensitiveNamespaces' => $caseSensitiveNamespaces, |
| 2306 |
'wgCommentCodePointLimit' => CommentStore::COMMENT_CHARACTER_LIMIT, |
| 2307 |
'wgExtensionAssetsPath' => $conf->get( MainConfigNames::ExtensionAssetsPath ), |
| 2308 |
]; |
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
$vars += [ |
| 2313 |
|
| 2314 |
'wgUrlProtocols' => $services->getUrlUtils()->validProtocols(), |
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
'wgActionPaths' => (object)$conf->get( MainConfigNames::ActionPaths ), |
| 2319 |
|
| 2320 |
'wgTranslateNumerals' => $conf->get( MainConfigNames::TranslateNumerals ), |
| 2321 |
|
| 2322 |
'wgExtraSignatureNamespaces' => $conf->get( MainConfigNames::ExtraSignatureNamespaces ), |
| 2323 |
'wgLegalTitleChars' => Title::convertByteClassToUnicodeClass( Title::legalChars() ), |
| 2324 |
'wgIllegalFileChars' => Title::convertByteClassToUnicodeClass( $illegalFileChars ), |
| 2325 |
]; |
| 2326 |
|
| 2327 |
( new HookRunner( $services->getHookContainer() ) ) |
| 2328 |
->onResourceLoaderGetConfigVars( $vars, $skin, $conf ); |
| 2329 |
|
| 2330 |
return $vars; |
| 2331 |
} |
| 2332 |
|
| 2333 |
|
| 2334 |
|
| 2335 |
|
| 2336 |
|
| 2337 |
public function getErrors() { |
| 2338 |
return $this->errors; |
| 2339 |
} |
| 2340 |
} |