Commit graph

413 commits

Author SHA1 Message Date
Derick A
fb5650684a resourceloader: Optimization - avoid repeated $this->getModule() calls
`$this->getModule()` returns a RL object or a null. The check above already
makes sure that $module is not null so we can just set file dependencies
from the RL object returned.

In addition, this is an optimization as we reduce repeated calls to
`$this->getModule()`. Similar logic already exist in the same file from
L#116 - L#118.

Change-Id: If50c28ff5cd8eb435c9fa4f277e2d1038b52ca74
2020-02-09 21:18:44 +01:00
Brad Jorsch
036cde7a04 resourceloader: Move site-level mw.config from startup to mediawiki.base
This data isn't needed for startup, and we can shave off a few K from
startup by moving it to mediawiki.base instead.

It was requested that this be done as a "package file", which
necessitated some other minor structural changes to mediawiki.base as
well.

Bug: T235350
Change-Id: I525a5203533089d5a542f83a847be58a10cb6319
2020-02-07 19:44:57 +00:00
James D. Forrester
0958a0bce4 Coding style: Auto-fix MediaWiki.Usage.IsNull.IsNull
Change-Id: I90cfe8366c0245c9c67e598d17800684897a4e27
2020-01-10 14:17:13 -08:00
Kunal Mehta
d6390ba1ee registration: Implement lazy-loaded attributes
These attributes are only accessed at a time when performance isn't
critical (e.g. tests), so we can skip caching them and read each
extension.json file again when we do need them.

At the same time, we want to reduce the size of the array that we're
caching due to PHP 7 performance regressions with APC (T187154).

'QUnitModules' is the first attribute to be converted into a lazy-loaded
attribute.

Bug: T220994
Change-Id: I224cdace05dbac00ff947c41daecb74b07c967bb
2019-12-15 19:04:50 +00:00
Timo Tijhof
9c0dee3965 resourceloader: Use ConvertibleTimestamp and Logger directly
Avoid use of wfTimestamp and wfDebugLog global functions.

Also simplify some of the error messages around processing of
'packageFiles' definitions and throw generic LogicException
instead of MWException.

Change-Id: I55ce1f107f53dfdfe673cbe4411b0a7c4e24b2ea
2019-12-04 16:24:40 -08:00
Umherirrender
c7ad21c25f Improve param docs
Change-Id: I746a69f6ed01c3ff000da125457df62b02d13b34
2019-11-28 19:08:59 +01:00
Brian Wolff
67ea4f5747 Mild refactoring of ContentSecurityPolicy
This is to make it behave in a more object orientied way. The
goal is to make it be easier to allow extensions to mark certain
pages as requiring a different policy (For example, CodeEditor
extension uses a blob: url with a WebWorker. We don't want to
include that on the policy of every page, but allow the extension
to mark it as required whenever needed).

This commit does not change code behaviour in any way.

Change-Id: I4bf53dabb6e6c5446cea99a64db68b300cef2fd4
2019-10-28 09:02:14 -07:00
Ammar Abdulhamid
0454b34c4d resourceloader: Simplify $wgResourceLoaderMaxage config
Remove support for the 'client' and 'server' subkeys.

Bug: T235314
Change-Id: Ibeb2404b0f7dc8a9198e73344d54a3cb0176de08
2019-10-19 21:28:58 +01:00
Daimona Eaytoy
bd5b6f98ba Fix new phan errors, part 3
These are almost only doc changes, with two exceptions:
1-In LinkHolderArray, int-alike array keys are now cast to int, to be uniform with what we do in other code paths
2-In ExtensionRegistration, changed a line to throw an Exception
immediately, instead of an ExtensionDependencyError. This is because the
latter takes an array with msg and type, but we were passing it a plain
string (and in fact the code was bugged).

Bug: T231636
Change-Id: I8b0ef50d279c2a87490dde6a467a4e22c0710afd
2019-10-12 10:35:22 +00:00
Fomafix
7c7f3efcb8 resourceloader: Drop deprecated ResourceLoader::getLessVars
ResourceLoader::getLessVars is deprecated since ca510f742 included in
MW 1.32 and hard-deprecated since 1b32592d9 included in MW 1.34.

Bug: T140804
Change-Id: If88c71ed7fdf3c6a5cf89a50197e246c201ebaa2
2019-10-09 22:31:52 +00:00
Fomafix
a9823d16f6 resourceloader: Add array type hints
Change-Id: I4844eae68e85adc46e52646ea066b459bbabdcce
2019-10-05 15:26:56 +00:00
Fomafix
1b32592d9c resourceloader: Hard deprecate ResourceLoader::getLessVars
ResourceLoader::getLessVars is already deprecated since MW 1.32.

Change-Id: If2fbe7828d8cde73575c0306486835fa1ea06b84
2019-09-30 00:07:30 +00:00
Fomafix
9d2d608993 resourceloader: Simplify makeLoaderStateScript and makeLoaderSourcesScript
The signatures without array are not used anymore.

Change-Id: Ice6a09ff6cba0c605ed1c89a25fd8e02af041b05
2019-09-28 18:26:40 +00:00
jenkins-bot
1791c92893 Merge "resourceloader: Add $modules parameter to makeVersionQuery()" 2019-09-28 01:26:59 +00:00
jenkins-bot
5155abe0e6 Merge "resourceloader: Add $context to static functions in ResourceLoader" 2019-09-27 20:48:15 +00:00
Fomafix
2fc229116d resourceloader: Add $context to static functions in ResourceLoader
This change allows to use the context in the functions.

The following internal static functions from ResourceLoader get now a
reference to the ResourceLoaderContext object:
* makeLoaderImplementScript
* makeLoaderStateScript
* makeLoaderRegisterScript
* makeLoaderSourcesScript

ResouceLoader::encodeJsonForScript is duplicated to
ResourceLoaderContext::encodeJson loading the debug mode from context.

ResourceLoader::encodeJsonForScript is kept for other usages without
context.

The debug mode is loaded from $context->getDebug() instead of from
ResourceLoader::inDebugMode(). This does not support to enable the debug
mode by setting the cookie 'resourceLoaderDebug' or the configuration
variable wgResourceLoaderDebug. Only the URL parameter debug=true
enables the debug mode. This should be sufficient for the subsequent
ResourceLoader requests. The tests don't need the global variable
wgResourceLoaderDebug anymore. The initial ResourceLoader context in
OutputPage still uses ResourceLoader::inDebugMode() with cookie and
global configuration variable.

This change adds the parameter $context with a ResourceLoaderContext
object to ResourceLoaderModule::getDeprecationInformation and deprecates
omitting the parameter. Ifa1a3bb56b731b83864022a358916c6aca5d7c10
updates this in extension ExtJSBase.

Bug: T229311
Change-Id: I5341f18625209446a6d006f60244990f65530319
2019-09-27 18:35:55 +02:00
Timo Tijhof
e6ff1ff915 resourceloader: Add $modules parameter to makeVersionQuery()
The 'version' and 'modules' parameters are a somewhat problematic
part of the ResourceLoaderContext object as we often pass around
the context but may be dealing with only a subset of the modules
in the outer request, or even entirely different ones (e.g.
for OutputPage's fake context 'modules' and 'version' are both
null).

This is already visible in ClientHtml where we create a derivative
context just to call setModules() and have makeVersionQuery()
work. This is now fixed.

This change is in preparation for use in ResourceLoaderImage
(to fix T233343) where we'll need to compute a version for only
1 module of a larger set, and ideally without needing to create
another context.

Bug: T233343
Change-Id: Icc1a4fd1f58c4e49e58eee43ca4ba2de6cfffc76
2019-09-26 22:30:07 +00:00
Timo Tijhof
a5ce1d7792 resourceloader: Add Doxygen group and improve overall docs
* Add license header where missing.
* Add missing `@since` (1.17 for most classes), except
  ResourceLoaderLessVarFileModule since 1.32 (1bc62c548c).

* Remove duplicate file-level description for class-only files,
  merge with the class description instead.

* Remove my own `@author` annotation from one file.

* Mark core's own FileModule subclasses as `@internal`, except
  for the following which we support use of in extensions:
  ResourceLoaderLessVarFileModule,
  ResourceLoaderOOUIIconPackModule, and
  ResourceLoaderWikiModule.

Change-Id: I336af2e4ccdbe2512594e8861b72628d24194e41
2019-09-14 18:37:36 +00:00
Fomafix
d78d9fe3fb resourceloader: Replace Xml::encodeJsCall by encodeJsonForScript
Also document that encodeJsonForScript can return false on invalid UTF-8
characters.

Bug: T32956
Change-Id: I9c2fd33fb2130ada67fa70ff176e5488f1a014bf
2019-09-10 12:51:16 +02:00
Fomafix
a253c6d56c resourceloader: Remove comment about XmlJsCode wrapper
The support for the XmlJsCode wrapper got removed in 23d066618d.

Change-Id: If14065277699aa9cca70107ff0174bd51f757c31
2019-09-10 07:30:07 +02:00
Umherirrender
8e7b469a12 resourceloader: Document encodeJsonForScript() type as mixed
Objects passed to it since 23d066618d
json_encode allows mixed type, so use it also here.

Change-Id: I0897a6a144fd1c90b3ead205cedf21c19682b9df
2019-09-04 20:11:38 +00:00
Timo Tijhof
9f516f1d3b resourceloader: Reduce width of module hash from 7 chars to 5
In a nut shell:

* We very often (52% of modules on enwiki) pad the hash with a zero,
  which means the amount of bits we currently compute already fit in
  6 characters already for most modules. For some modules (3%) we
  even padded two zeroes.

* For the (now documented) use cases, the space of 78 Giga
  (78 billion, or 78 milliard) seems more than we need. The space of
  60 million should be enough.

  This follows-up dfd046412f from 2016, which previously shortened the hash
  down from 8 chars of base 64 (or 12 chars of hex) to 7 chars of base 32.
  Before that change, the space was 281 Tera (64^8, or 16^12).

For more details see the added inline comment for ResourceLoader::makeHash,
and also the data at <https://phabricator.wikimedia.org/T229245>.

Bug: T229245
Change-Id: I9ad11772a33b3a44cb625275b1d7353e1393ee49
2019-09-02 01:25:48 +00:00
Timo Tijhof
e1bf44cd21 resourceloader: Add tests for disallowing access to private modules
* Add a test to confirm that the ResourceLoader::respond() logic
  works as intended.

* Remove the client code for preventing it from being loaded.
  This can never happen in production unless there is a bug.
  Instead of optimising to avoid a pointless request that only
  happens when the software is broken, instead optimise for when
  the software is not broken by just letting it happen. The server
  already handles it just fine.

  This was originally added in 2015 with 1dd7390372 to reduce
  logspam, but that was instead fixed in 6d6b037e12 by making the
  log message debug-only (because it's not a software problem,
  it's a client-error, e.g. a broken user script or a third
  party trying out different things on the load.php entry point).

  Removing this makes the client a bit smaller, too :)

Change-Id: Ic5420d9329a73514f4fc27baa46ae58d94addafb
2019-08-24 20:36:37 +01:00
Aaron Schulz
440dfcf6d8 Reorganize ResourceLoader fields
Change-Id: I9c9aece69b869165b8d9037336c8a2e7d7189c1e
2019-08-02 14:54:40 -04:00
jenkins-bot
5e2788a0c5 Merge "resourceloader: Merge $fileCache conditional blocks" 2019-08-01 21:52:48 +00:00
Timo Tijhof
5f47d994bc resourceloader: Don't explicitly enqueue test libs on SpecialJavaScriptTest
The test-only modules registered by QUnitTestResources.php are currently
were previously caught by the array_keys() catch-all in registerTestModules()
which meant that modules like 'test.sinonjs' would be requested on
SpecialJavaScriptTest despite not doing anything by itself, nor executing
at the "right" time per se through this means.

In order for it to execute at the right time, the testrunner has to depend
on it (which it does, already). But, that also means it doesn't need to
be requested separately. Doing so could be confusing.

This is neccecary in order to move 'jquery.qunit' from Resources.php
to QUnitTestResources.php as otherwise, listing in QUnitTestResources.php,
would implicitly mean SpecialJavaScriptTest.php thinks it's a test suite
and load it. That is a problem, because when we run the tests headless from
the command-line with Karma, the environment already has a QUnit interface
defined, and should not be loaded a second time by MW.

Change-Id: I08b31cd1dee516cf0d26bafdb8cc7c1223633bad
2019-07-30 16:00:49 +00:00
Timo Tijhof
69016bd2c3 resourceloader: Merge $fileCache conditional blocks
Also avoid conditional existence of variables ($fileCache). This avoids
isset(), which means we won't hide problems during refactor.

Raised by Codehealth (sonarcloud.io) as Major Code Smell:

> Merge this if statement with the enclosing one.
> https://sonarcloud.io/organizations/wmftest/rules?open=php%3AS1066&rule_key=php%3AS1066

Change-Id: Iacebbe6a68dac46cdfd1415a33a547d105b24b98
2019-07-27 14:29:37 +00:00
Timo Tijhof
1dd7af5754 resourceloader: Remove incomprehensible @todo in getTestModuleNames()
I added this 7.5 years ago with r111378 (5bf04171dc), but I can't
recall what this would have been for. Perhaps we wanted to expose
the list of modules (or test frameworks) via ApiQuerySiteInfo, but
I don't know why, or what purpose it would serve.

Change-Id: I4005979252533a752178e6f1ac9900f32132c27e
2019-07-25 23:46:11 +01:00
jenkins-bot
4677b12af9 Merge "resourceloader: Replace some Xml::encodeJs calls with RL's own encodeJson" 2019-07-16 06:26:42 +00:00
Timo Tijhof
23d066618d resourceloader: Replace some Xml::encodeJs calls with RL's own encodeJson
Bug: T32956
Change-Id: I614fe0e80ff308b857639a27d7772f969899b468
2019-07-16 01:15:32 +01:00
Timo Tijhof
9bee7ab8a1 resourceloader: Remove support for 'object' in wgResourceModules
Obsolete since MediaWiki 1.17alpha, no known usage since.

Bug: T222637
Change-Id: Ie820b16022ced6767c32aee7f2497a99260b1641
2019-07-16 00:33:19 +01:00
Timo Tijhof
d6dd6e4d72 resourceloader: Remove use of object registering in test suites
This was done as a "clever" shortcut to make sure tests a little
but shorter, but also made them less consistent with normal code.

Remove this in favour of 'class' or 'factory' options as needed.
Also remove a bunch of unneeded register() calls.

The tests cover everything affected by this change.

Side fix - isFileModule should reject modules with 'factory'
the same way it rejected raw objects and non-FileModule 'class'
cases already. This is now covered by tests as well.

Bug: T222637
Change-Id: I3996317dbcd780cc6e0f82c84e769c08a3fc42bb
2019-07-12 01:17:44 +00:00
Timo Tijhof
c554ee8e64 resourceloader: Remove support for raw modules
Being a raw module means that when it is requested from load.php with
"only=scripts" set, then the output is *not* wrapped in an
'mw.loader.implement' closure *and* there no 'mw.loader.state()' appendix.
Instead, it is served "raw".

Before 2018, the modules 'mediawiki' and 'jquery' were raw modules.
They were needed before the client could define 'mw.loader.implement', and
could never be valid dependencies. Module 'mediawiki' merged to 'startup',
and 'jquery' became a regular module (T192623). Based on the architecture
of modules being deliverable bundles, it doesn't make sense for there to
ever be raw modules again. Anything that 'startup' needs should be bundled
with it. Anything else is a regular module.

On top of that, we never actually needed this feature because specifying
the 'only=scripts' and 'raw=1' parameters does the same thing.

The only special bit about marking modules (not requests) as "raw" was that
it allowed the client to forget to specify "raw=1" and the server would
automatically omit the 'mw.loader.state()' appendix based on whether the
module is marked as raw. As of Ie4564ec8e26ad53f2, the two remaining use
cases for raw responses now specify the 'raw=1' request parameter, and we
can get rid of the "raw module" feature and all the complexity around it.

== Startup module

In the startup module there was an interesting use of isRaw() that has
little to do with the above. The "ATTENTION" warning there applies to the
startup module only, not raw modules in general. This is now fixed by
explicitly checking for StartupModule.

Above that warning, it talked about saving bytes, which was an optimisation
given that "raw" modules don't communicate with mw.loader, they also don't
need to be registered there because even if mw.loader would try to load
them, the server would never inform mw.loader about the module having
arrived. There are now no longer any such modules.

Bug: T201483
Change-Id: I8839036e7b2b76919b6cd3aa42ccfde4d1247899
2019-06-27 00:08:14 +00:00
Fomafix
4821c63624 resourceloader: Add method ResourceLoaderModule::getVary
Change-Id: I79cd3ad7ad65533717bab52462ffaab5b2179789
2019-06-24 18:45:05 +02:00
Fomafix
86a1f6f080 resourceloader: Define the default language and skin in constants
This allows to use the default values at several places.

Change-Id: I53f29c2162c4cac9caf672e816ebfeb97c452389
2019-06-19 15:52:58 +02:00
Timo Tijhof
253bee13bf resourceloader: Change cache keys to use first segment for grouping by purpose
Remove use of a complete "namespace" for grouping and instead use dashes
within the first key segment for that.

This way, we can leverage the various features WANObjectCache provides
for statistics relating to a particular kind of thing being cached.

These statistics are currently combined for all of resourceloader,
which isn't useful. Mildly related to T223647.

Change-Id: I5df69e46ac436d04e765726a36fc3eb70697a7ed
2019-06-13 18:04:17 +00:00
Fomafix
63fc0a8cc7 resourceloader: Omit default 'lang' and 'skin' params from load.php urls
lang=qqx and skin=fallback are the default values.

This change removes the default values from the load request of the
html5shiv module.

Before this change
 <script src="/w/load.php?lang=qqx&amp;modules=html5shiv&amp;only=scripts&amp;skin=fallback&amp;sync=1"></script>
With this change
 <script src="/w/load.php?modules=html5shiv&amp;only=scripts&amp;sync=1"></script>

Change-Id: Ie384ce0f7ab1bd0b6c2d3f0ca4a990c3cf3a7f15
2019-06-13 17:22:07 +00:00
Timo Tijhof
c9318edc2d resourceloader: Warn on ResourceLoader::construct without Config
The only remaining use of 'new ResourceLoader' is in tests, which have
been migrated in this commit to either passing the real config explicitly
(for integration tests), or by passing a HashConfig from a new
'getMinimalConfig' method which has only the keys required for the tests
to pass (e.g. avoid any ConfigExeption for unknown keys).

Also clean up some related code quality issues:

* Migrate wfScript() to $conf->get() so that the local Config is used,
  instead of implicitly using global variables. This isn't deprecated for
  MediaWiki generally, but done here to prepare ResourceLoader for becoming
  a standalone library.

* Remove mocking of 'CacheEpoch' config, this is no longer used anywhere
  in ResourceLoader.

* Change EmptyResourceLoader to use the minimal config by default and
  remove code duplication by calling the parent.

  Update the small number of uses that are integration tests, to explicitly
  pass in the live config as needed. And for the one case that tests the
  'startup' module, it no longer needs to register it manually given this
  is part of ResourceLoader::__construct() by default.

Bug: T32956
Change-Id: I127346fd530fa66f205156e545758b1c29d0fac0
2019-06-10 15:24:45 +00:00
Timo Tijhof
df010dd284 resourceloader: Remove redundant 'window' indirection
Use global variables directly as globals, except for the specific
case of accessing it when it may not exist. In those cases we
use the fact that undefined properties of an object yield the
`undefined` value which we can cast to false. Accessing an undefined
variable would yield a ReferenceError exception.

Change-Id: I1d9e9aa5845ba3c756ad6e31358d8594e003b04b
2019-05-08 21:24:30 +00:00
Timo Tijhof
a259a2ed0d resourceloader: Move remaining module registrations to ServiceWiring
Also restore the order of registrations as it was before
last week with 47422fabe2. (That is, core modules are registered
before extension modules, in case of conflicts with a warning, the
core one wins).

Bug: T32956
Change-Id: I3a50508178159dfc8e5db1e218a5e6d10e2d4b2a
2019-04-17 19:01:29 +00:00
Fomafix
46b361d743 resourceloader: Add newline after 'mw.loader.implement()' in debug mode
The next mw.loader.implement() starts on its own line.

Change-Id: Iae0a5eab58f1065c354018ed23c6a9834973b19d
2019-04-13 19:20:22 +01:00
Timo Tijhof
f2bf73a4c4 resourceloader: Move expandModuleNames() to ResourceLoader.php
This has always been an odd case, as indicicated by the cross-class
comment references, and the fact that its test cases are already
in ResourceLoaderTest.php, for convenience, as that's also where
the creation of 'module name strings' is done and tested.

Actually move it there instead of pretending it is there.

Change-Id: Ied9569436cc78704a5c1b75eeebb73f8631350f6
2019-04-11 21:06:53 +00:00
jenkins-bot
a0b185c802 Merge "resourceloader: Move registration of Resources.php to ServiceWiring" 2019-04-08 03:54:04 +00:00
jenkins-bot
c4ef867c87 Merge "resourceloader: Fix ensureNewline( '' ) support for PHP 7" 2019-04-07 16:52:25 +00:00
Fomafix
1f555b423f resourceloader: Fix ensureNewline( '' ) support for PHP 7
substr( '', -1 ) returns '', not false, in PHP 7.

Change-Id: Ie48fe064385e70f070e3fbfe0b62198f36cccd6d
2019-04-07 16:32:54 +00:00
Fomafix
640cc31ebe resourceloader: Use self::class instead of 'self'
Change-Id: I66d9974875d404fb5f7669f3962e694e1d56c350
2019-04-07 16:29:52 +00:00
Timo Tijhof
47422fabe2 resourceloader: Move registration of Resources.php to ServiceWiring
This identified something we hadn't codified before which is that
we expect the 'startup' module to always exist. The code and tests
for ResourceLoaderClientHtml also assume this, which we now
acknowledge explicitly by leaving it in the constructor.

Bug: T32956
Change-Id: Iffc545eb32078aed0d059e5902a5c9382e14c641
2019-04-06 23:35:58 +00:00
Kunal Mehta
4ef179e335 Fix/suppress misc phan errors (#5)
Add lots of missing return statements, or remove incorrect doc blocks.

Change-Id: I0881e98fbb9d0d4cf79ecc824064d24538055d3f
2019-04-05 15:53:37 -07:00
Reedy
c13fee87d4 Collapse some nested if statements
Change-Id: I9a97325d738d09370d29d35d5254bc0dadc57ff4
2019-04-04 19:02:22 +00:00
Timo Tijhof
5c90f8d137 resourceloader: Move registering of custom sources to ServiceWiring
Remove the now-redundant mocking of this configuration from various
test classes.

Bug: T32956
Change-Id: If90a10a76b8289c4ba707382ac915441c17d831f
2019-03-29 01:46:34 +00:00