Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.27% covered (warning)
86.27%
44 / 51
87.50% covered (warning)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
InterwikiLookupAdapter
86.27% covered (warning)
86.27%
44 / 51
87.50% covered (warning)
87.50%
7 / 8
18.84
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isValidInterwiki
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fetch
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getAllPrefixes
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 invalidateCache
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 loadInterwikiMap
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getInterwikiMap
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getSiteInterwikis
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
3
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
21namespace MediaWiki\Interwiki;
22
23use MediaWiki\Site\MediaWikiSite;
24use MediaWiki\Site\Site;
25use MediaWiki\Site\SiteLookup;
26
27/**
28 * InterwikiLookupAdapter on top of SiteLookup
29 *
30 * @since 1.29
31 * @license GPL-2.0-or-later
32 */
33class InterwikiLookupAdapter implements InterwikiLookup {
34
35    /**
36     * @var SiteLookup
37     */
38    private $siteLookup;
39
40    /**
41     * @var Interwiki[]|null associative array mapping interwiki prefixes to Interwiki objects
42     */
43    private $interwikiMap;
44
45    public function __construct(
46        SiteLookup $siteLookup,
47        ?array $interwikiMap = null
48    ) {
49        $this->siteLookup = $siteLookup;
50        $this->interwikiMap = $interwikiMap;
51    }
52
53    /**
54     * See InterwikiLookup::isValidInterwiki
55     * It loads the whole interwiki map.
56     *
57     * @param string $prefix Interwiki prefix to use
58     * @return bool Whether it exists
59     */
60    public function isValidInterwiki( $prefix ) {
61        return array_key_exists( $prefix, $this->getInterwikiMap() );
62    }
63
64    /**
65     * See InterwikiLookup::fetch
66     * It loads the whole interwiki map.
67     *
68     * @param string $prefix Interwiki prefix to use
69     * @return Interwiki|null|bool
70     */
71    public function fetch( $prefix ) {
72        if ( $prefix == '' ) {
73            return null;
74        }
75
76        if ( !$this->isValidInterwiki( $prefix ) ) {
77            return false;
78        }
79
80        // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
81        return $this->interwikiMap[$prefix];
82    }
83
84    /**
85     * See InterwikiLookup::getAllPrefixes
86     *
87     * @param bool|null $local If set, limits output to local/non-local interwikis
88     * @return array[] interwiki rows
89     */
90    public function getAllPrefixes( $local = null ) {
91        $res = [];
92        foreach ( $this->getInterwikiMap() as $interwikiId => $interwiki ) {
93            if ( $local === null || $interwiki->isLocal() === $local ) {
94                $res[] = [
95                    'iw_prefix' => $interwikiId,
96                    'iw_url' => $interwiki->getURL(),
97                    'iw_api' => $interwiki->getAPI(),
98                    'iw_wikiid' => $interwiki->getWikiID(),
99                    'iw_local' => $interwiki->isLocal(),
100                    'iw_trans' => $interwiki->isTranscludable(),
101                ];
102            }
103        }
104        return $res;
105    }
106
107    /**
108     * See InterwikiLookup::invalidateCache
109     *
110     * @param string $prefix
111     */
112    public function invalidateCache( $prefix ) {
113        if ( !isset( $this->interwikiMap[$prefix] ) ) {
114            return;
115        }
116        $globalId = $this->interwikiMap[$prefix]->getWikiID();
117        unset( $this->interwikiMap[$prefix] );
118
119        // Reload the interwiki
120        $site = $this->siteLookup->getSites()->getSite( $globalId );
121        $interwikis = $this->getSiteInterwikis( $site );
122        $this->interwikiMap = array_merge( $this->interwikiMap, [ $interwikis[$prefix] ] );
123    }
124
125    /**
126     * Load interwiki map to use as cache
127     */
128    private function loadInterwikiMap() {
129        $interwikiMap = [];
130        $siteList = $this->siteLookup->getSites();
131        foreach ( $siteList as $site ) {
132            $interwikis = $this->getSiteInterwikis( $site );
133            $interwikiMap = array_merge( $interwikiMap, $interwikis );
134        }
135        $this->interwikiMap = $interwikiMap;
136    }
137
138    /**
139     * Get interwikiMap attribute, load if needed.
140     *
141     * @return Interwiki[]
142     */
143    private function getInterwikiMap() {
144        if ( $this->interwikiMap === null ) {
145            $this->loadInterwikiMap();
146        }
147        return $this->interwikiMap;
148    }
149
150    /**
151     * Load interwikis for the given site
152     *
153     * @param Site $site
154     * @return Interwiki[]
155     */
156    private function getSiteInterwikis( Site $site ) {
157        $url = $site->getPageUrl();
158        if ( $site instanceof MediaWikiSite ) {
159            $path = $site->getFileUrl( 'api.php' );
160        } else {
161            $path = '';
162        }
163        $local = $site->getSource() === 'local';
164
165        $interwikis = [];
166        foreach ( $site->getInterwikiIds() as $interwiki ) {
167            // TODO: How to adapt trans?
168            $interwikis[$interwiki] = new Interwiki(
169                $interwiki,
170                $url,
171                $path,
172                $site->getGlobalId(),
173                $local
174            );
175        }
176        return $interwikis;
177    }
178}