MediaWiki  1.34.0
GadgetDefinitionNamespaceRepo.php
Go to the documentation of this file.
1 <?php
2 
6 
17  const CACHE_TTL = 86400;
18 
22  private $wanCache;
23 
24  public function __construct() {
25  $this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
26  }
27 
33  public function getGadgetIds() {
34  $key = $this->getGadgetIdsKey();
35 
36  $fname = __METHOD__;
37  return $this->wanCache->getWithSetCallback(
38  $key,
39  self::CACHE_TTL,
40  function ( $oldValue, &$ttl, array &$setOpts ) use ( $fname ) {
41  $dbr = wfGetDB( DB_REPLICA );
42  $setOpts += Database::getCacheSetOptions( $dbr );
43 
44  return $dbr->selectFieldValues(
45  'page',
46  'page_title',
47  [ 'page_namespace' => NS_GADGET_DEFINITION ],
48  $fname
49  );
50  },
51  [
52  'checkKeys' => [ $key ],
54  'lockTSE' => 30
55  ]
56  );
57  }
58 
62  public function handlePageUpdate( LinkTarget $target ) {
63  if ( $target->inNamespace( NS_GADGET_DEFINITION ) ) {
64  $this->purgeGadgetEntry( $target->getText() );
65  }
66  }
67 
71  public function handlePageCreation( LinkTarget $target ) {
72  if ( $target->inNamespace( NS_GADGET_DEFINITION ) ) {
73  $this->purgeGadgetIdsList();
74  }
75  }
76 
80  public function handlePageDeletion( LinkTarget $target ) {
81  if ( $target->inNamespace( NS_GADGET_DEFINITION ) ) {
82  $this->purgeGadgetIdsList();
83  $this->purgeGadgetEntry( $target->getText() );
84  }
85  }
86 
90  public function purgeGadgetIdsList() {
91  $this->wanCache->touchCheckKey( $this->getGadgetIdsKey() );
92  }
93 
99  public function getGadget( $id ) {
100  $key = $this->getGadgetCacheKey( $id );
101  $gadget = $this->wanCache->getWithSetCallback(
102  $key,
103  self::CACHE_TTL,
104  function ( $old, &$ttl, array &$setOpts ) use ( $id ) {
105  $setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) );
106  $title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $id );
107  if ( !$title ) {
108  $ttl = WANObjectCache::TTL_UNCACHEABLE;
109  return null;
110  }
111 
112  $rev = Revision::newFromTitle( $title );
113  if ( !$rev ) {
114  $ttl = WANObjectCache::TTL_UNCACHEABLE;
115  return null;
116  }
117 
118  $content = $rev->getContent();
119  if ( !$content instanceof GadgetDefinitionContent ) {
120  // Uhm...
121  $ttl = WANObjectCache::TTL_UNCACHEABLE;
122  return null;
123  }
124 
126  },
127  [
128  'checkKeys' => [ $key ],
130  'lockTSE' => 30
131  ]
132  );
133 
134  if ( $gadget === null ) {
135  throw new InvalidArgumentException( "No gadget registered for '$id'" );
136  }
137 
138  return $gadget;
139  }
140 
146  public function purgeGadgetEntry( $id ) {
147  $this->wanCache->touchCheckKey( $this->getGadgetCacheKey( $id ) );
148  }
149 
153  private function getGadgetIdsKey() {
154  return $this->wanCache->makeKey( 'gadgets', 'namespace', 'ids' );
155  }
156 
161  private function getGadgetCacheKey( $id ) {
162  return $this->wanCache->makeKey(
163  'gadgets', 'object', md5( $id ), Gadget::GADGET_CLASS_VERSION );
164  }
165 }
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:49
MediaWiki\Linker\LinkTarget\getText
getText()
Returns the link in text form, without namespace prefix or fragment.
GadgetDefinitionNamespaceRepo\getGadgetCacheKey
getGadgetCacheKey( $id)
Definition: GadgetDefinitionNamespaceRepo.php:161
GadgetDefinitionNamespaceRepo\CACHE_TTL
const CACHE_TTL
How long in seconds the list of gadget ids and individual gadgets should be cached for (1 day)
Definition: GadgetDefinitionNamespaceRepo.php:17
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
GadgetDefinitionNamespaceRepo\handlePageDeletion
handlePageDeletion(LinkTarget $target)
Given that the provided page was updated, invalidate caches if necessary.void
Definition: GadgetDefinitionNamespaceRepo.php:80
Gadget\newFromDefinitionContent
static newFromDefinitionContent( $id, GadgetDefinitionContent $content)
Create a object based on the metadata in a GadgetDefinitionContent object.
Definition: Gadget.php:74
MediaWiki\Linker\LinkTarget\inNamespace
inNamespace( $ns)
Convenience function to test if it is in the namespace.
GadgetDefinitionNamespaceRepo\__construct
__construct()
Definition: GadgetDefinitionNamespaceRepo.php:24
$dbr
$dbr
Definition: testCompression.php:50
Revision\newFromTitle
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:138
GadgetDefinitionNamespaceRepo\purgeGadgetIdsList
purgeGadgetIdsList()
Purge the list of gadget ids when a page is deleted or if a new page is created.
Definition: GadgetDefinitionNamespaceRepo.php:90
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
GadgetDefinitionNamespaceRepo\$wanCache
WANObjectCache $wanCache
Definition: GadgetDefinitionNamespaceRepo.php:22
$title
$title
Definition: testCompression.php:34
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
GadgetRepo
Definition: GadgetRepo.php:5
GadgetDefinitionNamespaceRepo\getGadgetIds
getGadgetIds()
Get a list of gadget ids from cache/database.
Definition: GadgetDefinitionNamespaceRepo.php:33
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:613
$content
$content
Definition: router.php:78
GadgetDefinitionContent
Definition: GadgetDefinitionContent.php:23
IExpiringStore\TTL_PROC_SHORT
const TTL_PROC_SHORT
Definition: IExpiringStore.php:41
GadgetDefinitionNamespaceRepo
GadgetRepo implementation where each gadget has a page in the Gadget definition namespace,...
Definition: GadgetDefinitionNamespaceRepo.php:12
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:116
GadgetDefinitionNamespaceRepo\handlePageUpdate
handlePageUpdate(LinkTarget $target)
Given that the provided page was updated, invalidate caches if necessary.void
Definition: GadgetDefinitionNamespaceRepo.php:62
GadgetDefinitionNamespaceRepo\getGadget
getGadget( $id)
Definition: GadgetDefinitionNamespaceRepo.php:99
GadgetDefinitionNamespaceRepo\getGadgetIdsKey
getGadgetIdsKey()
Definition: GadgetDefinitionNamespaceRepo.php:153
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
GadgetDefinitionNamespaceRepo\purgeGadgetEntry
purgeGadgetEntry( $id)
Update the cache for a specific Gadget whenever it is updated.
Definition: GadgetDefinitionNamespaceRepo.php:146
Gadget\GADGET_CLASS_VERSION
const GADGET_CLASS_VERSION
Increment this when changing class structure.
Definition: Gadget.php:21
GadgetDefinitionNamespaceRepo\handlePageCreation
handlePageCreation(LinkTarget $target)
Given that the provided page was created, invalidate caches if necessary.void
Definition: GadgetDefinitionNamespaceRepo.php:71