2015-07-18 22:40:42 +00:00
|
|
|
<?php
|
2017-11-07 19:36:44 +00:00
|
|
|
|
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2016-10-11 21:20:18 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
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 {
|
2015-12-09 00:21:34 +00:00
|
|
|
const CACHE_VERSION = 2;
|
2015-07-18 22:40:42 +00:00
|
|
|
|
|
|
|
private $definitionCache;
|
|
|
|
|
2018-02-25 02:41:50 +00:00
|
|
|
/**
|
|
|
|
* @param string $id
|
|
|
|
*
|
|
|
|
* @return Gadget
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2015-07-18 22:40:42 +00:00
|
|
|
public function getGadget( $id ) {
|
|
|
|
$gadgets = $this->loadGadgets();
|
|
|
|
if ( !isset( $gadgets[$id] ) ) {
|
|
|
|
throw new InvalidArgumentException( "No gadget registered for '$id'" );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $gadgets[$id];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGadgetIds() {
|
|
|
|
$gadgets = $this->loadGadgets();
|
|
|
|
if ( $gadgets ) {
|
|
|
|
return array_keys( $gadgets );
|
|
|
|
} else {
|
2016-12-28 10:25:47 +00:00
|
|
|
return [];
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 19:36:44 +00:00
|
|
|
public function handlePageUpdate( LinkTarget $target ) {
|
|
|
|
if ( $target->getNamespace() == NS_MEDIAWIKI && $target->getText() == 'Gadgets-definition' ) {
|
|
|
|
$this->purgeDefinitionCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-18 22:40:42 +00:00
|
|
|
/**
|
|
|
|
* Purge the definitions cache, for example if MediaWiki:Gadgets-definition
|
|
|
|
* was edited.
|
|
|
|
*/
|
2017-11-07 19:36:44 +00:00
|
|
|
private function purgeDefinitionCache() {
|
2016-10-11 21:20:18 +00:00
|
|
|
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
|
|
|
$cache->touchCheckKey( $this->getCheckKey() );
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getCheckKey() {
|
|
|
|
return wfMemcKey( 'gadgets-definition', Gadget::GADGET_CLASS_VERSION, self::CACHE_VERSION );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads list of gadgets and returns it as associative array of sections with gadgets
|
2016-12-28 10:25:47 +00:00
|
|
|
* e.g. [ 'sectionnname1' => [ $gadget1, $gadget2 ],
|
|
|
|
* 'sectionnname2' => [ $gadget3 ] ];
|
2015-07-18 22:40:42 +00:00
|
|
|
* @return array|bool Gadget array or false on failure
|
|
|
|
*/
|
|
|
|
protected function loadGadgets() {
|
|
|
|
if ( $this->definitionCache !== null ) {
|
|
|
|
return $this->definitionCache; // process cache hit
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally $t1Cache is APC, and $wanCache is memcached
|
2016-10-11 21:20:18 +00:00
|
|
|
$t1Cache = ObjectCache::getLocalServerInstance( 'hash' );
|
|
|
|
$wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
2015-07-18 22:40:42 +00:00
|
|
|
|
|
|
|
$key = $this->getCheckKey();
|
|
|
|
|
|
|
|
// (a) Check the tier 1 cache
|
|
|
|
$value = $t1Cache->get( $key );
|
|
|
|
// Check if it passes a blind TTL check (avoids I/O)
|
|
|
|
if ( $value && ( microtime( true ) - $value['time'] ) < 10 ) {
|
|
|
|
$this->definitionCache = $value['gadgets']; // process cache
|
|
|
|
return $this->definitionCache;
|
|
|
|
}
|
|
|
|
// Cache generated after the "check" time should be up-to-date
|
|
|
|
$ckTime = $wanCache->getCheckKeyTime( $key ) + WANObjectCache::HOLDOFF_TTL;
|
|
|
|
if ( $value && $value['time'] > $ckTime ) {
|
|
|
|
$this->definitionCache = $value['gadgets']; // process cache
|
|
|
|
return $this->definitionCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
// (b) Fetch value from WAN cache or regenerate if needed.
|
|
|
|
// This is hit occasionally and more so when the list changes.
|
|
|
|
$us = $this;
|
|
|
|
$value = $wanCache->getWithSetCallback(
|
|
|
|
$key,
|
2015-10-08 02:14:41 +00:00
|
|
|
Gadget::CACHE_TTL,
|
2017-08-31 22:29:00 +00:00
|
|
|
function ( $old, &$ttl, &$setOpts ) use ( $us ) {
|
|
|
|
$setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) );
|
|
|
|
|
2015-07-18 22:40:42 +00:00
|
|
|
$now = microtime( true );
|
|
|
|
$gadgets = $us->fetchStructuredList();
|
|
|
|
if ( $gadgets === false ) {
|
|
|
|
$ttl = WANObjectCache::TTL_UNCACHEABLE;
|
|
|
|
}
|
|
|
|
|
2016-12-28 10:25:47 +00:00
|
|
|
return [ 'gadgets' => $gadgets, 'time' => $now ];
|
2015-07-18 22:40:42 +00:00
|
|
|
},
|
2016-12-28 10:25:47 +00:00
|
|
|
[ 'checkKeys' => [ $key ], 'lockTSE' => 300 ]
|
2015-07-18 22:40:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Update the tier 1 cache as needed
|
|
|
|
if ( $value['gadgets'] !== false && $value['time'] > $ckTime ) {
|
|
|
|
// Set a modest TTL to keep the WAN key in cache
|
|
|
|
$t1Cache->set( $key, $value, mt_rand( 300, 600 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->definitionCache = $value['gadgets'];
|
|
|
|
|
|
|
|
return $this->definitionCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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. ]
|
2017-08-27 14:07:08 +00:00
|
|
|
* @param string $forceNewText Injected text of MediaWiki:gadgets-definition [optional]
|
2015-07-18 22:40:42 +00:00
|
|
|
* @return array|bool
|
|
|
|
*/
|
|
|
|
public function fetchStructuredList( $forceNewText = null ) {
|
|
|
|
if ( $forceNewText === null ) {
|
2017-03-08 23:34:41 +00:00
|
|
|
// T157210: avoid using wfMessage() to avoid staleness due to cache layering
|
|
|
|
$title = Title::makeTitle( NS_MEDIAWIKI, 'Gadgets-definition' );
|
|
|
|
$rev = Revision::newFromTitle( $title );
|
|
|
|
if ( !$rev || !$rev->getContent() || $rev->getContent()->isEmpty() ) {
|
2015-07-18 22:40:42 +00:00
|
|
|
return false; // don't cache
|
|
|
|
}
|
|
|
|
|
2017-03-08 23:34:41 +00:00
|
|
|
$g = $rev->getContent()->getNativeData();
|
2015-07-18 22:40:42 +00:00
|
|
|
} else {
|
|
|
|
$g = $forceNewText;
|
|
|
|
}
|
|
|
|
|
|
|
|
$gadgets = $this->listFromDefinition( $g );
|
|
|
|
if ( !count( $gadgets ) ) {
|
|
|
|
return false; // don't cache; Bug 37228
|
|
|
|
}
|
|
|
|
|
|
|
|
$source = $forceNewText !== null ? 'input text' : 'MediaWiki:Gadgets-definition';
|
|
|
|
wfDebug( __METHOD__ . ": $source parsed, cache entry should be updated\n" );
|
|
|
|
|
|
|
|
return $gadgets;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a structured list of Gadget objects from a definition
|
|
|
|
*
|
2017-02-27 22:08:31 +00:00
|
|
|
* @param string $definition
|
2017-05-30 20:40:09 +00:00
|
|
|
* @return Gadget[] List of Gadget objects indexed by the gadget's name.
|
2015-07-18 22:40:42 +00:00
|
|
|
*/
|
|
|
|
private function listFromDefinition( $definition ) {
|
|
|
|
$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 ) {
|
|
|
|
$gadgets[$gadget->getName()] = $gadget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $gadgets;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an instance of this class from definition in MediaWiki:Gadgets-definition
|
|
|
|
* @param string $definition Gadget definition
|
|
|
|
* @param string $category
|
|
|
|
* @return Gadget|bool Instance of Gadget class or false if $definition is invalid
|
|
|
|
*/
|
|
|
|
public function newFromDefinition( $definition, $category ) {
|
2016-12-28 10:25:47 +00:00
|
|
|
$m = [];
|
2017-02-27 22:08:31 +00:00
|
|
|
if ( !preg_match(
|
|
|
|
'/^\*+ *([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)(\s*\[.*?\])?\s*((\|[^|]*)+)\s*$/',
|
|
|
|
$definition,
|
|
|
|
$m
|
|
|
|
) ) {
|
2015-07-18 22:40:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 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.
|
2016-12-28 10:25:47 +00:00
|
|
|
$info = [ 'category' => $category ];
|
2015-07-18 22:40:42 +00:00
|
|
|
$info['name'] = trim( str_replace( ' ', '_', $m[1] ) );
|
|
|
|
// If the name is too long, then RL will throw an MWException when
|
|
|
|
// we try to register the module
|
|
|
|
if ( !Gadget::isValidGadgetID( $info['name'] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$info['definition'] = $definition;
|
|
|
|
$options = trim( $m[2], ' []' );
|
|
|
|
|
|
|
|
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;
|
|
|
|
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;
|
2015-07-18 22:40:42 +00:00
|
|
|
case 'skins':
|
|
|
|
$info['requiredSkins'] = $params;
|
|
|
|
break;
|
|
|
|
case 'default':
|
|
|
|
$info['onByDefault'] = true;
|
|
|
|
break;
|
|
|
|
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
|
|
|
|
$info['type'] = isset( $params[0] ) ? $params[0] : '';
|
|
|
|
break;
|
2015-07-18 22:40:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( preg_split( '/\s*\|\s*/', $m[3], -1, PREG_SPLIT_NO_EMPTY ) as $page ) {
|
2015-08-03 06:37:32 +00:00
|
|
|
$page = "MediaWiki:Gadget-$page";
|
2015-07-18 22:40:42 +00:00
|
|
|
|
|
|
|
if ( preg_match( '/\.js/', $page ) ) {
|
|
|
|
$info['scripts'][] = $page;
|
|
|
|
} elseif ( preg_match( '/\.css/', $page ) ) {
|
|
|
|
$info['styles'][] = $page;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Gadget( $info );
|
|
|
|
}
|
|
|
|
}
|