MediaWiki  1.29.2
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 
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  if ( $local === null ) {
94  return array_keys( $this->getInterwikiMap() );
95  }
96  $res = [];
97  foreach ( $this->getInterwikiMap() as $interwikiId => $interwiki ) {
98  if ( $interwiki->isLocal() === $local ) {
99  $res[] = $interwikiId;
100  }
101  }
102  return $res;
103  }
104 
110  public function invalidateCache( $prefix ) {
111  if ( !isset( $this->interwikiMap[$prefix] ) ) {
112  return;
113  }
114  $globalId = $this->interwikiMap[$prefix]->getWikiID();
115  unset( $this->interwikiMap[$prefix] );
116 
117  // Reload the interwiki
118  $site = $this->siteLookup->getSites()->getSite( $globalId );
119  $interwikis = $this->getSiteInterwikis( $site );
120  $this->interwikiMap = array_merge( $this->interwikiMap, [ $interwikis[$prefix] ] );
121  }
122 
126  private function loadInterwikiMap() {
127  $interwikiMap = [];
128  $siteList = $this->siteLookup->getSites();
129  foreach ( $siteList as $site ) {
130  $interwikis = $this->getSiteInterwikis( $site );
131  $interwikiMap = array_merge( $interwikiMap, $interwikis );
132  }
133  $this->interwikiMap = $interwikiMap;
134  }
135 
141  private function getInterwikiMap() {
142  if ( $this->interwikiMap === null ) {
143  $this->loadInterwikiMap();
144  }
145  return $this->interwikiMap;
146  }
147 
154  private function getSiteInterwikis( Site $site ) {
155  $interwikis = [];
156  foreach ( $site->getInterwikiIds() as $interwiki ) {
157  $url = $site->getPageUrl();
158  if ( $site instanceof MediaWikiSite ) {
159  $path = $site->getFileUrl( 'api.php' );
160  } else {
161  $path = '';
162  }
163  $local = $site->getSource() === 'local';
164  // TODO: How to adapt trans?
165  $interwikis[$interwiki] = new Interwiki(
166  $interwiki,
167  $url,
168  $path,
169  $site->getGlobalId(),
170  $local
171  );
172  }
173  return $interwikis;
174  }
175 }
Site\getInterwikiIds
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition: Site.php:546
Site\getPageUrl
getPageUrl( $pageName=false)
Returns the full URL for the given page on the site.
Definition: Site.php:372
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:110
Site\getGlobalId
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:142
Site\getSource
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition: Site.php:209
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:92
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:74
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:154
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:141
MediaWiki\Interwiki\InterwikiLookupAdapter\loadInterwikiMap
loadInterwikiMap()
Load interwiki map to use as cache.
Definition: InterwikiLookupAdapter.php:126
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.