]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/interwiki/InterwikiLookupAdapter.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / interwiki / InterwikiLookupAdapter.php
1 <?php
2 namespace MediaWiki\Interwiki;
3
4 /**
5  * InterwikiLookupAdapter on top of SiteLookup
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  * http://www.gnu.org/copyleft/gpl.html
21  *
22  * @file
23  *
24  * @since 1.29
25  * @ingroup InterwikiLookup
26  *
27  * @license GNU GPL v2+
28  */
29
30 use Interwiki;
31 use Site;
32 use SiteLookup;
33 use MediaWikiSite;
34
35 class InterwikiLookupAdapter implements InterwikiLookup {
36
37         /**
38          * @var SiteLookup
39          */
40         private $siteLookup;
41
42         /**
43          * @var Interwiki[]|null associative array mapping interwiki prefixes to Interwiki objects
44          */
45         private $interwikiMap;
46
47         function __construct(
48                 SiteLookup $siteLookup,
49                 array $interwikiMap = null
50         ) {
51                 $this->siteLookup = $siteLookup;
52                 $this->interwikiMap = $interwikiMap;
53         }
54
55         /**
56          * See InterwikiLookup::isValidInterwiki
57          * It loads the whole interwiki map.
58          *
59          * @param string $prefix Interwiki prefix to use
60          * @return bool Whether it exists
61          */
62         public function isValidInterwiki( $prefix ) {
63                 return array_key_exists( $prefix, $this->getInterwikiMap() );
64         }
65
66         /**
67          * See InterwikiLookup::fetch
68          * It loads the whole interwiki map.
69          *
70          * @param string $prefix Interwiki prefix to use
71          * @return Interwiki|null|bool
72          */
73         public function fetch( $prefix ) {
74                 if ( $prefix == '' ) {
75                         return null;
76                 }
77
78                 if ( !$this->isValidInterwiki( $prefix ) ) {
79                         return false;
80                 }
81
82                 return $this->interwikiMap[$prefix];
83         }
84
85         /**
86          * See InterwikiLookup::getAllPrefixes
87          *
88          * @param string|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                 $interwikis = [];
159                 foreach ( $site->getInterwikiIds() as $interwiki ) {
160                         $url = $site->getPageUrl();
161                         if ( $site instanceof MediaWikiSite ) {
162                                 $path = $site->getFileUrl( 'api.php' );
163                         } else {
164                                 $path = '';
165                         }
166                         $local = $site->getSource() === 'local';
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 }