Merge "Don't create Language objects during ResourceLoader tests"
[lhc/web/wiklou.git] / includes / site / SiteListFileCache.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 *
20 * @license GNU GPL v2+
21 */
22
23 /**
24 * Provides a file-based cache of a SiteStore, stored as a json file.
25 * The cache can be built with the rebuildSitesCache.php maintenance script,
26 * and a MediaWiki instance can be setup to use this by setting the
27 * 'wgSitesCacheFile' configuration to the cache file location.
28 *
29 * @since 1.25
30 */
31 class SiteListFileCache {
32
33 /**
34 * @var SiteList
35 */
36 private $sites = null;
37
38 /**
39 * @var string
40 */
41 private $cacheFile;
42
43 /**
44 * @param string $cacheFile
45 */
46 public function __construct( $cacheFile ) {
47 $this->cacheFile = $cacheFile;
48 }
49
50 /**
51 * @since 1.25
52 *
53 * @return SiteList
54 */
55 public function getSites() {
56 if ( $this->sites === null ) {
57 $this->sites = $this->loadSitesFromCache();
58 }
59
60 return $this->sites;
61 }
62
63 /**
64 * @param string $globalId
65 *
66 * @since 1.25
67 *
68 * @return Site|null
69 */
70 public function getSite( $globalId ) {
71 $sites = $this->getSites();
72
73 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
74 }
75
76 /**
77 * @return SiteList
78 */
79 private function loadSitesFromCache() {
80 $data = $this->loadJsonFile();
81
82 $sites = new SiteList();
83
84 // @todo lazy initialize the site objects in the site list (e.g. only when needed to access)
85 foreach ( $data['sites'] as $siteArray ) {
86 $sites[] = $this->newSiteFromArray( $siteArray );
87 }
88
89 return $sites;
90 }
91
92 /**
93 * @throws MWException
94 * @return array
95 */
96 private function loadJsonFile() {
97 if ( !is_readable( $this->cacheFile ) ) {
98 throw new MWException( 'SiteList cache file not found.' );
99 }
100
101 $contents = file_get_contents( $this->cacheFile );
102 $data = json_decode( $contents, true );
103
104 if ( !is_array( $data ) || !array_key_exists( 'sites', $data ) ) {
105 throw new MWException( 'SiteStore json cache data is invalid.' );
106 }
107
108 return $data;
109 }
110
111 /**
112 * @param array $data
113 *
114 * @return Site
115 */
116 private function newSiteFromArray( array $data ) {
117 $siteType = array_key_exists( 'type', $data ) ? $data['type'] : Site::TYPE_UNKNOWN;
118 $site = Site::newForType( $siteType );
119
120 $site->setGlobalId( $data['globalid'] );
121 $site->setInternalId( $data['internalid'] );
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 }