MediaWiki REL1_35
InterwikiLookupAdapter.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Interwiki;
4
31use Interwiki;
33use Site;
34use SiteLookup;
35
37
41 private $siteLookup;
42
47
48 public function __construct(
50 array $interwikiMap = null
51 ) {
52 $this->siteLookup = $siteLookup;
53 $this->interwikiMap = $interwikiMap;
54 }
55
63 public function isValidInterwiki( $prefix ) {
64 return array_key_exists( $prefix, $this->getInterwikiMap() );
65 }
66
74 public function fetch( $prefix ) {
75 if ( $prefix == '' ) {
76 return null;
77 }
78
79 if ( !$this->isValidInterwiki( $prefix ) ) {
80 return false;
81 }
82
83 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
84 return $this->interwikiMap[$prefix];
85 }
86
93 public function getAllPrefixes( $local = null ) {
94 $res = [];
95 foreach ( $this->getInterwikiMap() as $interwikiId => $interwiki ) {
96 if ( $local === null || $interwiki->isLocal() === $local ) {
97 $res[] = [
98 'iw_prefix' => $interwikiId,
99 'iw_url' => $interwiki->getURL(),
100 'iw_api' => $interwiki->getAPI(),
101 'iw_wikiid' => $interwiki->getWikiID(),
102 'iw_local' => $interwiki->isLocal(),
103 'iw_trans' => $interwiki->isTranscludable(),
104 ];
105 }
106 }
107 return $res;
108 }
109
115 public function invalidateCache( $prefix ) {
116 if ( !isset( $this->interwikiMap[$prefix] ) ) {
117 return;
118 }
119 $globalId = $this->interwikiMap[$prefix]->getWikiID();
120 unset( $this->interwikiMap[$prefix] );
121
122 // Reload the interwiki
123 $site = $this->siteLookup->getSites()->getSite( $globalId );
124 $interwikis = $this->getSiteInterwikis( $site );
125 $this->interwikiMap = array_merge( $this->interwikiMap, [ $interwikis[$prefix] ] );
126 }
127
131 private function loadInterwikiMap() {
132 $interwikiMap = [];
133 $siteList = $this->siteLookup->getSites();
134 foreach ( $siteList as $site ) {
135 $interwikis = $this->getSiteInterwikis( $site );
136 $interwikiMap = array_merge( $interwikiMap, $interwikis );
137 }
138 $this->interwikiMap = $interwikiMap;
139 }
140
146 private function getInterwikiMap() {
147 if ( $this->interwikiMap === null ) {
148 $this->loadInterwikiMap();
149 }
150 return $this->interwikiMap;
151 }
152
159 private function getSiteInterwikis( Site $site ) {
160 $interwikis = [];
161 foreach ( $site->getInterwikiIds() as $interwiki ) {
162 $url = $site->getPageUrl();
163 if ( $site instanceof MediaWikiSite ) {
164 $path = $site->getFileUrl( 'api.php' );
165 } else {
166 $path = '';
167 }
168 $local = $site->getSource() === 'local';
169 // TODO: How to adapt trans?
170 $interwikis[$interwiki] = new Interwiki(
171 $interwiki,
172 $url,
173 $path,
174 $site->getGlobalId(),
175 $local
176 );
177 }
178 return $interwikis;
179 }
180}
Value object for representing interwiki records.
Definition Interwiki.php:26
Class representing a MediaWiki site.
Interwiki[] null $interwikiMap
associative array mapping interwiki prefixes to Interwiki objects
getSiteInterwikis(Site $site)
Load interwikis for the given site.
fetch( $prefix)
See InterwikiLookup::fetch It loads the whole interwiki map.
getAllPrefixes( $local=null)
See InterwikiLookup::getAllPrefixes.
__construct(SiteLookup $siteLookup, array $interwikiMap=null)
loadInterwikiMap()
Load interwiki map to use as cache.
isValidInterwiki( $prefix)
See InterwikiLookup::isValidInterwiki It loads the whole interwiki map.
invalidateCache( $prefix)
See InterwikiLookup::invalidateCache.
getInterwikiMap()
Get interwikiMap attribute, load if needed.
Definition Site.php:31
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition Site.php:209
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition Site.php:142
getPageUrl( $pageName=false)
Returns the full URL for the given page on the site.
Definition Site.php:378
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition Site.php:558
Service interface for looking up Interwiki records.
Copyright (C) 2018 Kunal Mehta legoktm@member.fsf.org