Apply $wgMaxArticleSize more exactly
[mediawiki.git] / includes / site / FileBasedSiteLookup.php
blob965444038f8ddbaa6225abc1cd3348cbfb402af8
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
20 * @license GNU GPL v2+
23 /**
24 * Provides a file-based cache of a SiteStore. The sites are stored in
25 * a json file. (see docs/sitescache.txt regarding format)
27 * The cache can be built with the rebuildSitesCache.php maintenance script,
28 * and a MediaWiki instance can be setup to use this by setting the
29 * 'wgSitesCacheFile' configuration to the cache file location.
31 * @since 1.25
33 class FileBasedSiteLookup implements SiteLookup {
35 /**
36 * @var SiteList
38 private $sites = null;
40 /**
41 * @var string
43 private $cacheFile;
45 /**
46 * @param string $cacheFile
48 public function __construct( $cacheFile ) {
49 $this->cacheFile = $cacheFile;
52 /**
53 * @since 1.25
55 * @return SiteList
57 public function getSites() {
58 if ( $this->sites === null ) {
59 $this->sites = $this->loadSitesFromCache();
62 return $this->sites;
65 /**
66 * @param string $globalId
68 * @since 1.25
70 * @return Site|null
72 public function getSite( $globalId ) {
73 $sites = $this->getSites();
75 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
78 /**
79 * @return SiteList
81 private function loadSitesFromCache() {
82 $data = $this->loadJsonFile();
84 $sites = new SiteList();
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 );
91 return $sites;
94 /**
95 * @throws MWException
96 * @return array see docs/sitescache.txt for format of the array.
98 private function loadJsonFile() {
99 if ( !is_readable( $this->cacheFile ) ) {
100 throw new MWException( 'SiteList cache file not found.' );
103 $contents = file_get_contents( $this->cacheFile );
104 $data = json_decode( $contents, true );
106 if ( !is_array( $data ) || !is_array( $data['sites'] )
107 || !array_key_exists( 'sites', $data )
109 throw new MWException( 'SiteStore json cache data is invalid.' );
112 return $data;
116 * @param array $data
118 * @return Site
120 private function newSiteFromArray( array $data ) {
121 $siteType = array_key_exists( 'type', $data ) ? $data['type'] : Site::TYPE_UNKNOWN;
122 $site = Site::newForType( $siteType );
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'] );
132 foreach ( $data['identifiers'] as $identifier ) {
133 $site->addLocalId( $identifier['type'], $identifier['key'] );
136 return $site;