Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / site / DBSiteStore.php
blobe5247f2fc1ca8050c4fa61ae06f974dac59d6182
1 <?php
3 /**
4 * Represents the site configuration of a wiki.
5 * Holds a list of sites (ie SiteList), stored in the database.
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.
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.
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
22 * @since 1.25
24 * @file
25 * @ingroup Site
27 * @license GNU GPL v2+
28 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 * @author Daniel Kinzler
31 class DBSiteStore implements SiteStore {
33 /**
34 * @var SiteList|null
36 protected $sites = null;
38 /**
39 * @var LoadBalancer
41 private $dbLoadBalancer;
43 /**
44 * @since 1.27
46 * @todo: inject some kind of connection manager that is aware of the target wiki,
47 * instead of injecting a LoadBalancer.
49 * @param LoadBalancer $dbLoadBalancer
51 public function __construct( LoadBalancer $dbLoadBalancer ) {
52 $this->dbLoadBalancer = $dbLoadBalancer;
55 /**
56 * @see SiteStore::getSites
58 * @since 1.25
60 * @return SiteList
62 public function getSites() {
63 $this->loadSites();
65 return $this->sites;
68 /**
69 * Fetches the site from the database and loads them into the sites field.
71 * @since 1.25
73 protected function loadSites() {
74 $this->sites = new SiteList();
76 $dbr = $this->dbLoadBalancer->getConnection( DB_REPLICA );
78 $res = $dbr->select(
79 'sites',
81 'site_id',
82 'site_global_key',
83 'site_type',
84 'site_group',
85 'site_source',
86 'site_language',
87 'site_protocol',
88 'site_domain',
89 'site_data',
90 'site_forward',
91 'site_config',
93 '',
94 __METHOD__,
95 [ 'ORDER BY' => 'site_global_key' ]
98 foreach ( $res as $row ) {
99 $site = Site::newForType( $row->site_type );
100 $site->setGlobalId( $row->site_global_key );
101 $site->setInternalId( (int)$row->site_id );
102 $site->setForward( (bool)$row->site_forward );
103 $site->setGroup( $row->site_group );
104 $site->setLanguageCode( $row->site_language === ''
105 ? null
106 : $row->site_language
108 $site->setSource( $row->site_source );
109 $site->setExtraData( unserialize( $row->site_data ) );
110 $site->setExtraConfig( unserialize( $row->site_config ) );
111 $this->sites[] = $site;
114 // Batch load the local site identifiers.
115 $ids = $dbr->select(
116 'site_identifiers',
118 'si_site',
119 'si_type',
120 'si_key',
123 __METHOD__
126 foreach ( $ids as $id ) {
127 if ( $this->sites->hasInternalId( $id->si_site ) ) {
128 $site = $this->sites->getSiteByInternalId( $id->si_site );
129 $site->addLocalId( $id->si_type, $id->si_key );
130 $this->sites->setSite( $site );
136 * @see SiteStore::getSite
138 * @since 1.25
140 * @param string $globalId
142 * @return Site|null
144 public function getSite( $globalId ) {
145 if ( $this->sites === null ) {
146 $this->sites = $this->getSites();
149 return $this->sites->hasSite( $globalId ) ? $this->sites->getSite( $globalId ) : null;
153 * @see SiteStore::saveSite
155 * @since 1.25
157 * @param Site $site
159 * @return bool Success indicator
161 public function saveSite( Site $site ) {
162 return $this->saveSites( [ $site ] );
166 * @see SiteStore::saveSites
168 * @since 1.25
170 * @param Site[] $sites
172 * @return bool Success indicator
174 public function saveSites( array $sites ) {
175 if ( empty( $sites ) ) {
176 return true;
179 $dbw = $this->dbLoadBalancer->getConnection( DB_MASTER );
181 $dbw->startAtomic( __METHOD__ );
183 $success = true;
185 $internalIds = [];
186 $localIds = [];
188 foreach ( $sites as $site ) {
189 if ( $site->getInternalId() !== null ) {
190 $internalIds[] = $site->getInternalId();
193 $fields = [
194 // Site data
195 'site_global_key' => $site->getGlobalId(), // TODO: check not null
196 'site_type' => $site->getType(),
197 'site_group' => $site->getGroup(),
198 'site_source' => $site->getSource(),
199 'site_language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
200 'site_protocol' => $site->getProtocol(),
201 'site_domain' => strrev( $site->getDomain() ) . '.',
202 'site_data' => serialize( $site->getExtraData() ),
204 // Site config
205 'site_forward' => $site->shouldForward() ? 1 : 0,
206 'site_config' => serialize( $site->getExtraConfig() ),
209 $rowId = $site->getInternalId();
210 if ( $rowId !== null ) {
211 $success = $dbw->update(
212 'sites', $fields, [ 'site_id' => $rowId ], __METHOD__
213 ) && $success;
214 } else {
215 $rowId = $dbw->nextSequenceValue( 'sites_site_id_seq' );
216 $fields['site_id'] = $rowId;
217 $success = $dbw->insert( 'sites', $fields, __METHOD__ ) && $success;
218 $rowId = $dbw->insertId();
221 foreach ( $site->getLocalIds() as $idType => $ids ) {
222 foreach ( $ids as $id ) {
223 $localIds[] = [ $rowId, $idType, $id ];
228 if ( $internalIds !== [] ) {
229 $dbw->delete(
230 'site_identifiers',
231 [ 'si_site' => $internalIds ],
232 __METHOD__
236 foreach ( $localIds as $localId ) {
237 $dbw->insert(
238 'site_identifiers',
240 'si_site' => $localId[0],
241 'si_type' => $localId[1],
242 'si_key' => $localId[2],
244 __METHOD__
248 $dbw->endAtomic( __METHOD__ );
250 $this->reset();
252 return $success;
256 * Resets the SiteList
258 * @since 1.25
260 public function reset() {
261 $this->sites = null;
265 * Clears the list of sites stored in the database.
267 * @see SiteStore::clear()
269 * @return bool Success
271 public function clear() {
272 $dbw = $this->dbLoadBalancer->getConnection( DB_MASTER );
274 $dbw->startAtomic( __METHOD__ );
275 $ok = $dbw->delete( 'sites', '*', __METHOD__ );
276 $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
277 $dbw->endAtomic( __METHOD__ );
279 $this->reset();
281 return $ok;