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