MediaWiki master
InterwikiLookupAdapter.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Interwiki;
22
26
34
38 private $siteLookup;
39
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
60 public function isValidInterwiki( $prefix ) {
61 return array_key_exists( $prefix, $this->getInterwikiMap() );
62 }
63
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
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
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
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
143 private function getInterwikiMap() {
144 if ( $this->interwikiMap === null ) {
145 $this->loadInterwikiMap();
146 }
147 return $this->interwikiMap;
148 }
149
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}
InterwikiLookupAdapter on top of SiteLookup.
fetch( $prefix)
See InterwikiLookup::fetch It loads the whole interwiki map.
getAllPrefixes( $local=null)
See InterwikiLookup::getAllPrefixes.
isValidInterwiki( $prefix)
See InterwikiLookup::isValidInterwiki It loads the whole interwiki map.
invalidateCache( $prefix)
See InterwikiLookup::invalidateCache.
__construct(SiteLookup $siteLookup, ?array $interwikiMap=null)
An interwiki record value object.
Definition Interwiki.php:29
Class representing a MediaWiki site.
Represents a single site.
Definition Site.php:36
Service interface for looking up Interwiki records.
Interface to retrieve Site objects, for implementation by service classes.