MediaWiki REL1_33
|
Dynamic JavaScript and CSS resource loading system. More...
Public Member Functions | |
__construct (Config $config=null, LoggerInterface $logger=null) | |
Register core modules and runs registration hooks. | |
addSource ( $id, $loadUrl=null) | |
Add a foreign source of modules. | |
createLoaderURL ( $source, ResourceLoaderContext $context, $extraQuery=[]) | |
Build a load.php URL. | |
getCombinedVersion (ResourceLoaderContext $context, array $moduleNames) | |
Helper method to get and combine versions of multiple modules. | |
getConfig () | |
getLessCompiler ( $vars=[]) | |
Returns LESS compiler set up for use with MediaWiki. | |
getLessVars () | |
Get global LESS variables. | |
getLoadScript ( $source) | |
Get the URL to the load.php endpoint for the given ResourceLoader source. | |
getLogger () | |
getMessageBlobStore () | |
getModule ( $name) | |
Get the ResourceLoaderModule object for a given module name. | |
getModuleNames () | |
Get a list of module names. | |
getModulesByMessage ( $messageKey) | |
Get names of modules that use a certain message. | |
getSources () | |
Get the list of sources. | |
getTestModuleNames ( $framework='all') | |
Get a list of test module names for one (or all) frameworks. | |
isModuleRegistered ( $name) | |
Check whether a ResourceLoader module is registered. | |
makeModuleResponse (ResourceLoaderContext $context, array $modules, array $missing=[]) | |
Generate code for a response. | |
makeVersionQuery (ResourceLoaderContext $context) | |
Get the expected value of the 'version' query parameter. | |
outputErrorAndLog (Exception $e, $msg, array $context=[]) | |
Add an error to the 'errors' array and log it. | |
preloadModuleInfo (array $moduleNames, ResourceLoaderContext $context) | |
Load information stored in the database about modules. | |
register ( $name, $info=null) | |
Register a module with the ResourceLoader system. | |
registerTestModules () | |
respond (ResourceLoaderContext $context) | |
Output a response to a load request, including the content-type header. | |
setLogger (LoggerInterface $logger) | |
setMessageBlobStore (MessageBlobStore $blobStore) | |
Static Public Member Functions | |
static | clearCache () |
Reset static members used for caching. | |
static | encodeJsonForScript ( $data) |
Wrapper around json_encode that avoids needless escapes, and pretty-prints in debug mode. | |
static | expandModuleNames ( $modules) |
Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to an array of module names like ‘[ 'jquery.foo’, 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ]`. | |
static | filter ( $filter, $data, array $options=[]) |
Run JavaScript or CSS data through a filter, caching the filtered result for future calls. | |
static | formatException ( $e) |
Handle exception display. | |
static | inDebugMode () |
Determine whether debug mode was requested Order of priority is 1) request param, 2) cookie, 3) $wg setting. | |
static | isValidModuleName ( $moduleName) |
Check a module name for validity. | |
static | makeCombinedStyles (array $stylePairs) |
Combines an associative array mapping media type to CSS into a single stylesheet with "@media" blocks. | |
static | makeComment ( $text) |
Generate a CSS or JS comment block. | |
static | makeConfigSetScript (array $configuration) |
Returns JS code which will set the MediaWiki configuration array to the given value. | |
static | makeHash ( $value) |
static | makeInlineCodeWithModule ( $modules, $script) |
Wraps JavaScript code to run after a required module. | |
static | makeInlineScript ( $script, $nonce=null) |
Returns an HTML script tag that runs given JS code after startup and base modules. | |
static | makeLoaderConditionalScript ( $script) |
Wraps JavaScript code to run after the startup module. | |
static | makeLoaderQuery ( $modules, $lang, $skin, $user=null, $version=null, $debug=false, $only=null, $printable=false, $handheld=false, $extraQuery=[]) |
Build a query array (array representation of query string) for load.php. | |
static | makeLoaderRegisterScript (array $modules) |
Returns JS code which calls mw.loader.register with the given parameter. | |
static | makeLoaderSourcesScript ( $sources, $loadUrl=null) |
Returns JS code which calls mw.loader.addSource() with the given parameters. | |
static | makeLoaderStateScript ( $states, $state=null) |
Returns a JS call to mw.loader.state, which sets the state of one ore more modules to a given value. | |
static | makeMessageSetScript ( $messages) |
Returns JS code which, when called, will register a given list of messages. | |
static | makePackedModulesString ( $modules) |
Convert an array of module names to a packed query string. | |
Protected Member Functions | |
isFileModule ( $name) | |
Whether the module is a ResourceLoaderFileModule (including subclasses). | |
measureResponseTime (Timing $timing) | |
sendResponseHeaders (ResourceLoaderContext $context, $etag, $errors, array $extra=[]) | |
Send main response headers to the client. | |
tryRespondFromFileCache (ResourceFileCache $fileCache, ResourceLoaderContext $context, $etag) | |
Send out code for a response from file cache if possible. | |
tryRespondNotModified (ResourceLoaderContext $context, $etag) | |
Respond with HTTP 304 Not Modified if appropiate. | |
Static Protected Member Functions | |
static | createLoaderQuery (ResourceLoaderContext $context, $extraQuery=[]) |
Helper for createLoaderURL() | |
static | formatExceptionNoComment ( $e) |
Handle exception display. | |
static | makeLoaderImplementScript ( $name, $scripts, $styles, $messages, $templates) |
Return JS code that calls mw.loader.implement with given module properties. | |
Protected Attributes | |
MessageBlobStore | $blobStore |
$config | |
array | $errors = [] |
Errors accumulated during current respond() call. | |
array | $extraHeaders = [] |
List of extra HTTP response headers provided by loaded modules. | |
array | $moduleInfos = [] |
Associative array mapping module name to info associative array. | |
array | $modules = [] |
Module name/ResourceLoaderModule object pairs. | |
array | $sources = [] |
E.g. | |
array | $testModuleNames = [] |
Associative array mapping framework ids to a list of names of test suite modules like [ 'qunit' => [ 'mediawiki.tests.qunit.suites', 'ext.foo.tests', ... ], ... ]. | |
Static Protected Attributes | |
static bool | $debugMode = null |
Private Member Functions | |
ensureNewline ( $str) | |
Ensure the string is either empty or ends in a line break. | |
Static Private Member Functions | |
static | applyFilter ( $filter, $data) |
static | isEmptyObject (stdClass $obj) |
static | trimArray (array &$array) |
Remove empty values from the end of an array. | |
Private Attributes | |
LoggerInterface | $logger |
Dynamic JavaScript and CSS resource loading system.
Most of the documentation is on the MediaWiki documentation wiki starting at: https://www.mediawiki.org/wiki/ResourceLoader
Definition at line 38 of file ResourceLoader.php.
Register core modules and runs registration hooks.
Config | null | $config | [optional] |
LoggerInterface | null | $logger | [optional] |
Reimplemented in EmptyResourceLoader.
Definition at line 242 of file ResourceLoader.php.
References $config, addSource(), registerTestModules(), and setMessageBlobStore().
ResourceLoader::addSource | ( | $id, | |
$loadUrl = null |
|||
) |
Add a foreign source of modules.
Source IDs are typically the same as the Wiki ID or database name (e.g. lowercase a-z).
array | string | $id | Source ID (string), or [ id1 => loadUrl, id2 => loadUrl, ... ] |
string | array | null | $loadUrl | load.php url (string), or array with loadUrl key for backwards-compatibility. |
MWException |
Definition at line 451 of file ResourceLoader.php.
Referenced by __construct(), EmptyResourceLoader\__construct(), and ResourceLoaderTest\testAddSource().
|
staticprivate |
Definition at line 222 of file ResourceLoader.php.
References $data, $e, $filter, CSSMin\minify(), and JavaScriptMinifier\minify().
|
static |
Reset static members used for caching.
Global state and $wgRequest are evil, but we're using it right now and sometimes we need to be able to force ResourceLoader to re-evaluate the context because it has changed (e.g. in the test suite).
Definition at line 1655 of file ResourceLoader.php.
|
staticprotected |
Helper for createLoaderURL()
ResourceLoaderContext | $context | |
array | $extraQuery |
Definition at line 1686 of file ResourceLoader.php.
ResourceLoader::createLoaderURL | ( | $source, | |
ResourceLoaderContext | $context, | ||
$extraQuery = [] |
|||
) |
Build a load.php URL.
string | $source | Name of the ResourceLoader source |
ResourceLoaderContext | $context | |
array | $extraQuery |
Definition at line 1668 of file ResourceLoader.php.
|
static |
Wrapper around json_encode that avoids needless escapes, and pretty-prints in debug mode.
Definition at line 1321 of file ResourceLoader.php.
|
private |
Ensure the string is either empty or ends in a line break.
string | $str |
Definition at line 1171 of file ResourceLoader.php.
|
static |
Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux
to an array of module names like ‘[ 'jquery.foo’, 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ]`.
This process is reversed by ResourceLoader::makePackedModulesString().
string | $modules | Packed module name list |
Definition at line 1603 of file ResourceLoader.php.
|
static |
Run JavaScript or CSS data through a filter, caching the filtered result for future calls.
Available filters are:
If $data is empty, only contains whitespace or the filter was unknown, $data is returned unmodified.
string | $filter | Name of filter to run |
string | $data | Text to filter, such as JavaScript or CSS text |
array | $options | Keys:
|
Definition at line 186 of file ResourceLoader.php.
References $cache, $data, $filter, $options, and CACHE_ANYTHING.
|
static |
Handle exception display.
Exception | $e | Exception to be shown to the user |
Definition at line 999 of file ResourceLoader.php.
References $e.
|
staticprotected |
Handle exception display.
Exception | $e | Exception to be shown to the user |
Definition at line 1010 of file ResourceLoader.php.
References $e, and $wgShowExceptionDetails.
ResourceLoader::getCombinedVersion | ( | ResourceLoaderContext | $context, |
array | $moduleNames | ||
) |
Helper method to get and combine versions of multiple modules.
ResourceLoaderContext | $context | |
string[] | $moduleNames | List of known module names |
Definition at line 650 of file ResourceLoader.php.
ResourceLoader::getConfig | ( | ) |
Definition at line 275 of file ResourceLoader.php.
ResourceLoader::getLessCompiler | ( | $vars = [] | ) |
Returns LESS compiler set up for use with MediaWiki.
array | $vars | Associative array of variables that should be used for compilation. Since 1.32, this method no longer automatically includes global LESS vars from ResourceLoader::getLessVars (T191937). |
MWException |
Definition at line 1775 of file ResourceLoader.php.
ResourceLoader::getLessVars | ( | ) |
Get global LESS variables.
Definition at line 1801 of file ResourceLoader.php.
ResourceLoader::getLoadScript | ( | $source | ) |
Get the URL to the load.php endpoint for the given ResourceLoader source.
string | $source |
MWException | On an invalid $source name |
Definition at line 607 of file ResourceLoader.php.
References $source.
ResourceLoader::getLogger | ( | ) |
Definition at line 291 of file ResourceLoader.php.
Referenced by EmptyResourceLoader\__construct().
ResourceLoader::getMessageBlobStore | ( | ) |
Definition at line 299 of file ResourceLoader.php.
Referenced by preloadModuleInfo().
ResourceLoader::getModule | ( | $name | ) |
Get the ResourceLoaderModule object for a given module name.
If an array of module parameters exists but a ResourceLoaderModule object has not yet been instantiated, this method will instantiate and cache that object such that subsequent calls simply return the same object.
string | $name | Module name |
Definition at line 536 of file ResourceLoader.php.
References $name.
Referenced by ResourceLoaderClientHtml\getData(), and preloadModuleInfo().
ResourceLoader::getModuleNames | ( | ) |
Get a list of module names.
Definition at line 487 of file ResourceLoader.php.
ResourceLoader::getModulesByMessage | ( | $messageKey | ) |
Get names of modules that use a certain message.
string | $messageKey |
Definition at line 1185 of file ResourceLoader.php.
ResourceLoader::getSources | ( | ) |
Get the list of sources.
Definition at line 594 of file ResourceLoader.php.
ResourceLoader::getTestModuleNames | ( | $framework = 'all' | ) |
Get a list of test module names for one (or all) frameworks.
If the given framework id is unknkown, or if the in-object variable is not an array, then it will return an empty array.
string | $framework | Get only the test module names for one particular framework (optional) |
Definition at line 501 of file ResourceLoader.php.
|
static |
Determine whether debug mode was requested Order of priority is 1) request param, 2) cookie, 3) $wg setting.
Definition at line 1635 of file ResourceLoader.php.
|
staticprivate |
Definition at line 1366 of file ResourceLoader.php.
|
protected |
Whether the module is a ResourceLoaderFileModule (including subclasses).
string | $name | Module name |
Definition at line 572 of file ResourceLoader.php.
References $name.
ResourceLoader::isModuleRegistered | ( | $name | ) |
Check whether a ResourceLoader module is registered.
string | $name |
Definition at line 521 of file ResourceLoader.php.
References $name.
|
static |
Check a module name for validity.
Module names may not contain pipes (|), commas (,) or exclamation marks (!) and can be at most 255 bytes.
string | $moduleName | Module name to check |
Definition at line 1761 of file ResourceLoader.php.
|
static |
Combines an associative array mapping media type to CSS into a single stylesheet with "@media" blocks.
array | $stylePairs | Array keyed by media type containing (arrays of) CSS strings |
Definition at line 1285 of file ResourceLoader.php.
|
static |
Generate a CSS or JS comment block.
Only use this for public data, not error message details.
string | $text |
Definition at line 988 of file ResourceLoader.php.
|
static |
Returns JS code which will set the MediaWiki configuration array to the given value.
array | $configuration | List of configuration values keyed by variable name |
Exception |
Definition at line 1545 of file ResourceLoader.php.
|
static |
string | $value |
Definition at line 619 of file ResourceLoader.php.
References $value.
|
static |
Wraps JavaScript code to run after a required module.
string | string[] | $modules | Module name(s) |
string | $script | JavaScript code |
Definition at line 1499 of file ResourceLoader.php.
|
static |
Returns an HTML script tag that runs given JS code after startup and base modules.
The code will be wrapped in a closure, and it will be executed by ResourceLoader's startup module if the client has adequate support for MediaWiki JavaScript code.
string | $script | JavaScript code |
string | null | $nonce | [optional] Content-Security-Policy nonce (from OutputPage::getCSPNonce) |
Definition at line 1518 of file ResourceLoader.php.
|
static |
Wraps JavaScript code to run after the startup module.
string | $script | JavaScript code |
Definition at line 1485 of file ResourceLoader.php.
|
staticprotected |
Return JS code that calls mw.loader.implement with given module properties.
string | $name | Module name or implement key (format "`[name]@[version]`") |
XmlJsCode | array | string | $scripts | Code as XmlJsCode (to be wrapped in a closure), list of URLs to JavaScript files, string of JavaScript for $.globalEval , or array with 'files' and 'main' properties (see ResourceLoaderModule::getScript()) |
mixed | $styles | Array of CSS strings keyed by media type, or an array of lists of URLs to CSS files keyed by media type |
mixed | $messages | List of messages associated with this module. May either be an associative array mapping message key to value, or a JSON-encoded message blob containing the same data, wrapped in an XmlJsCode object. |
array | $templates | Keys are name of templates and values are the source of the template. |
MWException |
Definition at line 1213 of file ResourceLoader.php.
|
static |
Build a query array (array representation of query string) for load.php.
Helper function for createLoaderURL().
array | $modules | |
string | $lang | |
string | $skin | |
string | null | $user | |
string | null | $version | |
bool | $debug | |
string | null | $only | |
bool | $printable | |
bool | $handheld | |
array | $extraQuery |
Definition at line 1718 of file ResourceLoader.php.
|
static |
Returns JS code which calls mw.loader.register with the given parameter.
Definition at line 1425 of file ResourceLoader.php.
|
static |
Returns JS code which calls mw.loader.addSource() with the given parameters.
Has two calling conventions:
string | array | $sources | Source ID |
string | null | $loadUrl | load.php url |
Definition at line 1468 of file ResourceLoader.php.
|
static |
Returns a JS call to mw.loader.state, which sets the state of one ore more modules to a given value.
Has two calling conventions:
array | string | $states | |
string | null | $state |
Definition at line 1355 of file ResourceLoader.php.
|
static |
Returns JS code which, when called, will register a given list of messages.
mixed | $messages | Either an associative array mapping message key to value, or a JSON-encoded message blob containing the same data, wrapped in an XmlJsCode object. |
Definition at line 1270 of file ResourceLoader.php.
ResourceLoader::makeModuleResponse | ( | ResourceLoaderContext | $context, |
array | $modules, | ||
array | $missing = [] |
||
) |
Generate code for a response.
Calling this method also populates the errors
and headers
members, later used by respond().
ResourceLoaderContext | $context | Context in which to generate a response |
ResourceLoaderModule[] | $modules | List of module objects keyed by module name |
string[] | $missing | List of requested module names that are unregistered (optional) |
Definition at line 1033 of file ResourceLoader.php.
References $content, $context, $data, $e, $filter, $image, $modules, $name, $out, as, and errors.
|
static |
Convert an array of module names to a packed query string.
For example, ‘[ 'foo.bar’, 'foo.baz', 'bar.baz', 'bar.quux' ] becomes
'foo.bar,baz|bar.baz,quux'`.
This process is reversed by ResourceLoader::expandModuleNames(). See also mw.loader::buildModulesString() which is a port of this, used on the client-side.
array | $modules | List of module names (strings) |
Definition at line 1575 of file ResourceLoader.php.
ResourceLoader::makeVersionQuery | ( | ResourceLoaderContext | $context | ) |
Get the expected value of the 'version' query parameter.
This is used by respond() to set a short Cache-Control header for requests with information newer than the current server has. This avoids pollution of edge caches. Typically during deployment. (T117587)
This MUST match return value of mw.loader#getCombinedVersion()
client-side.
ResourceLoaderContext | $context |
Definition at line 685 of file ResourceLoader.php.
|
protected |
Definition at line 820 of file ResourceLoader.php.
References Timing\measure(), and use.
ResourceLoader::outputErrorAndLog | ( | Exception | $e, |
$msg, | |||
array | $context = [] |
||
) |
Add an error to the 'errors' array and log it.
Exception | $e | |
string | $msg | |
array | $context |
Definition at line 633 of file ResourceLoader.php.
ResourceLoader::preloadModuleInfo | ( | array | $moduleNames, |
ResourceLoaderContext | $context | ||
) |
Load information stored in the database about modules.
This method grabs modules dependencies from the database and updates modules objects.
This is not inside the module code because it is much faster to request all of the information at once than it is to have each module requests its own information. This sacrifice of modularity yields a substantial performance improvement.
array | $moduleNames | List of module names to preload information for |
ResourceLoaderContext | $context | Context to load the information within |
Definition at line 115 of file ResourceLoader.php.
References $blob, $context, $dbr, $lang, $modules, $name, $res, $skin, as, DB_REPLICA, ResourceLoaderModule\expandRelativePaths(), getMessageBlobStore(), getModule(), and wfGetDB().
ResourceLoader::register | ( | $name, | |
$info = null |
|||
) |
Register a module with the ResourceLoader system.
mixed | $name | Name of module as a string or List of name/object pairs as an array |
array | null | $info | Module info array. For backwards compatibility with 1.17alpha, this may also be a ResourceLoaderModule object. Optional when using multiple-registration calling style. |
MWException | If a duplicate module registration is attempted |
MWException | If a module name contains illegal characters (pipes or commas) |
MWException | If something other than a ResourceLoaderModule is being registered |
Definition at line 322 of file ResourceLoader.php.
References $name, $path, array(), as, definition, ResourceLoaderFileModule\extractBasePaths(), from, list, and or.
ResourceLoader::registerTestModules | ( | ) |
Definition at line 397 of file ResourceLoader.php.
References $IP, as, ExtensionRegistry\getInstance(), and include.
Referenced by __construct().
ResourceLoader::respond | ( | ResourceLoaderContext | $context | ) |
Output a response to a load request, including the content-type header.
ResourceLoaderContext | $context | Context in which a response should be formed |
Definition at line 708 of file ResourceLoader.php.
References $context, $e, $modules, $name, $response, as, errors, ResourceFileCache\newFromContext(), and ResourceFileCache\useFileCache().
|
protected |
Send main response headers to the client.
Deals with Content-Type, CORS (for stylesheets), and caching.
ResourceLoaderContext | $context | |
string | $etag | ETag header value |
bool | $errors | Whether there are errors in the response |
string[] | $extra | Array of extra HTTP response headers |
Definition at line 841 of file ResourceLoader.php.
References $context, $header, as, and wfTimestamp().
ResourceLoader::setLogger | ( | LoggerInterface | $logger | ) |
LoggerInterface | $logger |
Definition at line 283 of file ResourceLoader.php.
Referenced by EmptyResourceLoader\__construct().
ResourceLoader::setMessageBlobStore | ( | MessageBlobStore | $blobStore | ) |
MessageBlobStore | $blobStore |
Definition at line 307 of file ResourceLoader.php.
Referenced by __construct(), and EmptyResourceLoader\__construct().
|
staticprivate |
Remove empty values from the end of an array.
Values considered empty:
array | $array |
Definition at line 1385 of file ResourceLoader.php.
|
protected |
Send out code for a response from file cache if possible.
ResourceFileCache | $fileCache | Cache object for this request URL |
ResourceLoaderContext | $context | Context in which to generate a response |
string | $etag | ETag header value |
Definition at line 935 of file ResourceLoader.php.
References $context, $e, $response, FileCacheBase\cacheTimestamp(), DB_REPLICA, FileCacheBase\fetchText(), FileCacheBase\isCacheGood(), wfGetDB(), and wfTimestamp().
|
protected |
Respond with HTTP 304 Not Modified if appropiate.
If there's an If-None-Match header, respond with a 304 appropriately and clear out the output buffer. If the client cache is too old then do nothing.
ResourceLoaderContext | $context | |
string | $etag | ETag header value |
Definition at line 902 of file ResourceLoader.php.
References $context, HttpStatus\header(), and wfResetOutputBuffers().
|
protected |
Definition at line 91 of file ResourceLoader.php.
|
protected |
Definition at line 58 of file ResourceLoader.php.
Referenced by __construct(), and EmptyResourceLoader\__construct().
|
staticprotected |
Definition at line 43 of file ResourceLoader.php.
|
protected |
Errors accumulated during current respond() call.
Definition at line 77 of file ResourceLoader.php.
Referenced by EmptyResourceLoader\getErrors().
|
protected |
List of extra HTTP response headers provided by loaded modules.
Populated by makeModuleResponse().
Definition at line 86 of file ResourceLoader.php.
|
private |
Definition at line 96 of file ResourceLoader.php.
Referenced by EmptyResourceLoader\__construct().
|
protected |
Associative array mapping module name to info associative array.
Definition at line 55 of file ResourceLoader.php.
|
protected |
Module name/ResourceLoaderModule object pairs.
Definition at line 49 of file ResourceLoader.php.
|
protected |
|
protected |
Associative array mapping framework ids to a list of names of test suite modules like [ 'qunit' => [ 'mediawiki.tests.qunit.suites', 'ext.foo.tests', ... ], ... ].
Definition at line 65 of file ResourceLoader.php.