MediaWiki  1.30.0
InterwikiLookupAdapter.php
Go to the documentation of this file.
1 <?php
2 namespace MediaWiki\Interwiki;
3 
31 use Site;
34 
36 
40  private $siteLookup;
41 
45  private $interwikiMap;
46 
47  function __construct(
49  array $interwikiMap = null
50  ) {
51  $this->siteLookup = $siteLookup;
52  $this->interwikiMap = $interwikiMap;
53  }
54 
62  public function isValidInterwiki( $prefix ) {
63  return array_key_exists( $prefix, $this->getInterwikiMap() );
64  }
65 
73  public function fetch( $prefix ) {
74  if ( $prefix == '' ) {
75  return null;
76  }
77 
78  if ( !$this->isValidInterwiki( $prefix ) ) {
79  return false;
80  }
81 
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  $interwikis = [];
159  foreach ( $site->getInterwikiIds() as $interwiki ) {
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  // TODO: How to adapt trans?
168  $interwikis[$interwiki] = new Interwiki(
169  $interwiki,
170  $url,
171  $path,
172  $site->getGlobalId(),
173  $local
174  );
175  }
176  return $interwikis;
177  }
178 }
Site\getInterwikiIds
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition: Site.php:544
Site\getPageUrl
getPageUrl( $pageName=false)
Returns the full URL for the given page on the site.
Definition: Site.php:370
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWiki\Interwiki\InterwikiLookupAdapter
Definition: InterwikiLookupAdapter.php:35
$res
$res
Definition: database.txt:21
SiteLookup
Definition: SiteLookup.php:28
php
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
MediaWiki\Interwiki\InterwikiLookupAdapter\invalidateCache
invalidateCache( $prefix)
See InterwikiLookup::invalidateCache.
Definition: InterwikiLookupAdapter.php:113
Site\getGlobalId
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:140
Site\getSource
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:207
MediaWiki\Interwiki\InterwikiLookup
Service interface for looking up Interwiki records.
Definition: InterwikiLookup.php:31
MediaWiki\Interwiki\InterwikiLookupAdapter\getAllPrefixes
getAllPrefixes( $local=null)
See InterwikiLookup::getAllPrefixes.
Definition: InterwikiLookupAdapter.php:91
MediaWiki\Interwiki\InterwikiLookupAdapter\$siteLookup
SiteLookup $siteLookup
Definition: InterwikiLookupAdapter.php:40
Site
Definition: Site.php:29
MediaWiki\Interwiki\InterwikiLookupAdapter\fetch
fetch( $prefix)
See InterwikiLookup::fetch It loads the whole interwiki map.
Definition: InterwikiLookupAdapter.php:73
MediaWiki\Interwiki\InterwikiLookupAdapter\__construct
__construct(SiteLookup $siteLookup, array $interwikiMap=null)
Definition: InterwikiLookupAdapter.php:47
MediaWiki\Interwiki\InterwikiLookupAdapter\getSiteInterwikis
getSiteInterwikis(Site $site)
Load interwikis for the given site.
Definition: InterwikiLookupAdapter.php:157
MediaWiki\Interwiki\InterwikiLookupAdapter\$interwikiMap
Interwiki[] null $interwikiMap
associative array mapping interwiki prefixes to Interwiki objects
Definition: InterwikiLookupAdapter.php:45
MediaWiki\Interwiki\InterwikiLookupAdapter\getInterwikiMap
getInterwikiMap()
Get interwikiMap attribute, load if needed.
Definition: InterwikiLookupAdapter.php:144
MediaWiki\Interwiki\InterwikiLookupAdapter\loadInterwikiMap
loadInterwikiMap()
Load interwiki map to use as cache.
Definition: InterwikiLookupAdapter.php:129
MediaWiki\Interwiki\InterwikiLookupAdapter\isValidInterwiki
isValidInterwiki( $prefix)
See InterwikiLookup::isValidInterwiki It loads the whole interwiki map.
Definition: InterwikiLookupAdapter.php:62
$path
$path
Definition: NoLocalSettings.php:26
Interwiki
Value object for representing interwiki records.
Definition: Interwiki.php:27
as
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
MediaWiki\Interwiki
Definition: ClassicInterwikiLookup.php:2
MediaWikiSite
Class representing a MediaWiki site.
Definition: MediaWikiSite.php:38
array
the array() calling protocol came about after MediaWiki 1.4rc1.