MediaWiki  1.29.2
DBSiteStore.php
Go to the documentation of this file.
1 <?php
2 
4 
33 class DBSiteStore implements SiteStore {
34 
38  protected $sites = null;
39 
43  private $dbLoadBalancer;
44 
54  $this->dbLoadBalancer = $dbLoadBalancer;
55  }
56 
64  public function getSites() {
65  $this->loadSites();
66 
67  return $this->sites;
68  }
69 
75  protected function loadSites() {
76  $this->sites = new SiteList();
77 
78  $dbr = $this->dbLoadBalancer->getConnection( DB_REPLICA );
79 
80  $res = $dbr->select(
81  'sites',
82  [
83  'site_id',
84  'site_global_key',
85  'site_type',
86  'site_group',
87  'site_source',
88  'site_language',
89  'site_protocol',
90  'site_domain',
91  'site_data',
92  'site_forward',
93  'site_config',
94  ],
95  '',
96  __METHOD__,
97  [ 'ORDER BY' => 'site_global_key' ]
98  );
99 
100  foreach ( $res as $row ) {
101  $site = Site::newForType( $row->site_type );
102  $site->setGlobalId( $row->site_global_key );
103  $site->setInternalId( (int)$row->site_id );
104  $site->setForward( (bool)$row->site_forward );
105  $site->setGroup( $row->site_group );
106  $site->setLanguageCode( $row->site_language === ''
107  ? null
108  : $row->site_language
109  );
110  $site->setSource( $row->site_source );
111  $site->setExtraData( unserialize( $row->site_data ) );
112  $site->setExtraConfig( unserialize( $row->site_config ) );
113  $this->sites[] = $site;
114  }
115 
116  // Batch load the local site identifiers.
117  $ids = $dbr->select(
118  'site_identifiers',
119  [
120  'si_site',
121  'si_type',
122  'si_key',
123  ],
124  [],
125  __METHOD__
126  );
127 
128  foreach ( $ids as $id ) {
129  if ( $this->sites->hasInternalId( $id->si_site ) ) {
130  $site = $this->sites->getSiteByInternalId( $id->si_site );
131  $site->addLocalId( $id->si_type, $id->si_key );
132  $this->sites->setSite( $site );
133  }
134  }
135  }
136 
146  public function getSite( $globalId ) {
147  if ( $this->sites === null ) {
148  $this->sites = $this->getSites();
149  }
150 
151  return $this->sites->hasSite( $globalId ) ? $this->sites->getSite( $globalId ) : null;
152  }
153 
163  public function saveSite( Site $site ) {
164  return $this->saveSites( [ $site ] );
165  }
166 
176  public function saveSites( array $sites ) {
177  if ( empty( $sites ) ) {
178  return true;
179  }
180 
181  $dbw = $this->dbLoadBalancer->getConnection( DB_MASTER );
182 
183  $dbw->startAtomic( __METHOD__ );
184 
185  $success = true;
186 
187  $internalIds = [];
188  $localIds = [];
189 
190  foreach ( $sites as $site ) {
191  if ( $site->getInternalId() !== null ) {
192  $internalIds[] = $site->getInternalId();
193  }
194 
195  $fields = [
196  // Site data
197  'site_global_key' => $site->getGlobalId(), // TODO: check not null
198  'site_type' => $site->getType(),
199  'site_group' => $site->getGroup(),
200  'site_source' => $site->getSource(),
201  'site_language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
202  'site_protocol' => $site->getProtocol(),
203  'site_domain' => strrev( $site->getDomain() ) . '.',
204  'site_data' => serialize( $site->getExtraData() ),
205 
206  // Site config
207  'site_forward' => $site->shouldForward() ? 1 : 0,
208  'site_config' => serialize( $site->getExtraConfig() ),
209  ];
210 
211  $rowId = $site->getInternalId();
212  if ( $rowId !== null ) {
213  $success = $dbw->update(
214  'sites', $fields, [ 'site_id' => $rowId ], __METHOD__
215  ) && $success;
216  } else {
217  $rowId = $dbw->nextSequenceValue( 'sites_site_id_seq' );
218  $fields['site_id'] = $rowId;
219  $success = $dbw->insert( 'sites', $fields, __METHOD__ ) && $success;
220  $rowId = $dbw->insertId();
221  }
222 
223  foreach ( $site->getLocalIds() as $idType => $ids ) {
224  foreach ( $ids as $id ) {
225  $localIds[] = [ $rowId, $idType, $id ];
226  }
227  }
228  }
229 
230  if ( $internalIds !== [] ) {
231  $dbw->delete(
232  'site_identifiers',
233  [ 'si_site' => $internalIds ],
234  __METHOD__
235  );
236  }
237 
238  foreach ( $localIds as $localId ) {
239  $dbw->insert(
240  'site_identifiers',
241  [
242  'si_site' => $localId[0],
243  'si_type' => $localId[1],
244  'si_key' => $localId[2],
245  ],
246  __METHOD__
247  );
248  }
249 
250  $dbw->endAtomic( __METHOD__ );
251 
252  $this->reset();
253 
254  return $success;
255  }
256 
262  public function reset() {
263  $this->sites = null;
264  }
265 
273  public function clear() {
274  $dbw = $this->dbLoadBalancer->getConnection( DB_MASTER );
275 
276  $dbw->startAtomic( __METHOD__ );
277  $ok = $dbw->delete( 'sites', '*', __METHOD__ );
278  $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
279  $dbw->endAtomic( __METHOD__ );
280 
281  $this->reset();
282 
283  return $ok;
284  }
285 
286 }
DBSiteStore\saveSites
saveSites(array $sites)
Definition: DBSiteStore.php:176
DBSiteStore\getSite
getSite( $globalId)
Definition: DBSiteStore.php:146
DBSiteStore\$dbLoadBalancer
LoadBalancer $dbLoadBalancer
Definition: DBSiteStore.php:43
DBSiteStore\clear
clear()
Clears the list of sites stored in the database.
Definition: DBSiteStore.php:273
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:185
serialize
serialize()
Definition: ApiMessage.php:177
$res
$res
Definition: database.txt:21
$success
$success
Definition: NoLocalSettings.php:44
DBSiteStore\saveSite
saveSite(Site $site)
Definition: DBSiteStore.php:163
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Site\newForType
static newForType( $siteType)
Definition: Site.php:646
SiteStore
Definition: SiteStore.php:29
DBSiteStore\reset
reset()
Resets the SiteList.
Definition: DBSiteStore.php:262
SiteList
Definition: SiteList.php:29
DBSiteStore
Definition: DBSiteStore.php:33
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
DBSiteStore\__construct
__construct(LoadBalancer $dbLoadBalancer)
Definition: DBSiteStore.php:53
Site
Definition: Site.php:29
DBSiteStore\getSites
getSites()
Definition: DBSiteStore.php:64
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:41
DBSiteStore\loadSites
loadSites()
Fetches the site from the database and loads them into the sites field.
Definition: DBSiteStore.php:75
DBSiteStore\$sites
SiteList null $sites
Definition: DBSiteStore.php:38
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
array
the array() calling protocol came about after MediaWiki 1.4rc1.