Merge "Handle phpunit being autoloaded from checkLess.php"
[lhc/web/wiklou.git] / includes / site / SiteListFileCache.php
1 <?php
2
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.
8 *
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.
13 *
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
18 *
19 * @since 1.25
20 *
21 * @file
22 *
23 * @license GNU GPL v2+
24 */
25 class SiteListFileCache {
26
27 /**
28 * @var SiteList
29 */
30 private $sites = null;
31
32 /**
33 * @var string
34 */
35 private $cacheFile;
36
37 /**
38 * @param string $cacheFile
39 */
40 public function __construct( $cacheFile ) {
41 $this->cacheFile = $cacheFile;
42 }
43
44 /**
45 * @since 1.25
46 *
47 * @return SiteList
48 */
49 public function getSites() {
50 if ( $this->sites === null ) {
51 $this->sites = $this->loadSitesFromCache();
52 }
53
54 return $this->sites;
55 }
56
57 /**
58 * @since 1.25
59 */
60 public function getSite( $globalId ) {
61 $sites = $this->getSites();
62
63 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
64 }
65
66 /**
67 * @return SiteList
68 */
69 private function loadSitesFromCache() {
70 $data = $this->loadJsonFile();
71
72 $sites = new SiteList();
73
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 );
77 }
78
79 return $sites;
80 }
81
82 /**
83 * @throws MWException
84 * @return array
85 */
86 private function loadJsonFile() {
87 if ( !is_readable( $this->cacheFile ) ) {
88 throw new MWException( 'SiteList cache file not found.' );
89 }
90
91 $contents = file_get_contents( $this->cacheFile );
92 $data = json_decode( $contents, true );
93
94 if ( !is_array( $data ) || !array_key_exists( 'sites', $data ) ) {
95 throw new MWException( 'SiteStore json cache data is invalid.' );
96 }
97
98 return $data;
99 }
100
101 /**
102 * @param array $data
103 *
104 * @return Site
105 */
106 private function newSiteFromArray( array $data ) {
107 $siteType = array_key_exists( 'type', $data ) ? $data['type'] : Site::TYPE_UNKNOWN;
108 $site = Site::newForType( $siteType );
109
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'] );
118
119 foreach ( $data['identifiers'] as $identifier ) {
120 $site->addLocalId( $identifier['type'], $identifier['key'] );
121 }
122
123 return $site;
124 }
125
126 }