Merge "If a user logs in while not on https, then the user should be sent back to...
[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 wfProfileIn( __METHOD__ );
108
109 if ( $this->cacheKey === null ) {
110 $type = 'SiteList#' . SiteList::getSerialVersionId();
111 $source = $this->sitesTable->getName();
112
113 if ( $this->sitesTable->getTargetWiki() !== false ) {
114 $source = $this->sitesTable->getTargetWiki() . '.' . $source;
115 }
116
117 $this->cacheKey = wfMemcKey( "$source/$type" );
118 }
119
120 wfProfileOut( __METHOD__ );
121 return $this->cacheKey;
122 }
123
124 /**
125 * @see SiteStore::getSites
126 *
127 * @since 1.21
128 *
129 * @param string $source Either 'cache' or 'recache'
130 *
131 * @return SiteList
132 */
133 public function getSites( $source = 'cache' ) {
134 wfProfileIn( __METHOD__ );
135
136 if ( $source === 'cache' ) {
137 if ( $this->sites === null ) {
138 $sites = $this->cache->get( $this->getCacheKey() );
139
140 if ( is_object( $sites ) ) {
141 $this->sites = $sites;
142 } else {
143 $this->loadSites();
144 }
145 }
146 }
147 else {
148 $this->loadSites();
149 }
150
151 wfProfileOut( __METHOD__ );
152 return $this->sites;
153 }
154
155 /**
156 * Returns a new Site object constructed from the provided ORMRow.
157 *
158 * @since 1.21
159 *
160 * @param ORMRow $siteRow
161 *
162 * @return Site
163 */
164 protected function siteFromRow( ORMRow $siteRow ) {
165 wfProfileIn( __METHOD__ );
166
167 $site = Site::newForType( $siteRow->getField( 'type', Site::TYPE_UNKNOWN ) );
168
169 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
170
171 $site->setInternalId( $siteRow->getField( 'id' ) );
172
173 if ( $siteRow->hasField( 'forward' ) ) {
174 $site->setForward( $siteRow->getField( 'forward' ) );
175 }
176
177 if ( $siteRow->hasField( 'group' ) ) {
178 $site->setGroup( $siteRow->getField( 'group' ) );
179 }
180
181 if ( $siteRow->hasField( 'language' ) ) {
182 $site->setLanguageCode( $siteRow->getField( 'language' ) === ''
183 ? null
184 : $siteRow->getField( 'language' )
185 );
186 }
187
188 if ( $siteRow->hasField( 'source' ) ) {
189 $site->setSource( $siteRow->getField( 'source' ) );
190 }
191
192 if ( $siteRow->hasField( 'data' ) ) {
193 $site->setExtraData( $siteRow->getField( 'data' ) );
194 }
195
196 if ( $siteRow->hasField( 'config' ) ) {
197 $site->setExtraConfig( $siteRow->getField( 'config' ) );
198 }
199
200 wfProfileOut( __METHOD__ );
201 return $site;
202 }
203
204 /**
205 * Get a new ORMRow from a Site object
206 *
207 * @since 1.22
208 *
209 * @param Site $site
210 *
211 * @return ORMRow
212 */
213 protected function getRowFromSite( Site $site ) {
214 $fields = array(
215 // Site data
216 'global_key' => $site->getGlobalId(), // TODO: check not null
217 'type' => $site->getType(),
218 'group' => $site->getGroup(),
219 'source' => $site->getSource(),
220 'language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
221 'protocol' => $site->getProtocol(),
222 'domain' => strrev( $site->getDomain() ) . '.',
223 'data' => $site->getExtraData(),
224
225 // Site config
226 'forward' => $site->shouldForward(),
227 'config' => $site->getExtraConfig(),
228 );
229
230 if ( $site->getInternalId() !== null ) {
231 $fields['id'] = $site->getInternalId();
232 }
233
234 return new ORMRow( $this->sitesTable, $fields );
235 }
236
237 /**
238 * Fetches the site from the database and loads them into the sites field.
239 *
240 * @since 1.21
241 */
242 protected function loadSites() {
243 wfProfileIn( __METHOD__ );
244
245 $this->sites = new SiteList();
246
247 foreach ( $this->sitesTable->select() as $siteRow ) {
248 $this->sites[] = $this->siteFromRow( $siteRow );
249 }
250
251 // Batch load the local site identifiers.
252 $ids = wfGetDB( $this->sitesTable->getReadDb() )->select(
253 'site_identifiers',
254 array(
255 'si_site',
256 'si_type',
257 'si_key',
258 ),
259 array(),
260 __METHOD__
261 );
262
263 foreach ( $ids as $id ) {
264 if ( $this->sites->hasInternalId( $id->si_site ) ) {
265 $site = $this->sites->getSiteByInternalId( $id->si_site );
266 $site->addLocalId( $id->si_type, $id->si_key );
267 $this->sites->setSite( $site );
268 }
269 }
270
271 $this->cache->set( $this->getCacheKey(), $this->sites, $this->cacheTimeout );
272
273 wfProfileOut( __METHOD__ );
274 }
275
276 /**
277 * @see SiteStore::getSite
278 *
279 * @since 1.21
280 *
281 * @param string $globalId
282 * @param string $source
283 *
284 * @return Site|null
285 */
286 public function getSite( $globalId, $source = 'cache' ) {
287 wfProfileIn( __METHOD__ );
288
289 $sites = $this->getSites( $source );
290
291 wfProfileOut( __METHOD__ );
292 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
293 }
294
295 /**
296 * @see SiteStore::saveSite
297 *
298 * @since 1.21
299 *
300 * @param Site $site
301 *
302 * @return bool Success indicator
303 */
304 public function saveSite( Site $site ) {
305 return $this->saveSites( array( $site ) );
306 }
307
308 /**
309 * @see SiteStore::saveSites
310 *
311 * @since 1.21
312 *
313 * @param Site[] $sites
314 *
315 * @return bool Success indicator
316 */
317 public function saveSites( array $sites ) {
318 wfProfileIn( __METHOD__ );
319
320 if ( empty( $sites ) ) {
321 wfProfileOut( __METHOD__ );
322 return true;
323 }
324
325 $dbw = $this->sitesTable->getWriteDbConnection();
326
327 $dbw->startAtomic( __METHOD__ );
328
329 $success = true;
330
331 $internalIds = array();
332 $localIds = array();
333
334 foreach ( $sites as $site ) {
335 if ( $site->getInternalId() !== null ) {
336 $internalIds[] = $site->getInternalId();
337 }
338
339 $siteRow = $this->getRowFromSite( $site );
340 $success = $siteRow->save( __METHOD__ ) && $success;
341
342 foreach ( $site->getLocalIds() as $idType => $ids ) {
343 foreach ( $ids as $id ) {
344 $localIds[] = array( $siteRow->getId(), $idType, $id );
345 }
346 }
347 }
348
349 if ( $internalIds !== array() ) {
350 $dbw->delete(
351 'site_identifiers',
352 array( 'si_site' => $internalIds ),
353 __METHOD__
354 );
355 }
356
357 foreach ( $localIds as $localId ) {
358 $dbw->insert(
359 'site_identifiers',
360 array(
361 'si_site' => $localId[0],
362 'si_type' => $localId[1],
363 'si_key' => $localId[2],
364 ),
365 __METHOD__
366 );
367 }
368
369 $dbw->endAtomic( __METHOD__ );
370
371 // purge cache
372 $this->reset();
373
374 wfProfileOut( __METHOD__ );
375 return $success;
376 }
377
378 /**
379 * Purges the internal and external cache of the site list, forcing the list
380 * of sites to be re-read from the database.
381 *
382 * @since 1.21
383 */
384 public function reset() {
385 wfProfileIn( __METHOD__ );
386 // purge cache
387 $this->cache->delete( $this->getCacheKey() );
388 $this->sites = null;
389
390 wfProfileOut( __METHOD__ );
391 }
392
393 /**
394 * Clears the list of sites stored in the database.
395 *
396 * @see SiteStore::clear()
397 *
398 * @return bool Success
399 */
400 public function clear() {
401 wfProfileIn( __METHOD__ );
402 $dbw = $this->sitesTable->getWriteDbConnection();
403
404 $dbw->startAtomic( __METHOD__ );
405 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
406 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
407 $dbw->endAtomic( __METHOD__ );
408
409 $this->reset();
410
411 wfProfileOut( __METHOD__ );
412 return $ok;
413 }
414
415 /**
416 * @since 1.21
417 *
418 * @return ORMTable
419 */
420 protected function newSitesTable() {
421 return new ORMTable(
422 'sites',
423 array(
424 'id' => 'id',
425
426 // Site data
427 'global_key' => 'str',
428 'type' => 'str',
429 'group' => 'str',
430 'source' => 'str',
431 'language' => 'str',
432 'protocol' => 'str',
433 'domain' => 'str',
434 'data' => 'array',
435
436 // Site config
437 'forward' => 'bool',
438 'config' => 'array',
439 ),
440 array(
441 'type' => Site::TYPE_UNKNOWN,
442 'group' => Site::GROUP_NONE,
443 'source' => Site::SOURCE_LOCAL,
444 'data' => array(),
445
446 'forward' => false,
447 'config' => array(),
448 'language' => '',
449 ),
450 'ORMRow',
451 'site_'
452 );
453 }
454
455 }