Merge "When aborting EnhancedRC block line, block should reflect that"
[lhc/web/wiklou.git] / includes / site / DBSiteStore.php
1 <?php
2
3 /**
4 * Represents the site configuration of a wiki.
5 * Holds a list of sites (ie SiteList), stored in the database.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @since 1.25
23 *
24 * @file
25 * @ingroup Site
26 *
27 * @license GNU GPL v2+
28 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 */
30 class DBSiteStore implements SiteStore {
31
32 /**
33 * @var SiteList|null
34 */
35 protected $sites = null;
36
37 /**
38 * @var ORMTable
39 */
40 protected $sitesTable;
41
42 /**
43 * @since 1.25
44 *
45 * @param ORMTable|null $sitesTable
46 */
47 public function __construct( ORMTable $sitesTable = null ) {
48 if ( $sitesTable === null ) {
49 $sitesTable = $this->newSitesTable();
50 }
51
52 $this->sitesTable = $sitesTable;
53 }
54
55 /**
56 * @see SiteStore::getSites
57 *
58 * @since 1.25
59 *
60 * @return SiteList
61 */
62 public function getSites() {
63 $this->loadSites();
64
65 return $this->sites;
66 }
67
68 /**
69 * Returns a new Site object constructed from the provided ORMRow.
70 *
71 * @since 1.25
72 *
73 * @param ORMRow $siteRow
74 *
75 * @return Site
76 */
77 protected function siteFromRow( ORMRow $siteRow ) {
78
79 $site = Site::newForType( $siteRow->getField( 'type', Site::TYPE_UNKNOWN ) );
80
81 $site->setGlobalId( $siteRow->getField( 'global_key' ) );
82
83 $site->setInternalId( $siteRow->getField( 'id' ) );
84
85 if ( $siteRow->hasField( 'forward' ) ) {
86 $site->setForward( $siteRow->getField( 'forward' ) );
87 }
88
89 if ( $siteRow->hasField( 'group' ) ) {
90 $site->setGroup( $siteRow->getField( 'group' ) );
91 }
92
93 if ( $siteRow->hasField( 'language' ) ) {
94 $site->setLanguageCode( $siteRow->getField( 'language' ) === ''
95 ? null
96 : $siteRow->getField( 'language' )
97 );
98 }
99
100 if ( $siteRow->hasField( 'source' ) ) {
101 $site->setSource( $siteRow->getField( 'source' ) );
102 }
103
104 if ( $siteRow->hasField( 'data' ) ) {
105 $site->setExtraData( $siteRow->getField( 'data' ) );
106 }
107
108 if ( $siteRow->hasField( 'config' ) ) {
109 $site->setExtraConfig( $siteRow->getField( 'config' ) );
110 }
111
112 return $site;
113 }
114
115 /**
116 * Get a new ORMRow from a Site object
117 *
118 * @since 1.25
119 *
120 * @param Site $site
121 *
122 * @return ORMRow
123 */
124 protected function getRowFromSite( Site $site ) {
125 $fields = array(
126 // Site data
127 'global_key' => $site->getGlobalId(), // TODO: check not null
128 'type' => $site->getType(),
129 'group' => $site->getGroup(),
130 'source' => $site->getSource(),
131 'language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
132 'protocol' => $site->getProtocol(),
133 'domain' => strrev( $site->getDomain() ) . '.',
134 'data' => $site->getExtraData(),
135
136 // Site config
137 'forward' => $site->shouldForward(),
138 'config' => $site->getExtraConfig(),
139 );
140
141 if ( $site->getInternalId() !== null ) {
142 $fields['id'] = $site->getInternalId();
143 }
144
145 return new ORMRow( $this->sitesTable, $fields );
146 }
147
148 /**
149 * Fetches the site from the database and loads them into the sites field.
150 *
151 * @since 1.25
152 */
153 protected function loadSites() {
154 $this->sites = new SiteList();
155
156 $siteRows = $this->sitesTable->select( null, array(), array(
157 'ORDER BY' => 'site_global_key'
158 ) );
159
160 foreach ( $siteRows as $siteRow ) {
161 $this->sites[] = $this->siteFromRow( $siteRow );
162 }
163
164 // Batch load the local site identifiers.
165 $ids = wfGetDB( $this->sitesTable->getReadDb() )->select(
166 'site_identifiers',
167 array(
168 'si_site',
169 'si_type',
170 'si_key',
171 ),
172 array(),
173 __METHOD__
174 );
175
176 foreach ( $ids as $id ) {
177 if ( $this->sites->hasInternalId( $id->si_site ) ) {
178 $site = $this->sites->getSiteByInternalId( $id->si_site );
179 $site->addLocalId( $id->si_type, $id->si_key );
180 $this->sites->setSite( $site );
181 }
182 }
183 }
184
185 /**
186 * @see SiteStore::getSite
187 *
188 * @since 1.25
189 *
190 * @param string $globalId
191 *
192 * @return Site|null
193 */
194 public function getSite( $globalId ) {
195 if ( $this->sites === null ) {
196 $this->sites = $this->getSites();
197 }
198
199 return $this->sites->hasSite( $globalId ) ? $this->sites->getSite( $globalId ) : null;
200 }
201
202 /**
203 * @see SiteStore::saveSite
204 *
205 * @since 1.25
206 *
207 * @param Site $site
208 *
209 * @return bool Success indicator
210 */
211 public function saveSite( Site $site ) {
212 return $this->saveSites( array( $site ) );
213 }
214
215 /**
216 * @see SiteStore::saveSites
217 *
218 * @since 1.25
219 *
220 * @param Site[] $sites
221 *
222 * @return bool Success indicator
223 */
224 public function saveSites( array $sites ) {
225 if ( empty( $sites ) ) {
226 return true;
227 }
228
229 $dbw = $this->sitesTable->getWriteDbConnection();
230
231 $dbw->startAtomic( __METHOD__ );
232
233 $success = true;
234
235 $internalIds = array();
236 $localIds = array();
237
238 foreach ( $sites as $site ) {
239 if ( $site->getInternalId() !== null ) {
240 $internalIds[] = $site->getInternalId();
241 }
242
243 $siteRow = $this->getRowFromSite( $site );
244 $success = $siteRow->save( __METHOD__ ) && $success;
245
246 foreach ( $site->getLocalIds() as $idType => $ids ) {
247 foreach ( $ids as $id ) {
248 $localIds[] = array( $siteRow->getId(), $idType, $id );
249 }
250 }
251 }
252
253 if ( $internalIds !== array() ) {
254 $dbw->delete(
255 'site_identifiers',
256 array( 'si_site' => $internalIds ),
257 __METHOD__
258 );
259 }
260
261 foreach ( $localIds as $localId ) {
262 $dbw->insert(
263 'site_identifiers',
264 array(
265 'si_site' => $localId[0],
266 'si_type' => $localId[1],
267 'si_key' => $localId[2],
268 ),
269 __METHOD__
270 );
271 }
272
273 $dbw->endAtomic( __METHOD__ );
274
275 $this->reset();
276
277 return $success;
278 }
279
280 /**
281 * Resets the SiteList
282 *
283 * @since 1.25
284 */
285 public function reset() {
286 $this->sites = null;
287 }
288
289 /**
290 * Clears the list of sites stored in the database.
291 *
292 * @see SiteStore::clear()
293 *
294 * @return bool Success
295 */
296 public function clear() {
297 $dbw = $this->sitesTable->getWriteDbConnection();
298
299 $dbw->startAtomic( __METHOD__ );
300 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
301 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
302 $dbw->endAtomic( __METHOD__ );
303
304 $this->reset();
305
306 return $ok;
307 }
308
309 /**
310 * @since 1.25
311 *
312 * @return ORMTable
313 */
314 protected function newSitesTable() {
315 return new ORMTable(
316 'sites',
317 array(
318 'id' => 'id',
319
320 // Site data
321 'global_key' => 'str',
322 'type' => 'str',
323 'group' => 'str',
324 'source' => 'str',
325 'language' => 'str',
326 'protocol' => 'str',
327 'domain' => 'str',
328 'data' => 'array',
329
330 // Site config
331 'forward' => 'bool',
332 'config' => 'array',
333 ),
334 array(
335 'type' => Site::TYPE_UNKNOWN,
336 'group' => Site::GROUP_NONE,
337 'source' => Site::SOURCE_LOCAL,
338 'data' => array(),
339
340 'forward' => false,
341 'config' => array(),
342 'language' => '',
343 ),
344 'ORMRow',
345 'site_'
346 );
347 }
348
349 }