MediaWiki REL1_32
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 ) {
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 ],
53 'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
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,
107 function ( $old, &$ttl, array &$setOpts ) use ( $id ) {
108 $setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) );
109 $title = Title::makeTitleSafe( NS_GADGET_DEFINITION, $id );
110 if ( !$title ) {
111 $ttl = WANObjectCache::TTL_UNCACHEABLE;
112 return null;
113 }
114
115 $rev = Revision::newFromTitle( $title );
116 if ( !$rev ) {
117 $ttl = WANObjectCache::TTL_UNCACHEABLE;
118 return null;
119 }
120
121 $content = $rev->getContent();
122 if ( !$content instanceof GadgetDefinitionContent ) {
123 // Uhm...
124 $ttl = WANObjectCache::TTL_UNCACHEABLE;
125 return null;
126 }
127
129 },
130 [
131 'checkKeys' => [ $key ],
132 'pcTTL' => WANObjectCache::TTL_PROC_SHORT,
133 'lockTSE' => 30
134 ]
135 );
136
137 if ( $gadget === null ) {
138 throw new InvalidArgumentException( "No gadget registered for '$id'" );
139 }
140
141 return $gadget;
142 }
143
149 public function purgeGadgetEntry( $id ) {
150 $this->wanCache->touchCheckKey( $this->getGadgetCacheKey( $id ) );
151 }
152
156 private function getGadgetIdsKey() {
157 return $this->wanCache->makeKey( 'gadgets', 'namespace', 'ids' );
158 }
159
164 private function getGadgetCacheKey( $id ) {
165 return $this->wanCache->makeKey(
166 'gadgets', 'object', md5( $id ), Gadget::GADGET_CLASS_VERSION );
167 }
168}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition Setup.php:121
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.
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:133
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:1818
inNamespace( $ns)
Convenience function to test if it is in the namespace.
getText()
Returns the link in text form, without namespace prefix or fragment.
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$content
const DB_REPLICA
Definition defines.php:25