2015-08-03 06:37:32 +00:00
|
|
|
<?php
|
|
|
|
|
2017-11-07 19:36:44 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2017-03-03 01:41:25 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-04-02 00:38:28 +00:00
|
|
|
use MediaWiki\Revision\RevisionLookup;
|
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2018-02-25 02:41:50 +00:00
|
|
|
use Wikimedia\Rdbms\Database;
|
2017-03-03 01:41:25 +00:00
|
|
|
|
2015-08-03 06:37:32 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
2020-05-20 00:10:09 +00:00
|
|
|
private const CACHE_TTL = 86400;
|
2015-08-03 06:37:32 +00:00
|
|
|
|
2021-10-17 13:05:15 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $titlePrefix = 'Gadget:';
|
|
|
|
|
2015-08-03 06:37:32 +00:00
|
|
|
/**
|
|
|
|
* @var WANObjectCache
|
|
|
|
*/
|
|
|
|
private $wanCache;
|
|
|
|
|
2020-04-02 00:38:28 +00:00
|
|
|
/**
|
|
|
|
* @var RevisionLookup
|
|
|
|
*/
|
|
|
|
private $revLookup;
|
|
|
|
|
2017-05-30 18:38:47 +00:00
|
|
|
public function __construct() {
|
2020-04-02 00:38:28 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$this->wanCache = $services->getMainWANObjectCache();
|
|
|
|
$this->revLookup = $services->getRevisionLookup();
|
2015-08-03 06:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of gadget ids from cache/database
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getGadgetIds() {
|
2017-03-03 01:41:25 +00:00
|
|
|
$key = $this->getGadgetIdsKey();
|
|
|
|
|
2018-09-30 11:38:44 +00:00
|
|
|
$fname = __METHOD__;
|
2015-08-03 06:37:32 +00:00
|
|
|
return $this->wanCache->getWithSetCallback(
|
2017-03-03 01:41:25 +00:00
|
|
|
$key,
|
2015-08-03 06:37:32 +00:00
|
|
|
self::CACHE_TTL,
|
2021-05-04 07:24:31 +00:00
|
|
|
static function ( $oldValue, &$ttl, array &$setOpts ) use ( $fname ) {
|
2017-09-24 05:26:03 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2015-08-03 06:37:32 +00:00
|
|
|
$setOpts += Database::getCacheSetOptions( $dbr );
|
2017-03-03 01:41:25 +00:00
|
|
|
|
2015-08-03 06:37:32 +00:00
|
|
|
return $dbr->selectFieldValues(
|
|
|
|
'page',
|
|
|
|
'page_title',
|
2017-03-03 01:41:25 +00:00
|
|
|
[ 'page_namespace' => NS_GADGET_DEFINITION ],
|
2018-09-30 11:38:44 +00:00
|
|
|
$fname
|
2015-08-03 06:37:32 +00:00
|
|
|
);
|
|
|
|
},
|
2016-12-28 10:25:47 +00:00
|
|
|
[
|
2017-03-03 01:41:25 +00:00
|
|
|
'checkKeys' => [ $key ],
|
|
|
|
'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
|
|
|
|
'lockTSE' => 30
|
2016-12-28 10:25:47 +00:00
|
|
|
]
|
2015-08-03 06:37:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-07 19:36:44 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function handlePageUpdate( LinkTarget $target ) {
|
|
|
|
if ( $target->inNamespace( NS_GADGET_DEFINITION ) ) {
|
|
|
|
$this->purgeGadgetEntry( $target->getText() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function handlePageCreation( LinkTarget $target ) {
|
|
|
|
if ( $target->inNamespace( NS_GADGET_DEFINITION ) ) {
|
|
|
|
$this->purgeGadgetIdsList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function handlePageDeletion( LinkTarget $target ) {
|
|
|
|
if ( $target->inNamespace( NS_GADGET_DEFINITION ) ) {
|
|
|
|
$this->purgeGadgetIdsList();
|
|
|
|
$this->purgeGadgetEntry( $target->getText() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 06:37:32 +00:00
|
|
|
/**
|
2017-03-03 01:41:25 +00:00
|
|
|
* Purge the list of gadget ids when a page is deleted or if a new page is created
|
2015-08-03 06:37:32 +00:00
|
|
|
*/
|
2017-03-03 01:41:25 +00:00
|
|
|
public function purgeGadgetIdsList() {
|
|
|
|
$this->wanCache->touchCheckKey( $this->getGadgetIdsKey() );
|
2015-08-03 06:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $id
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
* @return Gadget
|
|
|
|
*/
|
|
|
|
public function getGadget( $id ) {
|
|
|
|
$key = $this->getGadgetCacheKey( $id );
|
|
|
|
$gadget = $this->wanCache->getWithSetCallback(
|
|
|
|
$key,
|
|
|
|
self::CACHE_TTL,
|
2017-03-03 01:41:25 +00:00
|
|
|
function ( $old, &$ttl, array &$setOpts ) use ( $id ) {
|
2017-09-24 05:26:03 +00:00
|
|
|
$setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) );
|
2015-08-03 06:37:32 +00:00
|
|
|
$title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $id );
|
|
|
|
if ( !$title ) {
|
|
|
|
$ttl = WANObjectCache::TTL_UNCACHEABLE;
|
|
|
|
return null;
|
|
|
|
}
|
2017-03-03 01:41:25 +00:00
|
|
|
|
2020-04-02 00:38:28 +00:00
|
|
|
$revRecord = $this->revLookup->getRevisionByTitle( $title );
|
|
|
|
if ( !$revRecord ) {
|
2015-08-03 06:37:32 +00:00
|
|
|
$ttl = WANObjectCache::TTL_UNCACHEABLE;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-04-02 00:38:28 +00:00
|
|
|
$content = $revRecord->getContent( SlotRecord::MAIN );
|
2015-08-03 06:37:32 +00:00
|
|
|
if ( !$content instanceof GadgetDefinitionContent ) {
|
|
|
|
// Uhm...
|
|
|
|
$ttl = WANObjectCache::TTL_UNCACHEABLE;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Gadget::newFromDefinitionContent( $id, $content );
|
|
|
|
},
|
2016-12-28 10:25:47 +00:00
|
|
|
[
|
|
|
|
'checkKeys' => [ $key ],
|
2017-03-03 01:41:25 +00:00
|
|
|
'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
|
|
|
|
'lockTSE' => 30
|
2016-12-28 10:25:47 +00:00
|
|
|
]
|
2015-08-03 06:37:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( $gadget === null ) {
|
|
|
|
throw new InvalidArgumentException( "No gadget registered for '$id'" );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $gadget;
|
|
|
|
}
|
2017-03-03 01:41:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-25 02:41:50 +00:00
|
|
|
* @param string $id
|
2017-03-03 01:41:25 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getGadgetCacheKey( $id ) {
|
|
|
|
return $this->wanCache->makeKey(
|
|
|
|
'gadgets', 'object', md5( $id ), Gadget::GADGET_CLASS_VERSION );
|
|
|
|
}
|
2015-08-03 06:37:32 +00:00
|
|
|
}
|