Merge "Added --all option and other features to purgeList.php"
[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 /**
34 * @since 1.21
35 *
36 * @var SiteList|null
37 */
38 protected $sites = null;
39
40 /**
41 * @var ORMTable
42 */
43 protected $sitesTable;
44
45 /**
46 * @var string|null
47 */
48 private $cacheKey = null;
49
50 /**
51 * @var int
52 */
53 private $cacheTimeout = 3600;
54
55 /**
56 * @since 1.21
57 *
58 * @param ORMTable|null $sitesTable
59 *
60 * @return SiteStore
61 */
62 public static function newInstance( ORMTable $sitesTable = null ) {
63 return new static( $sitesTable );
64 }
65
66 /**
67 * Constructor.
68 *
69 * @since 1.21
70 *
71 * @param ORMTable|null $sitesTable
72 */
73 protected function __construct( ORMTable $sitesTable = null ) {
74 if ( $sitesTable === null ) {
75 $sitesTable = $this->newSitesTable();
76 }
77
78 $this->sitesTable = $sitesTable;
79 }
80
81 /**
82 * Constructs a cache key to use for caching the list of sites.
83 *
84 * This includes the concrete class name of the site list as well as a version identifier
85 * for the list's serialization, to avoid problems when unserializing site lists serialized
86 * by an older version, e.g. when reading from a cache.
87 *
88 * The cache key also includes information about where the sites were loaded from, e.g.
89 * the name of a database table.
90 *
91 * @see SiteList::getSerialVersionId
92 *
93 * @return String The cache key.
94 */
95 protected function getCacheKey() {
96 wfProfileIn( __METHOD__ );
97
98 if ( $this->cacheKey === null ) {
99 $type = 'SiteList#' . SiteList::getSerialVersionId();
100 $source = $this->sitesTable->getName();
101
102 if ( $this->sitesTable->getTargetWiki() !== false ) {
103 $source = $this->sitesTable->getTargetWiki() . '.' . $source;
104 }
105
106 $this->cacheKey = wfMemcKey( "$source/$type" );
107 }
108
109 wfProfileOut( __METHOD__ );
110 return $this->cacheKey;
111 }
112
113 /**
114 * @see SiteStore::getSites
115 *
116 * @since 1.21
117 *
118 * @param string $source either 'cache' or 'recache'
119 *
120 * @return SiteList
121 */
122 public function getSites( $source = 'cache' ) {
123 wfProfileIn( __METHOD__ );
124
125 if ( $source === 'cache' ) {
126 if ( $this->sites === null ) {
127 $cache = wfGetMainCache();
128 $sites = $cache->get( $this->getCacheKey() );
129
130 if ( is_object( $sites ) ) {
131 $this->sites = $sites;
132 } else {
133 $this->loadSites();
134 }
135 }
136 }
137 else {
138 $this->loadSites();
139 }
140
141 wfProfileOut( __METHOD__ );
142 return $this->sites;
143 }
144
145 /**
146 * Returns a new Site object constructed from the provided ORMRow.
147 *
148 * @since 1.21
149 *
150 * @param ORMRow $siteRow
151 *
152 * @return Site
153 */
154 protected function siteFromRow( ORMRow $siteRow ) {
155 wfProfileIn( __METHOD__ );
156
157 $site = Site::newForType( $siteRow->getField( 'type', Site::TYPE_UNKNOWN ) );
158
159 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
160
161 $site->setInternalId( $siteRow->getField( 'id' ) );
162
163 if ( $siteRow->hasField( 'forward' ) ) {
164 $site->setForward( $siteRow->getField( 'forward' ) );
165 }
166
167 if ( $siteRow->hasField( 'group' ) ) {
168 $site->setGroup( $siteRow->getField( 'group' ) );
169 }
170
171 if ( $siteRow->hasField( 'language' ) ) {
172 $site->setLanguageCode( $siteRow->getField( 'language' ) === '' ? null : $siteRow->getField( 'language' ) );
173 }
174
175 if ( $siteRow->hasField( 'source' ) ) {
176 $site->setSource( $siteRow->getField( 'source' ) );
177 }
178
179 if ( $siteRow->hasField( 'data' ) ) {
180 $site->setExtraData( $siteRow->getField( 'data' ) );
181 }
182
183 if ( $siteRow->hasField( 'config' ) ) {
184 $site->setExtraConfig( $siteRow->getField( 'config' ) );
185 }
186
187 wfProfileOut( __METHOD__ );
188 return $site;
189 }
190
191 /**
192 * Fetches the site from the database and loads them into the sites field.
193 *
194 * @since 1.21
195 */
196 protected function loadSites() {
197 wfProfileIn( __METHOD__ );
198
199 $this->sites = new SiteList();
200
201 foreach ( $this->sitesTable->select() as $siteRow ) {
202 $this->sites[] = $this->siteFromRow( $siteRow );
203 }
204
205 // Batch load the local site identifiers.
206 $ids = wfGetDB( $this->sitesTable->getReadDb() )->select(
207 'site_identifiers',
208 array(
209 'si_site',
210 'si_type',
211 'si_key',
212 ),
213 array(),
214 __METHOD__
215 );
216
217 foreach ( $ids as $id ) {
218 if ( $this->sites->hasInternalId( $id->si_site ) ) {
219 $site = $this->sites->getSiteByInternalId( $id->si_site );
220 $site->addLocalId( $id->si_type, $id->si_key );
221 $this->sites->setSite( $site );
222 }
223 }
224
225 $cache = wfGetMainCache();
226 $cache->set( $this->getCacheKey(), $this->sites, $this->cacheTimeout );
227
228 wfProfileOut( __METHOD__ );
229 }
230
231 /**
232 * @see SiteStore::getSite
233 *
234 * @since 1.21
235 *
236 * @param string $globalId
237 * @param string $source
238 *
239 * @return Site|null
240 */
241 public function getSite( $globalId, $source = 'cache' ) {
242 wfProfileIn( __METHOD__ );
243
244 $sites = $this->getSites( $source );
245
246 wfProfileOut( __METHOD__ );
247 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
248 }
249
250 /**
251 * @see SiteStore::saveSite
252 *
253 * @since 1.21
254 *
255 * @param Site $site
256 *
257 * @return boolean Success indicator
258 */
259 public function saveSite( Site $site ) {
260 return $this->saveSites( array( $site ) );
261 }
262
263 /**
264 * @see SiteStore::saveSites
265 *
266 * @since 1.21
267 *
268 * @param Site[] $sites
269 *
270 * @return boolean Success indicator
271 */
272 public function saveSites( array $sites ) {
273 wfProfileIn( __METHOD__ );
274
275 if ( empty( $sites ) ) {
276 wfProfileOut( __METHOD__ );
277 return true;
278 }
279
280 $dbw = $this->sitesTable->getWriteDbConnection();
281
282 $trx = $dbw->trxLevel();
283
284 if ( $trx == 0 ) {
285 $dbw->begin( __METHOD__ );
286 }
287
288 $success = true;
289
290 $internalIds = array();
291 $localIds = array();
292
293 foreach ( $sites as $site ) {
294 $fields = array(
295 // Site data
296 'global_key' => $site->getGlobalId(), // TODO: check not null
297 'type' => $site->getType(),
298 'group' => $site->getGroup(),
299 'source' => $site->getSource(),
300 'language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
301 'protocol' => $site->getProtocol(),
302 'domain' => strrev( $site->getDomain() ) . '.',
303 'data' => $site->getExtraData(),
304
305 // Site config
306 'forward' => $site->shouldForward(),
307 'config' => $site->getExtraConfig(),
308 );
309
310 if ( $site->getInternalId() !== null ) {
311 $fields['id'] = $site->getInternalId();
312 $internalIds[] = $site->getInternalId();
313 }
314
315 $siteRow = new ORMRow( $this->sitesTable, $fields );
316 $success = $siteRow->save( __METHOD__ ) && $success;
317
318 foreach ( $site->getLocalIds() as $idType => $ids ) {
319 foreach ( $ids as $id ) {
320 $localIds[] = array( $siteRow->getId(), $idType, $id );
321 }
322 }
323 }
324
325 if ( $internalIds !== array() ) {
326 $dbw->delete(
327 'site_identifiers',
328 array( 'si_site' => $internalIds ),
329 __METHOD__
330 );
331 }
332
333 foreach ( $localIds as $localId ) {
334 $dbw->insert(
335 'site_identifiers',
336 array(
337 'si_site' => $localId[0],
338 'si_type' => $localId[1],
339 'si_key' => $localId[2],
340 ),
341 __METHOD__
342 );
343 }
344
345 if ( $trx == 0 ) {
346 $dbw->commit( __METHOD__ );
347 }
348
349 // purge cache
350 $this->reset();
351
352 wfProfileOut( __METHOD__ );
353 return $success;
354 }
355
356 /**
357 * Purges the internal and external cache of the site list, forcing the list
358 * of sites to be re-read from the database.
359 *
360 * @since 1.21
361 */
362 public function reset() {
363 wfProfileIn( __METHOD__ );
364 // purge cache
365 $cache = wfGetMainCache();
366 $cache->delete( $this->getCacheKey() );
367 $this->sites = null;
368
369 wfProfileOut( __METHOD__ );
370 }
371
372 /**
373 * Clears the list of sites stored in the database.
374 *
375 * @see SiteStore::clear()
376 *
377 * @return bool success
378 */
379 public function clear() {
380 wfProfileIn( __METHOD__ );
381 $dbw = $this->sitesTable->getWriteDbConnection();
382
383 $trx = $dbw->trxLevel();
384
385 if ( $trx == 0 ) {
386 $dbw->begin( __METHOD__ );
387 }
388
389 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
390 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
391
392 if ( $trx == 0 ) {
393 $dbw->commit( __METHOD__ );
394 }
395
396 $this->reset();
397
398 wfProfileOut( __METHOD__ );
399 return $ok;
400 }
401
402 /**
403 * @since 1.21
404 *
405 * @return ORMTable
406 */
407 protected function newSitesTable() {
408 return new ORMTable(
409 'sites',
410 array(
411 'id' => 'id',
412
413 // Site data
414 'global_key' => 'str',
415 'type' => 'str',
416 'group' => 'str',
417 'source' => 'str',
418 'language' => 'str',
419 'protocol' => 'str',
420 'domain' => 'str',
421 'data' => 'array',
422
423 // Site config
424 'forward' => 'bool',
425 'config' => 'array',
426 ),
427 array(
428 'type' => Site::TYPE_UNKNOWN,
429 'group' => Site::GROUP_NONE,
430 'source' => Site::SOURCE_LOCAL,
431 'data' => array(),
432
433 'forward' => false,
434 'config' => array(),
435 'language' => '',
436 ),
437 'ORMRow',
438 'site_'
439 );
440 }
441
442 }
443
444 /**
445 * @deprecated
446 */
447 class Sites extends SiteSQLStore {
448
449 /**
450 * Factory for creating new site objects.
451 *
452 * @since 1.21
453 * @deprecated
454 *
455 * @param string|boolean false $globalId
456 *
457 * @return Site
458 */
459 public static function newSite( $globalId = false ) {
460 $site = new Site();
461
462 if ( $globalId !== false ) {
463 $site->setGlobalId( $globalId );
464 }
465
466 return $site;
467 }
468
469 /**
470 * @deprecated
471 * @return SiteStore
472 */
473 public static function singleton() {
474 static $singleton;
475
476 if ( $singleton === null ) {
477 $singleton = new static();
478 }
479
480 return $singleton;
481 }
482
483 /**
484 * @deprecated
485 * @return SiteList
486 */
487 public function getSiteGroup( $group ) {
488 return $this->getSites()->getGroup( $group );
489 }
490
491 }