MediaWiki  master
InterwikiLookupAdapter.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Interwiki;
4 
32 use Site;
35 
37 
41  private $siteLookup;
42 
46  private $interwikiMap;
47 
48  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  return $this->interwikiMap[$prefix];
84  }
85 
92  public function getAllPrefixes( $local = null ) {
93  $res = [];
94  foreach ( $this->getInterwikiMap() as $interwikiId => $interwiki ) {
95  if ( $local === null || $interwiki->isLocal() === $local ) {
96  $res[] = [
97  'iw_prefix' => $interwikiId,
98  'iw_url' => $interwiki->getURL(),
99  'iw_api' => $interwiki->getAPI(),
100  'iw_wikiid' => $interwiki->getWikiID(),
101  'iw_local' => $interwiki->isLocal(),
102  'iw_trans' => $interwiki->isTranscludable(),
103  ];
104  }
105  }
106  return $res;
107  }
108 
114  public function invalidateCache( $prefix ) {
115  if ( !isset( $this->interwikiMap[$prefix] ) ) {
116  return;
117  }
118  $globalId = $this->interwikiMap[$prefix]->getWikiID();
119  unset( $this->interwikiMap[$prefix] );
120 
121  // Reload the interwiki
122  $site = $this->siteLookup->getSites()->getSite( $globalId );
123  $interwikis = $this->getSiteInterwikis( $site );
124  $this->interwikiMap = array_merge( $this->interwikiMap, [ $interwikis[$prefix] ] );
125  }
126 
130  private function loadInterwikiMap() {
131  $interwikiMap = [];
132  $siteList = $this->siteLookup->getSites();
133  foreach ( $siteList as $site ) {
134  $interwikis = $this->getSiteInterwikis( $site );
135  $interwikiMap = array_merge( $interwikiMap, $interwikis );
136  }
137  $this->interwikiMap = $interwikiMap;
138  }
139 
145  private function getInterwikiMap() {
146  if ( $this->interwikiMap === null ) {
147  $this->loadInterwikiMap();
148  }
149  return $this->interwikiMap;
150  }
151 
158  private function getSiteInterwikis( Site $site ) {
159  $interwikis = [];
160  foreach ( $site->getInterwikiIds() as $interwiki ) {
161  $url = $site->getPageUrl();
162  if ( $site instanceof MediaWikiSite ) {
163  $path = $site->getFileUrl( 'api.php' );
164  } else {
165  $path = '';
166  }
167  $local = $site->getSource() === 'local';
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 }
fetch( $prefix)
See InterwikiLookup::fetch It loads the whole interwiki map.
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition: Site.php:546
Interwiki [] null $interwikiMap
associative array mapping interwiki prefixes to Interwiki objects
getPageUrl( $pageName=false)
Returns the full URL for the given page on the site.
Definition: Site.php:370
getAllPrefixes( $local=null)
See InterwikiLookup::getAllPrefixes.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
loadInterwikiMap()
Load interwiki map to use as cache.
getSource()
Returns the source of the site data (ie &#39;local&#39;, &#39;wikidata&#39;, &#39;my-magical-repo&#39;).
Definition: Site.php:207
getSiteInterwikis(Site $site)
Load interwikis for the given site.
__construct(SiteLookup $siteLookup, array $interwikiMap=null)
getInterwikiMap()
Get interwikiMap attribute, load if needed.
$res
Definition: database.txt:21
invalidateCache( $prefix)
See InterwikiLookup::invalidateCache.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
Service interface for looking up Interwiki records.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Value object for representing interwiki records.
Definition: Interwiki.php:27
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Copyright (C) 2018 Kunal Mehta legoktm@member.fsf.org
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:140
Definition: Site.php:29
isValidInterwiki( $prefix)
See InterwikiLookup::isValidInterwiki It loads the whole interwiki map.