]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - extensions/Gadgets/includes/GadgetDefinitionNamespaceRepo.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / extensions / Gadgets / includes / GadgetDefinitionNamespaceRepo.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
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.
9  */
10 class GadgetDefinitionNamespaceRepo extends GadgetRepo {
11         /**
12          * How long in seconds the list of gadget ids and
13          * individual gadgets should be cached for (1 day)
14          */
15         const CACHE_TTL = 86400;
16
17         /**
18          * @var WANObjectCache
19          */
20         private $wanCache;
21
22         public function __construct() {
23                 $this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
24         }
25
26         /**
27          * Get a list of gadget ids from cache/database
28          *
29          * @return string[]
30          */
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 ) {
38                                 $dbr = wfGetDB( DB_SLAVE );
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
56         /**
57          * Purge the list of gadget ids when a page is deleted or if a new page is created
58          */
59         public function purgeGadgetIdsList() {
60                 $this->wanCache->touchCheckKey( $this->getGadgetIdsKey() );
61         }
62
63         /**
64          * @param string $id
65          * @throws InvalidArgumentException
66          * @return Gadget
67          */
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
110         /**
111          * Update the cache for a specific Gadget whenever it is updated
112          *
113          * @param string $id
114          */
115         public function purgeGadgetEntry( $id ) {
116                 $this->wanCache->touchCheckKey( $this->getGadgetCacheKey( $id ) );
117         }
118
119         /**
120          * @return string
121          */
122         private function getGadgetIdsKey() {
123                 return $this->wanCache->makeKey( 'gadgets', 'namespace', 'ids' );
124         }
125
126         /**
127          * @param strng $id
128          * @return string
129          */
130         private function getGadgetCacheKey( $id ) {
131                 return $this->wanCache->makeKey(
132                         'gadgets', 'object', md5( $id ), Gadget::GADGET_CLASS_VERSION );
133         }
134 }