Map dummy language codes in sites
[lhc/web/wiklou.git] / includes / site / FileBasedSiteLookup.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. The sites are stored in
25 * a json file. (see docs/sitescache.txt regarding format)
26 *
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.
30 *
31 * @since 1.25
32 */
33 class FileBasedSiteLookup implements SiteLookup {
34
35 /**
36 * @var SiteList
37 */
38 private $sites = null;
39
40 /**
41 * @var string
42 */
43 private $cacheFile;
44
45 /**
46 * @var string[]
47 */
48 private $languageCodeMapping = [];
49
50 /**
51 * @param string $cacheFile
52 */
53 public function __construct( $cacheFile ) {
54 $this->cacheFile = $cacheFile;
55 }
56
57 /**
58 * @since 1.25
59 *
60 * @return SiteList
61 */
62 public function getSites() {
63 if ( $this->sites === null ) {
64 $this->sites = $this->loadSitesFromCache();
65 }
66
67 return $this->sites;
68 }
69
70 /**
71 * @param string $globalId
72 *
73 * @since 1.25
74 *
75 * @return Site|null
76 */
77 public function getSite( $globalId ) {
78 $sites = $this->getSites();
79
80 return $sites->hasSite( $globalId ) ? $sites->getSite( $globalId ) : null;
81 }
82
83 /**
84 * @return SiteList
85 */
86 private function loadSitesFromCache() {
87 $data = $this->loadJsonFile();
88
89 $sites = new SiteList();
90
91 // @todo lazy initialize the site objects in the site list (e.g. only when needed to access)
92 foreach ( $data['sites'] as $siteArray ) {
93 $sites[] = $this->newSiteFromArray( $siteArray );
94 }
95
96 return $sites;
97 }
98
99 /**
100 * @throws MWException
101 * @return array see docs/sitescache.txt for format of the array.
102 */
103 private function loadJsonFile() {
104 if ( !is_readable( $this->cacheFile ) ) {
105 throw new MWException( 'SiteList cache file not found.' );
106 }
107
108 $contents = file_get_contents( $this->cacheFile );
109 $data = json_decode( $contents, true );
110
111 if ( !is_array( $data ) || !is_array( $data['sites'] )
112 || !array_key_exists( 'sites', $data )
113 ) {
114 throw new MWException( 'SiteStore json cache data is invalid.' );
115 }
116
117 return $data;
118 }
119
120 /**
121 * @param array $data
122 *
123 * @return Site
124 */
125 private function newSiteFromArray( array $data ) {
126 $languageCode = $data['language'];
127 if ( isset( $this->languageCodeMapping[ $languageCode ] ) ) {
128 $languageCode = $this->languageCodeMapping[ $languageCode ];
129 }
130
131 $siteType = array_key_exists( 'type', $data ) ? $data['type'] : Site::TYPE_UNKNOWN;
132 $site = Site::newForType( $siteType );
133
134 $site->setGlobalId( $data['globalid'] );
135 $site->setForward( $data['forward'] );
136 $site->setGroup( $data['group'] );
137 $site->setLanguageCode( $languageCode );
138 $site->setSource( $data['source'] );
139 $site->setExtraData( $data['data'] );
140 $site->setExtraConfig( $data['config'] );
141
142 foreach ( $data['identifiers'] as $identifier ) {
143 $site->addLocalId( $identifier['type'], $identifier['key'] );
144 }
145
146 return $site;
147 }
148
149 /**
150 * Provide an array that maps language codes
151 *
152 * @param string[] $newMapping
153 */
154 public function setLanguageCodeMapping( array $newMapping ) {
155 $this->languageCodeMapping = $newMapping;
156 }
157
158 }