mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Gadgets
synced 2024-11-28 01:00:02 +00:00
ab753d805e
== 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
175 lines
4 KiB
PHP
175 lines
4 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\Gadgets;
|
|
|
|
use InvalidArgumentException;
|
|
use MediaWiki\Extension\Gadgets\Content\GadgetDefinitionContent;
|
|
use MediaWiki\Linker\LinkTarget;
|
|
use MediaWiki\MediaWikiServices;
|
|
use MediaWiki\Revision\RevisionLookup;
|
|
use MediaWiki\Revision\SlotRecord;
|
|
use Title;
|
|
use WANObjectCache;
|
|
use Wikimedia\Rdbms\Database;
|
|
|
|
/**
|
|
* GadgetRepo implementation where each gadget has a page in
|
|
* the Gadget definition namespace, and scripts and styles are
|
|
* located in the Gadget namespace.
|
|
*/
|
|
class GadgetDefinitionNamespaceRepo extends GadgetRepo {
|
|
/**
|
|
* How long in seconds the list of gadget ids and
|
|
* individual gadgets should be cached for (1 day)
|
|
*/
|
|
private const CACHE_TTL = 86400;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $titlePrefix = 'Gadget:';
|
|
|
|
/**
|
|
* @var WANObjectCache
|
|
*/
|
|
private $wanCache;
|
|
|
|
/**
|
|
* @var RevisionLookup
|
|
*/
|
|
private $revLookup;
|
|
|
|
public function __construct() {
|
|
$services = MediaWikiServices::getInstance();
|
|
$this->wanCache = $services->getMainWANObjectCache();
|
|
$this->revLookup = $services->getRevisionLookup();
|
|
}
|
|
|
|
/**
|
|
* Get a list of gadget ids from cache/database
|
|
*
|
|
* @return string[]
|
|
*/
|
|
public function getGadgetIds(): array {
|
|
$key = $this->getGadgetIdsKey();
|
|
|
|
$fname = __METHOD__;
|
|
return $this->wanCache->getWithSetCallback(
|
|
$key,
|
|
self::CACHE_TTL,
|
|
static function ( $oldValue, &$ttl, array &$setOpts ) use ( $fname ) {
|
|
$dbr = wfGetDB( DB_REPLICA );
|
|
$setOpts += Database::getCacheSetOptions( $dbr );
|
|
|
|
return $dbr->selectFieldValues(
|
|
'page',
|
|
'page_title',
|
|
[ 'page_namespace' => NS_GADGET_DEFINITION ],
|
|
$fname
|
|
);
|
|
},
|
|
[
|
|
'checkKeys' => [ $key ],
|
|
'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
|
|
'lockTSE' => 30
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function handlePageUpdate( LinkTarget $target ): void {
|
|
if ( $target->inNamespace( NS_GADGET_DEFINITION ) ) {
|
|
$this->purgeGadgetIdsList();
|
|
$this->purgeGadgetEntry( $target->getText() );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Purge the list of gadget ids when a page is deleted or if a new page is created
|
|
*/
|
|
public function purgeGadgetIdsList(): void {
|
|
$this->wanCache->touchCheckKey( $this->getGadgetIdsKey() );
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getGadgetDefinitionTitle( string $id ): ?Title {
|
|
return Title::makeTitleSafe( NS_GADGET_DEFINITION, $id );
|
|
}
|
|
|
|
/**
|
|
* @param string $id
|
|
* @throws InvalidArgumentException
|
|
* @return Gadget
|
|
*/
|
|
public function getGadget( string $id ): Gadget {
|
|
$key = $this->getGadgetCacheKey( $id );
|
|
$gadget = $this->wanCache->getWithSetCallback(
|
|
$key,
|
|
self::CACHE_TTL,
|
|
function ( $old, &$ttl, array &$setOpts ) use ( $id ) {
|
|
$setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) );
|
|
$title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $id );
|
|
if ( !$title ) {
|
|
$ttl = WANObjectCache::TTL_UNCACHEABLE;
|
|
return null;
|
|
}
|
|
|
|
$revRecord = $this->revLookup->getRevisionByTitle( $title );
|
|
if ( !$revRecord ) {
|
|
$ttl = WANObjectCache::TTL_UNCACHEABLE;
|
|
return null;
|
|
}
|
|
|
|
$content = $revRecord->getContent( SlotRecord::MAIN );
|
|
if ( !$content instanceof GadgetDefinitionContent ) {
|
|
// Uhm...
|
|
$ttl = WANObjectCache::TTL_UNCACHEABLE;
|
|
return null;
|
|
}
|
|
|
|
return Gadget::newFromDefinitionContent( $id, $content );
|
|
},
|
|
[
|
|
'checkKeys' => [ $key ],
|
|
'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
|
|
'lockTSE' => 30
|
|
]
|
|
);
|
|
|
|
if ( $gadget === null ) {
|
|
throw new InvalidArgumentException( "No gadget registered for '$id'" );
|
|
}
|
|
|
|
return $gadget;
|
|
}
|
|
|
|
/**
|
|
* Update the cache for a specific Gadget whenever it is updated
|
|
*
|
|
* @param string $id
|
|
*/
|
|
public function purgeGadgetEntry( $id ) {
|
|
$this->wanCache->touchCheckKey( $this->getGadgetCacheKey( $id ) );
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
private function getGadgetIdsKey() {
|
|
return $this->wanCache->makeKey( 'gadgets', 'namespace', 'ids' );
|
|
}
|
|
|
|
/**
|
|
* @param string $id
|
|
* @return string
|
|
*/
|
|
private function getGadgetCacheKey( $id ) {
|
|
return $this->wanCache->makeKey(
|
|
'gadgets', 'object', md5( $id ), Gadget::GADGET_CLASS_VERSION );
|
|
}
|
|
}
|