Fix hook situation for Skin::doEditSectionLink
[mediawiki.git] / includes / site / SiteSQLStore.php
blob8f25c762f496129f9f2ef4564825349b77b35508
1 <?php
3 /**
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
23 * @since 1.21
25 * @file
26 * @ingroup Site
28 * @license GNU GPL v2+
29 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
31 class SiteSQLStore implements SiteStore {
32 /**
33 * @since 1.21
35 * @var SiteList|null
37 protected $sites = null;
39 /**
40 * @var ORMTable
42 protected $sitesTable;
44 /**
45 * @var string|null
47 private $cacheKey = null;
49 /**
50 * @var int
52 private $cacheTimeout = 3600;
54 /**
55 * @var BagOStuff
57 private $cache;
59 /**
60 * @since 1.21
62 * @param ORMTable|null $sitesTable
63 * @param BagOStuff|null $cache
65 * @return SiteStore
67 public static function newInstance( ORMTable $sitesTable = null, BagOStuff $cache = null ) {
68 if ( $cache === null ) {
69 $cache = wfGetMainCache();
72 return new static( $cache, $sitesTable );
75 /**
76 * Constructor.
78 * @since 1.21
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;
92 /**
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() {
108 if ( $this->cacheKey === null ) {
109 $type = 'SiteList#' . SiteList::getSerialVersionId();
110 $source = $this->sitesTable->getName();
112 if ( $this->sitesTable->getTargetWiki() !== false ) {
113 $source = $this->sitesTable->getTargetWiki() . '.' . $source;
116 $this->cacheKey = wfMemcKey( "$source/$type" );
119 return $this->cacheKey;
123 * @see SiteStore::getSites
125 * @since 1.21
127 * @param string $source Either 'cache' or 'recache'
129 * @return SiteList
131 public function getSites( $source = 'cache' ) {
133 if ( $source === 'cache' ) {
134 if ( $this->sites === null ) {
135 $sites = $this->cache->get( $this->getCacheKey() );
137 if ( is_object( $sites ) ) {
138 $this->sites = $sites;
139 } else {
140 $this->loadSites();
144 else {
145 $this->loadSites();
148 return $this->sites;
152 * Returns a new Site object constructed from the provided ORMRow.
154 * @since 1.21
156 * @param ORMRow $siteRow
158 * @return Site
160 protected function siteFromRow( ORMRow $siteRow ) {
162 $site = Site::newForType( $siteRow->getField( 'type', Site::TYPE_UNKNOWN ) );
164 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
166 $site->setInternalId( $siteRow->getField( 'id' ) );
168 if ( $siteRow->hasField( 'forward' ) ) {
169 $site->setForward( $siteRow->getField( 'forward' ) );
172 if ( $siteRow->hasField( 'group' ) ) {
173 $site->setGroup( $siteRow->getField( 'group' ) );
176 if ( $siteRow->hasField( 'language' ) ) {
177 $site->setLanguageCode( $siteRow->getField( 'language' ) === ''
178 ? null
179 : $siteRow->getField( 'language' )
183 if ( $siteRow->hasField( 'source' ) ) {
184 $site->setSource( $siteRow->getField( 'source' ) );
187 if ( $siteRow->hasField( 'data' ) ) {
188 $site->setExtraData( $siteRow->getField( 'data' ) );
191 if ( $siteRow->hasField( 'config' ) ) {
192 $site->setExtraConfig( $siteRow->getField( 'config' ) );
195 return $site;
199 * Get a new ORMRow from a Site object
201 * @since 1.22
203 * @param Site $site
205 * @return ORMRow
207 protected function getRowFromSite( Site $site ) {
208 $fields = array(
209 // Site data
210 'global_key' => $site->getGlobalId(), // TODO: check not null
211 'type' => $site->getType(),
212 'group' => $site->getGroup(),
213 'source' => $site->getSource(),
214 'language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
215 'protocol' => $site->getProtocol(),
216 'domain' => strrev( $site->getDomain() ) . '.',
217 'data' => $site->getExtraData(),
219 // Site config
220 'forward' => $site->shouldForward(),
221 'config' => $site->getExtraConfig(),
224 if ( $site->getInternalId() !== null ) {
225 $fields['id'] = $site->getInternalId();
228 return new ORMRow( $this->sitesTable, $fields );
232 * Fetches the site from the database and loads them into the sites field.
234 * @since 1.21
236 protected function loadSites() {
238 $this->sites = new SiteList();
240 foreach ( $this->sitesTable->select() as $siteRow ) {
241 $this->sites[] = $this->siteFromRow( $siteRow );
244 // Batch load the local site identifiers.
245 $ids = wfGetDB( $this->sitesTable->getReadDb() )->select(
246 'site_identifiers',
247 array(
248 'si_site',
249 'si_type',
250 'si_key',
252 array(),
253 __METHOD__
256 foreach ( $ids as $id ) {
257 if ( $this->sites->hasInternalId( $id->si_site ) ) {
258 $site = $this->sites->getSiteByInternalId( $id->si_site );
259 $site->addLocalId( $id->si_type, $id->si_key );
260 $this->sites->setSite( $site );
264 $this->cache->set( $this->getCacheKey(), $this->sites, $this->cacheTimeout );
269 * @see SiteStore::getSite
271 * @since 1.21
273 * @param string $globalId
274 * @param string $source
276 * @return Site|null
278 public function getSite( $globalId, $source = 'cache' ) {
280 $sites = $this->getSites( $source );
282 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
286 * @see SiteStore::saveSite
288 * @since 1.21
290 * @param Site $site
292 * @return bool Success indicator
294 public function saveSite( Site $site ) {
295 return $this->saveSites( array( $site ) );
299 * @see SiteStore::saveSites
301 * @since 1.21
303 * @param Site[] $sites
305 * @return bool Success indicator
307 public function saveSites( array $sites ) {
309 if ( empty( $sites ) ) {
310 return true;
313 $dbw = $this->sitesTable->getWriteDbConnection();
315 $dbw->startAtomic( __METHOD__ );
317 $success = true;
319 $internalIds = array();
320 $localIds = array();
322 foreach ( $sites as $site ) {
323 if ( $site->getInternalId() !== null ) {
324 $internalIds[] = $site->getInternalId();
327 $siteRow = $this->getRowFromSite( $site );
328 $success = $siteRow->save( __METHOD__ ) && $success;
330 foreach ( $site->getLocalIds() as $idType => $ids ) {
331 foreach ( $ids as $id ) {
332 $localIds[] = array( $siteRow->getId(), $idType, $id );
337 if ( $internalIds !== array() ) {
338 $dbw->delete(
339 'site_identifiers',
340 array( 'si_site' => $internalIds ),
341 __METHOD__
345 foreach ( $localIds as $localId ) {
346 $dbw->insert(
347 'site_identifiers',
348 array(
349 'si_site' => $localId[0],
350 'si_type' => $localId[1],
351 'si_key' => $localId[2],
353 __METHOD__
357 $dbw->endAtomic( __METHOD__ );
359 // purge cache
360 $this->reset();
362 return $success;
366 * Purges the internal and external cache of the site list, forcing the list
367 * of sites to be re-read from the database.
369 * @since 1.21
371 public function reset() {
372 // purge cache
373 $this->cache->delete( $this->getCacheKey() );
374 $this->sites = null;
379 * Clears the list of sites stored in the database.
381 * @see SiteStore::clear()
383 * @return bool Success
385 public function clear() {
386 $dbw = $this->sitesTable->getWriteDbConnection();
388 $dbw->startAtomic( __METHOD__ );
389 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
390 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
391 $dbw->endAtomic( __METHOD__ );
393 $this->reset();
395 return $ok;
399 * @since 1.21
401 * @return ORMTable
403 protected function newSitesTable() {
404 return new ORMTable(
405 'sites',
406 array(
407 'id' => 'id',
409 // Site data
410 'global_key' => 'str',
411 'type' => 'str',
412 'group' => 'str',
413 'source' => 'str',
414 'language' => 'str',
415 'protocol' => 'str',
416 'domain' => 'str',
417 'data' => 'array',
419 // Site config
420 'forward' => 'bool',
421 'config' => 'array',
423 array(
424 'type' => Site::TYPE_UNKNOWN,
425 'group' => Site::GROUP_NONE,
426 'source' => Site::SOURCE_LOCAL,
427 'data' => array(),
429 'forward' => false,
430 'config' => array(),
431 'language' => '',
433 'ORMRow',
434 'site_'