]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/site/CachingSiteStore.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.git] / includes / site / CachingSiteStore.php
1 <?php
2
3 /**
4  * Represents the site configuration of a wiki.
5  * Holds a list of sites (ie SiteList), with a caching layer.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  * http://www.gnu.org/copyleft/gpl.html
21  *
22  * @since 1.25
23  *
24  * @file
25  * @ingroup Site
26  *
27  * @license GNU GPL v2+
28  * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29  * @author Katie Filbert < aude.wiki@gmail.com >
30  */
31 class CachingSiteStore implements SiteStore {
32
33         /**
34          * @var SiteList|null
35          */
36         private $sites = null;
37
38         /**
39          * @var string|null
40          */
41         private $cacheKey;
42
43         /**
44          * @var int
45          */
46         private $cacheTimeout;
47
48         /**
49          * @var BagOStuff
50          */
51         private $cache;
52
53         /**
54          * @var SiteStore
55          */
56         private $siteStore;
57
58         /**
59          * @param SiteStore $siteStore
60          * @param BagOStuff $cache
61          * @param string|null $cacheKey
62          * @param int $cacheTimeout
63          */
64         public function __construct(
65                 SiteStore $siteStore,
66                 BagOStuff $cache,
67                 $cacheKey = null,
68                 $cacheTimeout = 3600
69         ) {
70                 $this->siteStore = $siteStore;
71                 $this->cache = $cache;
72                 $this->cacheKey = $cacheKey;
73                 $this->cacheTimeout = $cacheTimeout;
74         }
75
76         /**
77          * Constructs a cache key to use for caching the list of sites.
78          *
79          * This includes the concrete class name of the site list as well as a version identifier
80          * for the list's serialization, to avoid problems when unserializing site lists serialized
81          * by an older version, e.g. when reading from a cache.
82          *
83          * The cache key also includes information about where the sites were loaded from, e.g.
84          * the name of a database table.
85          *
86          * @see SiteList::getSerialVersionId
87          *
88          * @return string The cache key.
89          */
90         private function getCacheKey() {
91                 if ( $this->cacheKey === null ) {
92                         $type = 'SiteList#' . SiteList::getSerialVersionId();
93                         $this->cacheKey = $this->cache->makeKey( "sites/$type" );
94                 }
95
96                 return $this->cacheKey;
97         }
98
99         /**
100          * @see SiteStore::getSites
101          *
102          * @since 1.25
103          *
104          * @return SiteList
105          */
106         public function getSites() {
107                 if ( $this->sites === null ) {
108                         $this->sites = $this->cache->get( $this->getCacheKey() );
109
110                         if ( !is_object( $this->sites ) ) {
111                                 $this->sites = $this->siteStore->getSites();
112
113                                 $this->cache->set( $this->getCacheKey(), $this->sites, $this->cacheTimeout );
114                         }
115                 }
116
117                 return $this->sites;
118         }
119
120         /**
121          * @see SiteStore::getSite
122          *
123          * @since 1.25
124          *
125          * @param string $globalId
126          *
127          * @return Site|null
128          */
129         public function getSite( $globalId ) {
130                 $sites = $this->getSites();
131
132                 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
133         }
134
135         /**
136          * @see SiteStore::saveSite
137          *
138          * @since 1.25
139          *
140          * @param Site $site
141          *
142          * @return bool Success indicator
143          */
144         public function saveSite( Site $site ) {
145                 return $this->saveSites( [ $site ] );
146         }
147
148         /**
149          * @see SiteStore::saveSites
150          *
151          * @since 1.25
152          *
153          * @param Site[] $sites
154          *
155          * @return bool Success indicator
156          */
157         public function saveSites( array $sites ) {
158                 if ( empty( $sites ) ) {
159                         return true;
160                 }
161
162                 $success = $this->siteStore->saveSites( $sites );
163
164                 // purge cache
165                 $this->reset();
166
167                 return $success;
168         }
169
170         /**
171          * Purges the internal and external cache of the site list, forcing the list.
172          * of sites to be reloaded.
173          *
174          * Only use this for testing, as APC is typically used and is per-server
175          *
176          * @since 1.25
177          */
178         public function reset() {
179                 // purge cache
180                 $this->cache->delete( $this->getCacheKey() );
181                 $this->sites = null;
182         }
183
184         /**
185          * Clears the list of sites stored.
186          *
187          * Only use this for testing, as APC is typically used and is per-server.
188          *
189          * @see SiteStore::clear()
190          *
191          * @return bool Success
192          */
193         public function clear() {
194                 $this->reset();
195
196                 return $this->siteStore->clear();
197         }
198
199 }