MediaWiki master
InterwikiLookupAdapter.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Interwiki;
8
12
20
24 private $siteLookup;
25
29 private $interwikiMap;
30
31 public function __construct(
32 SiteLookup $siteLookup,
33 ?array $interwikiMap = null
34 ) {
35 $this->siteLookup = $siteLookup;
36 $this->interwikiMap = $interwikiMap;
37 }
38
46 public function isValidInterwiki( $prefix ) {
47 return array_key_exists( $prefix, $this->getInterwikiMap() );
48 }
49
57 public function fetch( $prefix ) {
58 if ( $prefix == '' ) {
59 return null;
60 }
61
62 if ( !$this->isValidInterwiki( $prefix ) ) {
63 return false;
64 }
65
66 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
67 return $this->interwikiMap[$prefix];
68 }
69
76 public function getAllPrefixes( $local = null ) {
77 $res = [];
78 foreach ( $this->getInterwikiMap() as $interwikiId => $interwiki ) {
79 if ( $local === null || $interwiki->isLocal() === $local ) {
80 $res[] = [
81 'iw_prefix' => $interwikiId,
82 'iw_url' => $interwiki->getURL(),
83 'iw_api' => $interwiki->getAPI(),
84 'iw_wikiid' => $interwiki->getWikiID(),
85 'iw_local' => $interwiki->isLocal(),
86 'iw_trans' => $interwiki->isTranscludable(),
87 ];
88 }
89 }
90 return $res;
91 }
92
98 public function invalidateCache( $prefix ) {
99 if ( !isset( $this->interwikiMap[$prefix] ) ) {
100 return;
101 }
102 $globalId = $this->interwikiMap[$prefix]->getWikiID();
103 unset( $this->interwikiMap[$prefix] );
104
105 // Reload the interwiki
106 $site = $this->siteLookup->getSites()->getSite( $globalId );
107 $interwikis = $this->getSiteInterwikis( $site );
108 $this->interwikiMap[$prefix] = $interwikis[$prefix];
109 }
110
114 private function loadInterwikiMap() {
115 $interwikiMap = [];
116 $siteList = $this->siteLookup->getSites();
117 foreach ( $siteList as $site ) {
118 $interwikis = $this->getSiteInterwikis( $site );
119 $interwikiMap = array_merge( $interwikiMap, $interwikis );
120 }
121 $this->interwikiMap = $interwikiMap;
122 }
123
129 private function getInterwikiMap(): array {
130 if ( $this->interwikiMap === null ) {
131 $this->loadInterwikiMap();
132 }
133 return $this->interwikiMap;
134 }
135
142 private function getSiteInterwikis( Site $site ): array {
143 $url = $site->getPageUrl();
144 if ( $site instanceof MediaWikiSite ) {
145 $path = $site->getFileUrl( 'api.php' );
146 } else {
147 $path = '';
148 }
149 $local = $site->getSource() === 'local';
150
151 $interwikis = [];
152 foreach ( $site->getInterwikiIds() as $interwiki ) {
153 // TODO: How to adapt trans?
154 $interwikis[$interwiki] = new Interwiki(
155 $interwiki,
156 $url,
157 $path,
158 $site->getGlobalId(),
159 $local
160 );
161 }
162 return $interwikis;
163 }
164}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:71
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)
Class representing a MediaWiki site.
Represents a single site.
Definition Site.php:22
Service interface for looking up Interwiki records.
Interface to retrieve Site objects, for implementation by service classes.