4 * Represents the site configuration of a wiki.
5 * Holds a list of sites (ie SiteList) and takes care
6 * of retrieving and caching site information when appropriate.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
28 * @license GNU GPL v2+
29 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
31 class SiteSQLStore
implements SiteStore
{
37 protected $sites = null;
42 protected $sitesTable;
47 private $cacheKey = null;
52 private $cacheTimeout = 3600;
62 * @param ORMTable|null $sitesTable
63 * @param BagOStuff|null $cache
67 public static function newInstance( ORMTable
$sitesTable = null, BagOStuff
$cache = null ) {
68 if ( $cache === null ) {
69 $cache = wfGetMainCache();
72 return new static( $cache, $sitesTable );
80 * @param BagOStuff $cache
81 * @param ORMTable|null $sitesTable
83 protected function __construct( BagOStuff
$cache, ORMTable
$sitesTable = null ) {
84 if ( $sitesTable === null ) {
85 $sitesTable = $this->newSitesTable();
88 $this->cache
= $cache;
89 $this->sitesTable
= $sitesTable;
93 * Constructs a cache key to use for caching the list of sites.
95 * This includes the concrete class name of the site list as well as a version identifier
96 * for the list's serialization, to avoid problems when unserializing site lists serialized
97 * by an older version, e.g. when reading from a cache.
99 * The cache key also includes information about where the sites were loaded from, e.g.
100 * the name of a database table.
102 * @see SiteList::getSerialVersionId
104 * @return string The cache key.
106 protected function getCacheKey() {
107 wfProfileIn( __METHOD__
);
109 if ( $this->cacheKey
=== null ) {
110 $type = 'SiteList#' . SiteList
::getSerialVersionId();
111 $source = $this->sitesTable
->getName();
113 if ( $this->sitesTable
->getTargetWiki() !== false ) {
114 $source = $this->sitesTable
->getTargetWiki() . '.' . $source;
117 $this->cacheKey
= wfMemcKey( "$source/$type" );
120 wfProfileOut( __METHOD__
);
121 return $this->cacheKey
;
125 * @see SiteStore::getSites
129 * @param string $source Either 'cache' or 'recache'
133 public function getSites( $source = 'cache' ) {
134 wfProfileIn( __METHOD__
);
136 if ( $source === 'cache' ) {
137 if ( $this->sites
=== null ) {
138 $sites = $this->cache
->get( $this->getCacheKey() );
140 if ( is_object( $sites ) ) {
141 $this->sites
= $sites;
151 wfProfileOut( __METHOD__
);
156 * Returns a new Site object constructed from the provided ORMRow.
160 * @param ORMRow $siteRow
164 protected function siteFromRow( ORMRow
$siteRow ) {
165 wfProfileIn( __METHOD__
);
167 $site = Site
::newForType( $siteRow->getField( 'type', Site
::TYPE_UNKNOWN
) );
169 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
171 $site->setInternalId( $siteRow->getField( 'id' ) );
173 if ( $siteRow->hasField( 'forward' ) ) {
174 $site->setForward( $siteRow->getField( 'forward' ) );
177 if ( $siteRow->hasField( 'group' ) ) {
178 $site->setGroup( $siteRow->getField( 'group' ) );
181 if ( $siteRow->hasField( 'language' ) ) {
182 $site->setLanguageCode( $siteRow->getField( 'language' ) === ''
184 : $siteRow->getField( 'language' )
188 if ( $siteRow->hasField( 'source' ) ) {
189 $site->setSource( $siteRow->getField( 'source' ) );
192 if ( $siteRow->hasField( 'data' ) ) {
193 $site->setExtraData( $siteRow->getField( 'data' ) );
196 if ( $siteRow->hasField( 'config' ) ) {
197 $site->setExtraConfig( $siteRow->getField( 'config' ) );
200 wfProfileOut( __METHOD__
);
205 * Get a new ORMRow from a Site object
213 protected function getRowFromSite( Site
$site ) {
216 'global_key' => $site->getGlobalId(), // TODO: check not null
217 'type' => $site->getType(),
218 'group' => $site->getGroup(),
219 'source' => $site->getSource(),
220 'language' => $site->getLanguageCode() === null ?
'' : $site->getLanguageCode(),
221 'protocol' => $site->getProtocol(),
222 'domain' => strrev( $site->getDomain() ) . '.',
223 'data' => $site->getExtraData(),
226 'forward' => $site->shouldForward(),
227 'config' => $site->getExtraConfig(),
230 if ( $site->getInternalId() !== null ) {
231 $fields['id'] = $site->getInternalId();
234 return new ORMRow( $this->sitesTable
, $fields );
238 * Fetches the site from the database and loads them into the sites field.
242 protected function loadSites() {
243 wfProfileIn( __METHOD__
);
245 $this->sites
= new SiteList();
247 foreach ( $this->sitesTable
->select() as $siteRow ) {
248 $this->sites
[] = $this->siteFromRow( $siteRow );
251 // Batch load the local site identifiers.
252 $ids = wfGetDB( $this->sitesTable
->getReadDb() )->select(
263 foreach ( $ids as $id ) {
264 if ( $this->sites
->hasInternalId( $id->si_site
) ) {
265 $site = $this->sites
->getSiteByInternalId( $id->si_site
);
266 $site->addLocalId( $id->si_type
, $id->si_key
);
267 $this->sites
->setSite( $site );
271 $this->cache
->set( $this->getCacheKey(), $this->sites
, $this->cacheTimeout
);
273 wfProfileOut( __METHOD__
);
277 * @see SiteStore::getSite
281 * @param string $globalId
282 * @param string $source
286 public function getSite( $globalId, $source = 'cache' ) {
287 wfProfileIn( __METHOD__
);
289 $sites = $this->getSites( $source );
291 wfProfileOut( __METHOD__
);
292 return $sites->hasSite( $globalId ) ?
$sites->getSite( $globalId ) : null;
296 * @see SiteStore::saveSite
302 * @return bool Success indicator
304 public function saveSite( Site
$site ) {
305 return $this->saveSites( array( $site ) );
309 * @see SiteStore::saveSites
313 * @param Site[] $sites
315 * @return bool Success indicator
317 public function saveSites( array $sites ) {
318 wfProfileIn( __METHOD__
);
320 if ( empty( $sites ) ) {
321 wfProfileOut( __METHOD__
);
325 $dbw = $this->sitesTable
->getWriteDbConnection();
327 $dbw->startAtomic( __METHOD__
);
331 $internalIds = array();
334 foreach ( $sites as $site ) {
335 if ( $site->getInternalId() !== null ) {
336 $internalIds[] = $site->getInternalId();
339 $siteRow = $this->getRowFromSite( $site );
340 $success = $siteRow->save( __METHOD__
) && $success;
342 foreach ( $site->getLocalIds() as $idType => $ids ) {
343 foreach ( $ids as $id ) {
344 $localIds[] = array( $siteRow->getId(), $idType, $id );
349 if ( $internalIds !== array() ) {
352 array( 'si_site' => $internalIds ),
357 foreach ( $localIds as $localId ) {
361 'si_site' => $localId[0],
362 'si_type' => $localId[1],
363 'si_key' => $localId[2],
369 $dbw->endAtomic( __METHOD__
);
374 wfProfileOut( __METHOD__
);
379 * Purges the internal and external cache of the site list, forcing the list
380 * of sites to be re-read from the database.
384 public function reset() {
385 wfProfileIn( __METHOD__
);
387 $this->cache
->delete( $this->getCacheKey() );
390 wfProfileOut( __METHOD__
);
394 * Clears the list of sites stored in the database.
396 * @see SiteStore::clear()
398 * @return bool Success
400 public function clear() {
401 wfProfileIn( __METHOD__
);
402 $dbw = $this->sitesTable
->getWriteDbConnection();
404 $dbw->startAtomic( __METHOD__
);
405 $ok = $dbw->delete( 'sites', '*', __METHOD__
);
406 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__
) && $ok;
407 $dbw->endAtomic( __METHOD__
);
411 wfProfileOut( __METHOD__
);
420 protected function newSitesTable() {
427 'global_key' => 'str',
441 'type' => Site
::TYPE_UNKNOWN
,
442 'group' => Site
::GROUP_NONE
,
443 'source' => Site
::SOURCE_LOCAL
,