Merge "UnregisteredLocalFile.php: Override File::getBitDepth() stub"
[lhc/web/wiklou.git] / includes / site / SiteSQLStore.php
1 <?php
2
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.
7 *
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.
12 *
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.
17 *
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
22 *
23 * @since 1.21
24 *
25 * @file
26 * @ingroup Site
27 *
28 * @license GNU GPL v2+
29 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
30 */
31 class SiteSQLStore implements SiteStore {
32 /**
33 * @since 1.21
34 *
35 * @var SiteList|null
36 */
37 protected $sites = null;
38
39 /**
40 * @var ORMTable
41 */
42 protected $sitesTable;
43
44 /**
45 * @var string|null
46 */
47 private $cacheKey = null;
48
49 /**
50 * @var int
51 */
52 private $cacheTimeout = 3600;
53
54 /**
55 * @var BagOStuff
56 */
57 private $cache;
58
59 /**
60 * @since 1.21
61 *
62 * @param ORMTable|null $sitesTable
63 * @param BagOStuff|null $cache
64 *
65 * @return SiteStore
66 */
67 public static function newInstance( ORMTable $sitesTable = null, BagOStuff $cache = null ) {
68 if ( $cache === null ) {
69 $cache = wfGetMainCache();
70 }
71
72 return new static( $cache, $sitesTable );
73 }
74
75 /**
76 * Constructor.
77 *
78 * @since 1.21
79 *
80 * @param BagOStuff $cache
81 * @param ORMTable|null $sitesTable
82 */
83 protected function __construct( BagOStuff $cache, ORMTable $sitesTable = null ) {
84 if ( $sitesTable === null ) {
85 $sitesTable = $this->newSitesTable();
86 }
87
88 $this->cache = $cache;
89 $this->sitesTable = $sitesTable;
90 }
91
92 /**
93 * Constructs a cache key to use for caching the list of sites.
94 *
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.
98 *
99 * The cache key also includes information about where the sites were loaded from, e.g.
100 * the name of a database table.
101 *
102 * @see SiteList::getSerialVersionId
103 *
104 * @return string The cache key.
105 */
106 protected function getCacheKey() {
107
108 if ( $this->cacheKey === null ) {
109 $type = 'SiteList#' . SiteList::getSerialVersionId();
110 $source = $this->sitesTable->getName();
111
112 if ( $this->sitesTable->getTargetWiki() !== false ) {
113 $source = $this->sitesTable->getTargetWiki() . '.' . $source;
114 }
115
116 $this->cacheKey = wfMemcKey( "$source/$type" );
117 }
118
119 return $this->cacheKey;
120 }
121
122 /**
123 * @see SiteStore::getSites
124 *
125 * @since 1.21
126 *
127 * @param string $source Either 'cache' or 'recache'
128 *
129 * @return SiteList
130 */
131 public function getSites( $source = 'cache' ) {
132
133 if ( $source === 'cache' ) {
134 if ( $this->sites === null ) {
135 $sites = $this->cache->get( $this->getCacheKey() );
136
137 if ( is_object( $sites ) ) {
138 $this->sites = $sites;
139 } else {
140 $this->loadSites();
141 }
142 }
143 }
144 else {
145 $this->loadSites();
146 }
147
148 return $this->sites;
149 }
150
151 /**
152 * Returns a new Site object constructed from the provided ORMRow.
153 *
154 * @since 1.21
155 *
156 * @param ORMRow $siteRow
157 *
158 * @return Site
159 */
160 protected function siteFromRow( ORMRow $siteRow ) {
161
162 $site = Site::newForType( $siteRow->getField( 'type', Site::TYPE_UNKNOWN ) );
163
164 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
165
166 $site->setInternalId( $siteRow->getField( 'id' ) );
167
168 if ( $siteRow->hasField( 'forward' ) ) {
169 $site->setForward( $siteRow->getField( 'forward' ) );
170 }
171
172 if ( $siteRow->hasField( 'group' ) ) {
173 $site->setGroup( $siteRow->getField( 'group' ) );
174 }
175
176 if ( $siteRow->hasField( 'language' ) ) {
177 $site->setLanguageCode( $siteRow->getField( 'language' ) === ''
178 ? null
179 : $siteRow->getField( 'language' )
180 );
181 }
182
183 if ( $siteRow->hasField( 'source' ) ) {
184 $site->setSource( $siteRow->getField( 'source' ) );
185 }
186
187 if ( $siteRow->hasField( 'data' ) ) {
188 $site->setExtraData( $siteRow->getField( 'data' ) );
189 }
190
191 if ( $siteRow->hasField( 'config' ) ) {
192 $site->setExtraConfig( $siteRow->getField( 'config' ) );
193 }
194
195 return $site;
196 }
197
198 /**
199 * Get a new ORMRow from a Site object
200 *
201 * @since 1.22
202 *
203 * @param Site $site
204 *
205 * @return ORMRow
206 */
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(),
218
219 // Site config
220 'forward' => $site->shouldForward(),
221 'config' => $site->getExtraConfig(),
222 );
223
224 if ( $site->getInternalId() !== null ) {
225 $fields['id'] = $site->getInternalId();
226 }
227
228 return new ORMRow( $this->sitesTable, $fields );
229 }
230
231 /**
232 * Fetches the site from the database and loads them into the sites field.
233 *
234 * @since 1.21
235 */
236 protected function loadSites() {
237
238 $this->sites = new SiteList();
239
240 foreach ( $this->sitesTable->select() as $siteRow ) {
241 $this->sites[] = $this->siteFromRow( $siteRow );
242 }
243
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',
251 ),
252 array(),
253 __METHOD__
254 );
255
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 );
261 }
262 }
263
264 $this->cache->set( $this->getCacheKey(), $this->sites, $this->cacheTimeout );
265
266 }
267
268 /**
269 * @see SiteStore::getSite
270 *
271 * @since 1.21
272 *
273 * @param string $globalId
274 * @param string $source
275 *
276 * @return Site|null
277 */
278 public function getSite( $globalId, $source = 'cache' ) {
279
280 $sites = $this->getSites( $source );
281
282 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
283 }
284
285 /**
286 * @see SiteStore::saveSite
287 *
288 * @since 1.21
289 *
290 * @param Site $site
291 *
292 * @return bool Success indicator
293 */
294 public function saveSite( Site $site ) {
295 return $this->saveSites( array( $site ) );
296 }
297
298 /**
299 * @see SiteStore::saveSites
300 *
301 * @since 1.21
302 *
303 * @param Site[] $sites
304 *
305 * @return bool Success indicator
306 */
307 public function saveSites( array $sites ) {
308
309 if ( empty( $sites ) ) {
310 return true;
311 }
312
313 $dbw = $this->sitesTable->getWriteDbConnection();
314
315 $dbw->startAtomic( __METHOD__ );
316
317 $success = true;
318
319 $internalIds = array();
320 $localIds = array();
321
322 foreach ( $sites as $site ) {
323 if ( $site->getInternalId() !== null ) {
324 $internalIds[] = $site->getInternalId();
325 }
326
327 $siteRow = $this->getRowFromSite( $site );
328 $success = $siteRow->save( __METHOD__ ) && $success;
329
330 foreach ( $site->getLocalIds() as $idType => $ids ) {
331 foreach ( $ids as $id ) {
332 $localIds[] = array( $siteRow->getId(), $idType, $id );
333 }
334 }
335 }
336
337 if ( $internalIds !== array() ) {
338 $dbw->delete(
339 'site_identifiers',
340 array( 'si_site' => $internalIds ),
341 __METHOD__
342 );
343 }
344
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],
352 ),
353 __METHOD__
354 );
355 }
356
357 $dbw->endAtomic( __METHOD__ );
358
359 // purge cache
360 $this->reset();
361
362 return $success;
363 }
364
365 /**
366 * Purges the internal and external cache of the site list, forcing the list
367 * of sites to be re-read from the database.
368 *
369 * @since 1.21
370 */
371 public function reset() {
372 // purge cache
373 $this->cache->delete( $this->getCacheKey() );
374 $this->sites = null;
375
376 }
377
378 /**
379 * Clears the list of sites stored in the database.
380 *
381 * @see SiteStore::clear()
382 *
383 * @return bool Success
384 */
385 public function clear() {
386 $dbw = $this->sitesTable->getWriteDbConnection();
387
388 $dbw->startAtomic( __METHOD__ );
389 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
390 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
391 $dbw->endAtomic( __METHOD__ );
392
393 $this->reset();
394
395 return $ok;
396 }
397
398 /**
399 * @since 1.21
400 *
401 * @return ORMTable
402 */
403 protected function newSitesTable() {
404 return new ORMTable(
405 'sites',
406 array(
407 'id' => 'id',
408
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',
418
419 // Site config
420 'forward' => 'bool',
421 'config' => 'array',
422 ),
423 array(
424 'type' => Site::TYPE_UNKNOWN,
425 'group' => Site::GROUP_NONE,
426 'source' => Site::SOURCE_LOCAL,
427 'data' => array(),
428
429 'forward' => false,
430 'config' => array(),
431 'language' => '',
432 ),
433 'ORMRow',
434 'site_'
435 );
436 }
437
438 }