1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
<?php
use MediaWiki\Linker\LinkTarget;
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\SlotRecord;
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 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() {
$key = $this->getGadgetIdsKey();
$fname = __METHOD__;
return $this->wanCache->getWithSetCallback(
$key,
self::CACHE_TTL,
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 ) {
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() );
}
}
/**
* Purge the list of gadget ids when a page is deleted or if a new page is created
*/
public function purgeGadgetIdsList() {
$this->wanCache->touchCheckKey( $this->getGadgetIdsKey() );
}
/**
* @param string $id
* @throws InvalidArgumentException
* @return Gadget
*/
public function getGadget( $id ) {
$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 );
}
}
|