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