summaryrefslogtreecommitdiff
path: root/www/wiki/includes/interwiki/InterwikiLookupAdapter.php
blob: 076c37fe1fbd0e742f5bfb04f67be36a640e7383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace MediaWiki\Interwiki;

/**
 * InterwikiLookupAdapter on top of SiteLookup
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 *
 * @since 1.29
 * @ingroup InterwikiLookup
 *
 * @license GNU GPL v2+
 */

use Interwiki;
use Site;
use SiteLookup;
use MediaWikiSite;

class InterwikiLookupAdapter implements InterwikiLookup {

	/**
	 * @var SiteLookup
	 */
	private $siteLookup;

	/**
	 * @var Interwiki[]|null associative array mapping interwiki prefixes to Interwiki objects
	 */
	private $interwikiMap;

	function __construct(
		SiteLookup $siteLookup,
		array $interwikiMap = null
	) {
		$this->siteLookup = $siteLookup;
		$this->interwikiMap = $interwikiMap;
	}

	/**
	 * See InterwikiLookup::isValidInterwiki
	 * It loads the whole interwiki map.
	 *
	 * @param string $prefix Interwiki prefix to use
	 * @return bool Whether it exists
	 */
	public function isValidInterwiki( $prefix ) {
		return array_key_exists( $prefix, $this->getInterwikiMap() );
	}

	/**
	 * See InterwikiLookup::fetch
	 * It loads the whole interwiki map.
	 *
	 * @param string $prefix Interwiki prefix to use
	 * @return Interwiki|null|bool
	 */
	public function fetch( $prefix ) {
		if ( $prefix == '' ) {
			return null;
		}

		if ( !$this->isValidInterwiki( $prefix ) ) {
			return false;
		}

		return $this->interwikiMap[$prefix];
	}

	/**
	 * See InterwikiLookup::getAllPrefixes
	 *
	 * @param string|null $local If set, limits output to local/non-local interwikis
	 * @return array[] interwiki rows
	 */
	public function getAllPrefixes( $local = null ) {
		$res = [];
		foreach ( $this->getInterwikiMap() as $interwikiId => $interwiki ) {
			if ( $local === null || $interwiki->isLocal() === $local ) {
				$res[] = [
					'iw_prefix' => $interwikiId,
					'iw_url' => $interwiki->getURL(),
					'iw_api' => $interwiki->getAPI(),
					'iw_wikiid' => $interwiki->getWikiID(),
					'iw_local' => $interwiki->isLocal(),
					'iw_trans' => $interwiki->isTranscludable(),
				];
			}
		}
		return $res;
	}

	/**
	 * See InterwikiLookup::invalidateCache
	 *
	 * @param string $prefix
	 */
	public function invalidateCache( $prefix ) {
		if ( !isset( $this->interwikiMap[$prefix] ) ) {
			return;
		}
		$globalId = $this->interwikiMap[$prefix]->getWikiID();
		unset( $this->interwikiMap[$prefix] );

		// Reload the interwiki
		$site = $this->siteLookup->getSites()->getSite( $globalId );
		$interwikis = $this->getSiteInterwikis( $site );
		$this->interwikiMap = array_merge( $this->interwikiMap, [ $interwikis[$prefix] ] );
	}

	/**
	 * Load interwiki map to use as cache
	 */
	private function loadInterwikiMap() {
		$interwikiMap = [];
		$siteList = $this->siteLookup->getSites();
		foreach ( $siteList as $site ) {
			$interwikis = $this->getSiteInterwikis( $site );
			$interwikiMap = array_merge( $interwikiMap, $interwikis );
		}
		$this->interwikiMap = $interwikiMap;
	}

	/**
	 * Get interwikiMap attribute, load if needed.
	 *
	 * @return Interwiki[]
	 */
	private function getInterwikiMap() {
		if ( $this->interwikiMap === null ) {
			$this->loadInterwikiMap();
		}
		return $this->interwikiMap;
	}

	/**
	 * Load interwikis for the given site
	 *
	 * @param Site $site
	 * @return Interwiki[]
	 */
	private function getSiteInterwikis( Site $site ) {
		$interwikis = [];
		foreach ( $site->getInterwikiIds() as $interwiki ) {
			$url = $site->getPageUrl();
			if ( $site instanceof MediaWikiSite ) {
				$path = $site->getFileUrl( 'api.php' );
			} else {
				$path = '';
			}
			$local = $site->getSource() === 'local';
			// TODO: How to adapt trans?
			$interwikis[$interwiki] = new Interwiki(
				$interwiki,
				$url,
				$path,
				$site->getGlobalId(),
				$local
			);
		}
		return $interwikis;
	}
}