Profiler: Call getContentType() only once in logData()
[mediawiki.git] / includes / site / SiteSQLStore.php
blobe5d05bec547f8647efdcc4d55addb8ae80404aab
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 * @since 1.21
57 * @param ORMTable|null $sitesTable
59 * @return SiteStore
61 public static function newInstance( ORMTable $sitesTable = null ) {
62 return new static( $sitesTable );
65 /**
66 * Constructor.
68 * @since 1.21
70 * @param ORMTable|null $sitesTable
72 protected function __construct( ORMTable $sitesTable = null ) {
73 if ( $sitesTable === null ) {
74 $sitesTable = $this->newSitesTable();
77 $this->sitesTable = $sitesTable;
80 /**
81 * Constructs a cache key to use for caching the list of sites.
83 * This includes the concrete class name of the site list as well as a version identifier
84 * for the list's serialization, to avoid problems when unserializing site lists serialized
85 * by an older version, e.g. when reading from a cache.
87 * The cache key also includes information about where the sites were loaded from, e.g.
88 * the name of a database table.
90 * @see SiteList::getSerialVersionId
92 * @return string The cache key.
94 protected function getCacheKey() {
95 wfProfileIn( __METHOD__ );
97 if ( $this->cacheKey === null ) {
98 $type = 'SiteList#' . SiteList::getSerialVersionId();
99 $source = $this->sitesTable->getName();
101 if ( $this->sitesTable->getTargetWiki() !== false ) {
102 $source = $this->sitesTable->getTargetWiki() . '.' . $source;
105 $this->cacheKey = wfMemcKey( "$source/$type" );
108 wfProfileOut( __METHOD__ );
109 return $this->cacheKey;
113 * @see SiteStore::getSites
115 * @since 1.21
117 * @param string $source Either 'cache' or 'recache'
119 * @return SiteList
121 public function getSites( $source = 'cache' ) {
122 wfProfileIn( __METHOD__ );
124 if ( $source === 'cache' ) {
125 if ( $this->sites === null ) {
126 $cache = wfGetMainCache();
127 $sites = $cache->get( $this->getCacheKey() );
129 if ( is_object( $sites ) ) {
130 $this->sites = $sites;
131 } else {
132 $this->loadSites();
136 else {
137 $this->loadSites();
140 wfProfileOut( __METHOD__ );
141 return $this->sites;
145 * Returns a new Site object constructed from the provided ORMRow.
147 * @since 1.21
149 * @param ORMRow $siteRow
151 * @return Site
153 protected function siteFromRow( ORMRow $siteRow ) {
154 wfProfileIn( __METHOD__ );
156 $site = Site::newForType( $siteRow->getField( 'type', Site::TYPE_UNKNOWN ) );
158 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
160 $site->setInternalId( $siteRow->getField( 'id' ) );
162 if ( $siteRow->hasField( 'forward' ) ) {
163 $site->setForward( $siteRow->getField( 'forward' ) );
166 if ( $siteRow->hasField( 'group' ) ) {
167 $site->setGroup( $siteRow->getField( 'group' ) );
170 if ( $siteRow->hasField( 'language' ) ) {
171 $site->setLanguageCode( $siteRow->getField( 'language' ) === ''
172 ? null
173 : $siteRow->getField( 'language' )
177 if ( $siteRow->hasField( 'source' ) ) {
178 $site->setSource( $siteRow->getField( 'source' ) );
181 if ( $siteRow->hasField( 'data' ) ) {
182 $site->setExtraData( $siteRow->getField( 'data' ) );
185 if ( $siteRow->hasField( 'config' ) ) {
186 $site->setExtraConfig( $siteRow->getField( 'config' ) );
189 wfProfileOut( __METHOD__ );
190 return $site;
194 * Get a new ORMRow from a Site object
196 * @since 1.22
198 * @param Site $site
200 * @return ORMRow
202 protected function getRowFromSite( Site $site ) {
203 $fields = array(
204 // Site data
205 'global_key' => $site->getGlobalId(), // TODO: check not null
206 'type' => $site->getType(),
207 'group' => $site->getGroup(),
208 'source' => $site->getSource(),
209 'language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
210 'protocol' => $site->getProtocol(),
211 'domain' => strrev( $site->getDomain() ) . '.',
212 'data' => $site->getExtraData(),
214 // Site config
215 'forward' => $site->shouldForward(),
216 'config' => $site->getExtraConfig(),
219 if ( $site->getInternalId() !== null ) {
220 $fields['id'] = $site->getInternalId();
223 return new ORMRow( $this->sitesTable, $fields );
227 * Fetches the site from the database and loads them into the sites field.
229 * @since 1.21
231 protected function loadSites() {
232 wfProfileIn( __METHOD__ );
234 $this->sites = new SiteList();
236 foreach ( $this->sitesTable->select() as $siteRow ) {
237 $this->sites[] = $this->siteFromRow( $siteRow );
240 // Batch load the local site identifiers.
241 $ids = wfGetDB( $this->sitesTable->getReadDb() )->select(
242 'site_identifiers',
243 array(
244 'si_site',
245 'si_type',
246 'si_key',
248 array(),
249 __METHOD__
252 foreach ( $ids as $id ) {
253 if ( $this->sites->hasInternalId( $id->si_site ) ) {
254 $site = $this->sites->getSiteByInternalId( $id->si_site );
255 $site->addLocalId( $id->si_type, $id->si_key );
256 $this->sites->setSite( $site );
260 $cache = wfGetMainCache();
261 $cache->set( $this->getCacheKey(), $this->sites, $this->cacheTimeout );
263 wfProfileOut( __METHOD__ );
267 * @see SiteStore::getSite
269 * @since 1.21
271 * @param string $globalId
272 * @param string $source
274 * @return Site|null
276 public function getSite( $globalId, $source = 'cache' ) {
277 wfProfileIn( __METHOD__ );
279 $sites = $this->getSites( $source );
281 wfProfileOut( __METHOD__ );
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 ) {
308 wfProfileIn( __METHOD__ );
310 if ( empty( $sites ) ) {
311 wfProfileOut( __METHOD__ );
312 return true;
315 $dbw = $this->sitesTable->getWriteDbConnection();
317 $dbw->startAtomic( __METHOD__ );
319 $success = true;
321 $internalIds = array();
322 $localIds = array();
324 foreach ( $sites as $site ) {
325 if ( $site->getInternalId() !== null ) {
326 $internalIds[] = $site->getInternalId();
329 $siteRow = $this->getRowFromSite( $site );
330 $success = $siteRow->save( __METHOD__ ) && $success;
332 foreach ( $site->getLocalIds() as $idType => $ids ) {
333 foreach ( $ids as $id ) {
334 $localIds[] = array( $siteRow->getId(), $idType, $id );
339 if ( $internalIds !== array() ) {
340 $dbw->delete(
341 'site_identifiers',
342 array( 'si_site' => $internalIds ),
343 __METHOD__
347 foreach ( $localIds as $localId ) {
348 $dbw->insert(
349 'site_identifiers',
350 array(
351 'si_site' => $localId[0],
352 'si_type' => $localId[1],
353 'si_key' => $localId[2],
355 __METHOD__
359 $dbw->endAtomic( __METHOD__ );
361 // purge cache
362 $this->reset();
364 wfProfileOut( __METHOD__ );
365 return $success;
369 * Purges the internal and external cache of the site list, forcing the list
370 * of sites to be re-read from the database.
372 * @since 1.21
374 public function reset() {
375 wfProfileIn( __METHOD__ );
376 // purge cache
377 $cache = wfGetMainCache();
378 $cache->delete( $this->getCacheKey() );
379 $this->sites = null;
381 wfProfileOut( __METHOD__ );
385 * Clears the list of sites stored in the database.
387 * @see SiteStore::clear()
389 * @return bool Success
391 public function clear() {
392 wfProfileIn( __METHOD__ );
393 $dbw = $this->sitesTable->getWriteDbConnection();
395 $dbw->startAtomic( __METHOD__ );
396 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
397 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
398 $dbw->endAtomic( __METHOD__ );
400 $this->reset();
402 wfProfileOut( __METHOD__ );
403 return $ok;
407 * @since 1.21
409 * @return ORMTable
411 protected function newSitesTable() {
412 return new ORMTable(
413 'sites',
414 array(
415 'id' => 'id',
417 // Site data
418 'global_key' => 'str',
419 'type' => 'str',
420 'group' => 'str',
421 'source' => 'str',
422 'language' => 'str',
423 'protocol' => 'str',
424 'domain' => 'str',
425 'data' => 'array',
427 // Site config
428 'forward' => 'bool',
429 'config' => 'array',
431 array(
432 'type' => Site::TYPE_UNKNOWN,
433 'group' => Site::GROUP_NONE,
434 'source' => Site::SOURCE_LOCAL,
435 'data' => array(),
437 'forward' => false,
438 'config' => array(),
439 'language' => '',
441 'ORMRow',
442 'site_'
449 * @deprecated since 1.21
451 class Sites extends SiteSQLStore {
454 * Factory for creating new site objects.
456 * @since 1.21
457 * @deprecated since 1.21
459 * @param string|bool $globalId
461 * @return Site
463 public static function newSite( $globalId = false ) {
464 $site = new Site();
466 if ( $globalId !== false ) {
467 $site->setGlobalId( $globalId );
470 return $site;
474 * @deprecated since 1.21
475 * @return SiteStore
477 public static function singleton() {
478 static $singleton;
480 if ( $singleton === null ) {
481 $singleton = new static();
484 return $singleton;
488 * @deprecated since 1.21
489 * @param string $group
490 * @return SiteList
492 public function getSiteGroup( $group ) {
493 return $this->getSites()->getGroup( $group );