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
20 * @license GPL-2.0-or-later
24 * Provides a file-based cache of a SiteStore, using a json file.
27 * @deprecated since 1.33 Use CachingSiteStore instead.
29 class FileBasedSiteLookup
implements SiteLookup
{
34 private $sites = null;
42 * @param string $cacheFile
44 public function __construct( $cacheFile ) {
45 wfDeprecated( __CLASS__
, '1.33' );
46 $this->cacheFile
= $cacheFile;
54 public function getSites() {
55 if ( $this->sites
=== null ) {
56 $this->sites
= $this->loadSitesFromCache();
63 * @param string $globalId
69 public function getSite( $globalId ) {
70 $sites = $this->getSites();
72 return $sites->hasSite( $globalId ) ?
$sites->getSite( $globalId ) : null;
78 private function loadSitesFromCache() {
79 $data = $this->loadJsonFile();
81 $sites = new SiteList();
83 // @todo lazy initialize the site objects in the site list (e.g. only when needed to access)
84 foreach ( $data['sites'] as $siteArray ) {
85 $sites[] = $this->newSiteFromArray( $siteArray );
93 * @return array see docs/sitescache.txt for format of the array.
95 private function loadJsonFile() {
96 if ( !is_readable( $this->cacheFile
) ) {
97 throw new MWException( 'SiteList cache file not found.' );
100 $contents = file_get_contents( $this->cacheFile
);
101 $data = json_decode( $contents, true );
103 if ( !is_array( $data ) ||
!is_array( $data['sites'] )
104 ||
!array_key_exists( 'sites', $data )
106 throw new MWException( 'SiteStore json cache data is invalid.' );
117 private function newSiteFromArray( array $data ) {
118 $siteType = array_key_exists( 'type', $data ) ?
$data['type'] : Site
::TYPE_UNKNOWN
;
119 $site = Site
::newForType( $siteType );
121 $site->setGlobalId( $data['globalid'] );
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'] );
129 foreach ( $data['identifiers'] as $identifier ) {
130 $site->addLocalId( $identifier['type'], $identifier['key'] );