Merge "Update comment about enabled extensions"
[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 * @since 1.25
39 * @param null $sitesTable Unused since 1.27
40 */
41 public function __construct( $sitesTable = null ) {
42 if ( $sitesTable !== null ) {
43 throw new InvalidArgumentException(
44 __METHOD__ . ': $sitesTable parameter must be null'
45 );
46 }
47 }
48
49 /**
50 * @see SiteStore::getSites
51 *
52 * @since 1.25
53 *
54 * @return SiteList
55 */
56 public function getSites() {
57 $this->loadSites();
58
59 return $this->sites;
60 }
61
62 /**
63 * Fetches the site from the database and loads them into the sites field.
64 *
65 * @since 1.25
66 */
67 protected function loadSites() {
68 $this->sites = new SiteList();
69
70 $dbr = wfGetDB( DB_SLAVE );
71
72 $res = $dbr->select(
73 'sites',
74 array(
75 'site_id',
76 'site_global_key',
77 'site_type',
78 'site_group',
79 'site_source',
80 'site_language',
81 'site_protocol',
82 'site_domain',
83 'site_data',
84 'site_forward',
85 'site_config',
86 ),
87 '',
88 __METHOD__,
89 array( 'ORDER BY' => 'site_global_key' )
90 );
91
92 foreach ( $res as $row ) {
93 $site = Site::newForType( $row->site_type );
94 $site->setGlobalId( $row->site_global_key );
95 $site->setInternalId( (int)$row->site_id );
96 $site->setForward( (bool)$row->site_forward );
97 $site->setGroup( $row->site_group );
98 $site->setLanguageCode( $row->site_language === ''
99 ? null
100 : $row->site_language
101 );
102 $site->setSource( $row->site_source );
103 $site->setExtraData( unserialize( $row->site_data ) );
104 $site->setExtraConfig( unserialize( $row->site_config ) );
105 $this->sites[] = $site;
106 }
107
108 // Batch load the local site identifiers.
109 $ids = $dbr->select(
110 'site_identifiers',
111 array(
112 'si_site',
113 'si_type',
114 'si_key',
115 ),
116 array(),
117 __METHOD__
118 );
119
120 foreach ( $ids as $id ) {
121 if ( $this->sites->hasInternalId( $id->si_site ) ) {
122 $site = $this->sites->getSiteByInternalId( $id->si_site );
123 $site->addLocalId( $id->si_type, $id->si_key );
124 $this->sites->setSite( $site );
125 }
126 }
127 }
128
129 /**
130 * @see SiteStore::getSite
131 *
132 * @since 1.25
133 *
134 * @param string $globalId
135 *
136 * @return Site|null
137 */
138 public function getSite( $globalId ) {
139 if ( $this->sites === null ) {
140 $this->sites = $this->getSites();
141 }
142
143 return $this->sites->hasSite( $globalId ) ? $this->sites->getSite( $globalId ) : null;
144 }
145
146 /**
147 * @see SiteStore::saveSite
148 *
149 * @since 1.25
150 *
151 * @param Site $site
152 *
153 * @return bool Success indicator
154 */
155 public function saveSite( Site $site ) {
156 return $this->saveSites( array( $site ) );
157 }
158
159 /**
160 * @see SiteStore::saveSites
161 *
162 * @since 1.25
163 *
164 * @param Site[] $sites
165 *
166 * @return bool Success indicator
167 */
168 public function saveSites( array $sites ) {
169 if ( empty( $sites ) ) {
170 return true;
171 }
172
173 $dbw = wfGetDB( DB_MASTER );
174
175 $dbw->startAtomic( __METHOD__ );
176
177 $success = true;
178
179 $internalIds = array();
180 $localIds = array();
181
182 foreach ( $sites as $site ) {
183 if ( $site->getInternalId() !== null ) {
184 $internalIds[] = $site->getInternalId();
185 }
186
187 $fields = array(
188 // Site data
189 'site_global_key' => $site->getGlobalId(), // TODO: check not null
190 'site_type' => $site->getType(),
191 'site_group' => $site->getGroup(),
192 'site_source' => $site->getSource(),
193 'site_language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
194 'site_protocol' => $site->getProtocol(),
195 'site_domain' => strrev( $site->getDomain() ) . '.',
196 'site_data' => serialize( $site->getExtraData() ),
197
198 // Site config
199 'site_forward' => $site->shouldForward() ? 1 : 0,
200 'site_config' => serialize( $site->getExtraConfig() ),
201 );
202
203 $rowId = $site->getInternalId();
204 if ( $rowId !== null ) {
205 $success = $dbw->update(
206 'sites', $fields, array( 'site_id' => $rowId ), __METHOD__
207 ) && $success;
208 } else {
209 $rowId = $dbw->nextSequenceValue( 'sites_site_id_seq' );
210 $fields['site_id'] = $rowId;
211 $success = $dbw->insert( 'sites', $fields, __METHOD__ ) && $success;
212 $rowId = $dbw->insertId();
213 }
214
215 foreach ( $site->getLocalIds() as $idType => $ids ) {
216 foreach ( $ids as $id ) {
217 $localIds[] = array( $rowId, $idType, $id );
218 }
219 }
220 }
221
222 if ( $internalIds !== array() ) {
223 $dbw->delete(
224 'site_identifiers',
225 array( 'si_site' => $internalIds ),
226 __METHOD__
227 );
228 }
229
230 foreach ( $localIds as $localId ) {
231 $dbw->insert(
232 'site_identifiers',
233 array(
234 'si_site' => $localId[0],
235 'si_type' => $localId[1],
236 'si_key' => $localId[2],
237 ),
238 __METHOD__
239 );
240 }
241
242 $dbw->endAtomic( __METHOD__ );
243
244 $this->reset();
245
246 return $success;
247 }
248
249 /**
250 * Resets the SiteList
251 *
252 * @since 1.25
253 */
254 public function reset() {
255 $this->sites = null;
256 }
257
258 /**
259 * Clears the list of sites stored in the database.
260 *
261 * @see SiteStore::clear()
262 *
263 * @return bool Success
264 */
265 public function clear() {
266 $dbw = wfGetDB( DB_MASTER );
267
268 $dbw->startAtomic( __METHOD__ );
269 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
270 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
271 $dbw->endAtomic( __METHOD__ );
272
273 $this->reset();
274
275 return $ok;
276 }
277
278 }