Fix cache key used by SiteSQLStore.
[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 if ( $siteRow->hasField( 'forward' ) ) {
149 $site->setForward( $siteRow->getField( 'forward' ) );
150 }
151
152 if ( $siteRow->hasField( 'group' ) ) {
153 $site->setGroup( $siteRow->getField( 'group' ) );
154 }
155
156 if ( $siteRow->hasField( 'language' ) ) {
157 $site->setLanguageCode( $siteRow->getField( 'language' ) === '' ? null : $siteRow->getField( 'language' ) );
158 }
159
160 if ( $siteRow->hasField( 'source' ) ) {
161 $site->setSource( $siteRow->getField( 'source' ) );
162 }
163
164 if ( $siteRow->hasField( 'data' ) ) {
165 $site->setExtraData( $siteRow->getField( 'data' ) );
166 }
167
168 if ( $siteRow->hasField( 'config' ) ) {
169 $site->setExtraConfig( $siteRow->getField( 'config' ) );
170 }
171
172 return $site;
173 }
174
175 /**
176 * Fetches the site from the database and loads them into the sites field.
177 *
178 * @since 1.21
179 */
180 protected function loadSites() {
181 $this->sites = new SiteList();
182
183 foreach ( $this->sitesTable->select() as $siteRow ) {
184 $this->sites[] = $this->siteFromRow( $siteRow );
185 }
186
187 // Batch load the local site identifiers.
188 $ids = wfGetDB( $this->sitesTable->getReadDb() )->select(
189 'site_identifiers',
190 array(
191 'si_site',
192 'si_type',
193 'si_key',
194 ),
195 array(),
196 __METHOD__
197 );
198
199 foreach ( $ids as $id ) {
200 if ( $this->sites->hasInternalId( $id->si_site ) ) {
201 $site = $this->sites->getSiteByInternalId( $id->si_site );
202 $site->addLocalId( $id->si_type, $id->si_key );
203 $this->sites->setSite( $site );
204 }
205 }
206
207 $cache = wfGetMainCache();
208 $cache->set( $this->getCacheKey(), $this->sites );
209 }
210
211 /**
212 * @see SiteStore::getSite
213 *
214 * @since 1.21
215 *
216 * @param string $globalId
217 * @param string $source
218 *
219 * @return Site|null
220 */
221 public function getSite( $globalId, $source = 'cache' ) {
222 $sites = $this->getSites( $source );
223
224 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
225 }
226
227 /**
228 * @see SiteStore::saveSite
229 *
230 * @since 1.21
231 *
232 * @param Site $site
233 *
234 * @return boolean Success indicator
235 */
236 public function saveSite( Site $site ) {
237 return $this->saveSites( array( $site ) );
238 }
239
240 /**
241 * @see SiteStore::saveSites
242 *
243 * @since 1.21
244 *
245 * @param Site[] $sites
246 *
247 * @return boolean Success indicator
248 */
249 public function saveSites( array $sites ) {
250 if ( empty( $sites ) ) {
251 return true;
252 }
253
254 $dbw = $this->sitesTable->getWriteDbConnection();
255
256 $trx = $dbw->trxLevel();
257
258 if ( $trx == 0 ) {
259 $dbw->begin( __METHOD__ );
260 }
261
262 $success = true;
263
264 $internalIds = array();
265 $localIds = array();
266
267 foreach ( $sites as $site ) {
268 $fields = array(
269 // Site data
270 'global_key' => $site->getGlobalId(), // TODO: check not null
271 'type' => $site->getType(),
272 'group' => $site->getGroup(),
273 'source' => $site->getSource(),
274 'language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
275 'protocol' => $site->getProtocol(),
276 'domain' => strrev( $site->getDomain() ) . '.',
277 'data' => $site->getExtraData(),
278
279 // Site config
280 'forward' => $site->shouldForward(),
281 'config' => $site->getExtraConfig(),
282 );
283
284 if ( $site->getInternalId() !== null ) {
285 $fields['id'] = $site->getInternalId();
286 $internalIds[] = $site->getInternalId();
287 }
288
289 $siteRow = new ORMRow( $this->sitesTable, $fields );
290 $success = $siteRow->save( __METHOD__ ) && $success;
291
292 foreach ( $site->getLocalIds() as $idType => $ids ) {
293 foreach ( $ids as $id ) {
294 $localIds[] = array( $siteRow->getId(), $idType, $id );
295 }
296 }
297 }
298
299 if ( $internalIds !== array() ) {
300 $dbw->delete(
301 'site_identifiers',
302 array( 'si_site' => $internalIds ),
303 __METHOD__
304 );
305 }
306
307 foreach ( $localIds as $localId ) {
308 $dbw->insert(
309 'site_identifiers',
310 array(
311 'si_site' => $localId[0],
312 'si_type' => $localId[1],
313 'si_key' => $localId[2],
314 ),
315 __METHOD__
316 );
317 }
318
319 if ( $trx == 0 ) {
320 $dbw->commit( __METHOD__ );
321 }
322
323 return $success;
324 }
325
326 /**
327 * @since 1.21
328 *
329 * @return ORMTable
330 */
331 protected function newSitesTable() {
332 return new ORMTable(
333 'sites',
334 array(
335 'id' => 'id',
336
337 // Site data
338 'global_key' => 'str',
339 'type' => 'str',
340 'group' => 'str',
341 'source' => 'str',
342 'language' => 'str',
343 'protocol' => 'str',
344 'domain' => 'str',
345 'data' => 'array',
346
347 // Site config
348 'forward' => 'bool',
349 'config' => 'array',
350 ),
351 array(
352 'type' => Site::TYPE_UNKNOWN,
353 'group' => Site::GROUP_NONE,
354 'source' => Site::SOURCE_LOCAL,
355 'data' => array(),
356
357 'forward' => false,
358 'config' => array(),
359 'language' => '',
360 ),
361 'ORMRow',
362 'site_'
363 );
364 }
365
366 }
367
368 /**
369 * @deprecated
370 */
371 class Sites extends SiteSQLStore {
372
373 /**
374 * Factory for creating new site objects.
375 *
376 * @since 1.21
377 * @deprecated
378 *
379 * @param string|boolean false $globalId
380 *
381 * @return Site
382 */
383 public static function newSite( $globalId = false ) {
384 $site = new Site();
385
386 if ( $globalId !== false ) {
387 $site->setGlobalId( $globalId );
388 }
389
390 return $site;
391 }
392
393 /**
394 * @deprecated
395 * @return SiteStore
396 */
397 public static function singleton() {
398 return new static();
399 }
400
401 /**
402 * @deprecated
403 * @return SiteList
404 */
405 public function getSiteGroup( $group ) {
406 return $this->getSites()->getGroup( $group );
407 }
408
409 }