MediaWiki REL1_39
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
46 private $interwikiMap;
47
48 public function __construct(
49 SiteLookup $siteLookup,
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 $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
168 $interwikis = [];
169 foreach ( $site->getInterwikiIds() as $interwiki ) {
170 // TODO: How to adapt trans?
171 $interwikis[$interwiki] = new Interwiki(
172 $interwiki,
173 $url,
174 $path,
175 $site->getGlobalId(),
176 $local
177 );
178 }
179 return $interwikis;
180 }
181}
Value object for representing interwiki records.
Definition Interwiki.php:26
Class representing a MediaWiki site.
fetch( $prefix)
See InterwikiLookup::fetch It loads the whole interwiki map.
getAllPrefixes( $local=null)
See InterwikiLookup::getAllPrefixes.
__construct(SiteLookup $siteLookup, array $interwikiMap=null)
isValidInterwiki( $prefix)
See InterwikiLookup::isValidInterwiki It loads the whole interwiki map.
invalidateCache( $prefix)
See InterwikiLookup::invalidateCache.
Definition Site.php:33
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition Site.php:211
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition Site.php:144
getPageUrl( $pageName=false)
Returns the full URL for the given page on the site.
Definition Site.php:380
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition Site.php:563
Service interface for looking up Interwiki records.
Copyright (C) 2018 Kunal Mehta legoktm@debian.org