Debian Codegraph @

includes/interwiki/InterwikiLookupAdapter.php

raw symbols SCIP
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
21 namespace MediaWiki\Interwiki;
22
23 use Interwiki;
24 use MediaWiki\Site\MediaWikiSite;
25 use MediaWiki\Site\Site;
26 use MediaWiki\Site\SiteLookup;
27
28 /**
29 * InterwikiLookupAdapter on top of SiteLookup
30 *
31 * @since 1.29
32 * @license GPL-2.0-or-later
33 */
34 class 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 }