| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\ResourceLoader; |
| 23 |
|
| 24 |
use DomainException; |
| 25 |
use Exception; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use Wikimedia\RequestTimeout\TimeoutException; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class StartUpModule extends Module { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private const STORAGE_VERSION = '2'; |
| 52 |
|
| 53 |
|
| 54 |
private $groupIds = [ |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
self::GROUP_USER => 0, |
| 60 |
self::GROUP_PRIVATE => 1, |
| 61 |
]; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
protected static function getImplicitDependencies( |
| 73 |
array $registryData, |
| 74 |
string $moduleName, |
| 75 |
array $handled = [] |
| 76 |
): array { |
| 77 |
static $dependencyCache = []; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
if ( !isset( $dependencyCache[$moduleName] ) ) { |
| 82 |
if ( !isset( $registryData[$moduleName] ) ) { |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
$dependencyCache[$moduleName] = []; |
| 87 |
return []; |
| 88 |
} |
| 89 |
|
| 90 |
$data = $registryData[$moduleName]; |
| 91 |
$flat = $data['dependencies']; |
| 92 |
|
| 93 |
|
| 94 |
$handled[] = $moduleName; |
| 95 |
foreach ( $data['dependencies'] as $dependency ) { |
| 96 |
if ( in_array( $dependency, $handled, true ) ) { |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
throw new CircularDependencyError(); |
| 103 |
} |
| 104 |
|
| 105 |
$flat = array_merge( |
| 106 |
$flat, |
| 107 |
self::getImplicitDependencies( $registryData, $dependency, $handled ) |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
$dependencyCache[$moduleName] = $flat; |
| 112 |
} |
| 113 |
|
| 114 |
return $dependencyCache[$moduleName]; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public static function compileUnresolvedDependencies( array &$registryData ): void { |
| 137 |
foreach ( $registryData as &$data ) { |
| 138 |
$dependencies = $data['dependencies']; |
| 139 |
try { |
| 140 |
foreach ( $data['dependencies'] as $dependency ) { |
| 141 |
$implicitDependencies = self::getImplicitDependencies( $registryData, $dependency ); |
| 142 |
$dependencies = array_diff( $dependencies, $implicitDependencies ); |
| 143 |
} |
| 144 |
} catch ( CircularDependencyError $err ) { |
| 145 |
|
| 146 |
$dependencies = $data['dependencies']; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
$data['dependencies'] = array_values( $dependencies ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public function getModuleRegistrations( Context $context ): string { |
| 161 |
$resourceLoader = $context->getResourceLoader(); |
| 162 |
|
| 163 |
|
| 164 |
$safemode = $context->getRequest()->getRawVal( 'safemode' ) === '1'; |
| 165 |
$skin = $context->getSkin(); |
| 166 |
|
| 167 |
$moduleNames = $resourceLoader->getModuleNames(); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
try { |
| 172 |
$resourceLoader->preloadModuleInfo( $moduleNames, $context ); |
| 173 |
} catch ( TimeoutException $e ) { |
| 174 |
throw $e; |
| 175 |
} catch ( Exception $e ) { |
| 176 |
|
| 177 |
|
| 178 |
$resourceLoader->outputErrorAndLog( $e, |
| 179 |
'Preloading module info from startup failed: {exception}', |
| 180 |
[ 'exception' => $e ] |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
$states = []; |
| 186 |
$registryData = []; |
| 187 |
foreach ( $moduleNames as $name ) { |
| 188 |
$module = $resourceLoader->getModule( $name ); |
| 189 |
$moduleSkins = $module->getSkins(); |
| 190 |
if ( |
| 191 |
( $safemode && $module->getOrigin() > Module::ORIGIN_CORE_INDIVIDUAL ) |
| 192 |
|| ( $moduleSkins !== null && !in_array( $skin, $moduleSkins ) ) |
| 193 |
) { |
| 194 |
continue; |
| 195 |
} |
| 196 |
|
| 197 |
if ( $module instanceof StartUpModule ) { |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
continue; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
if ( $module->getGroup() === self::GROUP_NOSCRIPT ) { |
| 215 |
continue; |
| 216 |
} |
| 217 |
|
| 218 |
try { |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
$versionHash = $module->getVersionHash( $context ); |
| 223 |
} catch ( TimeoutException $e ) { |
| 224 |
throw $e; |
| 225 |
} catch ( Exception $e ) { |
| 226 |
|
| 227 |
|
| 228 |
$resourceLoader->outputErrorAndLog( $e, |
| 229 |
'Calculating version for "{module}" failed: {exception}', |
| 230 |
[ |
| 231 |
'module' => $name, |
| 232 |
'exception' => $e, |
| 233 |
] |
| 234 |
); |
| 235 |
$versionHash = ''; |
| 236 |
$states[$name] = 'error'; |
| 237 |
} |
| 238 |
|
| 239 |
$skipFunction = $module->getSkipFunction(); |
| 240 |
if ( $skipFunction !== null && !$context->getDebug() ) { |
| 241 |
$skipFunction = ResourceLoader::filter( 'minify-js', $skipFunction ); |
| 242 |
} |
| 243 |
|
| 244 |
$registryData[$name] = [ |
| 245 |
'version' => $versionHash, |
| 246 |
'dependencies' => $module->getDependencies( $context ), |
| 247 |
'group' => $this->getGroupId( $module->getGroup() ), |
| 248 |
'source' => $module->getSource(), |
| 249 |
'skip' => $skipFunction, |
| 250 |
]; |
| 251 |
} |
| 252 |
|
| 253 |
self::compileUnresolvedDependencies( $registryData ); |
| 254 |
|
| 255 |
|
| 256 |
$sources = $oldSources = $resourceLoader->getSources(); |
| 257 |
$this->getHookRunner()->onResourceLoaderModifyEmbeddedSourceUrls( $sources ); |
| 258 |
if ( array_keys( $sources ) !== array_keys( $oldSources ) ) { |
| 259 |
throw new DomainException( 'ResourceLoaderModifyEmbeddedSourceUrls hook must not add or remove sources' ); |
| 260 |
} |
| 261 |
$out = ResourceLoader::makeLoaderSourcesScript( $context, $sources ); |
| 262 |
|
| 263 |
|
| 264 |
$registrations = []; |
| 265 |
foreach ( $registryData as $name => $data ) { |
| 266 |
|
| 267 |
$registrations[] = [ |
| 268 |
$name, |
| 269 |
$data['version'], |
| 270 |
$data['dependencies'], |
| 271 |
$data['group'], |
| 272 |
|
| 273 |
$data['source'] === 'local' ? null : $data['source'], |
| 274 |
$data['skip'] |
| 275 |
]; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
$out .= "\n" . ResourceLoader::makeLoaderRegisterScript( $context, $registrations ); |
| 280 |
|
| 281 |
if ( $states ) { |
| 282 |
$out .= "\n" . ResourceLoader::makeLoaderStateScript( $context, $states ); |
| 283 |
} |
| 284 |
|
| 285 |
return $out; |
| 286 |
} |
| 287 |
|
| 288 |
private function getGroupId( $groupName ): ?int { |
| 289 |
if ( $groupName === null ) { |
| 290 |
return null; |
| 291 |
} |
| 292 |
|
| 293 |
if ( !array_key_exists( $groupName, $this->groupIds ) ) { |
| 294 |
$this->groupIds[$groupName] = count( $this->groupIds ); |
| 295 |
} |
| 296 |
|
| 297 |
return $this->groupIds[$groupName]; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
private function getBaseModules(): array { |
| 306 |
return [ 'jquery', 'mediawiki.base' ]; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
private function getStoreKey(): string { |
| 316 |
return 'MediaWikiModuleStore:' . $this->getConfig()->get( MainConfigNames::DBname ); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
private function getMaxQueryLength(): int { |
| 324 |
$len = $this->getConfig()->get( MainConfigNames::ResourceLoaderMaxQueryLength ); |
| 325 |
|
| 326 |
|
| 327 |
if ( $len === false || $len < 0 ) { |
| 328 |
|
| 329 |
$len = 2000; |
| 330 |
} |
| 331 |
return $len; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
private function getStoreVary( Context $context ): string { |
| 341 |
return implode( ':', [ |
| 342 |
$context->getSkin(), |
| 343 |
self::STORAGE_VERSION, |
| 344 |
$this->getConfig()->get( MainConfigNames::ResourceLoaderStorageVersion ), |
| 345 |
$context->getLanguage(), |
| 346 |
] ); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public function getScript( Context $context ) { |
| 354 |
global $IP; |
| 355 |
$conf = $this->getConfig(); |
| 356 |
|
| 357 |
if ( $context->getOnly() !== 'scripts' ) { |
| 358 |
return '/* Requires only=scripts */'; |
| 359 |
} |
| 360 |
|
| 361 |
$enableJsProfiler = $conf->get( MainConfigNames::ResourceLoaderEnableJSProfiler ); |
| 362 |
|
| 363 |
$startupCode = file_get_contents( "$IP/resources/src/startup/startup.js" ); |
| 364 |
|
| 365 |
$mwLoaderCode = file_get_contents( "$IP/resources/src/startup/mediawiki.js" ) . |
| 366 |
file_get_contents( "$IP/resources/src/startup/mediawiki.loader.js" ) . |
| 367 |
file_get_contents( "$IP/resources/src/startup/mediawiki.requestIdleCallback.js" ); |
| 368 |
if ( $conf->get( MainConfigNames::ResourceLoaderEnableJSProfiler ) ) { |
| 369 |
$mwLoaderCode .= file_get_contents( "$IP/resources/src/startup/profiler.js" ); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
$mwLoaderPairs = [ |
| 374 |
|
| 375 |
|
| 376 |
'$VARS.reqBase' => $context->encodeJson( (object)$context->getReqBase() ), |
| 377 |
'$VARS.baseModules' => $context->encodeJson( $this->getBaseModules() ), |
| 378 |
'$VARS.maxQueryLength' => $context->encodeJson( |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
( !$context->getDebug() || $context->getDebug() === $context::DEBUG_LEGACY ) ? |
| 384 |
$this->getMaxQueryLength() : |
| 385 |
0 |
| 386 |
), |
| 387 |
'$VARS.storeEnabled' => $context->encodeJson( |
| 388 |
$conf->get( MainConfigNames::ResourceLoaderStorageEnabled ) |
| 389 |
&& !$context->getDebug() |
| 390 |
&& $context->getRequest()->getRawVal( 'safemode' ) !== '1' |
| 391 |
), |
| 392 |
'$VARS.storeKey' => $context->encodeJson( $this->getStoreKey() ), |
| 393 |
'$VARS.storeVary' => $context->encodeJson( $this->getStoreVary( $context ) ), |
| 394 |
'$VARS.groupUser' => $context->encodeJson( $this->getGroupId( self::GROUP_USER ) ), |
| 395 |
'$VARS.groupPrivate' => $context->encodeJson( $this->getGroupId( self::GROUP_PRIVATE ) ), |
| 396 |
'$VARS.sourceMapLinks' => $context->encodeJson( |
| 397 |
$conf->get( MainConfigNames::ResourceLoaderEnableSourceMapLinks ) |
| 398 |
), |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
'$CODE.profileExecuteStart();' => $enableJsProfiler |
| 403 |
? 'mw.loader.profiler.onExecuteStart( module );' |
| 404 |
: '', |
| 405 |
'$CODE.profileExecuteEnd();' => $enableJsProfiler |
| 406 |
? 'mw.loader.profiler.onExecuteEnd( module );' |
| 407 |
: '', |
| 408 |
'$CODE.profileScriptStart();' => $enableJsProfiler |
| 409 |
? 'mw.loader.profiler.onScriptStart( module );' |
| 410 |
: '', |
| 411 |
'$CODE.profileScriptEnd();' => $enableJsProfiler |
| 412 |
? 'mw.loader.profiler.onScriptEnd( module );' |
| 413 |
: '', |
| 414 |
|
| 415 |
|
| 416 |
'$CODE.consoleLog();' => $context->getDebug() |
| 417 |
? 'console.log.apply( console, arguments );' |
| 418 |
: '', |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
'$CODE.undefineQUnit();' => !$conf->get( MainConfigNames::EnableJavaScriptTest ) |
| 423 |
? 'window.QUnit = undefined;' |
| 424 |
: '', |
| 425 |
]; |
| 426 |
$mwLoaderCode = strtr( $mwLoaderCode, $mwLoaderPairs ); |
| 427 |
|
| 428 |
|
| 429 |
$pairs = [ |
| 430 |
|
| 431 |
'$CODE.registrations();' => trim( $this->getModuleRegistrations( $context ) ), |
| 432 |
'$CODE.defineLoader();' => $mwLoaderCode, |
| 433 |
]; |
| 434 |
$startupCode = strtr( $startupCode, $pairs ); |
| 435 |
|
| 436 |
return [ |
| 437 |
'plainScripts' => [ |
| 438 |
[ |
| 439 |
'virtualFilePath' => new FilePath( |
| 440 |
'resources/src/startup/startup.js', |
| 441 |
MW_INSTALL_PATH, |
| 442 |
$conf->get( MainConfigNames::ResourceBasePath ) |
| 443 |
), |
| 444 |
'content' => $startupCode, |
| 445 |
], |
| 446 |
], |
| 447 |
]; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public function supportsURLLoading(): bool { |
| 454 |
return false; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
public function enableModuleContentVersion(): bool { |
| 461 |
|
| 462 |
|
| 463 |
return true; |
| 464 |
} |
| 465 |
} |