Apply $wgMaxArticleSize more exactly
[mediawiki.git] / includes / site / CachingSiteStore.php
bloba2f43f307b9f6c4e3f734d8f16f6303118c15717
1 <?php
3 /**
4 * Represents the site configuration of a wiki.
5 * Holds a list of sites (ie SiteList), with a caching layer.
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.
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.
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
22 * @since 1.25
24 * @file
25 * @ingroup Site
27 * @license GNU GPL v2+
28 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 * @author Katie Filbert < aude.wiki@gmail.com >
31 class CachingSiteStore implements SiteStore {
33 /**
34 * @var SiteList|null
36 private $sites = null;
38 /**
39 * @var string|null
41 private $cacheKey;
43 /**
44 * @var int
46 private $cacheTimeout;
48 /**
49 * @var BagOStuff
51 private $cache;
53 /**
54 * @var SiteStore
56 private $siteStore;
58 /**
59 * @param SiteStore $siteStore
60 * @param BagOStuff $cache
61 * @param string|null $cacheKey
62 * @param int $cacheTimeout
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;
76 /**
77 * Constructs a cache key to use for caching the list of sites.
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.
83 * The cache key also includes information about where the sites were loaded from, e.g.
84 * the name of a database table.
86 * @see SiteList::getSerialVersionId
88 * @return string The cache key.
90 private function getCacheKey() {
91 if ( $this->cacheKey === null ) {
92 $type = 'SiteList#' . SiteList::getSerialVersionId();
93 $this->cacheKey = wfMemcKey( "sites/$type" );
96 return $this->cacheKey;
99 /**
100 * @see SiteStore::getSites
102 * @since 1.25
104 * @return SiteList
106 public function getSites() {
107 if ( $this->sites === null ) {
108 $this->sites = $this->cache->get( $this->getCacheKey() );
110 if ( !is_object( $this->sites ) ) {
111 $this->sites = $this->siteStore->getSites();
113 $this->cache->set( $this->getCacheKey(), $this->sites, $this->cacheTimeout );
117 return $this->sites;
121 * @see SiteStore::getSite
123 * @since 1.25
125 * @param string $globalId
127 * @return Site|null
129 public function getSite( $globalId ) {
130 $sites = $this->getSites();
132 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
136 * @see SiteStore::saveSite
138 * @since 1.25
140 * @param Site $site
142 * @return bool Success indicator
144 public function saveSite( Site $site ) {
145 return $this->saveSites( [ $site ] );
149 * @see SiteStore::saveSites
151 * @since 1.25
153 * @param Site[] $sites
155 * @return bool Success indicator
157 public function saveSites( array $sites ) {
158 if ( empty( $sites ) ) {
159 return true;
162 $success = $this->siteStore->saveSites( $sites );
164 // purge cache
165 $this->reset();
167 return $success;
171 * Purges the internal and external cache of the site list, forcing the list.
172 * of sites to be reloaded.
174 * Only use this for testing, as APC is typically used and is per-server
176 * @since 1.25
178 public function reset() {
179 // purge cache
180 $this->cache->delete( $this->getCacheKey() );
181 $this->sites = null;
185 * Clears the list of sites stored.
187 * Only use this for testing, as APC is typically used and is per-server.
189 * @see SiteStore::clear()
191 * @return bool Success
193 public function clear() {
194 $this->reset();
196 return $this->siteStore->clear();