MediaWiki REL1_31
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 return $this->wanCache->getWithSetCallback(
37 $key,
38 self::CACHE_TTL,
39 function ( $oldValue, &$ttl, array &$setOpts ) {
41 $setOpts += Database::getCacheSetOptions( $dbr );
42
43 return $dbr->selectFieldValues(
44 'page',
45 'page_title',
46 [ 'page_namespace' => NS_GADGET_DEFINITION ],
47 __METHOD__
48 );
49 },
50 [
51 'checkKeys' => [ $key ],
52 'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
53 'lockTSE' => 30
54 ]
55 );
56 }
57
61 public function handlePageUpdate( LinkTarget $target ) {
62 if ( $target->inNamespace( NS_GADGET_DEFINITION ) ) {
63 $this->purgeGadgetEntry( $target->getText() );
64 }
65 }
66
70 public function handlePageCreation( LinkTarget $target ) {
71 if ( $target->inNamespace( NS_GADGET_DEFINITION ) ) {
72 $this->purgeGadgetIdsList();
73 }
74 }
75
79 public function handlePageDeletion( LinkTarget $target ) {
80 if ( $target->inNamespace( NS_GADGET_DEFINITION ) ) {
81 $this->purgeGadgetIdsList();
82 $this->purgeGadgetEntry( $target->getText() );
83 }
84 }
85
89 public function purgeGadgetIdsList() {
90 $this->wanCache->touchCheckKey( $this->getGadgetIdsKey() );
91 }
92
98 public function getGadget( $id ) {
99 $key = $this->getGadgetCacheKey( $id );
100 $gadget = $this->wanCache->getWithSetCallback(
101 $key,
102 self::CACHE_TTL,
106 function ( $old, &$ttl, array &$setOpts ) use ( $id ) {
107 $setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) );
108 $title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $id );
109 if ( !$title ) {
110 $ttl = WANObjectCache::TTL_UNCACHEABLE;
111 return null;
112 }
113
114 $rev = Revision::newFromTitle( $title );
115 if ( !$rev ) {
116 $ttl = WANObjectCache::TTL_UNCACHEABLE;
117 return null;
118 }
119
120 $content = $rev->getContent();
121 if ( !$content instanceof GadgetDefinitionContent ) {
122 // Uhm...
123 $ttl = WANObjectCache::TTL_UNCACHEABLE;
124 return null;
125 }
126
127 return Gadget::newFromDefinitionContent( $id, $content );
128 },
129 [
130 'checkKeys' => [ $key ],
131 'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
132 'lockTSE' => 30
133 ]
134 );
135
136 if ( $gadget === null ) {
137 throw new InvalidArgumentException( "No gadget registered for '$id'" );
138 }
139
140 return $gadget;
141 }
142
148 public function purgeGadgetEntry( $id ) {
149 $this->wanCache->touchCheckKey( $this->getGadgetCacheKey( $id ) );
150 }
151
155 private function getGadgetIdsKey() {
156 return $this->wanCache->makeKey( 'gadgets', 'namespace', 'ids' );
157 }
158
163 private function getGadgetCacheKey( $id ) {
164 return $this->wanCache->makeKey(
165 'gadgets', 'object', md5( $id ), Gadget::GADGET_CLASS_VERSION );
166 }
167}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
GadgetRepo implementation where each gadget has a page in the Gadget definition namespace,...
handlePageDeletion(LinkTarget $target)
@inheritDoc
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.
handlePageCreation(LinkTarget $target)
@inheritDoc
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.
handlePageUpdate(LinkTarget $target)
@inheritDoc
const GADGET_CLASS_VERSION
Increment this when changing class structure.
Definition Gadget.php:21
static newFromDefinitionContent( $id, GadgetDefinitionContent $content)
Create a object based on the metadata in a GadgetDefinitionContent object.
Definition Gadget.php:74
MediaWikiServices is the service locator for the application scope of MediaWiki.
Multi-datacenter aware caching interface.
Relational database abstraction object.
Definition Database.php:48
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:1777
inNamespace( $ns)
Convenience function to test if it is in the namespace.
getText()
Returns the link in text form, without namespace prefix or fragment.
const DB_REPLICA
Definition defines.php:25