MediaWiki  1.23.2
SiteSQLStore.php
Go to the documentation of this file.
1 <?php
2 
31 class SiteSQLStore implements SiteStore {
32 
38  protected $sites = null;
39 
43  protected $sitesTable;
44 
48  private $cacheKey = null;
49 
53  private $cacheTimeout = 3600;
54 
62  public static function newInstance( ORMTable $sitesTable = null ) {
63  return new static( $sitesTable );
64  }
65 
73  protected function __construct( ORMTable $sitesTable = null ) {
74  if ( $sitesTable === null ) {
75  $sitesTable = $this->newSitesTable();
76  }
77 
78  $this->sitesTable = $sitesTable;
79  }
80 
95  protected function getCacheKey() {
96  wfProfileIn( __METHOD__ );
97 
98  if ( $this->cacheKey === null ) {
99  $type = 'SiteList#' . SiteList::getSerialVersionId();
100  $source = $this->sitesTable->getName();
101 
102  if ( $this->sitesTable->getTargetWiki() !== false ) {
103  $source = $this->sitesTable->getTargetWiki() . '.' . $source;
104  }
105 
106  $this->cacheKey = wfMemcKey( "$source/$type" );
107  }
108 
109  wfProfileOut( __METHOD__ );
110  return $this->cacheKey;
111  }
112 
122  public function getSites( $source = 'cache' ) {
123  wfProfileIn( __METHOD__ );
124 
125  if ( $source === 'cache' ) {
126  if ( $this->sites === null ) {
128  $sites = $cache->get( $this->getCacheKey() );
129 
130  if ( is_object( $sites ) ) {
131  $this->sites = $sites;
132  } else {
133  $this->loadSites();
134  }
135  }
136  }
137  else {
138  $this->loadSites();
139  }
140 
141  wfProfileOut( __METHOD__ );
142  return $this->sites;
143  }
144 
154  protected function siteFromRow( ORMRow $siteRow ) {
155  wfProfileIn( __METHOD__ );
156 
157  $site = Site::newForType( $siteRow->getField( 'type', Site::TYPE_UNKNOWN ) );
158 
159  $site->setGlobalId( $siteRow->getField( 'global_key' ) );
160 
161  $site->setInternalId( $siteRow->getField( 'id' ) );
162 
163  if ( $siteRow->hasField( 'forward' ) ) {
164  $site->setForward( $siteRow->getField( 'forward' ) );
165  }
166 
167  if ( $siteRow->hasField( 'group' ) ) {
168  $site->setGroup( $siteRow->getField( 'group' ) );
169  }
170 
171  if ( $siteRow->hasField( 'language' ) ) {
172  $site->setLanguageCode( $siteRow->getField( 'language' ) === '' ? null : $siteRow->getField( 'language' ) );
173  }
174 
175  if ( $siteRow->hasField( 'source' ) ) {
176  $site->setSource( $siteRow->getField( 'source' ) );
177  }
178 
179  if ( $siteRow->hasField( 'data' ) ) {
180  $site->setExtraData( $siteRow->getField( 'data' ) );
181  }
182 
183  if ( $siteRow->hasField( 'config' ) ) {
184  $site->setExtraConfig( $siteRow->getField( 'config' ) );
185  }
186 
187  wfProfileOut( __METHOD__ );
188  return $site;
189  }
190 
200  protected function getRowFromSite( Site $site ) {
201  $fields = array(
202  // Site data
203  'global_key' => $site->getGlobalId(), // TODO: check not null
204  'type' => $site->getType(),
205  'group' => $site->getGroup(),
206  'source' => $site->getSource(),
207  'language' => $site->getLanguageCode() === null ? '' : $site->getLanguageCode(),
208  'protocol' => $site->getProtocol(),
209  'domain' => strrev( $site->getDomain() ) . '.',
210  'data' => $site->getExtraData(),
211 
212  // Site config
213  'forward' => $site->shouldForward(),
214  'config' => $site->getExtraConfig(),
215  );
216 
217  if ( $site->getInternalId() !== null ) {
218  $fields['id'] = $site->getInternalId();
219  }
220 
221  return new ORMRow( $this->sitesTable, $fields );
222  }
223 
229  protected function loadSites() {
230  wfProfileIn( __METHOD__ );
231 
232  $this->sites = new SiteList();
233 
234  foreach ( $this->sitesTable->select() as $siteRow ) {
235  $this->sites[] = $this->siteFromRow( $siteRow );
236  }
237 
238  // Batch load the local site identifiers.
239  $ids = wfGetDB( $this->sitesTable->getReadDb() )->select(
240  'site_identifiers',
241  array(
242  'si_site',
243  'si_type',
244  'si_key',
245  ),
246  array(),
247  __METHOD__
248  );
249 
250  foreach ( $ids as $id ) {
251  if ( $this->sites->hasInternalId( $id->si_site ) ) {
252  $site = $this->sites->getSiteByInternalId( $id->si_site );
253  $site->addLocalId( $id->si_type, $id->si_key );
254  $this->sites->setSite( $site );
255  }
256  }
257 
259  $cache->set( $this->getCacheKey(), $this->sites, $this->cacheTimeout );
260 
261  wfProfileOut( __METHOD__ );
262  }
263 
274  public function getSite( $globalId, $source = 'cache' ) {
275  wfProfileIn( __METHOD__ );
276 
277  $sites = $this->getSites( $source );
278 
279  wfProfileOut( __METHOD__ );
280  return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
281  }
282 
292  public function saveSite( Site $site ) {
293  return $this->saveSites( array( $site ) );
294  }
295 
305  public function saveSites( array $sites ) {
306  wfProfileIn( __METHOD__ );
307 
308  if ( empty( $sites ) ) {
309  wfProfileOut( __METHOD__ );
310  return true;
311  }
312 
313  $dbw = $this->sitesTable->getWriteDbConnection();
314 
315  $trx = $dbw->trxLevel();
316 
317  if ( $trx == 0 ) {
318  $dbw->begin( __METHOD__ );
319  }
320 
321  $success = true;
322 
323  $internalIds = array();
324  $localIds = array();
325 
326  foreach ( $sites as $site ) {
327  if ( $site->getInternalId() !== null ) {
328  $internalIds[] = $site->getInternalId();
329  }
330 
331  $siteRow = $this->getRowFromSite( $site );
332  $success = $siteRow->save( __METHOD__ ) && $success;
333 
334  foreach ( $site->getLocalIds() as $idType => $ids ) {
335  foreach ( $ids as $id ) {
336  $localIds[] = array( $siteRow->getId(), $idType, $id );
337  }
338  }
339  }
340 
341  if ( $internalIds !== array() ) {
342  $dbw->delete(
343  'site_identifiers',
344  array( 'si_site' => $internalIds ),
345  __METHOD__
346  );
347  }
348 
349  foreach ( $localIds as $localId ) {
350  $dbw->insert(
351  'site_identifiers',
352  array(
353  'si_site' => $localId[0],
354  'si_type' => $localId[1],
355  'si_key' => $localId[2],
356  ),
357  __METHOD__
358  );
359  }
360 
361  if ( $trx == 0 ) {
362  $dbw->commit( __METHOD__ );
363  }
364 
365  // purge cache
366  $this->reset();
367 
368  wfProfileOut( __METHOD__ );
369  return $success;
370  }
371 
378  public function reset() {
379  wfProfileIn( __METHOD__ );
380  // purge cache
382  $cache->delete( $this->getCacheKey() );
383  $this->sites = null;
384 
385  wfProfileOut( __METHOD__ );
386  }
387 
395  public function clear() {
396  wfProfileIn( __METHOD__ );
397  $dbw = $this->sitesTable->getWriteDbConnection();
398 
399  $trx = $dbw->trxLevel();
400 
401  if ( $trx == 0 ) {
402  $dbw->begin( __METHOD__ );
403  }
404 
405  $ok = $dbw->delete( 'sites', '*', __METHOD__ );
406  $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
407 
408  if ( $trx == 0 ) {
409  $dbw->commit( __METHOD__ );
410  }
411 
412  $this->reset();
413 
414  wfProfileOut( __METHOD__ );
415  return $ok;
416  }
417 
423  protected function newSitesTable() {
424  return new ORMTable(
425  'sites',
426  array(
427  'id' => 'id',
428 
429  // Site data
430  'global_key' => 'str',
431  'type' => 'str',
432  'group' => 'str',
433  'source' => 'str',
434  'language' => 'str',
435  'protocol' => 'str',
436  'domain' => 'str',
437  'data' => 'array',
438 
439  // Site config
440  'forward' => 'bool',
441  'config' => 'array',
442  ),
443  array(
444  'type' => Site::TYPE_UNKNOWN,
445  'group' => Site::GROUP_NONE,
446  'source' => Site::SOURCE_LOCAL,
447  'data' => array(),
448 
449  'forward' => false,
450  'config' => array(),
451  'language' => '',
452  ),
453  'ORMRow',
454  'site_'
455  );
456  }
457 
458 }
459 
463 class Sites extends SiteSQLStore {
464 
475  public static function newSite( $globalId = false ) {
476  $site = new Site();
477 
478  if ( $globalId !== false ) {
479  $site->setGlobalId( $globalId );
480  }
481 
482  return $site;
483  }
484 
489  public static function singleton() {
490  static $singleton;
491 
492  if ( $singleton === null ) {
493  $singleton = new static();
494  }
495 
496  return $singleton;
497  }
498 
503  public function getSiteGroup( $group ) {
504  return $this->getSites()->getGroup( $group );
505  }
506 
507 }
SiteList\getSerialVersionId
static getSerialVersionId()
Returns the version ID that identifies the serialization structure used by getSerializationData() and...
Definition: SiteList.php:308
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ORMRow\hasField
hasField( $name)
Gets if a certain field is set.
Definition: ORMRow.php:220
ORMTable
Definition: ORMTable.php:31
Site\getType
getType()
Returns the type of the site (ie mediawiki).
Definition: Site.php:161
Sites\singleton
static singleton()
Definition: SiteSQLStore.php:485
Site\getDomain
getDomain()
Returns the domain of the site, ie en.wikipedia.org Or false if it's not known.
Definition: Site.php:259
SiteSQLStore\saveSite
saveSite(Site $site)
Definition: SiteSQLStore.php:288
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
SiteSQLStore\getSite
getSite( $globalId, $source='cache')
Definition: SiteSQLStore.php:270
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
Site\getInternalId
getInternalId()
Returns the set internal identifier for the site.
Definition: Site.php:466
SiteList\hasSite
hasSite( $globalSiteId)
Returns if the list contains the site with the provided global site identifier.
Definition: SiteList.php:138
Site\getLanguageCode
getLanguageCode()
Returns language code of the sites primary language.
Definition: Site.php:444
SiteSQLStore\$sitesTable
ORMTable $sitesTable
Definition: SiteSQLStore.php:41
ORMRow\save
save( $functionName=null)
Writes the answer to the database, either updating it when it already exists, or inserting it when it...
Definition: ORMRow.php:344
Site\newForType
static newForType( $siteType)
Definition: Site.php:629
SiteSQLStore\__construct
__construct(ORMTable $sitesTable=null)
Constructor.
Definition: SiteSQLStore.php:69
SiteStore
Definition: SiteStore.php:29
$success
$success
Definition: Utf8Test.php:91
wfGetMainCache
wfGetMainCache()
Get the main cache object.
Definition: GlobalFunctions.php:3957
wfMemcKey
wfMemcKey()
Get a cache key.
Definition: GlobalFunctions.php:3571
SiteSQLStore\loadSites
loadSites()
Fetches the site from the database and loads them into the sites field.
Definition: SiteSQLStore.php:225
Sites\getSiteGroup
getSiteGroup( $group)
Definition: SiteSQLStore.php:499
SiteSQLStore\clear
clear()
Clears the list of sites stored in the database.
Definition: SiteSQLStore.php:391
SiteSQLStore
Definition: SiteSQLStore.php:31
Site\TYPE_UNKNOWN
const TYPE_UNKNOWN
Definition: Site.php:31
Site\getGlobalId
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:133
Site\getSource
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:200
Sites\newSite
static newSite( $globalId=false)
Factory for creating new site objects.
Definition: SiteSQLStore.php:471
SiteList
Definition: SiteList.php:29
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
SiteSQLStore\newSitesTable
newSitesTable()
Definition: SiteSQLStore.php:419
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
Site\getGroup
getGroup()
Gets the type of the site (ie wikipedia).
Definition: Site.php:172
Site\addLocalId
addLocalId( $type, $identifier)
Adds a local identifier.
Definition: Site.php:490
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:188
Site
Definition: Site.php:29
$ok
$ok
Definition: UtfNormalTest.php:71
ORMRow\getId
getId()
Returns the objects database id.
Definition: ORMRow.php:196
SiteSQLStore\$cacheKey
string null $cacheKey
Definition: SiteSQLStore.php:45
SiteSQLStore\getRowFromSite
getRowFromSite(Site $site)
Get a new ORMRow from a Site object.
Definition: SiteSQLStore.php:196
Sites
Definition: SiteSQLStore.php:459
ORMRow\getField
getField( $name, $default=null)
Gets the value of a field.
Definition: ORMRow.php:150
SiteSQLStore\getCacheKey
getCacheKey()
Constructs a cache key to use for caching the list of sites.
Definition: SiteSQLStore.php:91
Site\getExtraConfig
getExtraConfig()
Returns the type specific config.
Definition: Site.php:421
SiteSQLStore\siteFromRow
siteFromRow(ORMRow $siteRow)
Returns a new Site object constructed from the provided ORMRow.
Definition: SiteSQLStore.php:150
ORMRow
Definition: ORMRow.php:34
$cache
$cache
Definition: mcc.php:32
SiteSQLStore\newInstance
static newInstance(ORMTable $sitesTable=null)
Definition: SiteSQLStore.php:58
SiteSQLStore\$sites
SiteList null $sites
Definition: SiteSQLStore.php:37
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
Site\shouldForward
shouldForward()
Gets if site.tld/path/key:pageTitle should forward users to the page on the actual site,...
Definition: Site.php:229
SiteSQLStore\getSites
getSites( $source='cache')
Definition: SiteSQLStore.php:118
SiteList\getSite
getSite( $globalSiteId)
Returns the Site with the provided global site identifier.
Definition: SiteList.php:152
SiteSQLStore\$cacheTimeout
int $cacheTimeout
Definition: SiteSQLStore.php:49
Site\GROUP_NONE
const GROUP_NONE
Definition: Site.php:34
$source
if(PHP_SAPI !='cli') $source
Definition: mwdoc-filter.php:18
Site\SOURCE_LOCAL
const SOURCE_LOCAL
Definition: Site.php:39
Site\getProtocol
getProtocol()
Returns the protocol of the site.
Definition: Site.php:277
select
We use the convention $dbr for read and $dbw for write to help you keep track of whether the database object is a the world will explode Or to be a subsequent write query which succeeded on the master may fail when replicated to the slave due to a unique key collision Replication on the slave will stop and it may take hours to repair the database and get it back online Setting read_only in my cnf on the slave will avoid this but given the dire we prefer to have as many checks as possible We provide a but the wrapper functions like select() and insert() are usually more convenient. They take care of things like table prefixes and escaping for you. If you really need to make your own SQL
SiteSQLStore\reset
reset()
Purges the internal and external cache of the site list, forcing the list of sites to be re-read from...
Definition: SiteSQLStore.php:374
Site\getExtraData
getExtraData()
Returns the type specific fields.
Definition: Site.php:399
SiteSQLStore\saveSites
saveSites(array $sites)
Definition: SiteSQLStore.php:301
$type
$type
Definition: testCompression.php:46