Merge "Implemented getAllAcquiredJobs in JobQueueDB"
[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 foreach ( $this->sitesTable->select() as $siteRow ) {
157 $this->sites[] = $this->siteFromRow( $siteRow );
158 }
159
160 // Batch load the local site identifiers.
161 $ids = wfGetDB( $this->sitesTable->getReadDb() )->select(
162 'site_identifiers',
163 array(
164 'si_site',
165 'si_type',
166 'si_key',
167 ),
168 array(),
169 __METHOD__
170 );
171
172 foreach ( $ids as $id ) {
173 if ( $this->sites->hasInternalId( $id->si_site ) ) {
174 $site = $this->sites->getSiteByInternalId( $id->si_site );
175 $site->addLocalId( $id->si_type, $id->si_key );
176 $this->sites->setSite( $site );
177 }
178 }
179 }
180
181 /**
182 * @see SiteStore::getSite
183 *
184 * @since 1.25
185 *
186 * @param string $globalId
187 *
188 * @return Site|null
189 */
190 public function getSite( $globalId ) {
191 if ( $this->sites === null ) {
192 $this->sites = $this->getSites();
193 }
194
195 return $this->sites->hasSite( $globalId ) ? $this->sites->getSite( $globalId ) : null;
196 }
197
198 /**
199 * @see SiteStore::saveSite
200 *
201 * @since 1.25
202 *
203 * @param Site $site
204 *
205 * @return bool Success indicator
206 */
207 public function saveSite( Site $site ) {
208 return $this->saveSites( array( $site ) );
209 }
210
211 /**
212 * @see SiteStore::saveSites
213 *
214 * @since 1.25
215 *
216 * @param Site[] $sites
217 *
218 * @return bool Success indicator
219 */
220 public function saveSites( array $sites ) {
221 if ( empty( $sites ) ) {
222 return true;
223 }
224
225 $dbw = $this->sitesTable->getWriteDbConnection();
226
227 $dbw->startAtomic( __METHOD__ );
228
229 $success = true;
230
231 $internalIds = array();
232 $localIds = array();
233
234 foreach ( $sites as $site ) {
235 if ( $site->getInternalId() !== null ) {
236 $internalIds[] = $site->getInternalId();
237 }
238
239 $siteRow = $this->getRowFromSite( $site );
240 $success = $siteRow->save( __METHOD__ ) && $success;
241
242 foreach ( $site->getLocalIds() as $idType => $ids ) {
243 foreach ( $ids as $id ) {
244 $localIds[] = array( $siteRow->getId(), $idType, $id );
245 }
246 }
247 }
248
249 if ( $internalIds !== array() ) {
250 $dbw->delete(
251 'site_identifiers',
252 array( 'si_site' => $internalIds ),
253 __METHOD__
254 );
255 }
256
257 foreach ( $localIds as $localId ) {
258 $dbw->insert(
259 'site_identifiers',
260 array(
261 'si_site' => $localId[0],
262 'si_type' => $localId[1],
263 'si_key' => $localId[2],
264 ),
265 __METHOD__
266 );
267 }
268
269 $dbw->endAtomic( __METHOD__ );
270
271 $this->reset();
272
273 return $success;
274 }
275
276 /**
277 * Resets the SiteList
278 *
279 * @since 1.25
280 */
281 public function reset() {
282 $this->sites = null;
283 }
284
285 /**
286 * Clears the list of sites stored in the database.
287 *
288 * @see SiteStore::clear()
289 *
290 * @return bool Success
291 */
292 public function clear() {
293 $dbw = $this->sitesTable->getWriteDbConnection();
294
295 $dbw->startAtomic( __METHOD__ );
296 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
297 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
298 $dbw->endAtomic( __METHOD__ );
299
300 $this->reset();
301
302 return $ok;
303 }
304
305 /**
306 * @since 1.25
307 *
308 * @return ORMTable
309 */
310 protected function newSitesTable() {
311 return new ORMTable(
312 'sites',
313 array(
314 'id' => 'id',
315
316 // Site data
317 'global_key' => 'str',
318 'type' => 'str',
319 'group' => 'str',
320 'source' => 'str',
321 'language' => 'str',
322 'protocol' => 'str',
323 'domain' => 'str',
324 'data' => 'array',
325
326 // Site config
327 'forward' => 'bool',
328 'config' => 'array',
329 ),
330 array(
331 'type' => Site::TYPE_UNKNOWN,
332 'group' => Site::GROUP_NONE,
333 'source' => Site::SOURCE_LOCAL,
334 'data' => array(),
335
336 'forward' => false,
337 'config' => array(),
338 'language' => '',
339 ),
340 'ORMRow',
341 'site_'
342 );
343 }
344
345 }