Support offsets in prefix searching
[mediawiki.git] / includes / site / SiteListFileCache.php
blobc0ecab15499bb45d72ec287267e3562c78311bad
1 <?php
3 /**
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
19 * @since 1.25
21 * @file
23 * @license GNU GPL v2+
25 class SiteListFileCache {
27 /**
28 * @var SiteList
30 private $sites = null;
32 /**
33 * @var string
35 private $cacheFile;
37 /**
38 * @param string $cacheFile
40 public function __construct( $cacheFile ) {
41 $this->cacheFile = $cacheFile;
44 /**
45 * @since 1.25
47 * @return SiteList
49 public function getSites() {
50 if ( $this->sites === null ) {
51 $this->sites = $this->loadSitesFromCache();
54 return $this->sites;
57 /**
58 * @since 1.25
60 public function getSite( $globalId ) {
61 $sites = $this->getSites();
63 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
66 /**
67 * @return SiteList
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 );
79 return $sites;
82 /**
83 * @throws MWException
84 * @return array
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.' );
98 return $data;
102 * @param array $data
104 * @return Site
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'] );
123 return $site;