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