MediaWiki REL1_31
InterwikiLookupAdapter.php
Go to the documentation of this file.
1<?php
2namespace MediaWiki\Interwiki;
3
31use Site;
34
36
40 private $siteLookup;
41
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}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Value object for representing interwiki records.
Definition Interwiki.php:27
Class representing a MediaWiki site.
Interwiki[] null $interwikiMap
associative array mapping interwiki prefixes to Interwiki objects
getSiteInterwikis(Site $site)
Load interwikis for the given site.
fetch( $prefix)
See InterwikiLookup::fetch It loads the whole interwiki map.
getAllPrefixes( $local=null)
See InterwikiLookup::getAllPrefixes.
__construct(SiteLookup $siteLookup, array $interwikiMap=null)
loadInterwikiMap()
Load interwiki map to use as cache.
isValidInterwiki( $prefix)
See InterwikiLookup::isValidInterwiki It loads the whole interwiki map.
invalidateCache( $prefix)
See InterwikiLookup::invalidateCache.
getInterwikiMap()
Get interwikiMap attribute, load if needed.
Definition Site.php:29
getSource()
Returns the source of the site data (ie 'local', 'wikidata', 'my-magical-repo').
Definition Site.php:207
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition Site.php:140
getPageUrl( $pageName=false)
Returns the full URL for the given page on the site.
Definition Site.php:370
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition Site.php:546
$res
Definition database.txt:21
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
the array() calling protocol came about after MediaWiki 1.4rc1.
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:37
Service interface for looking up Interwiki records.
Copyright (C) 2018 Kunal Mehta legoktm@member.fsf.org