resourceloader: Embed 'mediawiki' directly in startup response

Embed the essential files to define mw.loader directly as part of
the startup module.

* This means the internal 'mediawiki' module no longer exists.
  This is safe to remove because:
  1) While registered server-side for loading from startup.js, a PHPUnit
     structure test disallowed being specified as a dependency.
  2) Anything that attempted to load it client-side failed because the
     module was marked in the registry as 'raw', thereby excluding it
     from the data sent to the client-side. As such, it was seen as an
     unknown module that the client refused to fetch from the server.

* Deprecate getStartupModules() and getLegacyModules().
  These are no longer needed. There are no known callers anywhere in
  Wikimedia Git or elsewhere indexed by Codesearch, but easy enough
  to leave as no-op for one release.

* Remove ResourceLoaderRawFileModule class.
  No longer needed. Was created as a hack specifically for the 'mediawiki'
  module so that it would not leak global variables in debug mode.
  It has no usage anywhere in Wikimedia Git, nor elsewhere in Codesearch.
  Remove without deprecation given this was meant to be a 'private' class.

* Introduce (private) getBaseModules(). Previously, this list only existed
  locally in getStartupModulesUrl() by merging getStartupModules() and
  getLegacyModules(). This value was factored out into its own method.

* Make getStartupModulesUrl() private and rename to getBaseModulesUrl().
  It is only used internally to export the 'baseModulesUri' value.
  Its name was already confusing before, but it would've been even more
  confusing now given it doesn't even call getStartupModules() any more.

Bug: T192623
Change-Id: I14ba282d7b65e99ca54b7c2f77ba6e1adaddd11c
This commit is contained in:
Timo Tijhof 2018-06-15 21:20:14 +01:00 committed by Krinkle
parent f90a2c2a76
commit b7b84d55d4
12 changed files with 60 additions and 90 deletions

View file

@ -219,6 +219,8 @@ because of Phabricator reports.
context title is unset is now deprecated; anything creating an EditPage
instance should set the context title via ::setContextTitle().
* The 'jquery.hidpi' module (polyfill for IMG srcset) is deprecated.
* ResourceLoaderStartUpModule::getStartupModules() and ::getLegacyModules()
are deprecated. These concepts are obsolete and have no replacement.
=== Other changes in 1.32 ===
* …

View file

@ -1232,7 +1232,6 @@ $wgAutoloadLocalClasses = [
'ResourceLoaderOOUIFileModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderOOUIFileModule.php',
'ResourceLoaderOOUIImageModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderOOUIImageModule.php',
'ResourceLoaderOOUIModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderOOUIModule.php',
'ResourceLoaderRawFileModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderRawFileModule.php',
'ResourceLoaderSiteModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderSiteModule.php',
'ResourceLoaderSiteStylesModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderSiteStylesModule.php',
'ResourceLoaderSkinModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderSkinModule.php',

View file

@ -1,52 +0,0 @@
<?php
/**
* Module containing files that are loaded without ResourceLoader.
*
* Primary usecase being "base" modules loaded by the startup module,
* such as jquery and the mw.loader client itself. These make use of
* ResourceLoaderModule and load.php for convenience but aren't actually
* registered in the startup module (as it would have to load itself).
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @author Timo Tijhof
*/
class ResourceLoaderRawFileModule extends ResourceLoaderFileModule {
/**
* Enable raw mode to omit mw.loader.state() call as mw.loader
* does not yet exist when these modules execute.
* @var bool
*/
protected $raw = true;
/**
* Get all JavaScript code.
*
* @param ResourceLoaderContext $context
* @return string JavaScript code
*/
public function getScript( ResourceLoaderContext $context ) {
$script = parent::getScript( $context );
// Add closure explicitly because raw modules can't be wrapped mw.loader.implement.
// Unlike with mw.loader.implement, this closure is immediately invoked.
// @see ResourceLoader::makeModuleResponse
// @see ResourceLoader::makeLoaderImplementScript
return "(function () {\n{$script}\n}());";
}
}

View file

@ -324,48 +324,64 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
* @return array
*/
public function getPreloadLinks( ResourceLoaderContext $context ) {
$url = self::getStartupModulesUrl( $context );
$url = $this->getBaseModulesUrl( $context );
return [
$url => [ 'as' => 'script' ]
];
}
/**
* Base modules required for the base environment of ResourceLoader
* Internal modules used by ResourceLoader that cannot be depended on.
*
* These module(s) should have isRaw() return true, and are not
* legal dependencies (enforced by structure/ResourcesTest).
*
* @deprecated since 1.32 No longer used.
* @return array
*/
public static function getStartupModules() {
return [ 'jquery', 'mediawiki', 'mediawiki.base' ];
wfDeprecated( __METHOD__, '1.32' );
return [];
}
/**
* @deprecated since 1.32 No longer used.
* @return array
*/
public static function getLegacyModules() {
wfDeprecated( __METHOD__, '1.32' );
return [];
}
/**
* Base modules implicitly available to all modules.
*
* @since 1.32
* @return array
*/
private function getBaseModules() {
global $wgIncludeLegacyJavaScript;
$legacyModules = [];
$baseModules = [ 'jquery', 'mediawiki.base' ];
if ( $wgIncludeLegacyJavaScript ) {
$legacyModules[] = 'mediawiki.legacy.wikibits';
$baseModules[] = 'mediawiki.legacy.wikibits';
}
return $legacyModules;
return $baseModules;
}
/**
* Get the load URL of the startup modules.
*
* This is a helper for getScript(), but can also be called standalone, such
* as when generating an AppCache manifest.
* This is a helper for getScript().
*
* @param ResourceLoaderContext $context
* @return string
*/
public static function getStartupModulesUrl( ResourceLoaderContext $context ) {
private function getBaseModulesUrl( ResourceLoaderContext $context ) {
$rl = $context->getResourceLoader();
$derivative = new DerivativeResourceLoaderContext( $context );
$derivative->setModules( array_merge(
self::getStartupModules(),
self::getLegacyModules()
) );
$derivative->setModules( $this->getBaseModules() );
$derivative->setOnly( 'scripts' );
// Must setModules() before makeVersionQuery()
$derivative->setVersion( $rl->makeVersionQuery( $derivative ) );
@ -383,7 +399,15 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
return '/* Requires only=script */';
}
$out = file_get_contents( "$IP/resources/src/startup.js" );
$out = file_get_contents( "$IP/resources/src/startup/startup.js" );
// Keep in sync with maintenance/jsduck/eg-iframe.html and,
// keep in sync with 'fileHashes' in StartUpModule::getDefinitionSummary().
$mwLoaderCode = file_get_contents( "$IP/resources/src/startup/mediawiki.js" ) .
file_get_contents( "$IP/resources/src/startup/mediawiki.requestIdleCallback.js" );
if ( $context->getDebug() ) {
$mwLoaderCode .= file_get_contents( "$IP/resources/src/startup/mediawiki.log.js" );
}
$pairs = array_map( function ( $value ) {
$value = FormatJson::encode( $value, ResourceLoader::inDebugMode(), FormatJson::ALL_OK );
@ -394,13 +418,14 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
'$VARS.wgLegacyJavaScriptGlobals' => $this->getConfig()->get( 'LegacyJavaScriptGlobals' ),
'$VARS.configuration' => $this->getConfigSettings( $context ),
// This url may be preloaded. See getPreloadLinks().
'$VARS.baseModulesUri' => self::getStartupModulesUrl( $context ),
'$VARS.baseModulesUri' => $this->getBaseModulesUrl( $context ),
] );
$pairs['$CODE.registrations();'] = str_replace(
"\n",
"\n\t",
trim( $this->getModuleRegistrations( $context ) )
);
$pairs['$CODE.defineLoader();'] = $mwLoaderCode;
return strtr( $out, $pairs );
}
@ -430,7 +455,9 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
'moduleHashes' => $this->getAllModuleHashes( $context ),
'fileHashes' => [
$this->safeFileHash( "$IP/resources/src/startup.js" ),
$this->safeFileHash( "$IP/resources/src/startup/startup.js" ),
$this->safeFileHash( "$IP/resources/src/startup/mediawiki.js" ),
$this->safeFileHash( "$IP/resources/src/startup/mediawiki.requestIdleCallback.js" ),
],
];
return $summary;

View file

@ -19,7 +19,7 @@
"resources/src/mediawiki.legacy",
"resources/src/mediawiki.libs.jpegmeta/jpegmeta.js",
"resources/src/mediawiki.skinning",
"resources/src/startup.js"
"resources/src/startup/startup.js"
],
"--": [
"maintenance/jsduck/external.js",

View file

@ -41,8 +41,9 @@
mw.config = new mw.Map();
}
</script>
<script src="modules/src/startup/mediawiki.js"></script>
<script src="modules/src/startup/mediawiki.requestIdleCallback.js"></script>
<script src="modules/lib/jquery/jquery.js"></script>
<script src="modules/src/mediawiki/mediawiki.js"></script>
<script src="modules/src/mediawiki.base/mediawiki.base.js"></script>
<script src="modules/src/mediawiki.base/mediawiki.errorLogger.js"></script>
<script src="modules/lib/oojs/oojs.jquery.js"></script>

View file

@ -843,16 +843,6 @@ return [
/* MediaWiki */
'mediawiki' => [
'class' => ResourceLoaderRawFileModule::class,
// Keep in sync with maintenance/jsduck/eg-iframe.html
'scripts' => [
'resources/src/mediawiki/mediawiki.js',
'resources/src/mediawiki/mediawiki.requestIdleCallback.js',
],
'debugScripts' => 'resources/src/mediawiki/mediawiki.log.js',
'targets' => [ 'desktop', 'mobile' ],
],
'mediawiki.base' => [
// Keep in sync with maintenance/jsduck/eg-iframe.html
'scripts' => [

View file

@ -8,7 +8,7 @@
* @singleton
*/
( function ( $ ) {
( function () {
'use strict';
var mw, StringSet, log,
@ -294,7 +294,7 @@
*/
log.warn = console && console.warn && Function.prototype.bind ?
Function.prototype.bind.call( console.warn, console ) :
$.noop;
function () {};
/**
* Write a message to the browser console's error channel.
@ -305,11 +305,11 @@
* This method is a no-op in browsers that don't implement the Console API.
*
* @since 1.26
* @param {Error|...string} msg Messages to output to console
* @param {...Mixed} msg Messages to output to console
*/
log.error = console && console.error && Function.prototype.bind ?
Function.prototype.bind.call( console.error, console ) :
$.noop;
function () {};
/**
* Create a property on a host object that, when accessed, will produce
@ -2270,4 +2270,4 @@
// Attach to window and globally alias
window.mw = window.mediaWiki = mw;
}( jQuery ) );
}() );

View file

@ -140,6 +140,10 @@ window.isCompatible = function ( str ) {
if ( window.performance && performance.mark ) {
performance.mark( 'mwStartup' );
}
// This embeds mediawiki.js, which defines 'mw' and 'mw.loader'.
$CODE.defineLoader();
script = document.createElement( 'script' );
script.src = $VARS.baseModulesUri;
script.onload = function () {

View file

@ -45,24 +45,23 @@ class ResourcesTest extends MediaWikiTestCase {
}
/**
* Verify that nothing explicitly depends on base modules, or other raw modules.
* Verify that nothing explicitly depends on raw modules (such as "query").
*
* Depending on them is unsupported as they are not registered client-side by the startup module.
*
* TODO Modules can dynamically choose dependencies based on context. This method does not
* @todo Modules can dynamically choose dependencies based on context. This method does not
* test such dependencies. The same goes for testMissingDependencies() and
* testUnsatisfiableDependencies().
*/
public function testIllegalDependencies() {
$data = self::getAllModules();
$illegalDeps = ResourceLoaderStartUpModule::getStartupModules();
$illegalDeps = [];
foreach ( $data['modules'] as $moduleName => $module ) {
if ( $module->isRaw() ) {
$illegalDeps[] = $moduleName;
}
}
$illegalDeps = array_unique( $illegalDeps );
/** @var ResourceLoaderModule $module */
foreach ( $data['modules'] as $moduleName => $module ) {