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;
|
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)
|
|
|
|
*/
|
|
|
|
const CACHE_TTL = 86400;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var WANObjectCache
|
|
|
|
*/
|
|
|
|
private $wanCache;
|
|
|
|
|
2017-05-30 18:38:47 +00:00
|
|
|
public function __construct() {
|
2017-03-03 01:41:25 +00:00
|
|
|
$this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
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();
|
|
|
|
|
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,
|
2017-03-03 01:41:25 +00:00
|
|
|
function ( $oldValue, &$ttl, array &$setOpts ) {
|
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 ],
|
2015-08-03 06:37:32 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
},
|
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,
|
2018-02-25 02:41:50 +00:00
|
|
|
/**
|
|
|
|
* @suppress PhanTypeMismatchArgument
|
|
|
|
*/
|
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
|
|
|
|
2015-08-03 06:37:32 +00:00
|
|
|
$rev = Revision::newFromTitle( $title );
|
|
|
|
if ( !$rev ) {
|
|
|
|
$ttl = WANObjectCache::TTL_UNCACHEABLE;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $rev->getContent();
|
|
|
|
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
|
|
|
}
|