MediaWiki  master
InterwikiLookupAdapter.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Interwiki;
22 
23 use Interwiki;
24 use MediaWikiSite;
25 use Site;
26 use SiteLookup;
27 
35 
39  private $siteLookup;
40 
44  private $interwikiMap;
45 
46  public function __construct(
47  SiteLookup $siteLookup,
48  array $interwikiMap = null
49  ) {
50  $this->siteLookup = $siteLookup;
51  $this->interwikiMap = $interwikiMap;
52  }
53 
61  public function isValidInterwiki( $prefix ) {
62  return array_key_exists( $prefix, $this->getInterwikiMap() );
63  }
64 
72  public function fetch( $prefix ) {
73  if ( $prefix == '' ) {
74  return null;
75  }
76 
77  if ( !$this->isValidInterwiki( $prefix ) ) {
78  return false;
79  }
80 
81  // @phan-suppress-next-line PhanTypeArraySuspiciousNullable
82  return $this->interwikiMap[$prefix];
83  }
84 
91  public function getAllPrefixes( $local = null ) {
92  $res = [];
93  foreach ( $this->getInterwikiMap() as $interwikiId => $interwiki ) {
94  if ( $local === null || $interwiki->isLocal() === $local ) {
95  $res[] = [
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(),
102  ];
103  }
104  }
105  return $res;
106  }
107 
113  public function invalidateCache( $prefix ) {
114  if ( !isset( $this->interwikiMap[$prefix] ) ) {
115  return;
116  }
117  $globalId = $this->interwikiMap[$prefix]->getWikiID();
118  unset( $this->interwikiMap[$prefix] );
119 
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] ] );
124  }
125 
129  private function loadInterwikiMap() {
130  $interwikiMap = [];
131  $siteList = $this->siteLookup->getSites();
132  foreach ( $siteList as $site ) {
133  $interwikis = $this->getSiteInterwikis( $site );
134  $interwikiMap = array_merge( $interwikiMap, $interwikis );
135  }
136  $this->interwikiMap = $interwikiMap;
137  }
138 
144  private function getInterwikiMap() {
145  if ( $this->interwikiMap === null ) {
146  $this->loadInterwikiMap();
147  }
148  return $this->interwikiMap;
149  }
150 
157  private function getSiteInterwikis( Site $site ) {
158  $url = $site->getPageUrl();
159  if ( $site instanceof MediaWikiSite ) {
160  $path = $site->getFileUrl( 'api.php' );
161  } else {
162  $path = '';
163  }
164  $local = $site->getSource() === 'local';
165 
166  $interwikis = [];
167  foreach ( $site->getInterwikiIds() as $interwiki ) {
168  // TODO: How to adapt trans?
169  $interwikis[$interwiki] = new Interwiki(
170  $interwiki,
171  $url,
172  $path,
173  $site->getGlobalId(),
174  $local
175  );
176  }
177  return $interwikis;
178  }
179 }
An interwiki record value object.
Definition: Interwiki.php:27
Class representing a MediaWiki site.
InterwikiLookupAdapter on top of SiteLookup.
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.
Represents a single site.
Definition: Site.php:32
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:196
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:143
getPageUrl( $pageName=false)
Get the full URL for the given page on the site.
Definition: Site.php:346
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition: Site.php:529
Service interface for looking up Interwiki records.
Interface to retrieve Site objects, for implementation by service classes.
Definition: SiteLookup.php:29