MediaWiki master
ClassicInterwikiLookup.php
Go to the documentation of this file.
1<?php
8
13use MediaWiki\Languages\LanguageNameUtils;
19
37 public const CONSTRUCTOR_OPTIONS = [
44 'wikiId',
45 ];
46
47 private ServiceOptions $options;
48 private Language $contLang;
49 private WANObjectCache $wanCache;
50 private HookRunner $hookRunner;
51 private IConnectionProvider $dbProvider;
52 private LanguageNameUtils $languageNameUtils;
53
55 private MapCacheLRU $instances;
62 private int $interwikiScopes;
64 private ?array $data;
65 private string $wikiId;
66 private ?string $thisSite = null;
67 private array $virtualDomainsMapping;
68
77 public function __construct(
78 ServiceOptions $options,
79 Language $contLang,
80 WANObjectCache $wanCache,
81 HookContainer $hookContainer,
82 IConnectionProvider $dbProvider,
83 LanguageNameUtils $languageNameUtils
84 ) {
85 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
86 $this->options = $options;
87
88 $this->contLang = $contLang;
89 $this->wanCache = $wanCache;
90 $this->hookRunner = new HookRunner( $hookContainer );
91 $this->dbProvider = $dbProvider;
92 $this->languageNameUtils = $languageNameUtils;
93
94 $this->instances = new MapCacheLRU( 1000 );
95 $this->interwikiScopes = $options->get( MainConfigNames::InterwikiScopes );
96
97 $interwikiData = $options->get( MainConfigNames::InterwikiCache );
98 $this->data = is_array( $interwikiData ) ? $interwikiData : null;
99 $this->wikiId = $options->get( 'wikiId' );
100 $this->virtualDomainsMapping = $options->get( MainConfigNames::VirtualDomainsMapping ) ?? [];
101 }
102
108 public function isValidInterwiki( $prefix ) {
109 $iw = $this->fetch( $prefix );
110 return (bool)$iw;
111 }
112
118 public function fetch( $prefix ) {
119 if ( $prefix === null || $prefix === '' ) {
120 return null;
121 }
122
123 $prefix = $this->contLang->lc( $prefix );
124
125 return $this->instances->getWithSetCallback(
126 $prefix,
127 function () use ( $prefix ) {
128 return $this->load( $prefix );
129 }
130 );
131 }
132
142 public function invalidateCache( $prefix ) {
143 $this->instances->clear( $prefix );
144
145 $key = $this->wanCache->makeKey( 'interwiki', $prefix );
146 $this->wanCache->delete( $key );
147 }
148
155 private function getPregenValue( string $prefix ) {
156 // Lazily resolve site name
157 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
158 $this->thisSite = $this->data['__sites:' . $this->wikiId]
159 ?? $this->options->get( MainConfigNames::InterwikiFallbackSite );
160 }
161
162 $value = $this->data[$this->wikiId . ':' . $prefix] ?? false;
163 // Site level
164 if ( $value === false && $this->interwikiScopes >= 3 ) {
165 $value = $this->data["_{$this->thisSite}:{$prefix}"] ?? false;
166 }
167 // Global level
168 if ( $value === false && $this->interwikiScopes >= 2 ) {
169 $value = $this->data["__global:{$prefix}"] ?? false;
170 }
171
172 return $value;
173 }
174
184 private function load( $prefix ) {
185 if ( $this->data !== null ) {
186 $value = $this->getPregenValue( $prefix );
187 return $value ? $this->makeFromPregen( $prefix, $value ) : false;
188 }
189
190 $iwData = [];
191 $abort = !$this->hookRunner->onInterwikiLoadPrefix( $prefix, $iwData );
192 if ( isset( $iwData['iw_url'] ) ) {
193 // Hook provided data
194 return $this->makeFromRow( $iwData );
195 }
196 if ( $abort ) {
197 // Hook indicated no other source may be considered
198 return false;
199 }
200
201 $iwData = $this->wanCache->getWithSetCallback(
202 $this->wanCache->makeKey( 'interwiki', $prefix ),
203 $this->options->get( MainConfigNames::InterwikiExpiry ),
204 function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix ) {
205 // Global interlanguage link
206 if ( $this->options->get( MainConfigNames::InterwikiMagic )
207 && $this->languageNameUtils->getLanguageName( $prefix )
208 ) {
209 $row = $this->loadFromDB( $prefix, 'virtual-interwiki-interlanguage' );
210 } else {
211 // Local interwiki link
212 $row = $this->loadFromDB( $prefix );
213
214 // Global interwiki link (not interlanguage)
215 if ( !$row && isset( $this->virtualDomainsMapping['virtual-interwiki'] ) ) {
216 $row = $this->loadFromDB( $prefix, 'virtual-interwiki' );
217 }
218 }
219
220 return $row ? (array)$row : '!NONEXISTENT';
221 }
222 );
223
224 // Handle non-existent case
225 return is_array( $iwData ) ? $this->makeFromRow( $iwData ) : false;
226 }
227
235 private function loadFromDB( $prefix, $domain = false ) {
236 $dbr = $this->dbProvider->getReplicaDatabase( $domain );
237 $row = $dbr->newSelectQueryBuilder()
238 ->select( self::selectFields() )
239 ->from( 'interwiki' )
240 ->where( [ 'iw_prefix' => $prefix ] )
241 ->caller( __METHOD__ )->fetchRow();
242
243 return $row;
244 }
245
250 private function makeFromRow( array $row ) {
251 $url = $row['iw_url'];
252 $local = $row['iw_local'] ?? 0;
253 $trans = $row['iw_trans'] ?? 0;
254 $api = $row['iw_api'] ?? '';
255 $wikiId = $row['iw_wikiid'] ?? '';
256
257 return new Interwiki( null, $url, $api, $wikiId, $local, $trans );
258 }
259
265 private function makeFromPregen( string $prefix, string $value ) {
266 // Split values
267 [ $local, $url ] = explode( ' ', $value, 2 );
268 return new Interwiki( $prefix, $url, '', '', (int)$local );
269 }
270
277 private function getAllPrefixesPregenerated( $local ) {
278 // Lazily resolve site name
279 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
280 $this->thisSite = $this->data['__sites:' . $this->wikiId]
281 ?? $this->options->get( MainConfigNames::InterwikiFallbackSite );
282 }
283
284 // List of interwiki sources
285 $sources = [];
286 // Global level
287 if ( $this->interwikiScopes >= 2 ) {
288 $sources[] = '__global';
289 }
290 // Site level
291 if ( $this->interwikiScopes >= 3 ) {
292 $sources[] = '_' . $this->thisSite;
293 }
294 $sources[] = $this->wikiId;
295
296 $data = [];
297 foreach ( $sources as $source ) {
298 $list = $this->data['__list:' . $source] ?? '';
299 foreach ( explode( ' ', $list ) as $iw_prefix ) {
300 $row = $this->data["{$source}:{$iw_prefix}"] ?? null;
301 if ( !$row ) {
302 continue;
303 }
304
305 [ $iw_local, $iw_url ] = explode( ' ', $row );
306
307 if ( $local !== null && $local != $iw_local ) {
308 continue;
309 }
310
311 $data[$iw_prefix] = [
312 'iw_prefix' => $iw_prefix,
313 'iw_url' => $iw_url,
314 'iw_local' => $iw_local,
315 ];
316 }
317 }
318
319 return array_values( $data );
320 }
321
337 public static function buildCdbHash(
338 array $allPrefixes, int $scope = 1, ?string $thisSite = null
339 ): array {
340 $result = [];
341 $wikiId = WikiMap::getCurrentWikiId();
342 $keyPrefix = ( $scope >= 2 ) ? '__global' : $wikiId;
343 if ( $scope >= 3 && $thisSite ) {
344 $result[ "__sites:$wikiId" ] = $thisSite;
345 $keyPrefix = "_$thisSite";
346 }
347 $list = [];
348 foreach ( $allPrefixes as $iwInfo ) {
349 $prefix = $iwInfo['iw_prefix'];
350 $result["$keyPrefix:$prefix"] = implode( ' ', [
351 $iwInfo['iw_local'] ?? 0, $iwInfo['iw_url']
352 ] );
353 $list[] = $prefix;
354 }
355 $result["__list:$keyPrefix"] = implode( ' ', $list );
356 $result["__list:__sites"] = $wikiId;
357 return $result;
358 }
359
366 private function getAllPrefixesDB( $local ) {
367 $where = [];
368 if ( $local !== null ) {
369 $where['iw_local'] = (int)$local;
370 }
371
372 $dbr = $this->dbProvider->getReplicaDatabase();
373 $res = $dbr->newSelectQueryBuilder()
374 ->select( self::selectFields() )
375 ->from( 'interwiki' )
376 ->where( $where )
377 ->orderBy( 'iw_prefix' )
378 ->caller( __METHOD__ )->fetchResultSet();
379
380 $retval = [];
381 foreach ( $res as $row ) {
382 $retval[] = (array)$row;
383 }
384 return $retval;
385 }
386
393 public function getAllPrefixes( $local = null ) {
394 if ( $this->data !== null ) {
395 return $this->getAllPrefixesPregenerated( $local );
396 } else {
397 return $this->getAllPrefixesDB( $local );
398 }
399 }
400
406 private static function selectFields() {
407 return [
408 'iw_prefix',
409 'iw_url',
410 'iw_api',
411 'iw_wikiid',
412 'iw_local',
413 'iw_trans'
414 ];
415 }
416
417}
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
InterwikiLookup backed by the interwiki database table or $wgInterwikiCache.
__construct(ServiceOptions $options, Language $contLang, WANObjectCache $wanCache, HookContainer $hookContainer, IConnectionProvider $dbProvider, LanguageNameUtils $languageNameUtils)
fetch( $prefix)
Get the Interwiki object for a given prefix.Interwiki|null|false Null for invalid,...
invalidateCache( $prefix)
Purge the instance cache and memcached for an interwiki prefix.
getAllPrefixes( $local=null)
Fetch all interwiki data.
isValidInterwiki( $prefix)
Check whether an interwiki prefix exists.bool Whether it exists
static buildCdbHash(array $allPrefixes, int $scope=1, ?string $thisSite=null)
Build an array in the format accepted by $wgInterwikiCache.
Base class for language-specific code.
Definition Language.php:70
A class containing constants representing the names of configuration variables.
const InterwikiCache
Name constant for the InterwikiCache setting, for use with Config::get()
const InterwikiScopes
Name constant for the InterwikiScopes setting, for use with Config::get()
const InterwikiMagic
Name constant for the InterwikiMagic setting, for use with Config::get()
const InterwikiFallbackSite
Name constant for the InterwikiFallbackSite setting, for use with Config::get()
const InterwikiExpiry
Name constant for the InterwikiExpiry setting, for use with Config::get()
const VirtualDomainsMapping
Name constant for the VirtualDomainsMapping setting, for use with Config::get()
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:19
Store key-value entries in a size-limited in-memory LRU cache.
Multi-datacenter aware caching interface.
Service interface for looking up Interwiki records.
Provide primary and replica IDatabase connections.
$source