076c37fe1fbd0e742f5bfb04f67be36a640e7383
2 namespace MediaWiki\Interwiki
;
5 * InterwikiLookupAdapter on top of SiteLookup
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.
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.
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
25 * @ingroup InterwikiLookup
27 * @license GNU GPL v2+
35 class InterwikiLookupAdapter
implements InterwikiLookup
{
43 * @var Interwiki[]|null associative array mapping interwiki prefixes to Interwiki objects
45 private $interwikiMap;
48 SiteLookup
$siteLookup,
49 array $interwikiMap = null
51 $this->siteLookup
= $siteLookup;
52 $this->interwikiMap
= $interwikiMap;
56 * See InterwikiLookup::isValidInterwiki
57 * It loads the whole interwiki map.
59 * @param string $prefix Interwiki prefix to use
60 * @return bool Whether it exists
62 public function isValidInterwiki( $prefix ) {
63 return array_key_exists( $prefix, $this->getInterwikiMap() );
67 * See InterwikiLookup::fetch
68 * It loads the whole interwiki map.
70 * @param string $prefix Interwiki prefix to use
71 * @return Interwiki|null|bool
73 public function fetch( $prefix ) {
74 if ( $prefix == '' ) {
78 if ( !$this->isValidInterwiki( $prefix ) ) {
82 return $this->interwikiMap
[$prefix];
86 * See InterwikiLookup::getAllPrefixes
88 * @param string|null $local If set, limits output to local/non-local interwikis
89 * @return array[] interwiki rows
91 public function getAllPrefixes( $local = null ) {
93 foreach ( $this->getInterwikiMap() as $interwikiId => $interwiki ) {
94 if ( $local === null ||
$interwiki->isLocal() === $local ) {
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(),
109 * See InterwikiLookup::invalidateCache
111 * @param string $prefix
113 public function invalidateCache( $prefix ) {
114 if ( !isset( $this->interwikiMap
[$prefix] ) ) {
117 $globalId = $this->interwikiMap
[$prefix]->getWikiID();
118 unset( $this->interwikiMap
[$prefix] );
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] ] );
127 * Load interwiki map to use as cache
129 private function loadInterwikiMap() {
131 $siteList = $this->siteLookup
->getSites();
132 foreach ( $siteList as $site ) {
133 $interwikis = $this->getSiteInterwikis( $site );
134 $interwikiMap = array_merge( $interwikiMap, $interwikis );
136 $this->interwikiMap
= $interwikiMap;
140 * Get interwikiMap attribute, load if needed.
142 * @return Interwiki[]
144 private function getInterwikiMap() {
145 if ( $this->interwikiMap
=== null ) {
146 $this->loadInterwikiMap();
148 return $this->interwikiMap
;
152 * Load interwikis for the given site
155 * @return Interwiki[]
157 private function getSiteInterwikis( Site
$site ) {
159 foreach ( $site->getInterwikiIds() as $interwiki ) {
160 $url = $site->getPageUrl();
161 if ( $site instanceof MediaWikiSite
) {
162 $path = $site->getFileUrl( 'api.php' );
166 $local = $site->getSource() === 'local';
167 // TODO: How to adapt trans?
168 $interwikis[$interwiki] = new Interwiki(
172 $site->getGlobalId(),