3 namespace MediaWiki\Interwiki
;
6 * InterwikiLookupAdapter on top of SiteLookup
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
26 * @ingroup InterwikiLookup
28 * @license GPL-2.0-or-later
36 class InterwikiLookupAdapter
implements InterwikiLookup
{
44 * @var Interwiki[]|null associative array mapping interwiki prefixes to Interwiki objects
46 private $interwikiMap;
49 SiteLookup
$siteLookup,
50 array $interwikiMap = null
52 $this->siteLookup
= $siteLookup;
53 $this->interwikiMap
= $interwikiMap;
57 * See InterwikiLookup::isValidInterwiki
58 * It loads the whole interwiki map.
60 * @param string $prefix Interwiki prefix to use
61 * @return bool Whether it exists
63 public function isValidInterwiki( $prefix ) {
64 return array_key_exists( $prefix, $this->getInterwikiMap() );
68 * See InterwikiLookup::fetch
69 * It loads the whole interwiki map.
71 * @param string $prefix Interwiki prefix to use
72 * @return Interwiki|null|bool
74 public function fetch( $prefix ) {
75 if ( $prefix == '' ) {
79 if ( !$this->isValidInterwiki( $prefix ) ) {
83 return $this->interwikiMap
[$prefix];
87 * See InterwikiLookup::getAllPrefixes
89 * @param string|null $local If set, limits output to local/non-local interwikis
90 * @return array[] interwiki rows
92 public function getAllPrefixes( $local = null ) {
94 foreach ( $this->getInterwikiMap() as $interwikiId => $interwiki ) {
95 if ( $local === null ||
$interwiki->isLocal() === $local ) {
97 'iw_prefix' => $interwikiId,
98 'iw_url' => $interwiki->getURL(),
99 'iw_api' => $interwiki->getAPI(),
100 'iw_wikiid' => $interwiki->getWikiID(),
101 'iw_local' => $interwiki->isLocal(),
102 'iw_trans' => $interwiki->isTranscludable(),
110 * See InterwikiLookup::invalidateCache
112 * @param string $prefix
114 public function invalidateCache( $prefix ) {
115 if ( !isset( $this->interwikiMap
[$prefix] ) ) {
118 $globalId = $this->interwikiMap
[$prefix]->getWikiID();
119 unset( $this->interwikiMap
[$prefix] );
121 // Reload the interwiki
122 $site = $this->siteLookup
->getSites()->getSite( $globalId );
123 $interwikis = $this->getSiteInterwikis( $site );
124 $this->interwikiMap
= array_merge( $this->interwikiMap
, [ $interwikis[$prefix] ] );
128 * Load interwiki map to use as cache
130 private function loadInterwikiMap() {
132 $siteList = $this->siteLookup
->getSites();
133 foreach ( $siteList as $site ) {
134 $interwikis = $this->getSiteInterwikis( $site );
135 $interwikiMap = array_merge( $interwikiMap, $interwikis );
137 $this->interwikiMap
= $interwikiMap;
141 * Get interwikiMap attribute, load if needed.
143 * @return Interwiki[]
145 private function getInterwikiMap() {
146 if ( $this->interwikiMap
=== null ) {
147 $this->loadInterwikiMap();
149 return $this->interwikiMap
;
153 * Load interwikis for the given site
156 * @return Interwiki[]
158 private function getSiteInterwikis( Site
$site ) {
160 foreach ( $site->getInterwikiIds() as $interwiki ) {
161 $url = $site->getPageUrl();
162 if ( $site instanceof MediaWikiSite
) {
163 $path = $site->getFileUrl( 'api.php' );
167 $local = $site->getSource() === 'local';
168 // TODO: How to adapt trans?
169 $interwikis[$interwiki] = new Interwiki(
173 $site->getGlobalId(),