2015-07-18 22:40:42 +00:00
|
|
|
<?php
|
2023-10-25 22:32:58 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-11-07 19:36:44 +00:00
|
|
|
|
2022-02-06 18:54:47 +00:00
|
|
|
namespace MediaWiki\Extension\Gadgets;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2017-11-07 19:36:44 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2016-10-11 21:20:18 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-08-16 15:24:01 +00:00
|
|
|
use MediaWiki\Revision\RevisionLookup;
|
2020-04-02 00:38:28 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2023-08-19 04:15:51 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-02-06 18:54:47 +00:00
|
|
|
use ObjectCache;
|
|
|
|
use TextContent;
|
|
|
|
use WANObjectCache;
|
2017-08-31 22:29:00 +00:00
|
|
|
use Wikimedia\Rdbms\Database;
|
2015-07-18 22:40:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gadgets repo powered by MediaWiki:Gadgets-definition
|
|
|
|
*/
|
|
|
|
class MediaWikiGadgetsDefinitionRepo extends GadgetRepo {
|
2022-04-05 19:00:51 +00:00
|
|
|
private const CACHE_VERSION = 4;
|
2015-07-18 22:40:42 +00:00
|
|
|
|
2022-04-05 19:00:51 +00:00
|
|
|
/** @var array|null */
|
|
|
|
private $definitions;
|
2015-07-18 22:40:42 +00:00
|
|
|
|
2021-10-17 13:05:15 +00:00
|
|
|
/** @var string */
|
|
|
|
protected $titlePrefix = 'MediaWiki:Gadget-';
|
|
|
|
|
2022-08-16 15:24:01 +00:00
|
|
|
private WANObjectCache $wanCache;
|
|
|
|
|
|
|
|
private RevisionLookup $revLookup;
|
|
|
|
|
|
|
|
public function __construct( WANObjectCache $wanCache, RevisionLookup $revLookup ) {
|
|
|
|
$this->wanCache = $wanCache;
|
|
|
|
$this->revLookup = $revLookup;
|
|
|
|
}
|
|
|
|
|
2018-02-25 02:41:50 +00:00
|
|
|
/**
|
|
|
|
* @param string $id
|
|
|
|
* @throws InvalidArgumentException
|
2022-04-05 17:47:47 +00:00
|
|
|
* @return Gadget
|
2018-02-25 02:41:50 +00:00
|
|
|
*/
|
2022-04-05 17:47:47 +00:00
|
|
|
public function getGadget( string $id ): Gadget {
|
2015-07-18 22:40:42 +00:00
|
|
|
$gadgets = $this->loadGadgets();
|
|
|
|
if ( !isset( $gadgets[$id] ) ) {
|
|
|
|
throw new InvalidArgumentException( "No gadget registered for '$id'" );
|
|
|
|
}
|
|
|
|
|
2022-08-27 10:54:28 +00:00
|
|
|
return new Gadget( $gadgets[$id] );
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 17:47:47 +00:00
|
|
|
public function getGadgetIds(): array {
|
2023-11-20 13:06:57 +00:00
|
|
|
return array_keys( $this->loadGadgets() );
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 17:47:47 +00:00
|
|
|
public function handlePageUpdate( LinkTarget $target ): void {
|
2023-11-20 13:06:57 +00:00
|
|
|
if ( $target->inNamespace( NS_MEDIAWIKI ) && $target->getDBkey() === 'Gadgets-definition' ) {
|
2017-11-07 19:36:44 +00:00
|
|
|
$this->purgeDefinitionCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-18 22:40:42 +00:00
|
|
|
/**
|
2022-04-05 17:47:47 +00:00
|
|
|
* Purge the definitions cache, for example when MediaWiki:Gadgets-definition is edited.
|
2015-07-18 22:40:42 +00:00
|
|
|
*/
|
2022-04-05 17:47:47 +00:00
|
|
|
private function purgeDefinitionCache(): void {
|
2022-09-02 04:36:36 +00:00
|
|
|
$srvCache = ObjectCache::getLocalServerInstance( 'hash' );
|
2022-08-16 15:24:01 +00:00
|
|
|
$key = $this->makeDefinitionCacheKey( $this->wanCache );
|
2022-09-02 04:36:36 +00:00
|
|
|
|
2022-08-16 15:24:01 +00:00
|
|
|
$this->wanCache->delete( $key );
|
2022-09-02 04:36:36 +00:00
|
|
|
$srvCache->delete( $key );
|
|
|
|
$this->definitions = null;
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 09:32:09 +00:00
|
|
|
/**
|
2022-04-05 19:00:51 +00:00
|
|
|
* @param WANObjectCache $cache
|
2020-10-12 09:32:09 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-04-05 19:00:51 +00:00
|
|
|
private function makeDefinitionCacheKey( WANObjectCache $cache ) {
|
2018-10-05 10:29:24 +00:00
|
|
|
return $cache->makeKey(
|
|
|
|
'gadgets-definition',
|
|
|
|
Gadget::GADGET_CLASS_VERSION,
|
|
|
|
self::CACHE_VERSION
|
|
|
|
);
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-05 19:00:51 +00:00
|
|
|
* Get list of gadgets.
|
|
|
|
*
|
2022-08-27 10:54:28 +00:00
|
|
|
* @return array[] List of Gadget objects
|
2015-07-18 22:40:42 +00:00
|
|
|
*/
|
2022-04-05 19:00:51 +00:00
|
|
|
protected function loadGadgets(): array {
|
2023-07-29 12:41:59 +00:00
|
|
|
if ( defined( 'MW_PHPUNIT_TEST' ) && MediaWikiServices::getInstance()->isStorageDisabled() ) {
|
|
|
|
// Bail out immediately if storage is disabled. This should never happen in normal operations, but can
|
|
|
|
// happen a lot in tests: this method is called from the UserGetDefaultOptions hook handler, so any test
|
|
|
|
// that uses UserOptionsLookup will end up reaching this code, which is problematic if the test is not
|
|
|
|
// in the Database group (T155147).
|
|
|
|
return [];
|
|
|
|
}
|
2022-04-05 19:00:51 +00:00
|
|
|
// From back to front:
|
|
|
|
//
|
|
|
|
// 3. wan cache (e.g. memcached)
|
|
|
|
// This improves end-user latency and reduces database load.
|
|
|
|
// It is purged when the data changes.
|
|
|
|
//
|
|
|
|
// 2. server cache (e.g. APCu).
|
|
|
|
// Very short blind TTL, mainly to avoid high memcached I/O.
|
|
|
|
//
|
|
|
|
// 1. process cache. Faster repeat calls.
|
|
|
|
if ( $this->definitions === null ) {
|
2022-08-16 15:24:01 +00:00
|
|
|
$wanCache = $this->wanCache;
|
2022-04-05 19:00:51 +00:00
|
|
|
$srvCache = ObjectCache::getLocalServerInstance( 'hash' );
|
|
|
|
$key = $this->makeDefinitionCacheKey( $wanCache );
|
|
|
|
$this->definitions = $srvCache->getWithSetCallback(
|
|
|
|
$key,
|
2022-06-27 07:27:43 +00:00
|
|
|
// between 7 and 15 seconds to avoid memcached/lockTSE stampede (T203786)
|
|
|
|
mt_rand( 7, 15 ),
|
2022-04-05 19:00:51 +00:00
|
|
|
function () use ( $wanCache, $key ) {
|
|
|
|
return $wanCache->getWithSetCallback(
|
|
|
|
$key,
|
|
|
|
// 1 day
|
|
|
|
Gadget::CACHE_TTL,
|
|
|
|
function ( $old, &$ttl, &$setOpts ) {
|
|
|
|
// Reduce caching of known-stale data (T157210)
|
|
|
|
$setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) );
|
|
|
|
|
|
|
|
return $this->fetchStructuredList();
|
|
|
|
},
|
|
|
|
[
|
2022-08-27 10:54:28 +00:00
|
|
|
'version' => 2,
|
2022-04-05 19:00:51 +00:00
|
|
|
// Avoid database stampede
|
|
|
|
'lockTSE' => 300,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
2022-04-05 19:00:51 +00:00
|
|
|
return $this->definitions;
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch list of gadgets and returns it as associative array of sections with gadgets
|
2016-12-28 10:25:47 +00:00
|
|
|
* e.g. [ $name => $gadget1, etc. ]
|
2022-08-27 10:54:28 +00:00
|
|
|
* @return array[]
|
2015-07-18 22:40:42 +00:00
|
|
|
*/
|
2023-12-06 07:06:06 +00:00
|
|
|
public function fetchStructuredList() {
|
|
|
|
// T157210: avoid using wfMessage() to avoid staleness due to cache layering
|
|
|
|
$title = Title::makeTitle( NS_MEDIAWIKI, 'Gadgets-definition' );
|
|
|
|
$revRecord = $this->revLookup->getRevisionByTitle( $title );
|
|
|
|
if ( !$revRecord
|
|
|
|
|| !$revRecord->getContent( SlotRecord::MAIN )
|
|
|
|
|| $revRecord->getContent( SlotRecord::MAIN )->isEmpty()
|
|
|
|
) {
|
|
|
|
return [];
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 07:06:06 +00:00
|
|
|
$content = $revRecord->getContent( SlotRecord::MAIN );
|
|
|
|
$g = ( $content instanceof TextContent ) ? $content->getText() : '';
|
|
|
|
|
2015-07-18 22:40:42 +00:00
|
|
|
$gadgets = $this->listFromDefinition( $g );
|
|
|
|
|
2023-12-06 07:06:06 +00:00
|
|
|
wfDebug( __METHOD__ . ": MediaWiki:Gadgets-definition parsed, cache entry should be updated\n" );
|
2015-07-18 22:40:42 +00:00
|
|
|
|
|
|
|
return $gadgets;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a structured list of Gadget objects from a definition
|
|
|
|
*
|
2017-02-27 22:08:31 +00:00
|
|
|
* @param string $definition
|
2022-08-27 10:54:28 +00:00
|
|
|
* @return array[] List of Gadget objects indexed by the gadget's name.
|
2015-07-18 22:40:42 +00:00
|
|
|
*/
|
2022-08-27 10:54:28 +00:00
|
|
|
private function listFromDefinition( $definition ): array {
|
2015-07-18 22:40:42 +00:00
|
|
|
$definition = preg_replace( '/<!--.*?-->/s', '', $definition );
|
|
|
|
$lines = preg_split( '/(\r\n|\r|\n)+/', $definition );
|
|
|
|
|
2016-12-28 10:25:47 +00:00
|
|
|
$gadgets = [];
|
2015-07-18 22:40:42 +00:00
|
|
|
$section = '';
|
|
|
|
|
|
|
|
foreach ( $lines as $line ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
$m = [];
|
2015-07-18 22:40:42 +00:00
|
|
|
if ( preg_match( '/^==+ *([^*:\s|]+?)\s*==+\s*$/', $line, $m ) ) {
|
|
|
|
$section = $m[1];
|
|
|
|
} else {
|
|
|
|
$gadget = $this->newFromDefinition( $line, $section );
|
|
|
|
if ( $gadget ) {
|
2022-08-27 10:54:28 +00:00
|
|
|
$gadgets[$gadget->getName()] = $gadget->toArray();
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $gadgets;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an instance of this class from definition in MediaWiki:Gadgets-definition
|
|
|
|
* @param string $definition Gadget definition
|
|
|
|
* @param string $category
|
2020-12-17 19:07:37 +00:00
|
|
|
* @return Gadget|false Instance of Gadget class or false if $definition is invalid
|
2015-07-18 22:40:42 +00:00
|
|
|
*/
|
|
|
|
public function newFromDefinition( $definition, $category ) {
|
2017-02-27 22:08:31 +00:00
|
|
|
if ( !preg_match(
|
2021-10-09 00:34:45 +00:00
|
|
|
'/^\*+ *([a-zA-Z](?:[-_:.\w ]*[a-zA-Z0-9])?)(\s*\[.*?\])?\s*((\|[^|]*)+)\s*$/',
|
2017-02-27 22:08:31 +00:00
|
|
|
$definition,
|
2023-11-20 13:06:57 +00:00
|
|
|
$matches
|
2017-02-27 22:08:31 +00:00
|
|
|
) ) {
|
2015-07-18 22:40:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-11-20 13:06:57 +00:00
|
|
|
[ , $name, $options, $pages ] = $matches;
|
|
|
|
$options = trim( $options, ' []' );
|
|
|
|
|
2015-07-18 22:40:42 +00:00
|
|
|
// NOTE: the gadget name is used as part of the name of a form field,
|
2017-02-27 22:08:31 +00:00
|
|
|
// and must follow the rules defined in https://www.w3.org/TR/html4/types.html#type-cdata
|
|
|
|
// Also, title-normalization applies.
|
2023-11-20 13:06:57 +00:00
|
|
|
$name = str_replace( ' ', '_', $name );
|
2023-06-07 15:58:25 +00:00
|
|
|
// If the name is too long, then RL will throw an exception when
|
2015-07-18 22:40:42 +00:00
|
|
|
// we try to register the module
|
2023-11-20 13:06:57 +00:00
|
|
|
if ( !Gadget::isValidGadgetID( $name ) ) {
|
2015-07-18 22:40:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-11-20 13:06:57 +00:00
|
|
|
|
|
|
|
$info = [
|
|
|
|
'category' => $category,
|
|
|
|
'name' => $name,
|
|
|
|
'definition' => $definition,
|
|
|
|
];
|
2015-07-18 22:40:42 +00:00
|
|
|
|
|
|
|
foreach ( preg_split( '/\s*\|\s*/', $options, -1, PREG_SPLIT_NO_EMPTY ) as $option ) {
|
2015-10-23 21:11:01 +00:00
|
|
|
$arr = preg_split( '/\s*=\s*/', $option, 2 );
|
2015-07-18 22:40:42 +00:00
|
|
|
$option = $arr[0];
|
|
|
|
if ( isset( $arr[1] ) ) {
|
|
|
|
$params = explode( ',', $arr[1] );
|
|
|
|
$params = array_map( 'trim', $params );
|
|
|
|
} else {
|
2016-12-28 10:25:47 +00:00
|
|
|
$params = [];
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $option ) {
|
|
|
|
case 'ResourceLoader':
|
|
|
|
$info['resourceLoaded'] = true;
|
|
|
|
break;
|
2022-01-30 13:15:25 +00:00
|
|
|
case 'requiresES6':
|
|
|
|
$info['requiresES6'] = true;
|
|
|
|
break;
|
2015-07-18 22:40:42 +00:00
|
|
|
case 'dependencies':
|
|
|
|
$info['dependencies'] = $params;
|
|
|
|
break;
|
2016-11-18 04:54:17 +00:00
|
|
|
case 'peers':
|
|
|
|
$info['peers'] = $params;
|
|
|
|
break;
|
2015-07-18 22:40:42 +00:00
|
|
|
case 'rights':
|
|
|
|
$info['requiredRights'] = $params;
|
|
|
|
break;
|
2016-11-17 01:58:37 +00:00
|
|
|
case 'hidden':
|
|
|
|
$info['hidden'] = true;
|
|
|
|
break;
|
2021-12-14 13:10:22 +00:00
|
|
|
case 'actions':
|
|
|
|
$info['requiredActions'] = $params;
|
|
|
|
break;
|
2015-07-18 22:40:42 +00:00
|
|
|
case 'skins':
|
|
|
|
$info['requiredSkins'] = $params;
|
|
|
|
break;
|
2020-09-04 03:42:31 +00:00
|
|
|
case 'namespaces':
|
|
|
|
$info['requiredNamespaces'] = $params;
|
|
|
|
break;
|
2023-05-22 08:41:15 +00:00
|
|
|
case 'contentModels':
|
|
|
|
$info['requiredContentModels'] = $params;
|
|
|
|
break;
|
2015-07-18 22:40:42 +00:00
|
|
|
case 'default':
|
|
|
|
$info['onByDefault'] = true;
|
|
|
|
break;
|
2021-10-17 13:05:15 +00:00
|
|
|
case 'package':
|
|
|
|
$info['package'] = true;
|
|
|
|
break;
|
2015-07-18 22:40:42 +00:00
|
|
|
case 'targets':
|
|
|
|
$info['targets'] = $params;
|
|
|
|
break;
|
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':
|
|
|
|
// Single value, not a list
|
2019-03-17 23:39:02 +00:00
|
|
|
$info['type'] = $params[0] ?? '';
|
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
|
|
|
break;
|
2022-01-11 09:03:44 +00:00
|
|
|
case 'supportsUrlLoad':
|
|
|
|
$val = $params[0] ?? '';
|
|
|
|
$info['supportsUrlLoad'] = $val !== 'false';
|
|
|
|
break;
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 13:06:57 +00:00
|
|
|
foreach ( preg_split( '/\s*\|\s*/', $pages, -1, PREG_SPLIT_NO_EMPTY ) as $page ) {
|
2021-10-17 13:05:15 +00:00
|
|
|
$page = $this->titlePrefix . $page;
|
2023-12-11 17:16:30 +00:00
|
|
|
$info['pages'][] = $page;
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Gadget( $info );
|
|
|
|
}
|
|
|
|
}
|