MediaWiki master
ClassicInterwikiLookup.php
Go to the documentation of this file.
1<?php
22
23use Interwiki;
24use MapCacheLRU;
33
51 public const CONSTRUCTOR_OPTIONS = [
56 'wikiId',
57 ];
58
59 private ServiceOptions $options;
61 private $contLang;
63 private $wanCache;
65 private $hookRunner;
67 private $dbProvider;
68
70 private $instances;
78 private $interwikiScopes;
80 private $data;
82 private $wikiId;
84 private $thisSite = null;
85
93 public function __construct(
94 ServiceOptions $options,
95 Language $contLang,
96 WANObjectCache $wanCache,
97 HookContainer $hookContainer,
98 IConnectionProvider $dbProvider
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
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 }
115
121 public function isValidInterwiki( $prefix ) {
122 $iw = $this->fetch( $prefix );
123 return (bool)$iw;
124 }
125
131 public function fetch( $prefix ) {
132 if ( $prefix === null || $prefix === '' ) {
133 return null;
134 }
135
136 $prefix = $this->contLang->lc( $prefix );
137
138 return $this->instances->getWithSetCallback(
139 $prefix,
140 function () use ( $prefix ) {
141 return $this->load( $prefix );
142 }
143 );
144 }
145
155 public function invalidateCache( $prefix ) {
156 $this->instances->clear( $prefix );
157
158 $key = $this->wanCache->makeKey( 'interwiki', $prefix );
159 $this->wanCache->delete( $key );
160 }
161
168 private function getPregenValue( string $prefix ) {
169 // Lazily resolve site name
170 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
171 $this->thisSite = $this->data['__sites:' . $this->wikiId]
172 ?? $this->options->get( MainConfigNames::InterwikiFallbackSite );
173 }
174
175 $value = $this->data[$this->wikiId . ':' . $prefix] ?? false;
176 // Site level
177 if ( $value === false && $this->interwikiScopes >= 3 ) {
178 $value = $this->data["_{$this->thisSite}:{$prefix}"] ?? false;
179 }
180 // Global level
181 if ( $value === false && $this->interwikiScopes >= 2 ) {
182 $value = $this->data["__global:{$prefix}"] ?? false;
183 }
184
185 return $value;
186 }
187
197 private function load( $prefix ) {
198 if ( $this->data !== null ) {
199 $value = $this->getPregenValue( $prefix );
200 return $value ? $this->makeFromPregen( $prefix, $value ) : false;
201 }
202
203 $iwData = [];
204 $abort = !$this->hookRunner->onInterwikiLoadPrefix( $prefix, $iwData );
205 if ( isset( $iwData['iw_url'] ) ) {
206 // Hook provided data
207 return $this->makeFromRow( $iwData );
208 }
209 if ( $abort ) {
210 // Hook indicated no other source may be considered
211 return false;
212 }
213
214 $fname = __METHOD__;
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, $fname ) {
219 $dbr = $this->dbProvider->getReplicaDatabase();
220 $row = $dbr->newSelectQueryBuilder()
221 ->select( self::selectFields() )
222 ->from( 'interwiki' )
223 ->where( [ 'iw_prefix' => $prefix ] )
224 ->caller( $fname )->fetchRow();
225
226 return $row ? (array)$row : '!NONEXISTENT';
227 }
228 );
229
230 // Handle non-existent case
231 return is_array( $iwData ) ? $this->makeFromRow( $iwData ) : false;
232 }
233
238 private function makeFromRow( array $row ) {
239 $url = $row['iw_url'];
240 $local = $row['iw_local'] ?? 0;
241 $trans = $row['iw_trans'] ?? 0;
242 $api = $row['iw_api'] ?? '';
243 $wikiId = $row['iw_wikiid'] ?? '';
244
245 return new Interwiki( null, $url, $api, $wikiId, $local, $trans );
246 }
247
253 private function makeFromPregen( string $prefix, string $value ) {
254 // Split values
255 [ $local, $url ] = explode( ' ', $value, 2 );
256 return new Interwiki( $prefix, $url, '', '', (int)$local );
257 }
258
265 private function getAllPrefixesPregenerated( $local ) {
266 // Lazily resolve site name
267 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
268 $this->thisSite = $this->data['__sites:' . $this->wikiId]
269 ?? $this->options->get( MainConfigNames::InterwikiFallbackSite );
270 }
271
272 // List of interwiki sources
273 $sources = [];
274 // Global level
275 if ( $this->interwikiScopes >= 2 ) {
276 $sources[] = '__global';
277 }
278 // Site level
279 if ( $this->interwikiScopes >= 3 ) {
280 $sources[] = '_' . $this->thisSite;
281 }
282 $sources[] = $this->wikiId;
283
284 $data = [];
285 foreach ( $sources as $source ) {
286 $list = $this->data['__list:' . $source] ?? '';
287 foreach ( explode( ' ', $list ) as $iw_prefix ) {
288 $row = $this->data["{$source}:{$iw_prefix}"] ?? null;
289 if ( !$row ) {
290 continue;
291 }
292
293 [ $iw_local, $iw_url ] = explode( ' ', $row );
294
295 if ( $local !== null && $local != $iw_local ) {
296 continue;
297 }
298
299 $data[$iw_prefix] = [
300 'iw_prefix' => $iw_prefix,
301 'iw_url' => $iw_url,
302 'iw_local' => $iw_local,
303 ];
304 }
305 }
306
307 return array_values( $data );
308 }
309
325 public static function buildCdbHash(
326 array $allPrefixes, int $scope = 1, ?string $thisSite = null
327 ): array {
328 $result = [];
329 $wikiId = WikiMap::getCurrentWikiId();
330 $keyPrefix = ( $scope >= 2 ) ? '__global' : $wikiId;
331 if ( $scope >= 3 && $thisSite ) {
332 $result[ "__sites:$wikiId" ] = $thisSite;
333 $keyPrefix = "_$thisSite";
334 }
335 $list = [];
336 foreach ( $allPrefixes as $iwInfo ) {
337 $prefix = $iwInfo['iw_prefix'];
338 $result["$keyPrefix:$prefix"] = implode( ' ', [
339 $iwInfo['iw_local'] ?? 0, $iwInfo['iw_url']
340 ] );
341 $list[] = $prefix;
342 }
343 $result["__list:$keyPrefix"] = implode( ' ', $list );
344 $result["__list:__sites"] = $wikiId;
345 return $result;
346 }
347
354 private function getAllPrefixesDB( $local ) {
355 $where = [];
356 if ( $local !== null ) {
357 $where['iw_local'] = (int)$local;
358 }
359
360 $dbr = $this->dbProvider->getReplicaDatabase();
361 $res = $dbr->newSelectQueryBuilder()
362 ->select( self::selectFields() )
363 ->from( 'interwiki' )
364 ->where( $where )
365 ->orderBy( 'iw_prefix' )
366 ->caller( __METHOD__ )->fetchResultSet();
367
368 $retval = [];
369 foreach ( $res as $row ) {
370 $retval[] = (array)$row;
371 }
372 return $retval;
373 }
374
381 public function getAllPrefixes( $local = null ) {
382 if ( $this->data !== null ) {
383 return $this->getAllPrefixesPregenerated( $local );
384 } else {
385 return $this->getAllPrefixesDB( $local );
386 }
387 }
388
394 private static function selectFields() {
395 return [
396 'iw_prefix',
397 'iw_url',
398 'iw_api',
399 'iw_wikiid',
400 'iw_local',
401 'iw_trans'
402 ];
403 }
404
405}
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.
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.
__construct(ServiceOptions $options, Language $contLang, WANObjectCache $wanCache, HookContainer $hookContainer, IConnectionProvider $dbProvider)
Base class for language-specific code.
Definition Language.php:78
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 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()
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