mediawiki-extensions-Gadgets/includes/Gadget.php

571 lines
15 KiB
PHP
Raw Normal View History

2010-11-13 19:13:43 +00:00
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Extension\Gadgets;
use InvalidArgumentException;
use MediaWiki\MediaWikiServices;
use MediaWiki\Permissions\Authority;
use MediaWiki\ResourceLoader\ResourceLoader;
use MediaWiki\User\UserIdentity;
use Skin;
/**
* Represents one gadget definition.
*
* @copyright 2007 Daniel Kinzler
*/
class Gadget {
/**
* Increment this when changing class structure
*/
public const GADGET_CLASS_VERSION = 18;
public const CACHE_TTL = 86400;
/** @var string[] */
private $dependencies = [];
/** @var string[] */
private array $pages = [];
/** @var string[] */
private $peers = [];
/** @var string[] */
private $messages = [];
/** @var string|null */
private $name;
/** @var string|null */
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
private $definition = null;
/** @var bool */
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
private bool $resourceLoaded = false;
/** @var bool */
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
private bool $requiresES6 = false;
/** @var string[] */
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
private array $requiredRights = [];
/** @var string[] */
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
private array $requiredActions = [];
/** @var string[] */
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
private array $requiredSkins = [];
/** @var int[]|string[] */
private array $requiredNamespaces = [];
/** @var string[] */
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
private array $requiredCategories = [];
/** @var string[] */
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
private array $requiredContentModels = [];
/** @var bool */
private $onByDefault = false;
/** @var bool */
private $hidden = false;
/** @var bool */
private $package = false;
/** @var string */
private $type = '';
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
/** @var string */
private string $category = '';
/** @var bool */
private $supportsUrlLoad = false;
public function __construct( array $options ) {
foreach ( $options as $member => $option ) {
switch ( $member ) {
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
case 'category':
case 'definition':
case 'dependencies':
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
case 'hidden':
case 'messages':
case 'name':
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
case 'onByDefault':
case 'package':
case 'pages':
case 'peers':
case 'requiredActions':
case 'requiredCategories':
case 'requiredContentModels':
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
case 'requiredNamespaces':
case 'requiredRights':
case 'requiredSkins':
case 'requiresES6':
case 'resourceLoaded':
case 'supportsUrlLoad':
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
case 'type':
$this->{$member} = $option;
break;
default:
throw new InvalidArgumentException( "Unrecognized '$member' parameter" );
}
}
}
/**
* Create a serialized array based on the metadata in a GadgetDefinitionContent object,
* from which a Gadget object can be constructed.
*
* @param string $id
* @param array $data
* @return array
*/
public static function serializeDefinition( string $id, array $data ): array {
$prefixGadgetNs = static function ( $page ) {
Goodbye Gadget/Gadget_definition namespaces! == What == * Remove the empty Gadget and Gadget_definition namespaces. * Remove the "gadgets-definition-edit" user right. * Remove need for custom namespace permissions that previously had to extend editsitejs to apply to NS_GADGET. == Why == Simplify the (unused) "GadgetDefinitionNamespaceRepo" backend for Gadgets 2.0 by making it less radically different from the status quo. The experimental 2.0 branch will now make use of the "gadget definition" content model via "MediaWiki:Gadgets/<id>.json" pages, instead of through a dedicated namespace. When I first worked the Gadgets 2.0 branch, content models were not a thing in MediaWiki, and interface-admin wasn't a thing yet either. Now that we have per-page permissions and per-page content models, we don't really need a separate namespace. This follows the principle of least surprise, and fits well with other interface admin and site configuration tools such as: - Citoid, MediaWiki:Citoid-template-type-map.json, - VisualEditor, MediaWiki:Visualeditor-template-tools-definition.json, - AbuseFilter, MediaWiki:BlockedExternalDomains.json, - the upcoming "Community Config" initiative. If/when we develop the SpecialPage GUI for editing gadget definitions, this can save its data to these pages the same as it would in any other namespace. Similar to how Special:BlockedExternalDomains operates on MediaWiki:BlockedExternalDomains.json. See also bf1d6b3e93 (I6ffd5e9467), which recently removed the gadgets-edit user right in favour of the editsite{css,js,json} rights. Change-Id: I5b04ab251552e839087d0a8a6923d205adc7f771
2023-12-05 23:28:45 +00:00
return GadgetRepo::RESOURCE_TITLE_PREFIX . $page;
};
return [
'category' => $data['settings']['category'],
'dependencies' => $data['module']['dependencies'],
'hidden' => $data['settings']['hidden'],
'messages' => $data['module']['messages'],
'name' => $id,
'onByDefault' => $data['settings']['default'],
'package' => $data['settings']['package'],
'pages' => array_map( $prefixGadgetNs, $data['module']['pages'] ),
'peers' => $data['module']['peers'],
'requiredActions' => $data['settings']['actions'],
'requiredCategories' => $data['settings']['categories'],
'requiredContentModels' => $data['settings']['contentModels'],
'requiredNamespaces' => $data['settings']['namespaces'],
'requiredRights' => $data['settings']['rights'],
'requiredSkins' => $data['settings']['skins'],
'requiresES6' => $data['settings']['requiresES6'],
'resourceLoaded' => true,
'supportsUrlLoad' => $data['settings']['supportsUrlLoad'],
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'],
];
}
/**
* Serialize to an array
* @return array
*/
public function toArray(): array {
return [
'category' => $this->category,
'dependencies' => $this->dependencies,
'hidden' => $this->hidden,
'messages' => $this->messages,
'name' => $this->name,
'onByDefault' => $this->onByDefault,
'package' => $this->package,
'pages' => $this->pages,
'peers' => $this->peers,
'requiredActions' => $this->requiredActions,
'requiredCategories' => $this->requiredCategories,
'requiredContentModels' => $this->requiredContentModels,
'requiredNamespaces' => $this->requiredNamespaces,
'requiredRights' => $this->requiredRights,
'requiredSkins' => $this->requiredSkins,
'requiresES6' => $this->requiresES6,
'resourceLoaded' => $this->resourceLoaded,
'supportsUrlLoad' => $this->supportsUrlLoad,
'type' => $this->type,
// Legacy (specific to MediaWikiGadgetsDefinitionRepo)
'definition' => $this->definition,
];
}
/**
* Get a placeholder object to use if a gadget doesn't exist
*
* @param string $id name
* @return Gadget
*/
public static function newEmptyGadget( $id ) {
return new self( [ 'name' => $id ] );
}
/**
* Whether the provided gadget id is valid
*
* @param string $id
* @return bool
*/
public static function isValidGadgetID( $id ) {
return $id !== '' && ResourceLoader::isValidModuleName( self::getModuleName( $id ) );
}
/**
* @return string Gadget name
*/
public function getName() {
return $this->name;
}
/**
* @return string Message key
*/
public function getDescriptionMessageKey() {
return 'gadget-' . $this->name;
}
/**
* @return string Gadget description parsed into HTML
*/
public function getDescription() {
return wfMessage( $this->getDescriptionMessageKey() )->parse();
}
/**
* @return string Wikitext of gadget description
*/
public function getRawDescription() {
return wfMessage( $this->getDescriptionMessageKey() )->plain();
}
/**
* @return string Name of category (aka section) our gadget belongs to. Empty string if none.
*/
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
public function getCategory(): string {
return $this->category;
}
/**
* @param string $id Name of gadget
* @return string Name of ResourceLoader module for the gadget
*/
public static function getModuleName( $id ) {
return "ext.gadget.{$id}";
}
/**
* Checks whether this gadget is enabled for given user
*
* @param UserIdentity $user user to check against
* @return bool
*/
public function isEnabled( UserIdentity $user ) {
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
return (bool)$userOptionsLookup->getOption( $user, "gadget-{$this->name}", $this->onByDefault );
}
/**
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
* Checks whether a given user may enable this gadget
*
* @param Authority $user The user to check against
* @return bool
*/
public function isAllowed( Authority $user ) {
return !$this->requiredRights || $user->isAllowedAll( ...$this->requiredRights );
}
/**
* @return bool Whether this gadget is on by default for everyone
* (but can be disabled in preferences)
*/
public function isOnByDefault() {
return $this->onByDefault;
}
/**
* @return bool
*/
public function isHidden() {
return $this->hidden;
}
/**
* @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() && $this->getScripts() !== [];
}
/**
* Whether to load the gadget on a given page action.
*
* @param string $action 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'
if ( $action === 'submit' ) {
$action = 'edit';
}
return in_array( $action, $this->requiredActions, true );
}
/**
* Whether to load the gadget on pages in a given namespace ID.
*
* @param int $namespace Namespace ID
* @return bool
*/
public function isNamespaceSupported( int $namespace ) {
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
// This is intentionally a non-strict in_array() because
// MediaWikiGadgetsDefinitionRepo sets numerical strings.
return !$this->requiredNamespaces || in_array( $namespace, $this->requiredNamespaces );
}
/**
* Whether to load the gadget on pages in any of the given categories
*
* @param array $categories Category names (category title text, no namespace prefix, no dbkey-underscores)
* @return bool
*/
public function isCategorySupported( array $categories ) {
if ( !$this->requiredCategories ) {
return true;
}
foreach ( $categories as $category ) {
if ( in_array( $category, $this->requiredCategories, true ) ) {
return true;
}
}
return false;
}
/**
* Check if this gadget is compatible with a skin
*
* @param Skin $skin
* @return bool
*/
public function isSkinSupported( Skin $skin ) {
return !$this->requiredSkins || in_array( $skin->getSkinName(), $this->requiredSkins, true );
}
/**
* Check if this gadget is compatible with the given content model
*
* @param string $contentModel The content model ID
* @return bool
*/
public function isContentModelSupported( string $contentModel ) {
return !$this->requiredContentModels || in_array( $contentModel, $this->requiredContentModels );
}
/**
* @return bool Whether the gadget can be loaded with `?withgadget` query parameter.
*/
public function supportsUrlLoad() {
return $this->supportsUrlLoad;
}
/**
* @return bool Whether all of this gadget's JS components support ResourceLoader
*/
public function supportsResourceLoader() {
return $this->resourceLoaded;
}
/**
* @return bool Whether this gadget requires ES6
*/
public function requiresES6(): bool {
return $this->requiresES6 && !$this->onByDefault;
}
/**
* @return bool Whether this gadget has resources that can be loaded via ResourceLoader
*/
public function hasModule() {
return $this->getStyles() || ( $this->supportsResourceLoader() && $this->getScripts() );
}
/**
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
* @return string|null Definition for this gadget from MediaWiki:Gadgets-definition,
* or null if MediaWikiGadgetsJsonRepo is used.
*/
public function getDefinition() {
return $this->definition;
}
/**
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
* @return string[] JS page names (including namespace)
*/
public function getScripts() {
return array_values( array_filter( $this->pages, static function ( $page ) {
return str_ends_with( $page, '.js' );
} ) );
}
/**
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
* @return string[] CSS page names (including namespace)
*/
public function getStyles() {
return array_values( array_filter( $this->pages, static function ( $page ) {
return str_ends_with( $page, '.css' );
} ) );
}
/**
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
* @return string[] JSON page names (including namespace)
*/
public function getJSONs(): array {
return array_values( array_filter( $this->pages, static function ( $page ) {
return str_ends_with( $page, '.json' );
} ) );
}
/**
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
* @return string[] All page names for this gadget's resources
*/
public function getScriptsAndStyles() {
return array_merge( $this->getScripts(), $this->getStyles(), $this->getJSONs() );
}
/**
* Returns list of scripts that don't support ResourceLoader
* @return string[]
*/
public function getLegacyScripts() {
return $this->supportsResourceLoader() ? [] : $this->getScripts();
}
/**
* Returns names of resources this gadget depends on
* @return string[]
*/
public function getDependencies() {
return $this->dependencies;
}
/**
* 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.
*
* @return string[]
*/
public function getPeers() {
return $this->peers;
}
/**
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
* @return string[]
*/
public function getMessages() {
return $this->messages;
}
/**
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
* Get user rights required to enable this gadget
* @return string[]
*/
public function getRequiredRights() {
return $this->requiredRights;
}
/**
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
* Get page actions on which the gadget loads
* @return string[]
*/
public function getRequiredActions() {
return $this->requiredActions;
}
/**
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
* Get page namespaces in which this gadget loads
*
* Use isNamespaceSupported() instead for basic checks, as
* namespace IDs may be returned as numerical strings.
*
* Unknown namespaces and non-numerical values result in warnings
* on Special:Gadgets, via GadgetRepo::checkInvalidLoadConditions.
*
* @return int[]|string[]
*/
public function getRequiredNamespaces() {
return $this->requiredNamespaces;
}
/**
* Returns categories in which this gadget loads
* @return string[]
*/
public function getRequiredCategories() {
return $this->requiredCategories;
}
2011-10-22 19:09:25 +00:00
/**
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
* Get skins in which this gadget loads
* @return string[]
2011-10-22 19:09:25 +00:00
*/
public function getRequiredSkins() {
return $this->requiredSkins;
}
/**
Improve localisation on Special:Gadgets, minor code cleanups For each item, either display human-readable and translated text, or display a technical non-translatable identifier as `<code>`, with optional localised text in the title attribute. * Re-format "rights" as a sentence instead of a bullet list. It was the only one using a bullet list, which made it feel a bit long. * Re-format "actions" as `<code>` since they are not localised. * Re-format "contentModels" as `<code>`, and add the localised display name in a title attribute, same as we do with "rights" already. ContentHandler::getLocalizedName() is also used already on Special:ChangeContentModel. * Fix "contentModels" to set `needLineBreakAfter = true`, otherwise if a gadget also sets "supportsUrlLoad", then that sentence is appended to the previous line. Update phrasing and sorting to be consistent everywhere, and adopt native PHP types where possible. In most cases, I made things alphabetical, with the exception of Special:Gadgets user interface output, and Gadget class methods, which both follow the order of most recently added feature last (rights, skins, actions, namespaces, contentmodels, categories). Highlights: * Fix namespace IDs type. These can be strings when they are parsed from the gadget definition text, not always integers. * Add explicit default for 'category'. In theory not needed because MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where `category: ''` is part of both the initial page content, as well as merged via getDefaultMetadata. This default benefits simpler test cases, and static analysis, since the Gadget class constructor does not (yet) require it. Without this, getCategory() could TypeError due to returning null. Bug: T63007 Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-16 23:32:44 +00:00
* Get page content models for which this gadget loads
* @return string[]
*/
public function getRequiredContentModels() {
return $this->requiredContentModels;
}
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
* @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;
}
// Similar to ResourceLoaderWikiModule default
if ( $this->getStyles() && !$this->getScripts() && !$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';
}
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
}
/**
* Get validation warnings
* @return string[]
*/
public function getValidationWarnings(): array {
$warnings = [];
// Default gadget requiring ES6
if ( $this->onByDefault && $this->requiresES6 ) {
$warnings[] = "gadgets-validate-es6default";
}
// Gadget containing files with uncrecognised suffixes
if ( count( array_diff( $this->pages, $this->getScriptsAndStyles() ) ) !== 0 ) {
$warnings[] = "gadgets-validate-unknownpages";
}
// Non-package gadget containing JSON files
if ( !$this->package && count( $this->getJSONs() ) > 0 ) {
$warnings[] = "gadgets-validate-json";
}
// Package gadget without a script file in it (to serve as entry point)
if ( $this->package && count( $this->getScripts() ) === 0 ) {
$warnings[] = "gadgets-validate-noentrypoint";
}
// Gadget with type=styles having non-CSS files
if ( $this->type === 'styles' && count( $this->getScripts() ) > 0 ) {
$warnings[] = "gadgets-validate-scriptsnotallowed";
}
// Style-only gadgets having peers
if ( $this->getType() === 'styles' && count( $this->peers ) > 0 ) {
$warnings[] = "gadgets-validate-stylepeers";
}
return $warnings;
}
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
}