MediaWiki REL1_31
FileBasedSiteLookup.php
Go to the documentation of this file.
1<?php
34
38 private $sites = null;
39
43 private $cacheFile;
44
48 public function __construct( $cacheFile ) {
49 $this->cacheFile = $cacheFile;
50 }
51
57 public function getSites() {
58 if ( $this->sites === null ) {
59 $this->sites = $this->loadSitesFromCache();
60 }
61
62 return $this->sites;
63 }
64
72 public function getSite( $globalId ) {
73 $sites = $this->getSites();
74
75 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
76 }
77
81 private function loadSitesFromCache() {
82 $data = $this->loadJsonFile();
83
84 $sites = new SiteList();
85
86 // @todo lazy initialize the site objects in the site list (e.g. only when needed to access)
87 foreach ( $data['sites'] as $siteArray ) {
88 $sites[] = $this->newSiteFromArray( $siteArray );
89 }
90
91 return $sites;
92 }
93
98 private function loadJsonFile() {
99 if ( !is_readable( $this->cacheFile ) ) {
100 throw new MWException( 'SiteList cache file not found.' );
101 }
102
103 $contents = file_get_contents( $this->cacheFile );
104 $data = json_decode( $contents, true );
105
106 if ( !is_array( $data ) || !is_array( $data['sites'] )
107 || !array_key_exists( 'sites', $data )
108 ) {
109 throw new MWException( 'SiteStore json cache data is invalid.' );
110 }
111
112 return $data;
113 }
114
120 private function newSiteFromArray( array $data ) {
121 $siteType = array_key_exists( 'type', $data ) ? $data['type'] : Site::TYPE_UNKNOWN;
122 $site = Site::newForType( $siteType );
123
124 $site->setGlobalId( $data['globalid'] );
125 $site->setForward( $data['forward'] );
126 $site->setGroup( $data['group'] );
127 $site->setLanguageCode( $data['language'] );
128 $site->setSource( $data['source'] );
129 $site->setExtraData( $data['data'] );
130 $site->setExtraConfig( $data['config'] );
131
132 foreach ( $data['identifiers'] as $identifier ) {
133 $site->addLocalId( $identifier['type'], $identifier['key'] );
134 }
135
136 return $site;
137 }
138
139}
Provides a file-based cache of a SiteStore.
MediaWiki exception.
hasSite( $globalSiteId)
Returns if the list contains the site with the provided global site identifier.
Definition SiteList.php:140
getSite( $globalSiteId)
Returns the Site with the provided global site identifier.
Definition SiteList.php:154
static newForType( $siteType)
Definition Site.php:646
const TYPE_UNKNOWN
Definition Site.php:30