3 use MediaWiki\MediaWikiServices;
6 * GadgetRepo implementation where each gadget has a page in
7 * the Gadget definition namespace, and scripts and styles are
8 * located in the Gadget namespace.
10 class GadgetDefinitionNamespaceRepo extends GadgetRepo {
12 * How long in seconds the list of gadget ids and
13 * individual gadgets should be cached for (1 day)
15 const CACHE_TTL = 86400;
22 public function __construct() {
23 $this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
27 * Get a list of gadget ids from cache/database
31 public function getGadgetIds() {
32 $key = $this->getGadgetIdsKey();
34 return $this->wanCache->getWithSetCallback(
37 function ( $oldValue, &$ttl, array &$setOpts ) {
38 $dbr = wfGetDB( DB_SLAVE );
39 $setOpts += Database::getCacheSetOptions( $dbr );
41 return $dbr->selectFieldValues(
44 [ 'page_namespace' => NS_GADGET_DEFINITION ],
49 'checkKeys' => [ $key ],
50 'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
57 * Purge the list of gadget ids when a page is deleted or if a new page is created
59 public function purgeGadgetIdsList() {
60 $this->wanCache->touchCheckKey( $this->getGadgetIdsKey() );
65 * @throws InvalidArgumentException
68 public function getGadget( $id ) {
69 $key = $this->getGadgetCacheKey( $id );
70 $gadget = $this->wanCache->getWithSetCallback(
73 function ( $old, &$ttl, array &$setOpts ) use ( $id ) {
74 $setOpts += Database::getCacheSetOptions( wfGetDB( DB_SLAVE ) );
75 $title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $id );
77 $ttl = WANObjectCache::TTL_UNCACHEABLE;
81 $rev = Revision::newFromTitle( $title );
83 $ttl = WANObjectCache::TTL_UNCACHEABLE;
87 $content = $rev->getContent();
88 if ( !$content instanceof GadgetDefinitionContent ) {
90 $ttl = WANObjectCache::TTL_UNCACHEABLE;
94 return Gadget::newFromDefinitionContent( $id, $content );
97 'checkKeys' => [ $key ],
98 'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
103 if ( $gadget === null ) {
104 throw new InvalidArgumentException( "No gadget registered for '$id'" );
111 * Update the cache for a specific Gadget whenever it is updated
115 public function purgeGadgetEntry( $id ) {
116 $this->wanCache->touchCheckKey( $this->getGadgetCacheKey( $id ) );
122 private function getGadgetIdsKey() {
123 return $this->wanCache->makeKey( 'gadgets', 'namespace', 'ids' );
130 private function getGadgetCacheKey( $id ) {
131 return $this->wanCache->makeKey(
132 'gadgets', 'object', md5( $id ), Gadget::GADGET_CLASS_VERSION );