mediawiki-extensions-Gadgets/includes/MediaWikiGadgetsDefinitionRepo.php

295 lines
8.2 KiB
PHP
Raw Normal View History

<?php
namespace MediaWiki\Extension\Gadgets;
use InvalidArgumentException;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Title\Title;
use ObjectCache;
use TextContent;
use WANObjectCache;
use Wikimedia\Rdbms\Database;
/**
* Gadgets repo powered by MediaWiki:Gadgets-definition
*/
class MediaWikiGadgetsDefinitionRepo extends GadgetRepo {
private const CACHE_VERSION = 4;
/** @var array|null */
private $definitions;
/** @var string */
protected $titlePrefix = 'MediaWiki:Gadget-';
/**
* @param string $id
* @throws InvalidArgumentException
* @return Gadget
*/
public function getGadget( string $id ): Gadget {
$gadgets = $this->loadGadgets();
if ( !isset( $gadgets[$id] ) ) {
throw new InvalidArgumentException( "No gadget registered for '$id'" );
}
return new Gadget( $gadgets[$id] );
}
public function getGadgetIds(): array {
$gadgets = $this->loadGadgets();
if ( $gadgets ) {
return array_keys( $gadgets );
}
return [];
}
public function handlePageUpdate( LinkTarget $target ): void {
if ( $target->getNamespace() === NS_MEDIAWIKI && $target->getText() === 'Gadgets-definition' ) {
$this->purgeDefinitionCache();
}
}
/**
* Purge the definitions cache, for example when MediaWiki:Gadgets-definition is edited.
*/
private function purgeDefinitionCache(): void {
$wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$srvCache = ObjectCache::getLocalServerInstance( 'hash' );
$key = $this->makeDefinitionCacheKey( $wanCache );
$wanCache->delete( $key );
$srvCache->delete( $key );
$this->definitions = null;
}
/**
* @param WANObjectCache $cache
* @return string
*/
private function makeDefinitionCacheKey( WANObjectCache $cache ) {
return $cache->makeKey(
'gadgets-definition',
Gadget::GADGET_CLASS_VERSION,
self::CACHE_VERSION
);
}
/**
* Get list of gadgets.
*
* @return array[] List of Gadget objects
*/
protected function loadGadgets(): array {
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 [];
}
// 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 ) {
$wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
$srvCache = ObjectCache::getLocalServerInstance( 'hash' );
$key = $this->makeDefinitionCacheKey( $wanCache );
$this->definitions = $srvCache->getWithSetCallback(
$key,
// between 7 and 15 seconds to avoid memcached/lockTSE stampede (T203786)
mt_rand( 7, 15 ),
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();
},
[
'version' => 2,
// Avoid database stampede
'lockTSE' => 300,
]
);
}
);
}
return $this->definitions;
}
/**
* Fetch list of gadgets and returns it as associative array of sections with gadgets
* e.g. [ $name => $gadget1, etc. ]
* @param string|null $forceNewText Injected text of MediaWiki:gadgets-definition [optional]
* @return array[]
*/
public function fetchStructuredList( $forceNewText = null ) {
if ( $forceNewText === null ) {
// T157210: avoid using wfMessage() to avoid staleness due to cache layering
$title = Title::makeTitle( NS_MEDIAWIKI, 'Gadgets-definition' );
$revRecord = MediaWikiServices::getInstance()
->getRevisionLookup()
->getRevisionByTitle( $title );
if ( !$revRecord
|| !$revRecord->getContent( SlotRecord::MAIN )
|| $revRecord->getContent( SlotRecord::MAIN )->isEmpty()
) {
GadgetRepo: Fix missing purging on delete and simplify hook handling == Motivation == On a local dev wiki and in CI, where no gadgets are defined yet, fetchStructuredList() is called on every load.php request and doing uncached database look ups. This is because T39228 (stale cache after Gadgets-definition was empty or created) was fixed by disabling the cache until the page exists. This seems like a poor solution, and commit I3092bcb162d032 recognises that the problem was not understood at the time. I propose to instead cache it always and purge it when the page is modified in any way. Cold calling fetchStructuredList() accounted for ~170ms of ~400ms when trying out debug v2 (T85805), thus making it significantly slower and causing a chain of dozens of requests to pile up. == Previously == * Legacy repo (MediaWikiGadgetsDefinitionRepo) only implemented handlePageUpdate. * handlePageUpdate was called from onPageSaveComplete for both any page edit, and for creations in the experimental namespace. * The experimental GadgetDefinitionNamespaceRepo is based on ContentHandler rather than global hooks. This system does not have a create-specific callback. It called update for edit/create, and delete for delete. The experimental repo relied on create being called from the global hook for the legacy repo, and update was then called twice. * There was no global hook for onPageDeleteComplete, thus the legacy repo did not get purges. == Changes == * Add onPageDeleteComplete hook to fix purges after deletion, with the legacy repo now implementing handlePageDeletion() and doing the same as handlePageUpdate(). * Fix handlePageUpdate() docs to reflect that it covers page create, since onPageSaveComplete() called it either way. * Fix experimental repo to include its namespace purge in its handlePageUpdate() method. * Get rid of now-redundant handlePageCreation(). * Get rid of handlePageDeletion() since its implementations would now be identical to handlePageUpdate(). All these hooks and handle*() methods are just for a memc key holding gadget metadata. We don't need to microoptimise this on a per-page basis. Gadget edits are rare enough that purging them as a whole each time is fine. We do the same in MediaWiki core with ResourceLoaderGadgetsModule already. Bug: T85805 Change-Id: Ib27fd34fbfe7a75c851602c8a93a2e3e1f2c38a0
2022-04-05 18:10:38 +00:00
return [];
}
$content = $revRecord->getContent( SlotRecord::MAIN );
$g = ( $content instanceof TextContent ) ? $content->getText() : '';
} else {
$g = $forceNewText;
}
$gadgets = $this->listFromDefinition( $g );
$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
*
* @param string $definition
* @return array[] List of Gadget objects indexed by the gadget's name.
*/
private function listFromDefinition( $definition ): array {
$definition = preg_replace( '/<!--.*?-->/s', '', $definition );
$lines = preg_split( '/(\r\n|\r|\n)+/', $definition );
$gadgets = [];
$section = '';
foreach ( $lines as $line ) {
$m = [];
if ( preg_match( '/^==+ *([^*:\s|]+?)\s*==+\s*$/', $line, $m ) ) {
$section = $m[1];
} else {
$gadget = $this->newFromDefinition( $line, $section );
if ( $gadget ) {
$gadgets[$gadget->getName()] = $gadget->toArray();
}
}
}
return $gadgets;
}
/**
* Creates an instance of this class from definition in MediaWiki:Gadgets-definition
* @param string $definition Gadget definition
* @param string $category
* @return Gadget|false Instance of Gadget class or false if $definition is invalid
*/
public function newFromDefinition( $definition, $category ) {
$m = [];
if ( !preg_match(
'/^\*+ *([a-zA-Z](?:[-_:.\w ]*[a-zA-Z0-9])?)(\s*\[.*?\])?\s*((\|[^|]*)+)\s*$/',
$definition,
$m
) ) {
return false;
}
// NOTE: the gadget name is used as part of the name of a form field,
// and must follow the rules defined in https://www.w3.org/TR/html4/types.html#type-cdata
// Also, title-normalization applies.
$info = [ 'category' => $category ];
$info['name'] = trim( str_replace( ' ', '_', $m[1] ) );
// If the name is too long, then RL will throw an exception 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 ) {
$arr = preg_split( '/\s*=\s*/', $option, 2 );
$option = $arr[0];
if ( isset( $arr[1] ) ) {
$params = explode( ',', $arr[1] );
$params = array_map( 'trim', $params );
} else {
$params = [];
}
switch ( $option ) {
case 'ResourceLoader':
$info['resourceLoaded'] = true;
break;
case 'requiresES6':
$info['requiresES6'] = true;
break;
case 'dependencies':
$info['dependencies'] = $params;
break;
case 'peers':
$info['peers'] = $params;
break;
case 'rights':
$info['requiredRights'] = $params;
break;
case 'hidden':
$info['hidden'] = true;
break;
case 'actions':
$info['requiredActions'] = $params;
break;
case 'skins':
$info['requiredSkins'] = $params;
break;
case 'default':
$info['onByDefault'] = true;
break;
case 'package':
$info['package'] = 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'] = $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;
case 'supportsUrlLoad':
$val = $params[0] ?? '';
$info['supportsUrlLoad'] = $val !== 'false';
break;
}
}
foreach ( preg_split( '/\s*\|\s*/', $m[3], -1, PREG_SPLIT_NO_EMPTY ) as $page ) {
$page = $this->titlePrefix . $page;
if ( preg_match( '/\.json$/', $page ) ) {
$info['datas'][] = $page;
} elseif ( preg_match( '/\.js/', $page ) ) {
$info['scripts'][] = $page;
} elseif ( preg_match( '/\.css/', $page ) ) {
$info['styles'][] = $page;
}
}
return new Gadget( $info );
}
}