MediaWiki master
DBSiteStore.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Site;
8
11
20class DBSiteStore implements SiteStore {
22 protected $sites = null;
24 private $dbProvider;
25
30 public function __construct( IConnectionProvider $dbProvider ) {
31 $this->dbProvider = $dbProvider;
32 }
33
40 public function getSites() {
41 $this->loadSites();
42
43 return $this->sites;
44 }
45
51 protected function loadSites() {
52 $this->sites = new SiteList();
53
54 $dbr = $this->dbProvider->getReplicaDatabase();
55
56 $res = $dbr->newSelectQueryBuilder()
57 ->select( [
58 'site_id',
59 'site_global_key',
60 'site_type',
61 'site_group',
62 'site_source',
63 'site_language',
64 'site_protocol',
65 'site_domain',
66 'site_data',
67 'site_forward',
68 'site_config',
69 ] )
70 ->from( 'sites' )
71 ->orderBy( 'site_global_key' )
72 ->caller( __METHOD__ )->fetchResultSet();
73
74 foreach ( $res as $row ) {
75 $site = Site::newForType( $row->site_type );
76 $site->setGlobalId( $row->site_global_key );
77 $site->setInternalId( (int)$row->site_id );
78 $site->setForward( (bool)$row->site_forward );
79 $site->setGroup( $row->site_group );
80 $site->setLanguageCode( $row->site_language === ''
81 ? null
82 : $row->site_language
83 );
84 $site->setSource( $row->site_source );
85 $site->setExtraData( unserialize( $row->site_data ) );
86 $site->setExtraConfig( unserialize( $row->site_config ) );
87 $this->sites[] = $site;
88 }
89
90 // Batch load the local site identifiers.
91 $ids = $dbr->newSelectQueryBuilder()
92 ->select( [ 'si_site', 'si_type', 'si_key', ] )
93 ->from( 'site_identifiers' )
94 ->caller( __METHOD__ )->fetchResultSet();
95
96 foreach ( $ids as $id ) {
97 if ( $this->sites->hasInternalId( $id->si_site ) ) {
98 $site = $this->sites->getSiteByInternalId( $id->si_site );
99 $site->addLocalId( $id->si_type, $id->si_key );
100 $this->sites->setSite( $site );
101 }
102 }
103 }
104
112 public function getSite( $globalId ) {
113 if ( $this->sites === null ) {
114 $this->sites = $this->getSites();
115 }
116
117 return $this->sites->hasSite( $globalId ) ? $this->sites->getSite( $globalId ) : null;
118 }
119
127 public function saveSite( Site $site ) {
128 return $this->saveSites( [ $site ] );
129 }
130
138 public function saveSites( array $sites ) {
139 if ( !$sites ) {
140 return true;
141 }
142
143 $dbw = $this->dbProvider->getPrimaryDatabase();
144
145 $dbw->startAtomic( __METHOD__ );
146
147 $internalIds = [];
148 $localIds = [];
149
150 foreach ( $sites as $site ) {
151 if ( $site->getInternalId() !== null ) {
152 $internalIds[] = $site->getInternalId();
153 }
154
155 $fields = [
156 // Site data
157 'site_global_key' => $site->getGlobalId(), // TODO: check not null
158 'site_type' => $site->getType(),
159 'site_group' => $site->getGroup(),
160 'site_source' => $site->getSource(),
161 'site_language' => $site->getLanguageCode() ?? '',
162 'site_protocol' => $site->getProtocol(),
163 'site_domain' => strrev( $site->getDomain() ?? '' ) . '.',
164 'site_data' => serialize( $site->getExtraData() ),
165
166 // Site config
167 'site_forward' => $site->shouldForward() ? 1 : 0,
168 'site_config' => serialize( $site->getExtraConfig() ),
169 ];
170
171 $rowId = $site->getInternalId();
172 if ( $rowId !== null ) {
173 $dbw->newUpdateQueryBuilder()
174 ->update( 'sites' )
175 ->set( $fields )
176 ->where( [ 'site_id' => $rowId ] )
177 ->caller( __METHOD__ )->execute();
178 } else {
179 $dbw->newInsertQueryBuilder()
180 ->insertInto( 'sites' )
181 ->row( $fields )
182 ->caller( __METHOD__ )->execute();
183 $rowId = $dbw->insertId();
184 }
185
186 foreach ( $site->getLocalIds() as $idType => $ids ) {
187 foreach ( $ids as $id ) {
188 $localIds[] = [ $rowId, $idType, $id ];
189 }
190 }
191 }
192
193 if ( $internalIds !== [] ) {
194 $dbw->newDeleteQueryBuilder()
195 ->deleteFrom( 'site_identifiers' )
196 ->where( [ 'si_site' => $internalIds ] )
197 ->caller( __METHOD__ )->execute();
198 }
199
200 foreach ( $localIds as $localId ) {
201 $dbw->newInsertQueryBuilder()
202 ->insertInto( 'site_identifiers' )
203 ->row( [ 'si_site' => $localId[0], 'si_type' => $localId[1], 'si_key' => $localId[2] ] )
204 ->caller( __METHOD__ )->execute();
205 }
206
207 $dbw->endAtomic( __METHOD__ );
208
209 $this->reset();
210
211 return true;
212 }
213
219 public function reset() {
220 $this->sites = null;
221 }
222
228 public function clear() {
229 $dbw = $this->dbProvider->getPrimaryDatabase();
230
231 $dbw->startAtomic( __METHOD__ );
232 $dbw->newDeleteQueryBuilder()
233 ->deleteFrom( 'sites' )
234 ->where( IDatabase::ALL_ROWS )
235 ->caller( __METHOD__ )->execute();
236 $dbw->newDeleteQueryBuilder()
237 ->deleteFrom( 'site_identifiers' )
238 ->where( IDatabase::ALL_ROWS )
239 ->caller( __METHOD__ )->execute();
240 $dbw->endAtomic( __METHOD__ );
241
242 $this->reset();
243 }
244
245}
246
248class_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:23
Represents a single site.
Definition Site.php:22
static newForType( $siteType)
Definition Site.php:595
Interface for storing and retrieving Site objects.
Definition SiteStore.php:18
Provide primary and replica IDatabase connections.
Interface to a relational database.
Definition IDatabase.php:31