MediaWiki  1.28.0
DBSiteStore.php
Go to the documentation of this file.
1 <?php
2 
31 class DBSiteStore implements SiteStore {
32 
36  protected $sites = null;
37 
41  private $dbLoadBalancer;
42 
52  $this->dbLoadBalancer = $dbLoadBalancer;
53  }
54 
62  public function getSites() {
63  $this->loadSites();
64 
65  return $this->sites;
66  }
67 
73  protected function loadSites() {
74  $this->sites = new SiteList();
75 
76  $dbr = $this->dbLoadBalancer->getConnection( DB_REPLICA );
77 
78  $res = $dbr->select(
79  'sites',
80  [
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',
92  ],
93  '',
94  __METHOD__,
95  [ 'ORDER BY' => 'site_global_key' ]
96  );
97 
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
107  );
108  $site->setSource( $row->site_source );
109  $site->setExtraData( unserialize( $row->site_data ) );
110  $site->setExtraConfig( unserialize( $row->site_config ) );
111  $this->sites[] = $site;
112  }
113 
114  // Batch load the local site identifiers.
115  $ids = $dbr->select(
116  'site_identifiers',
117  [
118  'si_site',
119  'si_type',
120  'si_key',
121  ],
122  [],
123  __METHOD__
124  );
125 
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 );
131  }
132  }
133  }
134 
144  public function getSite( $globalId ) {
145  if ( $this->sites === null ) {
146  $this->sites = $this->getSites();
147  }
148 
149  return $this->sites->hasSite( $globalId ) ? $this->sites->getSite( $globalId ) : null;
150  }
151 
161  public function saveSite( Site $site ) {
162  return $this->saveSites( [ $site ] );
163  }
164 
174  public function saveSites( array $sites ) {
175  if ( empty( $sites ) ) {
176  return true;
177  }
178 
179  $dbw = $this->dbLoadBalancer->getConnection( DB_MASTER );
180 
181  $dbw->startAtomic( __METHOD__ );
182 
183  $success = true;
184 
185  $internalIds = [];
186  $localIds = [];
187 
188  foreach ( $sites as $site ) {
189  if ( $site->getInternalId() !== null ) {
190  $internalIds[] = $site->getInternalId();
191  }
192 
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() ),
203 
204  // Site config
205  'site_forward' => $site->shouldForward() ? 1 : 0,
206  'site_config' => serialize( $site->getExtraConfig() ),
207  ];
208 
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();
219  }
220 
221  foreach ( $site->getLocalIds() as $idType => $ids ) {
222  foreach ( $ids as $id ) {
223  $localIds[] = [ $rowId, $idType, $id ];
224  }
225  }
226  }
227 
228  if ( $internalIds !== [] ) {
229  $dbw->delete(
230  'site_identifiers',
231  [ 'si_site' => $internalIds ],
232  __METHOD__
233  );
234  }
235 
236  foreach ( $localIds as $localId ) {
237  $dbw->insert(
238  'site_identifiers',
239  [
240  'si_site' => $localId[0],
241  'si_type' => $localId[1],
242  'si_key' => $localId[2],
243  ],
244  __METHOD__
245  );
246  }
247 
248  $dbw->endAtomic( __METHOD__ );
249 
250  $this->reset();
251 
252  return $success;
253  }
254 
260  public function reset() {
261  $this->sites = null;
262  }
263 
271  public function clear() {
272  $dbw = $this->dbLoadBalancer->getConnection( DB_MASTER );
273 
274  $dbw->startAtomic( __METHOD__ );
275  $ok = $dbw->delete( 'sites', '*', __METHOD__ );
276  $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
277  $dbw->endAtomic( __METHOD__ );
278 
279  $this->reset();
280 
281  return $ok;
282  }
283 
284 }
the array() calling protocol came about after MediaWiki 1.4rc1.
$success
clear()
Clears the list of sites stored in the database.
LoadBalancer $dbLoadBalancer
Definition: DBSiteStore.php:41
const DB_MASTER
Definition: defines.php:23
getSite($globalId)
saveSite(Site $site)
__construct(LoadBalancer $dbLoadBalancer)
Definition: DBSiteStore.php:51
unserialize($serialized)
Definition: ApiMessage.php:102
$res
Definition: database.txt:21
reset()
Resets the SiteList.
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
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
Definition: Site.php:29
loadSites()
Fetches the site from the database and loads them into the sites field.
Definition: DBSiteStore.php:73
const DB_REPLICA
Definition: defines.php:22
saveSites(array $sites)
serialize()
Definition: ApiMessage.php:94
SiteList null $sites
Definition: DBSiteStore.php:36
static newForType($siteType)
Definition: Site.php:643