MediaWiki REL1_30
GadgetDefinitionNamespaceRepo.php
Go to the documentation of this file.
1<?php
2
4
15 const CACHE_TTL = 86400;
16
20 private $wanCache;
21
22 public function __construct() {
23 $this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
24 }
25
31 public function getGadgetIds() {
32 $key = $this->getGadgetIdsKey();
33
34 return $this->wanCache->getWithSetCallback(
35 $key,
36 self::CACHE_TTL,
37 function ( $oldValue, &$ttl, array &$setOpts ) {
39 $setOpts += Database::getCacheSetOptions( $dbr );
40
41 return $dbr->selectFieldValues(
42 'page',
43 'page_title',
44 [ 'page_namespace' => NS_GADGET_DEFINITION ],
45 __METHOD__
46 );
47 },
48 [
49 'checkKeys' => [ $key ],
50 'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
51 'lockTSE' => 30
52 ]
53 );
54 }
55
59 public function purgeGadgetIdsList() {
60 $this->wanCache->touchCheckKey( $this->getGadgetIdsKey() );
61 }
62
68 public function getGadget( $id ) {
69 $key = $this->getGadgetCacheKey( $id );
70 $gadget = $this->wanCache->getWithSetCallback(
71 $key,
72 self::CACHE_TTL,
73 function ( $old, &$ttl, array &$setOpts ) use ( $id ) {
74 $setOpts += Database::getCacheSetOptions( wfGetDB( DB_SLAVE ) );
75 $title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $id );
76 if ( !$title ) {
77 $ttl = WANObjectCache::TTL_UNCACHEABLE;
78 return null;
79 }
80
81 $rev = Revision::newFromTitle( $title );
82 if ( !$rev ) {
83 $ttl = WANObjectCache::TTL_UNCACHEABLE;
84 return null;
85 }
86
87 $content = $rev->getContent();
88 if ( !$content instanceof GadgetDefinitionContent ) {
89 // Uhm...
90 $ttl = WANObjectCache::TTL_UNCACHEABLE;
91 return null;
92 }
93
94 return Gadget::newFromDefinitionContent( $id, $content );
95 },
96 [
97 'checkKeys' => [ $key ],
98 'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
99 'lockTSE' => 30
100 ]
101 );
102
103 if ( $gadget === null ) {
104 throw new InvalidArgumentException( "No gadget registered for '$id'" );
105 }
106
107 return $gadget;
108 }
109
115 public function purgeGadgetEntry( $id ) {
116 $this->wanCache->touchCheckKey( $this->getGadgetCacheKey( $id ) );
117 }
118
122 private function getGadgetIdsKey() {
123 return $this->wanCache->makeKey( 'gadgets', 'namespace', 'ids' );
124 }
125
130 private function getGadgetCacheKey( $id ) {
131 return $this->wanCache->makeKey(
132 'gadgets', 'object', md5( $id ), Gadget::GADGET_CLASS_VERSION );
133 }
134}
const DB_SLAVE
Definition Defines.php:37
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
GadgetRepo implementation where each gadget has a page in the Gadget definition namespace,...
const CACHE_TTL
How long in seconds the list of gadget ids and individual gadgets should be cached for (1 day)
getGadgetIds()
Get a list of gadget ids from cache/database.
purgeGadgetEntry( $id)
Update the cache for a specific Gadget whenever it is updated.
purgeGadgetIdsList()
Purge the list of gadget ids when a page is deleted or if a new page is created.
const GADGET_CLASS_VERSION
Increment this when changing class structure.
static newFromDefinitionContent( $id, GadgetDefinitionContent $content)
Create a object based on the metadata in a GadgetDefinitionContent object.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
Definition Revision.php:134
Multi-datacenter aware caching interface.
if(! $regexes) $dbr
Definition cleanup.php:94
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition hooks.txt:1760