mediawiki-extensions-Gadgets/includes/Gadget.php

323 lines
7.3 KiB
PHP
Raw Normal View History

2010-11-13 19:13:43 +00:00
<?php
/**
* Gadgets extension - lets users select custom javascript gadgets
*
* For more info see https://www.mediawiki.org/wiki/Extension:Gadgets
*
* @file
* @ingroup Extensions
* @author Daniel Kinzler, brightbyte.de
* @copyright © 2007 Daniel Kinzler
* @license GPL-2.0-or-later
*/
/**
* Wrapper for one gadget.
*/
class Gadget {
/**
* Increment this when changing class structure
*/
public const GADGET_CLASS_VERSION = 9;
public const CACHE_TTL = 86400;
private $scripts = [],
$styles = [],
$dependencies = [],
$peers = [],
$messages = [],
$name,
$definition,
$resourceLoaded = false,
$requiredRights = [],
$requiredSkins = [],
$targets = [ 'desktop' ],
$onByDefault = false,
$hidden = false,
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 = '',
$category;
public function __construct( array $options ) {
foreach ( $options as $member => $option ) {
switch ( $member ) {
case 'scripts':
case 'styles':
case 'dependencies':
case 'peers':
case 'messages':
case 'name':
case 'definition':
case 'resourceLoaded':
case 'requiredRights':
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':
case 'hidden':
case 'category':
$this->{$member} = $option;
break;
default:
throw new InvalidArgumentException( "Unrecognized '$member' parameter" );
}
}
}
/**
* Create a object based on the metadata in a GadgetDefinitionContent object
*
* @param string $id
* @param GadgetDefinitionContent $content
* @return Gadget
*/
public static function newFromDefinitionContent( $id, GadgetDefinitionContent $content ) {
$data = $content->getAssocArray();
$prefixGadgetNs = function ( $page ) {
return 'Gadget:' . $page;
};
$info = [
'name' => $id,
'resourceLoaded' => true,
'requiredRights' => $data['settings']['rights'],
'onByDefault' => $data['settings']['default'],
'hidden' => $data['settings']['hidden'],
'requiredSkins' => $data['settings']['skins'],
'category' => $data['settings']['category'],
'scripts' => array_map( $prefixGadgetNs, $data['module']['scripts'] ),
'styles' => array_map( $prefixGadgetNs, $data['module']['styles'] ),
'dependencies' => $data['module']['dependencies'],
'peers' => $data['module']['peers'],
'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'],
];
return new self( $info );
}
/**
* 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 strlen( $id ) > 0 && ResourceLoader::isValidModuleName( self::getModuleName( $id ) );
}
/**
* @return string Gadget name
*/
public function getName() {
return $this->name;
}
/**
* @return string Gadget description parsed into HTML
*/
public function getDescription() {
return wfMessage( "gadget-{$this->getName()}" )->parse();
}
/**
* @return string Wikitext of gadget description
*/
public function getRawDescription() {
return wfMessage( "gadget-{$this->getName()}" )->plain();
}
/**
* @return string Name of category (aka section) our gadget belongs to. Empty string if none.
*/
public function getCategory() {
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 User $user user to check against
* @return bool
*/
public function isEnabled( $user ) {
return (bool)$user->getOption( "gadget-{$this->name}", $this->onByDefault );
}
/**
* Checks whether given user has permissions to use this gadget
*
* @param User $user The user to check against
* @return bool
*/
public function isAllowed( User $user ) {
return $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;
}
/**
* 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
|| in_array( $skin->getSkinName(), $this->requiredSkins )
);
}
/**
* @return bool Whether all of this gadget's JS components support ResourceLoader
*/
public function supportsResourceLoader() {
return $this->resourceLoaded;
}
/**
* @return bool Whether this gadget has resources that can be loaded via ResourceLoader
*/
public function hasModule() {
return count( $this->styles )
+ ( $this->supportsResourceLoader() ? count( $this->scripts ) : 0 )
> 0;
}
/**
* @return string Definition for this gadget from MediaWiki:gadgets-definition
*/
public function getDefinition() {
return $this->definition;
}
/**
* @return array Array of pages with JS (including namespace)
*/
public function getScripts() {
return $this->scripts;
}
/**
* @return array Array of pages with CSS (including namespace)
*/
public function getStyles() {
return $this->styles;
}
/**
* @return array Array of all of this gadget's resources
*/
public function getScriptsAndStyles() {
return array_merge( $this->scripts, $this->styles );
}
/**
* @return array
*/
public function getTargets() {
return $this->targets;
}
/**
* Returns list of scripts that don't support ResourceLoader
* @return string[]
*/
public function getLegacyScripts() {
if ( $this->supportsResourceLoader() ) {
return [];
}
return $this->scripts;
}
/**
* 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;
}
/**
* @return array
*/
public function getMessages() {
return $this->messages;
}
/**
* Returns array of permissions required by this gadget
* @return string[]
*/
public function getRequiredRights() {
return $this->requiredRights;
}
2011-10-22 19:09:25 +00:00
/**
* Returns array of skins where this gadget works
* @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
* @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->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';
} else {
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 'general';
}
}
}