4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
23 * @license GNU GPL v2+
25 class SiteListFileCache
{
30 private $sites = null;
38 * @param string $cacheFile
40 public function __construct( $cacheFile ) {
41 $this->cacheFile
= $cacheFile;
49 public function getSites() {
50 if ( $this->sites
=== null ) {
51 $this->sites
= $this->loadSitesFromCache();
60 public function getSite( $globalId ) {
61 $sites = $this->getSites();
63 return $sites->hasSite( $globalId ) ?
$sites->getSite( $globalId ) : null;
69 private function loadSitesFromCache() {
70 $data = $this->loadJsonFile();
72 $sites = new SiteList();
74 // @todo lazy initialize the site objects in the site list (e.g. only when needed to access)
75 foreach( $data['sites'] as $siteArray ) {
76 $sites[] = $this->newSiteFromArray( $siteArray );
86 private function loadJsonFile() {
87 if ( !is_readable( $this->cacheFile
) ) {
88 throw new MWException( 'SiteList cache file not found.' );
91 $contents = file_get_contents( $this->cacheFile
);
92 $data = json_decode( $contents, true );
94 if ( !is_array( $data ) ||
!array_key_exists( 'sites', $data ) ) {
95 throw new MWException( 'SiteStore json cache data is invalid.' );
106 private function newSiteFromArray( array $data ) {
107 $siteType = array_key_exists( 'type', $data ) ?
$data['type'] : Site
::TYPE_UNKNOWN
;
108 $site = Site
::newForType( $siteType );
110 $site->setGlobalId( $data['globalid'] );
111 $site->setInternalId( $data['internalid'] );
112 $site->setForward( $data['forward'] );
113 $site->setGroup( $data['group'] );
114 $site->setLanguageCode( $data['language'] );
115 $site->setSource( $data['source'] );
116 $site->setExtraData( $data['data'] );
117 $site->setExtraConfig( $data['config'] );
119 foreach( $data['identifiers'] as $identifier ) {
120 $site->addLocalId( $identifier['type'], $identifier['key'] );