2010-11-13 19:13:43 +00:00
|
|
|
<?php
|
2010-11-11 18:19:57 +00:00
|
|
|
/**
|
|
|
|
* Gadgets extension - lets users select custom javascript gadgets
|
|
|
|
*
|
2016-12-23 17:26:43 +00:00
|
|
|
* For more info see https://www.mediawiki.org/wiki/Extension:Gadgets
|
2010-11-11 18:19:57 +00:00
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @author Daniel Kinzler, brightbyte.de
|
|
|
|
* @copyright © 2007 Daniel Kinzler
|
2018-04-07 01:25:09 +00:00
|
|
|
* @license GPL-2.0-or-later
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
|
|
|
|
2022-02-06 18:54:47 +00:00
|
|
|
namespace MediaWiki\Extension\Gadgets;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2021-11-26 11:27:30 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-01-19 20:52:17 +00:00
|
|
|
use MediaWiki\Permissions\Authority;
|
|
|
|
use MediaWiki\User\UserIdentity;
|
2022-02-06 18:54:47 +00:00
|
|
|
use ResourceLoader;
|
|
|
|
use Skin;
|
2021-11-26 11:27:30 +00:00
|
|
|
|
2010-11-11 18:19:57 +00:00
|
|
|
/**
|
|
|
|
* Wrapper for one gadget.
|
|
|
|
*/
|
|
|
|
class Gadget {
|
|
|
|
/**
|
|
|
|
* Increment this when changing class structure
|
|
|
|
*/
|
2022-03-09 13:57:57 +00:00
|
|
|
public const GADGET_CLASS_VERSION = 13;
|
2015-04-30 05:33:04 +00:00
|
|
|
|
2020-05-20 00:10:09 +00:00
|
|
|
public const CACHE_TTL = 86400;
|
2010-11-11 18:19:57 +00:00
|
|
|
|
2020-12-17 19:07:37 +00:00
|
|
|
/** @var string[] */
|
|
|
|
private $scripts = [];
|
|
|
|
/** @var string[] */
|
|
|
|
private $styles = [];
|
|
|
|
/** @var string[] */
|
2021-10-17 13:05:15 +00:00
|
|
|
private $datas = [];
|
|
|
|
/** @var string[] */
|
2020-12-17 19:07:37 +00:00
|
|
|
private $dependencies = [];
|
|
|
|
/** @var string[] */
|
|
|
|
private $peers = [];
|
|
|
|
/** @var string[] */
|
|
|
|
private $messages = [];
|
|
|
|
/** @var string|null */
|
|
|
|
private $name;
|
|
|
|
/** @var string|null */
|
|
|
|
private $definition;
|
|
|
|
/** @var bool */
|
|
|
|
private $resourceLoaded = false;
|
2022-01-30 13:15:25 +00:00
|
|
|
/** @var bool */
|
|
|
|
private $requiresES6 = false;
|
2020-12-17 19:07:37 +00:00
|
|
|
/** @var string[] */
|
|
|
|
private $requiredRights = [];
|
|
|
|
/** @var string[] */
|
2021-12-14 13:10:22 +00:00
|
|
|
private $requiredActions = [];
|
|
|
|
/** @var string[] */
|
2020-12-17 19:07:37 +00:00
|
|
|
private $requiredSkins = [];
|
2023-06-29 00:57:04 +00:00
|
|
|
/** @var string[] used in Gadget::isTargetSupported */
|
2023-06-21 15:24:03 +00:00
|
|
|
private $targets = [ 'desktop', 'mobile' ];
|
2020-12-17 19:07:37 +00:00
|
|
|
/** @var bool */
|
|
|
|
private $onByDefault = false;
|
|
|
|
/** @var bool */
|
|
|
|
private $hidden = false;
|
2021-10-17 13:05:15 +00:00
|
|
|
/** @var bool */
|
|
|
|
private $package = false;
|
2020-12-17 19:07:37 +00:00
|
|
|
/** @var string */
|
|
|
|
private $type = '';
|
|
|
|
/** @var string|null */
|
|
|
|
private $category;
|
2022-01-11 09:03:44 +00:00
|
|
|
/** @var bool */
|
|
|
|
private $supportsUrlLoad = false;
|
2010-11-11 18:19:57 +00:00
|
|
|
|
2015-07-18 07:26:25 +00:00
|
|
|
public function __construct( array $options ) {
|
|
|
|
foreach ( $options as $member => $option ) {
|
|
|
|
switch ( $member ) {
|
|
|
|
case 'scripts':
|
|
|
|
case 'styles':
|
2021-10-17 13:05:15 +00:00
|
|
|
case 'datas':
|
2015-07-18 07:26:25 +00:00
|
|
|
case 'dependencies':
|
2016-11-18 04:54:17 +00:00
|
|
|
case 'peers':
|
2015-08-03 06:34:16 +00:00
|
|
|
case 'messages':
|
2015-07-18 07:26:25 +00:00
|
|
|
case 'name':
|
|
|
|
case 'definition':
|
|
|
|
case 'resourceLoaded':
|
2022-01-30 13:15:25 +00:00
|
|
|
case 'requiresES6':
|
2015-07-18 07:26:25 +00:00
|
|
|
case 'requiredRights':
|
2021-12-14 13:10:22 +00:00
|
|
|
case 'requiredActions':
|
2015-07-18 07:26:25 +00:00
|
|
|
case 'requiredSkins':
|
|
|
|
case 'targets':
|
|
|
|
case 'onByDefault':
|
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
|
|
|
case 'type':
|
2015-08-03 06:34:16 +00:00
|
|
|
case 'hidden':
|
2021-10-17 13:05:15 +00:00
|
|
|
case 'package':
|
2015-07-18 07:26:25 +00:00
|
|
|
case 'category':
|
2022-01-11 09:03:44 +00:00
|
|
|
case 'supportsUrlLoad':
|
2015-07-18 07:26:25 +00:00
|
|
|
$this->{$member} = $option;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new InvalidArgumentException( "Unrecognized '$member' parameter" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 06:37:32 +00:00
|
|
|
/**
|
2022-08-27 10:54:28 +00:00
|
|
|
* Create a serialized array based on the metadata in a GadgetDefinitionContent object,
|
|
|
|
* from which a Gadget object can be constructed.
|
2015-08-03 06:37:32 +00:00
|
|
|
*
|
|
|
|
* @param string $id
|
2022-08-27 10:54:28 +00:00
|
|
|
* @param array $data
|
|
|
|
* @return array
|
2015-08-03 06:37:32 +00:00
|
|
|
*/
|
2022-08-27 10:54:28 +00:00
|
|
|
public static function serializeDefinition( string $id, array $data ): array {
|
2021-05-04 07:24:31 +00:00
|
|
|
$prefixGadgetNs = static function ( $page ) {
|
2015-08-03 06:37:32 +00:00
|
|
|
return 'Gadget:' . $page;
|
|
|
|
};
|
2022-08-27 10:54:28 +00:00
|
|
|
return [
|
2015-08-03 06:37:32 +00:00
|
|
|
'name' => $id,
|
|
|
|
'resourceLoaded' => true,
|
2022-01-30 13:15:25 +00:00
|
|
|
'requiresES6' => $data['settings']['requiresES6'],
|
2015-08-03 06:37:32 +00:00
|
|
|
'requiredRights' => $data['settings']['rights'],
|
|
|
|
'onByDefault' => $data['settings']['default'],
|
2021-10-17 13:05:15 +00:00
|
|
|
'package' => $data['settings']['package'],
|
2015-08-03 06:37:32 +00:00
|
|
|
'hidden' => $data['settings']['hidden'],
|
2023-03-06 07:46:37 +00:00
|
|
|
'targets' => $data['settings']['targets'],
|
2021-12-14 13:10:22 +00:00
|
|
|
'requiredActions' => $data['settings']['actions'],
|
2015-08-03 06:37:32 +00:00
|
|
|
'requiredSkins' => $data['settings']['skins'],
|
|
|
|
'category' => $data['settings']['category'],
|
2022-01-11 09:03:44 +00:00
|
|
|
'supportsUrlLoad' => $data['settings']['supportsUrlLoad'],
|
2015-08-03 06:37:32 +00:00
|
|
|
'scripts' => array_map( $prefixGadgetNs, $data['module']['scripts'] ),
|
|
|
|
'styles' => array_map( $prefixGadgetNs, $data['module']['styles'] ),
|
2021-10-17 13:05:15 +00:00
|
|
|
'datas' => array_map( $prefixGadgetNs, $data['module']['datas'] ),
|
2015-08-03 06:37:32 +00:00
|
|
|
'dependencies' => $data['module']['dependencies'],
|
2016-11-18 04:54:17 +00:00
|
|
|
'peers' => $data['module']['peers'],
|
2015-08-03 06:37:32 +00:00
|
|
|
'messages' => $data['module']['messages'],
|
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
|
|
|
'type' => $data['module']['type'],
|
2016-12-28 10:25:47 +00:00
|
|
|
];
|
2022-08-27 10:54:28 +00:00
|
|
|
}
|
2015-08-03 06:37:32 +00:00
|
|
|
|
2022-08-27 10:54:28 +00:00
|
|
|
/**
|
|
|
|
* Serialize to an array
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toArray(): array {
|
|
|
|
return [
|
|
|
|
'name' => $this->name,
|
|
|
|
'definition' => $this->definition,
|
|
|
|
'resourceLoaded' => $this->resourceLoaded,
|
|
|
|
'requiresES6' => $this->requiresES6,
|
|
|
|
'requiredRights' => $this->requiredRights,
|
|
|
|
'onByDefault' => $this->onByDefault,
|
|
|
|
'package' => $this->package,
|
|
|
|
'hidden' => $this->hidden,
|
|
|
|
'requiredActions' => $this->requiredActions,
|
|
|
|
'requiredSkins' => $this->requiredSkins,
|
|
|
|
'category' => $this->category,
|
|
|
|
'supportsUrlLoad' => $this->supportsUrlLoad,
|
|
|
|
'scripts' => $this->scripts,
|
|
|
|
'styles' => $this->styles,
|
|
|
|
'datas' => $this->datas,
|
|
|
|
'dependencies' => $this->dependencies,
|
|
|
|
'peers' => $this->peers,
|
|
|
|
'messages' => $this->messages,
|
|
|
|
'type' => $this->type,
|
|
|
|
'targets' => $this->targets,
|
|
|
|
];
|
2015-08-03 06:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a placeholder object to use if a gadget doesn't exist
|
|
|
|
*
|
|
|
|
* @param string $id name
|
|
|
|
* @return Gadget
|
|
|
|
*/
|
|
|
|
public static function newEmptyGadget( $id ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
return new self( [ 'name' => $id ] );
|
2015-08-03 06:37:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-18 07:26:25 +00:00
|
|
|
/**
|
|
|
|
* Whether the provided gadget id is valid
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isValidGadgetID( $id ) {
|
2017-07-23 07:23:06 +00:00
|
|
|
return strlen( $id ) > 0 && ResourceLoader::isValidModuleName( self::getModuleName( $id ) );
|
2015-07-18 07:26:25 +00:00
|
|
|
}
|
|
|
|
|
2010-11-11 18:19:57 +00:00
|
|
|
/**
|
2017-08-27 14:07:08 +00:00
|
|
|
* @return string Gadget name
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
|
|
|
public function getName() {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2021-05-28 18:00:46 +00:00
|
|
|
/**
|
|
|
|
* @return string Message key
|
|
|
|
*/
|
|
|
|
public function getDescriptionMessageKey() {
|
|
|
|
return "gadget-{$this->getName()}";
|
|
|
|
}
|
|
|
|
|
2011-04-15 19:41:47 +00:00
|
|
|
/**
|
2017-08-27 14:07:08 +00:00
|
|
|
* @return string Gadget description parsed into HTML
|
2011-04-15 19:41:47 +00:00
|
|
|
*/
|
|
|
|
public function getDescription() {
|
2021-05-28 18:00:46 +00:00
|
|
|
return wfMessage( $this->getDescriptionMessageKey() )->parse();
|
2011-04-15 19:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-27 14:07:08 +00:00
|
|
|
* @return string Wikitext of gadget description
|
2011-04-15 19:41:47 +00:00
|
|
|
*/
|
|
|
|
public function getRawDescription() {
|
2021-05-28 18:00:46 +00:00
|
|
|
return wfMessage( $this->getDescriptionMessageKey() )->plain();
|
2011-04-15 19:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-27 14:07:08 +00:00
|
|
|
* @return string Name of category (aka section) our gadget belongs to. Empty string if none.
|
2011-04-15 19:41:47 +00:00
|
|
|
*/
|
|
|
|
public function getCategory() {
|
|
|
|
return $this->category;
|
|
|
|
}
|
|
|
|
|
2010-11-11 18:19:57 +00:00
|
|
|
/**
|
2015-08-03 06:37:32 +00:00
|
|
|
* @param string $id Name of gadget
|
|
|
|
* @return string Name of ResourceLoader module for the gadget
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
2015-08-03 06:37:32 +00:00
|
|
|
public static function getModuleName( $id ) {
|
|
|
|
return "ext.gadget.{$id}";
|
2010-11-11 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-03 19:01:52 +00:00
|
|
|
* Checks whether this gadget is enabled for given user
|
|
|
|
*
|
2022-01-19 20:52:17 +00:00
|
|
|
* @param UserIdentity $user user to check against
|
2017-08-27 14:07:08 +00:00
|
|
|
* @return bool
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
2022-01-19 20:52:17 +00:00
|
|
|
public function isEnabled( UserIdentity $user ) {
|
2021-11-26 11:27:30 +00:00
|
|
|
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
|
|
|
|
return (bool)$userOptionsLookup->getOption( $user, "gadget-{$this->name}", $this->onByDefault );
|
2011-04-03 19:01:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether given user has permissions to use this gadget
|
|
|
|
*
|
2022-01-19 20:52:17 +00:00
|
|
|
* @param Authority $user The user to check against
|
2017-08-27 14:07:08 +00:00
|
|
|
* @return bool
|
2011-04-03 19:01:52 +00:00
|
|
|
*/
|
2022-01-19 20:52:17 +00:00
|
|
|
public function isAllowed( Authority $user ) {
|
2021-01-19 21:15:45 +00:00
|
|
|
if ( count( $this->requiredRights ) ) {
|
|
|
|
return $user->isAllowedAll( ...$this->requiredRights );
|
|
|
|
}
|
|
|
|
return true;
|
2010-11-11 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
2011-04-12 18:09:50 +00:00
|
|
|
/**
|
2017-05-30 18:38:47 +00:00
|
|
|
* @return bool Whether this gadget is on by default for everyone
|
|
|
|
* (but can be disabled in preferences)
|
2011-04-12 18:09:50 +00:00
|
|
|
*/
|
|
|
|
public function isOnByDefault() {
|
|
|
|
return $this->onByDefault;
|
|
|
|
}
|
|
|
|
|
2015-08-03 06:34:16 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isHidden() {
|
|
|
|
return $this->hidden;
|
|
|
|
}
|
|
|
|
|
2021-10-17 13:05:15 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isPackaged(): bool {
|
|
|
|
// A packaged gadget needs to have a main script, so there must be at least one script
|
|
|
|
return $this->package && $this->supportsResourceLoader() && count( $this->scripts ) > 0;
|
|
|
|
}
|
|
|
|
|
2021-12-14 13:10:22 +00:00
|
|
|
/**
|
|
|
|
* @param string $action The action name
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isActionSupported( string $action ): bool {
|
|
|
|
if ( count( $this->requiredActions ) === 0 ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Don't require specifying 'submit' action in addition to 'edit'
|
2023-07-01 13:26:33 +00:00
|
|
|
if ( $action === 'submit' ) {
|
2021-12-14 13:10:22 +00:00
|
|
|
$action = 'edit';
|
|
|
|
}
|
|
|
|
return in_array( $action, $this->requiredActions );
|
|
|
|
}
|
|
|
|
|
2023-06-29 00:57:04 +00:00
|
|
|
/**
|
|
|
|
* Check whether the gadget should load on the mobile domain based on its definition.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isTargetSupported( bool $isMobileView ): bool {
|
|
|
|
if ( $isMobileView ) {
|
|
|
|
return in_array( 'mobile', $this->targets );
|
|
|
|
} else {
|
|
|
|
return in_array( 'desktop', $this->targets );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-20 06:27:26 +00:00
|
|
|
/**
|
|
|
|
* Check if this gadget is compatible with a skin
|
|
|
|
*
|
|
|
|
* @param Skin $skin The skin to check against
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isSkinSupported( Skin $skin ) {
|
|
|
|
return ( count( $this->requiredSkins ) === 0
|
2021-10-09 00:34:45 +00:00
|
|
|
|| in_array( $skin->getSkinName(), $this->requiredSkins )
|
|
|
|
);
|
2016-07-20 06:27:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 09:03:44 +00:00
|
|
|
/**
|
|
|
|
* @return bool Whether the gadget can be loaded with `?withgadget` query parameter.
|
|
|
|
*/
|
|
|
|
public function supportsUrlLoad() {
|
|
|
|
return $this->supportsUrlLoad;
|
|
|
|
}
|
|
|
|
|
2010-11-11 18:19:57 +00:00
|
|
|
/**
|
2017-08-27 14:07:08 +00:00
|
|
|
* @return bool Whether all of this gadget's JS components support ResourceLoader
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
|
|
|
public function supportsResourceLoader() {
|
|
|
|
return $this->resourceLoaded;
|
|
|
|
}
|
|
|
|
|
2022-01-30 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* @return bool Whether this gadget requires ES6
|
|
|
|
*/
|
|
|
|
public function requiresES6(): bool {
|
|
|
|
return $this->requiresES6 && !$this->onByDefault;
|
|
|
|
}
|
|
|
|
|
2010-11-11 18:19:57 +00:00
|
|
|
/**
|
2019-01-04 22:11:51 +00:00
|
|
|
* @return bool Whether this gadget has resources that can be loaded via ResourceLoader
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
|
|
|
public function hasModule() {
|
2021-10-09 00:34:45 +00:00
|
|
|
return (
|
|
|
|
count( $this->styles ) + ( $this->supportsResourceLoader() ? count( $this->scripts ) : 0 )
|
|
|
|
) > 0;
|
2010-11-11 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-27 14:07:08 +00:00
|
|
|
* @return string Definition for this gadget from MediaWiki:gadgets-definition
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
|
|
|
public function getDefinition() {
|
|
|
|
return $this->definition;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-27 14:07:08 +00:00
|
|
|
* @return array Array of pages with JS (including namespace)
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
|
|
|
public function getScripts() {
|
|
|
|
return $this->scripts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-27 14:07:08 +00:00
|
|
|
* @return array Array of pages with CSS (including namespace)
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
|
|
|
public function getStyles() {
|
|
|
|
return $this->styles;
|
|
|
|
}
|
|
|
|
|
2021-10-17 13:05:15 +00:00
|
|
|
/**
|
|
|
|
* @return array Array of pages with JSON (including namespace)
|
|
|
|
*/
|
|
|
|
public function getJSONs(): array {
|
|
|
|
return $this->isPackaged() ? $this->datas : [];
|
|
|
|
}
|
|
|
|
|
2010-11-11 18:19:57 +00:00
|
|
|
/**
|
2017-08-27 14:07:08 +00:00
|
|
|
* @return array Array of all of this gadget's resources
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
|
|
|
public function getScriptsAndStyles() {
|
2021-10-17 13:05:15 +00:00
|
|
|
return array_merge( $this->scripts, $this->styles, $this->getJSONs() );
|
2010-11-11 18:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns list of scripts that don't support ResourceLoader
|
2019-06-04 15:32:42 +00:00
|
|
|
* @return string[]
|
2010-11-11 18:19:57 +00:00
|
|
|
*/
|
|
|
|
public function getLegacyScripts() {
|
|
|
|
if ( $this->supportsResourceLoader() ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
return [];
|
2010-11-11 18:19:57 +00:00
|
|
|
}
|
|
|
|
return $this->scripts;
|
|
|
|
}
|
|
|
|
|
2010-11-13 18:45:21 +00:00
|
|
|
/**
|
|
|
|
* Returns names of resources this gadget depends on
|
2019-06-04 15:32:42 +00:00
|
|
|
* @return string[]
|
2010-11-13 18:45:21 +00:00
|
|
|
*/
|
|
|
|
public function getDependencies() {
|
|
|
|
return $this->dependencies;
|
|
|
|
}
|
|
|
|
|
2016-11-18 04:54:17 +00:00
|
|
|
/**
|
|
|
|
* Get list of extra modules that should be loaded when this gadget is enabled
|
|
|
|
*
|
|
|
|
* Primary use case is to allow a Gadget that includes JavaScript to also load
|
|
|
|
* a (usually, hidden) styles-type module to be applied to the page. Dependencies
|
|
|
|
* don't work for this use case as those would not be part of page rendering.
|
|
|
|
*
|
2019-06-04 15:32:42 +00:00
|
|
|
* @return string[]
|
2016-11-18 04:54:17 +00:00
|
|
|
*/
|
|
|
|
public function getPeers() {
|
|
|
|
return $this->peers;
|
|
|
|
}
|
|
|
|
|
2015-08-03 06:34:16 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getMessages() {
|
|
|
|
return $this->messages;
|
|
|
|
}
|
|
|
|
|
2011-04-03 19:01:52 +00:00
|
|
|
/**
|
|
|
|
* Returns array of permissions required by this gadget
|
2019-06-04 15:32:42 +00:00
|
|
|
* @return string[]
|
2011-04-03 19:01:52 +00:00
|
|
|
*/
|
|
|
|
public function getRequiredRights() {
|
|
|
|
return $this->requiredRights;
|
|
|
|
}
|
|
|
|
|
2021-12-14 13:10:22 +00:00
|
|
|
/**
|
|
|
|
* Returns array of page actions on which the gadget runs
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getRequiredActions() {
|
|
|
|
return $this->requiredActions;
|
|
|
|
}
|
|
|
|
|
2011-10-22 19:09:25 +00:00
|
|
|
/**
|
|
|
|
* Returns array of skins where this gadget works
|
2019-06-04 15:32:42 +00:00
|
|
|
* @return string[]
|
2011-10-22 19:09:25 +00:00
|
|
|
*/
|
|
|
|
public function getRequiredSkins() {
|
|
|
|
return $this->requiredSkins;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Returns the load type of this Gadget's ResourceLoader module
|
2017-05-12 17:37:49 +00:00
|
|
|
* @return string 'styles' or '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() {
|
|
|
|
if ( $this->type === 'styles' || $this->type === 'general' ) {
|
|
|
|
return $this->type;
|
|
|
|
}
|
2017-05-12 17:37:49 +00:00
|
|
|
// Similar to ResourceLoaderWikiModule default
|
2016-11-17 01:53:10 +00:00
|
|
|
if ( $this->styles && !$this->scripts && !$this->dependencies ) {
|
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
|
|
|
return 'styles';
|
|
|
|
}
|
2021-10-09 00:34:45 +00:00
|
|
|
|
|
|
|
return '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
|
|
|
}
|
|
|
|
}
|