MediaWiki  1.33.0
FileBasedSiteLookup.php
Go to the documentation of this file.
1 <?php
29 class FileBasedSiteLookup implements SiteLookup {
30 
34  private $sites = null;
35 
39  private $cacheFile;
40 
44  public function __construct( $cacheFile ) {
45  wfDeprecated( __CLASS__, '1.33' );
46  $this->cacheFile = $cacheFile;
47  }
48 
54  public function getSites() {
55  if ( $this->sites === null ) {
56  $this->sites = $this->loadSitesFromCache();
57  }
58 
59  return $this->sites;
60  }
61 
69  public function getSite( $globalId ) {
70  $sites = $this->getSites();
71 
72  return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
73  }
74 
78  private function loadSitesFromCache() {
79  $data = $this->loadJsonFile();
80 
81  $sites = new SiteList();
82 
83  // @todo lazy initialize the site objects in the site list (e.g. only when needed to access)
84  foreach ( $data['sites'] as $siteArray ) {
85  $sites[] = $this->newSiteFromArray( $siteArray );
86  }
87 
88  return $sites;
89  }
90 
95  private function loadJsonFile() {
96  if ( !is_readable( $this->cacheFile ) ) {
97  throw new MWException( 'SiteList cache file not found.' );
98  }
99 
100  $contents = file_get_contents( $this->cacheFile );
101  $data = json_decode( $contents, true );
102 
103  if ( !is_array( $data ) || !is_array( $data['sites'] )
104  || !array_key_exists( 'sites', $data )
105  ) {
106  throw new MWException( 'SiteStore json cache data is invalid.' );
107  }
108 
109  return $data;
110  }
111 
117  private function newSiteFromArray( array $data ) {
118  $siteType = array_key_exists( 'type', $data ) ? $data['type'] : Site::TYPE_UNKNOWN;
119  $site = Site::newForType( $siteType );
120 
121  $site->setGlobalId( $data['globalid'] );
122  $site->setForward( $data['forward'] );
123  $site->setGroup( $data['group'] );
124  $site->setLanguageCode( $data['language'] );
125  $site->setSource( $data['source'] );
126  $site->setExtraData( $data['data'] );
127  $site->setExtraConfig( $data['config'] );
128 
129  foreach ( $data['identifiers'] as $identifier ) {
130  $site->addLocalId( $identifier['type'], $identifier['key'] );
131  }
132 
133  return $site;
134  }
135 
136 }
FileBasedSiteLookup\getSite
getSite( $globalId)
Definition: FileBasedSiteLookup.php:69
FileBasedSiteLookup
Provides a file-based cache of a SiteStore, using a json file.
Definition: FileBasedSiteLookup.php:29
FileBasedSiteLookup\$cacheFile
string $cacheFile
Definition: FileBasedSiteLookup.php:39
SiteList\hasSite
hasSite( $globalSiteId)
Returns if the list contains the site with the provided global site identifier.
Definition: SiteList.php:140
SiteLookup
Definition: SiteLookup.php:28
FileBasedSiteLookup\newSiteFromArray
newSiteFromArray(array $data)
Definition: FileBasedSiteLookup.php:117
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
FileBasedSiteLookup\getSites
getSites()
Definition: FileBasedSiteLookup.php:54
FileBasedSiteLookup\loadJsonFile
loadJsonFile()
Definition: FileBasedSiteLookup.php:95
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
MWException
MediaWiki exception.
Definition: MWException.php:26
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1078
Site\TYPE_UNKNOWN
const TYPE_UNKNOWN
Definition: Site.php:30
SiteList
Definition: SiteList.php:29
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
FileBasedSiteLookup\loadSitesFromCache
loadSitesFromCache()
Definition: FileBasedSiteLookup.php:78
FileBasedSiteLookup\__construct
__construct( $cacheFile)
Definition: FileBasedSiteLookup.php:44
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
SiteList\getSite
getSite( $globalSiteId)
Returns the Site with the provided global site identifier.
Definition: SiteList.php:154
FileBasedSiteLookup\$sites
SiteList $sites
Definition: FileBasedSiteLookup.php:34