2015-08-01 05:17:35 +00:00
|
|
|
<?php
|
|
|
|
|
2022-02-06 18:54:47 +00:00
|
|
|
namespace MediaWiki\Extension\Gadgets;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2022-08-16 15:24:01 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-05-28 00:52:36 +00:00
|
|
|
use MediaWiki\ResourceLoader as RL;
|
2022-02-06 18:54:47 +00:00
|
|
|
|
2015-08-01 05:17:35 +00:00
|
|
|
/**
|
2015-08-03 06:37:32 +00:00
|
|
|
* Class representing a list of resources for one gadget, basically a wrapper
|
|
|
|
* around the Gadget class.
|
2015-08-01 05:17:35 +00:00
|
|
|
*/
|
2022-05-28 00:52:36 +00:00
|
|
|
class GadgetResourceLoaderModule extends RL\WikiModule {
|
2015-08-03 06:37:32 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Gadget
|
|
|
|
*/
|
|
|
|
private $gadget;
|
2015-08-01 05:17:35 +00:00
|
|
|
|
|
|
|
/**
|
2015-08-03 06:37:32 +00:00
|
|
|
* @param array $options
|
2015-08-01 05:17:35 +00:00
|
|
|
*/
|
2015-08-03 06:37:32 +00:00
|
|
|
public function __construct( array $options ) {
|
|
|
|
$this->id = $options['id'];
|
2015-08-01 05:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-03 06:37:32 +00:00
|
|
|
* @return Gadget instance this module is about
|
|
|
|
*/
|
|
|
|
private function getGadget() {
|
|
|
|
if ( !$this->gadget ) {
|
|
|
|
try {
|
2022-08-16 15:24:01 +00:00
|
|
|
$this->gadget = MediaWikiServices::getInstance()->getService( 'GadgetsRepo' )->getGadget( $this->id );
|
2015-08-03 06:37:32 +00:00
|
|
|
} catch ( InvalidArgumentException $e ) {
|
|
|
|
// Fallback to a placeholder object...
|
|
|
|
$this->gadget = Gadget::newEmptyGadget( $this->id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->gadget;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-28 00:52:36 +00:00
|
|
|
* Overrides the function from RL\WikiModule class
|
|
|
|
* @param RL\Context $context
|
2015-08-03 06:37:32 +00:00
|
|
|
* @return array
|
2015-08-01 05:17:35 +00:00
|
|
|
*/
|
2022-05-28 00:52:36 +00:00
|
|
|
protected function getPages( RL\Context $context ) {
|
2015-08-03 06:37:32 +00:00
|
|
|
$gadget = $this->getGadget();
|
2016-12-28 10:25:47 +00:00
|
|
|
$pages = [];
|
2015-08-03 06:37:32 +00:00
|
|
|
|
|
|
|
foreach ( $gadget->getStyles() as $style ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
$pages[$style] = [ 'type' => 'style' ];
|
2015-08-03 06:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $gadget->supportsResourceLoader() ) {
|
|
|
|
foreach ( $gadget->getScripts() as $script ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
$pages[$script] = [ 'type' => 'script' ];
|
2015-08-03 06:37:32 +00:00
|
|
|
}
|
2023-12-11 17:16:30 +00:00
|
|
|
if ( $gadget->isPackaged() ) {
|
|
|
|
foreach ( $gadget->getJSONs() as $json ) {
|
|
|
|
$pages[$json] = [ 'type' => 'data' ];
|
|
|
|
}
|
2021-10-17 13:05:15 +00:00
|
|
|
}
|
2015-08-03 06:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $pages;
|
2015-08-01 05:17:35 +00:00
|
|
|
}
|
|
|
|
|
2021-10-17 13:05:15 +00:00
|
|
|
/**
|
2022-05-28 00:52:36 +00:00
|
|
|
* Overrides RL\WikiModule::getRequireKey()
|
2021-10-17 13:05:15 +00:00
|
|
|
* @param string $titleText
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRequireKey( $titleText ): string {
|
2022-08-16 15:24:01 +00:00
|
|
|
return MediaWikiServices::getInstance()->getService( 'GadgetsRepo' )->titleWithoutPrefix( $titleText );
|
2021-10-17 13:05:15 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* @param string $fileName
|
|
|
|
* @param string $contents
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function validateScriptFile( $fileName, $contents ) {
|
|
|
|
// Temporary solution to support gadgets in ES6 by disabling validation
|
|
|
|
// for them and putting them in a separate resource group to avoid a syntax error in them
|
|
|
|
// from corrupting core/extension-loaded scripts or other non-ES6 gadgets.
|
|
|
|
if ( $this->requiresES6() ) {
|
|
|
|
return $contents;
|
|
|
|
}
|
|
|
|
return parent::validateScriptFile( $fileName, $contents );
|
|
|
|
}
|
|
|
|
|
2021-10-17 13:05:15 +00:00
|
|
|
/**
|
2022-05-28 00:52:36 +00:00
|
|
|
* Overrides RL\WikiModule::isPackaged()
|
2021-10-17 13:05:15 +00:00
|
|
|
* Returns whether this gadget is packaged.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPackaged(): bool {
|
|
|
|
return $this->gadget->isPackaged();
|
|
|
|
}
|
|
|
|
|
2015-08-01 05:17:35 +00:00
|
|
|
/**
|
2022-05-28 00:52:36 +00:00
|
|
|
* Overrides RL\Module::getDependencies()
|
|
|
|
* @param RL\Context|null $context
|
2017-03-03 01:41:25 +00:00
|
|
|
* @return string[] Names of resources this module depends on
|
2015-08-01 05:17:35 +00:00
|
|
|
*/
|
2022-05-28 00:52:36 +00:00
|
|
|
public function getDependencies( RL\Context $context = null ) {
|
2015-08-03 06:37:32 +00:00
|
|
|
return $this->getGadget()->getDependencies();
|
2015-08-01 05:17:35 +00:00
|
|
|
}
|
|
|
|
|
Implement support for specifying type=styles
T87871 formally introduced the concept of a styles module,
which sets mw.loader.state to "ready" when loaded through addModuleStyles().
Previously, addModuleStyles couldn't safely do that because a module may
contain scripts also, in which case mw.loader must still load the (rest)
of the module (causes styles to load twice).
In MediaWiki core or extensions this is easily avoided by calling not
calling both addModules() and addModuleStyles().
For Gadgets we call both as a workaround to allow users to provide styles
(without a FOUC), but also to provide scripts+styles. Since we don't declare
which one is intended (and some gadgets do both), we loaded them both ways.
This will no longer be allowed in the future (see T92459).
The new 'type=styles' Gadget attribute promises to ResourceLoader that a
gadget only contains styles.
Impact:
* [Bug fix] When mw.loader requires a styles module that already loaded,
it will not load again.
* [Feature] It is possible for a general scripts+styles gadget to depend on
a styles gadget. Previously this caused the styles to load twice.
* Specifying type=styles will load the module through addModuleStyles() only.
Use this for modules that contain styles that relate to elements already
on the page (e.g. when customising the skin, layout, or article content).
* Specifying type=general will load the module through addModules() only.
Use this if your module contains both scripts and styles and the styles
only relate to elements created by the script. This means the styles do not
need to be loaded separately through addModuleStyles() and will not apply
to noscript mode.
Effective difference:
* Gadgets with only styles: We assume type=styles.
This fixes the main bug (styles loading twice) and requires no migration!
* Gadgets with only scripts: We assume type=general.
This requires no migration! (And: No more empty stylesheet request)
* Gadgets with scripts (with or without styles): We assume type=general, but
unless type=general was explicitly set we'll still load it both ways so
that the styles apply directly on page load.
If this is not needed, set type=general.
If this is needed, it should become two separate modules. We do not support
a single module having two purposes (1: apply styles to the page,
2: provide scripts+styles). The styles module should be separate.
It can be made hidden, and listed as dependency of the other module.
The latter case is detected on page load and results in a console warning
with a link to T42284.
Bug: T42284
Bug: T92459
Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
|
|
|
/**
|
2022-05-28 00:52:36 +00:00
|
|
|
* Overrides RL\WikiModule::getType()
|
|
|
|
* @return string RL\Module::LOAD_STYLES or RL\Module::LOAD_GENERAL
|
Implement support for specifying type=styles
T87871 formally introduced the concept of a styles module,
which sets mw.loader.state to "ready" when loaded through addModuleStyles().
Previously, addModuleStyles couldn't safely do that because a module may
contain scripts also, in which case mw.loader must still load the (rest)
of the module (causes styles to load twice).
In MediaWiki core or extensions this is easily avoided by calling not
calling both addModules() and addModuleStyles().
For Gadgets we call both as a workaround to allow users to provide styles
(without a FOUC), but also to provide scripts+styles. Since we don't declare
which one is intended (and some gadgets do both), we loaded them both ways.
This will no longer be allowed in the future (see T92459).
The new 'type=styles' Gadget attribute promises to ResourceLoader that a
gadget only contains styles.
Impact:
* [Bug fix] When mw.loader requires a styles module that already loaded,
it will not load again.
* [Feature] It is possible for a general scripts+styles gadget to depend on
a styles gadget. Previously this caused the styles to load twice.
* Specifying type=styles will load the module through addModuleStyles() only.
Use this for modules that contain styles that relate to elements already
on the page (e.g. when customising the skin, layout, or article content).
* Specifying type=general will load the module through addModules() only.
Use this if your module contains both scripts and styles and the styles
only relate to elements created by the script. This means the styles do not
need to be loaded separately through addModuleStyles() and will not apply
to noscript mode.
Effective difference:
* Gadgets with only styles: We assume type=styles.
This fixes the main bug (styles loading twice) and requires no migration!
* Gadgets with only scripts: We assume type=general.
This requires no migration! (And: No more empty stylesheet request)
* Gadgets with scripts (with or without styles): We assume type=general, but
unless type=general was explicitly set we'll still load it both ways so
that the styles apply directly on page load.
If this is not needed, set type=general.
If this is needed, it should become two separate modules. We do not support
a single module having two purposes (1: apply styles to the page,
2: provide scripts+styles). The styles module should be separate.
It can be made hidden, and listed as dependency of the other module.
The latter case is detected on page load and results in a console warning
with a link to T42284.
Bug: T42284
Bug: T92459
Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
|
|
|
*/
|
|
|
|
public function getType() {
|
|
|
|
return $this->getGadget()->getType() === 'styles'
|
2022-05-28 00:52:36 +00:00
|
|
|
? RL\Module::LOAD_STYLES
|
|
|
|
: RL\Module::LOAD_GENERAL;
|
Implement support for specifying type=styles
T87871 formally introduced the concept of a styles module,
which sets mw.loader.state to "ready" when loaded through addModuleStyles().
Previously, addModuleStyles couldn't safely do that because a module may
contain scripts also, in which case mw.loader must still load the (rest)
of the module (causes styles to load twice).
In MediaWiki core or extensions this is easily avoided by calling not
calling both addModules() and addModuleStyles().
For Gadgets we call both as a workaround to allow users to provide styles
(without a FOUC), but also to provide scripts+styles. Since we don't declare
which one is intended (and some gadgets do both), we loaded them both ways.
This will no longer be allowed in the future (see T92459).
The new 'type=styles' Gadget attribute promises to ResourceLoader that a
gadget only contains styles.
Impact:
* [Bug fix] When mw.loader requires a styles module that already loaded,
it will not load again.
* [Feature] It is possible for a general scripts+styles gadget to depend on
a styles gadget. Previously this caused the styles to load twice.
* Specifying type=styles will load the module through addModuleStyles() only.
Use this for modules that contain styles that relate to elements already
on the page (e.g. when customising the skin, layout, or article content).
* Specifying type=general will load the module through addModules() only.
Use this if your module contains both scripts and styles and the styles
only relate to elements created by the script. This means the styles do not
need to be loaded separately through addModuleStyles() and will not apply
to noscript mode.
Effective difference:
* Gadgets with only styles: We assume type=styles.
This fixes the main bug (styles loading twice) and requires no migration!
* Gadgets with only scripts: We assume type=general.
This requires no migration! (And: No more empty stylesheet request)
* Gadgets with scripts (with or without styles): We assume type=general, but
unless type=general was explicitly set we'll still load it both ways so
that the styles apply directly on page load.
If this is not needed, set type=general.
If this is needed, it should become two separate modules. We do not support
a single module having two purposes (1: apply styles to the page,
2: provide scripts+styles). The styles module should be separate.
It can be made hidden, and listed as dependency of the other module.
The latter case is detected on page load and results in a console warning
with a link to T42284.
Bug: T42284
Bug: T92459
Change-Id: Ia3c9ddee243f710022144fc2884434350695699a
2016-09-01 23:31:14 +00:00
|
|
|
}
|
|
|
|
|
2015-08-03 06:34:16 +00:00
|
|
|
public function getMessages() {
|
2015-08-03 06:37:32 +00:00
|
|
|
return $this->getGadget()->getMessages();
|
|
|
|
}
|
|
|
|
|
2021-12-24 19:59:57 +00:00
|
|
|
public function getSkins(): ?array {
|
|
|
|
return $this->getGadget()->getRequiredSkins() ?: null;
|
|
|
|
}
|
|
|
|
|
2022-01-30 13:15:25 +00:00
|
|
|
public function requiresES6(): bool {
|
|
|
|
return $this->getGadget()->requiresES6();
|
|
|
|
}
|
|
|
|
|
2017-05-02 00:25:36 +00:00
|
|
|
public function getGroup() {
|
2022-01-30 13:15:25 +00:00
|
|
|
return $this->requiresES6() ? 'es6-gadget' : self::GROUP_SITE;
|
2017-05-02 00:25:36 +00:00
|
|
|
}
|
2015-08-01 05:17:35 +00:00
|
|
|
}
|