MediaWiki master
DBSiteStore.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Site;
22
25
34class DBSiteStore implements SiteStore {
36 protected $sites = null;
38 private $dbProvider;
39
44 public function __construct( IConnectionProvider $dbProvider ) {
45 $this->dbProvider = $dbProvider;
46 }
47
54 public function getSites() {
55 $this->loadSites();
56
57 return $this->sites;
58 }
59
65 protected function loadSites() {
66 $this->sites = new SiteList();
67
68 $dbr = $this->dbProvider->getReplicaDatabase();
69
70 $res = $dbr->newSelectQueryBuilder()
71 ->select( [
72 'site_id',
73 'site_global_key',
74 'site_type',
75 'site_group',
76 'site_source',
77 'site_language',
78 'site_protocol',
79 'site_domain',
80 'site_data',
81 'site_forward',
82 'site_config',
83 ] )
84 ->from( 'sites' )
85 ->orderBy( 'site_global_key' )
86 ->caller( __METHOD__ )->fetchResultSet();
87
88 foreach ( $res as $row ) {
89 $site = Site::newForType( $row->site_type );
90 $site->setGlobalId( $row->site_global_key );
91 $site->setInternalId( (int)$row->site_id );
92 $site->setForward( (bool)$row->site_forward );
93 $site->setGroup( $row->site_group );
94 $site->setLanguageCode( $row->site_language === ''
95 ? null
96 : $row->site_language
97 );
98 $site->setSource( $row->site_source );
99 $site->setExtraData( unserialize( $row->site_data ) );
100 $site->setExtraConfig( unserialize( $row->site_config ) );
101 $this->sites[] = $site;
102 }
103
104 // Batch load the local site identifiers.
105 $ids = $dbr->newSelectQueryBuilder()
106 ->select( [ 'si_site', 'si_type', 'si_key', ] )
107 ->from( 'site_identifiers' )
108 ->caller( __METHOD__ )->fetchResultSet();
109
110 foreach ( $ids as $id ) {
111 if ( $this->sites->hasInternalId( $id->si_site ) ) {
112 $site = $this->sites->getSiteByInternalId( $id->si_site );
113 $site->addLocalId( $id->si_type, $id->si_key );
114 $this->sites->setSite( $site );
115 }
116 }
117 }
118
126 public function getSite( $globalId ) {
127 if ( $this->sites === null ) {
128 $this->sites = $this->getSites();
129 }
130
131 return $this->sites->hasSite( $globalId ) ? $this->sites->getSite( $globalId ) : null;
132 }
133
141 public function saveSite( Site $site ) {
142 return $this->saveSites( [ $site ] );
143 }
144
152 public function saveSites( array $sites ) {
153 if ( !$sites ) {
154 return true;
155 }
156
157 $dbw = $this->dbProvider->getPrimaryDatabase();
158
159 $dbw->startAtomic( __METHOD__ );
160
161 $internalIds = [];
162 $localIds = [];
163
164 foreach ( $sites as $site ) {
165 if ( $site->getInternalId() !== null ) {
166 $internalIds[] = $site->getInternalId();
167 }
168
169 $fields = [
170 // Site data
171 'site_global_key' => $site->getGlobalId(), // TODO: check not null
172 'site_type' => $site->getType(),
173 'site_group' => $site->getGroup(),
174 'site_source' => $site->getSource(),
175 'site_language' => $site->getLanguageCode() ?? '',
176 'site_protocol' => $site->getProtocol(),
177 'site_domain' => strrev( $site->getDomain() ?? '' ) . '.',
178 'site_data' => serialize( $site->getExtraData() ),
179
180 // Site config
181 'site_forward' => $site->shouldForward() ? 1 : 0,
182 'site_config' => serialize( $site->getExtraConfig() ),
183 ];
184
185 $rowId = $site->getInternalId();
186 if ( $rowId !== null ) {
187 $dbw->newUpdateQueryBuilder()
188 ->update( 'sites' )
189 ->set( $fields )
190 ->where( [ 'site_id' => $rowId ] )
191 ->caller( __METHOD__ )->execute();
192 } else {
193 $dbw->newInsertQueryBuilder()
194 ->insertInto( 'sites' )
195 ->row( $fields )
196 ->caller( __METHOD__ )->execute();
197 $rowId = $dbw->insertId();
198 }
199
200 foreach ( $site->getLocalIds() as $idType => $ids ) {
201 foreach ( $ids as $id ) {
202 $localIds[] = [ $rowId, $idType, $id ];
203 }
204 }
205 }
206
207 if ( $internalIds !== [] ) {
208 $dbw->newDeleteQueryBuilder()
209 ->deleteFrom( 'site_identifiers' )
210 ->where( [ 'si_site' => $internalIds ] )
211 ->caller( __METHOD__ )->execute();
212 }
213
214 foreach ( $localIds as $localId ) {
215 $dbw->newInsertQueryBuilder()
216 ->insertInto( 'site_identifiers' )
217 ->row( [ 'si_site' => $localId[0], 'si_type' => $localId[1], 'si_key' => $localId[2] ] )
218 ->caller( __METHOD__ )->execute();
219 }
220
221 $dbw->endAtomic( __METHOD__ );
222
223 $this->reset();
224
225 return true;
226 }
227
233 public function reset() {
234 $this->sites = null;
235 }
236
242 public function clear() {
243 $dbw = $this->dbProvider->getPrimaryDatabase();
244
245 $dbw->startAtomic( __METHOD__ );
246 $dbw->newDeleteQueryBuilder()
247 ->deleteFrom( 'sites' )
248 ->where( IDatabase::ALL_ROWS )
249 ->caller( __METHOD__ )->execute();
250 $dbw->newDeleteQueryBuilder()
251 ->deleteFrom( 'site_identifiers' )
252 ->where( IDatabase::ALL_ROWS )
253 ->caller( __METHOD__ )->execute();
254 $dbw->endAtomic( __METHOD__ );
255
256 $this->reset();
257 }
258
259}
260
262class_alias( DBSiteStore::class, 'DBSiteStore' );
Holds a list of sites stored in the database.
__construct(IConnectionProvider $dbProvider)
clear()
Clears the list of sites stored in the database.
loadSites()
Fetches the site from the database and loads them into the sites field.
reset()
Resets the SiteList.
Array-like collection of Site objects.
Definition SiteList.php:37
Represents a single site.
Definition Site.php:36
static newForType( $siteType)
Definition Site.php:609
Interface for storing and retrieving Site objects.
Definition SiteStore.php:32
Provide primary and replica IDatabase connections.
Interface to a relational database.
Definition IDatabase.php:48