MediaWiki master
ClassicInterwikiLookup.php
Go to the documentation of this file.
1<?php
22
23use Interwiki;
24use MapCacheLRU;
34
52 public const CONSTRUCTOR_OPTIONS = [
59 'wikiId',
60 ];
61
62 private ServiceOptions $options;
63 private Language $contLang;
64 private WANObjectCache $wanCache;
65 private HookRunner $hookRunner;
66 private IConnectionProvider $dbProvider;
67 private LanguageNameUtils $languageNameUtils;
68
70 private MapCacheLRU $instances;
77 private int $interwikiScopes;
79 private ?array $data;
80 private string $wikiId;
81 private ?string $thisSite = null;
82 private array $virtualDomainsMapping;
83
92 public function __construct(
93 ServiceOptions $options,
94 Language $contLang,
95 WANObjectCache $wanCache,
96 HookContainer $hookContainer,
97 IConnectionProvider $dbProvider,
98 LanguageNameUtils $languageNameUtils
99 ) {
100 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
101 $this->options = $options;
102
103 $this->contLang = $contLang;
104 $this->wanCache = $wanCache;
105 $this->hookRunner = new HookRunner( $hookContainer );
106 $this->dbProvider = $dbProvider;
107 $this->languageNameUtils = $languageNameUtils;
108
109 $this->instances = new MapCacheLRU( 1000 );
110 $this->interwikiScopes = $options->get( MainConfigNames::InterwikiScopes );
111
112 $interwikiData = $options->get( MainConfigNames::InterwikiCache );
113 $this->data = is_array( $interwikiData ) ? $interwikiData : null;
114 $this->wikiId = $options->get( 'wikiId' );
115 $this->virtualDomainsMapping = $options->get( MainConfigNames::VirtualDomainsMapping ) ?? [];
116 }
117
123 public function isValidInterwiki( $prefix ) {
124 $iw = $this->fetch( $prefix );
125 return (bool)$iw;
126 }
127
133 public function fetch( $prefix ) {
134 if ( $prefix === null || $prefix === '' ) {
135 return null;
136 }
137
138 $prefix = $this->contLang->lc( $prefix );
139
140 return $this->instances->getWithSetCallback(
141 $prefix,
142 function () use ( $prefix ) {
143 return $this->load( $prefix );
144 }
145 );
146 }
147
157 public function invalidateCache( $prefix ) {
158 $this->instances->clear( $prefix );
159
160 $key = $this->wanCache->makeKey( 'interwiki', $prefix );
161 $this->wanCache->delete( $key );
162 }
163
170 private function getPregenValue( string $prefix ) {
171 // Lazily resolve site name
172 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
173 $this->thisSite = $this->data['__sites:' . $this->wikiId]
174 ?? $this->options->get( MainConfigNames::InterwikiFallbackSite );
175 }
176
177 $value = $this->data[$this->wikiId . ':' . $prefix] ?? false;
178 // Site level
179 if ( $value === false && $this->interwikiScopes >= 3 ) {
180 $value = $this->data["_{$this->thisSite}:{$prefix}"] ?? false;
181 }
182 // Global level
183 if ( $value === false && $this->interwikiScopes >= 2 ) {
184 $value = $this->data["__global:{$prefix}"] ?? false;
185 }
186
187 return $value;
188 }
189
199 private function load( $prefix ) {
200 if ( $this->data !== null ) {
201 $value = $this->getPregenValue( $prefix );
202 return $value ? $this->makeFromPregen( $prefix, $value ) : false;
203 }
204
205 $iwData = [];
206 $abort = !$this->hookRunner->onInterwikiLoadPrefix( $prefix, $iwData );
207 if ( isset( $iwData['iw_url'] ) ) {
208 // Hook provided data
209 return $this->makeFromRow( $iwData );
210 }
211 if ( $abort ) {
212 // Hook indicated no other source may be considered
213 return false;
214 }
215
216 $iwData = $this->wanCache->getWithSetCallback(
217 $this->wanCache->makeKey( 'interwiki', $prefix ),
218 $this->options->get( MainConfigNames::InterwikiExpiry ),
219 function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix ) {
220 // Global interlanguage link
221 if ( $this->options->get( MainConfigNames::InterwikiMagic )
222 && $this->languageNameUtils->getLanguageName( $prefix )
223 ) {
224 $row = $this->loadFromDB( $prefix, 'virtual-interwiki-interlanguage' );
225 } else {
226 // Local interwiki link
227 $row = $this->loadFromDB( $prefix );
228
229 // Global interwiki link (not interlanguage)
230 if ( !$row && isset( $this->virtualDomainsMapping['virtual-interwiki'] ) ) {
231 $row = $this->loadFromDB( $prefix, 'virtual-interwiki' );
232 }
233 }
234
235 return $row ? (array)$row : '!NONEXISTENT';
236 }
237 );
238
239 // Handle non-existent case
240 return is_array( $iwData ) ? $this->makeFromRow( $iwData ) : false;
241 }
242
243 /*
244 * Fetch interwiki data from a DB query.
245 *
246 * @param string $prefix The interwiki prefix
247 * @param string|false $domain Domain ID, or false for the current domain
248 * @return stdClass|false interwiki data
249 */
250 private function loadFromDB( $prefix, $domain = false ) {
251 $dbr = $this->dbProvider->getReplicaDatabase( $domain );
252 $row = $dbr->newSelectQueryBuilder()
253 ->select( self::selectFields() )
254 ->from( 'interwiki' )
255 ->where( [ 'iw_prefix' => $prefix ] )
256 ->caller( __METHOD__ )->fetchRow();
257
258 return $row;
259 }
260
265 private function makeFromRow( array $row ) {
266 $url = $row['iw_url'];
267 $local = $row['iw_local'] ?? 0;
268 $trans = $row['iw_trans'] ?? 0;
269 $api = $row['iw_api'] ?? '';
270 $wikiId = $row['iw_wikiid'] ?? '';
271
272 return new Interwiki( null, $url, $api, $wikiId, $local, $trans );
273 }
274
280 private function makeFromPregen( string $prefix, string $value ) {
281 // Split values
282 [ $local, $url ] = explode( ' ', $value, 2 );
283 return new Interwiki( $prefix, $url, '', '', (int)$local );
284 }
285
292 private function getAllPrefixesPregenerated( $local ) {
293 // Lazily resolve site name
294 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
295 $this->thisSite = $this->data['__sites:' . $this->wikiId]
296 ?? $this->options->get( MainConfigNames::InterwikiFallbackSite );
297 }
298
299 // List of interwiki sources
300 $sources = [];
301 // Global level
302 if ( $this->interwikiScopes >= 2 ) {
303 $sources[] = '__global';
304 }
305 // Site level
306 if ( $this->interwikiScopes >= 3 ) {
307 $sources[] = '_' . $this->thisSite;
308 }
309 $sources[] = $this->wikiId;
310
311 $data = [];
312 foreach ( $sources as $source ) {
313 $list = $this->data['__list:' . $source] ?? '';
314 foreach ( explode( ' ', $list ) as $iw_prefix ) {
315 $row = $this->data["{$source}:{$iw_prefix}"] ?? null;
316 if ( !$row ) {
317 continue;
318 }
319
320 [ $iw_local, $iw_url ] = explode( ' ', $row );
321
322 if ( $local !== null && $local != $iw_local ) {
323 continue;
324 }
325
326 $data[$iw_prefix] = [
327 'iw_prefix' => $iw_prefix,
328 'iw_url' => $iw_url,
329 'iw_local' => $iw_local,
330 ];
331 }
332 }
333
334 return array_values( $data );
335 }
336
352 public static function buildCdbHash(
353 array $allPrefixes, int $scope = 1, ?string $thisSite = null
354 ): array {
355 $result = [];
356 $wikiId = WikiMap::getCurrentWikiId();
357 $keyPrefix = ( $scope >= 2 ) ? '__global' : $wikiId;
358 if ( $scope >= 3 && $thisSite ) {
359 $result[ "__sites:$wikiId" ] = $thisSite;
360 $keyPrefix = "_$thisSite";
361 }
362 $list = [];
363 foreach ( $allPrefixes as $iwInfo ) {
364 $prefix = $iwInfo['iw_prefix'];
365 $result["$keyPrefix:$prefix"] = implode( ' ', [
366 $iwInfo['iw_local'] ?? 0, $iwInfo['iw_url']
367 ] );
368 $list[] = $prefix;
369 }
370 $result["__list:$keyPrefix"] = implode( ' ', $list );
371 $result["__list:__sites"] = $wikiId;
372 return $result;
373 }
374
381 private function getAllPrefixesDB( $local ) {
382 $where = [];
383 if ( $local !== null ) {
384 $where['iw_local'] = (int)$local;
385 }
386
387 $dbr = $this->dbProvider->getReplicaDatabase();
388 $res = $dbr->newSelectQueryBuilder()
389 ->select( self::selectFields() )
390 ->from( 'interwiki' )
391 ->where( $where )
392 ->orderBy( 'iw_prefix' )
393 ->caller( __METHOD__ )->fetchResultSet();
394
395 $retval = [];
396 foreach ( $res as $row ) {
397 $retval[] = (array)$row;
398 }
399 return $retval;
400 }
401
408 public function getAllPrefixes( $local = null ) {
409 if ( $this->data !== null ) {
410 return $this->getAllPrefixesPregenerated( $local );
411 } else {
412 return $this->getAllPrefixesDB( $local );
413 }
414 }
415
421 private static function selectFields() {
422 return [
423 'iw_prefix',
424 'iw_url',
425 'iw_api',
426 'iw_wikiid',
427 'iw_local',
428 'iw_trans'
429 ];
430 }
431
432}
An interwiki record value object.
Definition Interwiki.php:27
Store key-value entries in a size-limited in-memory LRU cache.
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:82
A service that provides utilities to do with language names and codes.
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:31
Multi-datacenter aware caching interface.
Service interface for looking up Interwiki records.
Provide primary and replica IDatabase connections.
$source