| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\ResourceLoader; |
| 22 |
|
| 23 |
use MediaWiki\Html\Html; |
| 24 |
use Wikimedia\WrappedString; |
| 25 |
use Wikimedia\WrappedStringList; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class ClientHtml { |
| 34 |
|
| 35 |
private $context; |
| 36 |
|
| 37 |
|
| 38 |
private $resourceLoader; |
| 39 |
|
| 40 |
|
| 41 |
private $options; |
| 42 |
|
| 43 |
|
| 44 |
private $config = []; |
| 45 |
|
| 46 |
|
| 47 |
private $modules = []; |
| 48 |
|
| 49 |
|
| 50 |
private $moduleStyles = []; |
| 51 |
|
| 52 |
|
| 53 |
private $exemptStates = []; |
| 54 |
|
| 55 |
|
| 56 |
private $data; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function __construct( Context $context, array $options = [] ) { |
| 67 |
$this->context = $context; |
| 68 |
$this->resourceLoader = $context->getResourceLoader(); |
| 69 |
$this->options = $options + [ |
| 70 |
'target' => null, |
| 71 |
'safemode' => null, |
| 72 |
'clientPrefEnabled' => false, |
| 73 |
'clientPrefCookiePrefix' => '', |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function setConfig( array $vars ): void { |
| 83 |
foreach ( $vars as $key => $value ) { |
| 84 |
$this->config[$key] = $value; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function setModules( array $modules ): void { |
| 94 |
$this->modules = $modules; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function setModuleStyles( array $modules ): void { |
| 103 |
$this->moduleStyles = $modules; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function setExemptStates( array $states ): void { |
| 114 |
$this->exemptStates = $states; |
| 115 |
} |
| 116 |
|
| 117 |
private function getData(): array { |
| 118 |
if ( $this->data ) { |
| 119 |
return $this->data; |
| 120 |
} |
| 121 |
|
| 122 |
$rl = $this->resourceLoader; |
| 123 |
$data = [ |
| 124 |
'states' => [ |
| 125 |
|
| 126 |
], |
| 127 |
'general' => [], |
| 128 |
'styles' => [], |
| 129 |
|
| 130 |
'embed' => [ |
| 131 |
'styles' => [], |
| 132 |
'general' => [], |
| 133 |
], |
| 134 |
|
| 135 |
'styleDeprecations' => [], |
| 136 |
]; |
| 137 |
|
| 138 |
foreach ( $this->modules as $name ) { |
| 139 |
$module = $rl->getModule( $name ); |
| 140 |
if ( !$module ) { |
| 141 |
continue; |
| 142 |
} |
| 143 |
|
| 144 |
$group = $module->getGroup(); |
| 145 |
$context = $this->getContext( $group, Module::TYPE_COMBINED ); |
| 146 |
$shouldEmbed = $module->shouldEmbedModule( $this->context ); |
| 147 |
|
| 148 |
if ( ( $group === Module::GROUP_USER || $shouldEmbed ) && |
| 149 |
$module->isKnownEmpty( $context ) ) { |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
$data['states'][$name] = 'ready'; |
| 155 |
} elseif ( $group === Module::GROUP_USER || $shouldEmbed ) { |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
$data['embed']['general'][] = $name; |
| 162 |
|
| 163 |
$data['states'][$name] = 'loading'; |
| 164 |
} else { |
| 165 |
|
| 166 |
$data['general'][] = $name; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
foreach ( $this->moduleStyles as $name ) { |
| 171 |
$module = $rl->getModule( $name ); |
| 172 |
if ( !$module ) { |
| 173 |
continue; |
| 174 |
} |
| 175 |
|
| 176 |
if ( $module->getType() !== Module::LOAD_STYLES ) { |
| 177 |
$logger = $rl->getLogger(); |
| 178 |
$logger->error( 'Unexpected general module "{module}" in styles queue.', [ |
| 179 |
'module' => $name, |
| 180 |
] ); |
| 181 |
continue; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
$group = $module->getGroup(); |
| 191 |
if ( $group !== Module::GROUP_NOSCRIPT ) { |
| 192 |
$data['states'][$name] = 'ready'; |
| 193 |
} |
| 194 |
|
| 195 |
$context = $this->getContext( $group, Module::TYPE_STYLES ); |
| 196 |
if ( $module->shouldEmbedModule( $this->context ) ) { |
| 197 |
|
| 198 |
|
| 199 |
if ( !$module->isKnownEmpty( $context ) ) { |
| 200 |
|
| 201 |
$data['embed']['styles'][] = $name; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
} elseif ( $group !== Module::GROUP_USER || !$module->isKnownEmpty( $context ) ) { |
| 218 |
|
| 219 |
$data['styles'][] = $name; |
| 220 |
} |
| 221 |
$deprecation = $module->getDeprecationWarning(); |
| 222 |
if ( $deprecation ) { |
| 223 |
$data['styleDeprecations'][] = $deprecation; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
$this->data = $data; |
| 228 |
|
| 229 |
return $this->data; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public function getDocumentAttributes() { |
| 236 |
return [ 'class' => 'client-nojs' ]; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
private function getDocumentClassNameScript( $nojsClass ) { |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
$nojsClass ??= $this->getDocumentAttributes()['class']; |
| 258 |
$jsClass = preg_replace( '/(^|\s)client-nojs(\s|$)/', '$1client-js$2', $nojsClass ); |
| 259 |
$jsClassJson = $this->context->encodeJson( $jsClass ); |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
if ( $this->options['clientPrefEnabled'] ) { |
| 264 |
$cookiePrefix = $this->options['clientPrefCookiePrefix']; |
| 265 |
$script = strtr( |
| 266 |
file_get_contents( MW_INSTALL_PATH . '/resources/src/startup/clientprefs.js' ), |
| 267 |
[ |
| 268 |
'$VARS.jsClass' => $jsClassJson, |
| 269 |
'__COOKIE_PREFIX__' => $cookiePrefix, |
| 270 |
] |
| 271 |
); |
| 272 |
} else { |
| 273 |
$script = "document.documentElement.className = {$jsClassJson};"; |
| 274 |
} |
| 275 |
|
| 276 |
return $script; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public function getHeadHtml( $nojsClass = null ) { |
| 295 |
$script = $this->getDocumentClassNameScript( $nojsClass ); |
| 296 |
|
| 297 |
|
| 298 |
if ( $this->config ) { |
| 299 |
$confJson = $this->context->encodeJson( $this->config ); |
| 300 |
$script .= " |
| 301 |
RLCONF = {$confJson}; |
| 302 |
"; |
| 303 |
} |
| 304 |
|
| 305 |
$data = $this->getData(); |
| 306 |
|
| 307 |
|
| 308 |
$states = array_merge( $this->exemptStates, $data['states'] ); |
| 309 |
if ( $states ) { |
| 310 |
$stateJson = $this->context->encodeJson( $states ); |
| 311 |
$script .= " |
| 312 |
RLSTATE = {$stateJson}; |
| 313 |
"; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
if ( $data['general'] ) { |
| 318 |
$pageModulesJson = $this->context->encodeJson( $data['general'] ); |
| 319 |
$script .= " |
| 320 |
RLPAGEMODULES = {$pageModulesJson}; |
| 321 |
"; |
| 322 |
} |
| 323 |
|
| 324 |
if ( !$this->context->getDebug() ) { |
| 325 |
$script = ResourceLoader::filter( 'minify-js', $script, [ 'cache' => false ] ); |
| 326 |
} |
| 327 |
|
| 328 |
$chunks = []; |
| 329 |
$chunks[] = Html::inlineScript( $script ); |
| 330 |
|
| 331 |
|
| 332 |
if ( $data['embed']['general'] ) { |
| 333 |
$chunks[] = $this->getLoad( |
| 334 |
$data['embed']['general'], |
| 335 |
Module::TYPE_COMBINED |
| 336 |
); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
if ( $data['styles'] ) { |
| 341 |
$chunks[] = $this->getLoad( |
| 342 |
$data['styles'], |
| 343 |
Module::TYPE_STYLES |
| 344 |
); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
if ( $data['embed']['styles'] ) { |
| 349 |
$chunks[] = $this->getLoad( |
| 350 |
$data['embed']['styles'], |
| 351 |
Module::TYPE_STYLES |
| 352 |
); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
$startupQuery = [ 'raw' => '1' ]; |
| 358 |
foreach ( [ 'target', 'safemode' ] as $param ) { |
| 359 |
if ( $this->options[$param] !== null ) { |
| 360 |
$startupQuery[$param] = (string)$this->options[$param]; |
| 361 |
} |
| 362 |
} |
| 363 |
$chunks[] = $this->getLoad( |
| 364 |
'startup', |
| 365 |
Module::TYPE_SCRIPTS, |
| 366 |
$startupQuery |
| 367 |
); |
| 368 |
|
| 369 |
return WrappedString::join( "\n", $chunks ); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public function getBodyHtml() { |
| 376 |
$data = $this->getData(); |
| 377 |
$chunks = []; |
| 378 |
|
| 379 |
|
| 380 |
if ( $data['styleDeprecations'] ) { |
| 381 |
$calls = ''; |
| 382 |
foreach ( $data['styleDeprecations'] as $warning ) { |
| 383 |
$calls .= Html::encodeJsCall( 'mw.log.warn', [ $warning ] ); |
| 384 |
} |
| 385 |
$chunks[] = ResourceLoader::makeInlineScript( $calls ); |
| 386 |
} |
| 387 |
|
| 388 |
return WrappedString::join( "\n", $chunks ); |
| 389 |
} |
| 390 |
|
| 391 |
private function getContext( $group, $type ): Context { |
| 392 |
return self::makeContext( $this->context, $group, $type ); |
| 393 |
} |
| 394 |
|
| 395 |
private function getLoad( $modules, $only, array $extraQuery = [] ) { |
| 396 |
return self::makeLoad( $this->context, (array)$modules, $only, $extraQuery ); |
| 397 |
} |
| 398 |
|
| 399 |
private static function makeContext( Context $mainContext, $group, $type, |
| 400 |
array $extraQuery = [] |
| 401 |
): DerivativeContext { |
| 402 |
|
| 403 |
$ret = new DerivativeContext( $mainContext ); |
| 404 |
|
| 405 |
$ret->setOnly( $type === Module::TYPE_COMBINED ? null : $type ); |
| 406 |
|
| 407 |
if ( $group !== Module::GROUP_USER && $group !== Module::GROUP_PRIVATE ) { |
| 408 |
$ret->setUser( null ); |
| 409 |
} |
| 410 |
if ( isset( $extraQuery['raw'] ) ) { |
| 411 |
$ret->setRaw( true ); |
| 412 |
} |
| 413 |
return $ret; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public static function makeLoad( Context $mainContext, array $modules, $only, |
| 426 |
array $extraQuery = [] |
| 427 |
) { |
| 428 |
$rl = $mainContext->getResourceLoader(); |
| 429 |
$chunks = []; |
| 430 |
|
| 431 |
|
| 432 |
sort( $modules ); |
| 433 |
|
| 434 |
if ( $mainContext->getDebug() && count( $modules ) > 1 ) { |
| 435 |
|
| 436 |
foreach ( $modules as $name ) { |
| 437 |
$chunks[] = self::makeLoad( $mainContext, [ $name ], $only, $extraQuery ); |
| 438 |
} |
| 439 |
return new WrappedStringList( "\n", $chunks ); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
$sortedModules = []; |
| 444 |
foreach ( $modules as $name ) { |
| 445 |
$module = $rl->getModule( $name ); |
| 446 |
if ( !$module ) { |
| 447 |
$rl->getLogger()->warning( 'Unknown module "{module}"', [ 'module' => $name ] ); |
| 448 |
continue; |
| 449 |
} |
| 450 |
$sortedModules[$module->getSource()][$module->getGroup() ?? ''][$name] = $module; |
| 451 |
} |
| 452 |
|
| 453 |
foreach ( $sortedModules as $source => $groups ) { |
| 454 |
foreach ( $groups as $group => $grpModules ) { |
| 455 |
$context = self::makeContext( $mainContext, $group, $only, $extraQuery ); |
| 456 |
|
| 457 |
|
| 458 |
$moduleSets = []; |
| 459 |
$idx = -1; |
| 460 |
foreach ( $grpModules as $name => $module ) { |
| 461 |
$shouldEmbed = $module->shouldEmbedModule( $context ); |
| 462 |
if ( !$moduleSets || $moduleSets[$idx][0] !== $shouldEmbed ) { |
| 463 |
$moduleSets[++$idx] = [ $shouldEmbed, [] ]; |
| 464 |
} |
| 465 |
$moduleSets[$idx][1][$name] = $module; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
foreach ( $moduleSets as [ $embed, $moduleSet ] ) { |
| 470 |
$moduleSetNames = array_keys( $moduleSet ); |
| 471 |
$context->setModules( $moduleSetNames ); |
| 472 |
if ( $embed ) { |
| 473 |
$response = $rl->makeModuleResponse( $context, $moduleSet ); |
| 474 |
|
| 475 |
if ( $only == Module::TYPE_STYLES ) { |
| 476 |
$chunks[] = Html::inlineStyle( $response ); |
| 477 |
} else { |
| 478 |
$chunks[] = ResourceLoader::makeInlineScript( $response ); |
| 479 |
} |
| 480 |
} else { |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
if ( $group === Module::GROUP_USER ) { |
| 489 |
$context->setVersion( $rl->makeVersionQuery( $context, $moduleSetNames ) ); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
$url = $rl->createLoaderURL( $source, $context, $extraQuery ); |
| 494 |
|
| 495 |
|
| 496 |
if ( $only === Module::TYPE_STYLES ) { |
| 497 |
$chunk = Html::linkedStyle( $url ); |
| 498 |
} elseif ( $context->getRaw() ) { |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
$chunk = Html::element( 'script', [ |
| 504 |
'async' => true, |
| 505 |
'src' => $url, |
| 506 |
] ); |
| 507 |
} else { |
| 508 |
$chunk = ResourceLoader::makeInlineScript( |
| 509 |
'mw.loader.load(' . $mainContext->encodeJson( $url ) . ');' |
| 510 |
); |
| 511 |
} |
| 512 |
|
| 513 |
if ( $group == Module::GROUP_NOSCRIPT ) { |
| 514 |
$chunks[] = Html::rawElement( 'noscript', [], $chunk ); |
| 515 |
} else { |
| 516 |
$chunks[] = $chunk; |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
return new WrappedStringList( "\n", $chunks ); |
| 524 |
} |
| 525 |
} |