52 public const CONSTRUCTOR_OPTIONS = [
79 private $interwikiScopes;
85 private $thisSite =
null;
102 $this->options = $options;
104 $this->contLang = $contLang;
105 $this->wanCache = $wanCache;
106 $this->hookRunner =
new HookRunner( $hookContainer );
107 $this->dbProvider = $dbProvider;
113 $this->data = is_array( $interwikiData ) ? $interwikiData :
null;
114 $this->wikiId = $options->
get(
'wikiId' );
123 $iw = $this->
fetch( $prefix );
133 if ( $prefix ===
null || $prefix ===
'' ) {
137 $prefix = $this->contLang->lc( $prefix );
139 return $this->instances->getWithSetCallback(
141 function () use ( $prefix ) {
142 return $this->load( $prefix );
157 $this->instances->clear( $prefix );
159 $key = $this->wanCache->makeKey(
'interwiki', $prefix );
160 $this->wanCache->delete( $key );
169 private function getPregenValue(
string $prefix ) {
171 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
172 $this->thisSite = $this->data[
'__sites:' . $this->wikiId]
176 $value = $this->data[$this->wikiId .
':' . $prefix] ??
false;
178 if ( $value ===
false && $this->interwikiScopes >= 3 ) {
179 $value = $this->data[
"_{$this->thisSite}:{$prefix}"] ??
false;
182 if ( $value ===
false && $this->interwikiScopes >= 2 ) {
183 $value = $this->data[
"__global:{$prefix}"] ??
false;
198 private function load( $prefix ) {
199 if ( $this->data !==
null ) {
200 $value = $this->getPregenValue( $prefix );
201 return $value ? $this->makeFromPregen( $prefix, $value ) : false;
205 $abort = !$this->hookRunner->onInterwikiLoadPrefix( $prefix, $iwData );
206 if ( isset( $iwData[
'iw_url'] ) ) {
208 return $this->makeFromRow( $iwData );
216 $iwData = $this->wanCache->getWithSetCallback(
217 $this->wanCache->makeKey(
'interwiki', $prefix ),
219 function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix, $fname ) {
220 $dbr = $this->dbProvider->getReplicaDatabase();
221 $row = $dbr->newSelectQueryBuilder()
222 ->select( self::selectFields() )
223 ->from(
'interwiki' )
224 ->where( [
'iw_prefix' => $prefix ] )
225 ->caller( $fname )->fetchRow();
227 return $row ? (array)$row :
'!NONEXISTENT';
232 return is_array( $iwData ) ? $this->makeFromRow( $iwData ) : false;
239 private function makeFromRow( array $row ) {
240 $url = $row[
'iw_url'];
241 $local = $row[
'iw_local'] ?? 0;
242 $trans = $row[
'iw_trans'] ?? 0;
243 $api = $row[
'iw_api'] ??
'';
244 $wikiId = $row[
'iw_wikiid'] ??
'';
246 return new Interwiki(
null, $url, $api, $wikiId, $local, $trans );
254 private function makeFromPregen(
string $prefix,
string $value ) {
256 [ $local, $url ] = explode(
' ', $value, 2 );
257 return new Interwiki( $prefix, $url,
'',
'', (
int)$local );
266 private function getAllPrefixesPregenerated( $local ) {
268 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
269 $this->thisSite = $this->data[
'__sites:' . $this->wikiId]
276 if ( $this->interwikiScopes >= 2 ) {
277 $sources[] =
'__global';
280 if ( $this->interwikiScopes >= 3 ) {
281 $sources[] =
'_' . $this->thisSite;
283 $sources[] = $this->wikiId;
286 foreach ( $sources as
$source ) {
287 $list = $this->data[
'__list:' .
$source] ??
'';
288 foreach ( explode(
' ', $list ) as $iw_prefix ) {
289 $row = $this->data[
"{$source}:{$iw_prefix}"] ??
null;
294 [ $iw_local, $iw_url ] = explode(
' ', $row );
296 if ( $local !==
null && $local != $iw_local ) {
300 $data[$iw_prefix] = [
301 'iw_prefix' => $iw_prefix,
303 'iw_local' => $iw_local,
308 return array_values( $data );
327 array $allPrefixes,
int $scope = 1, ?
string $thisSite =
null
331 $keyPrefix = ( $scope >= 2 ) ?
'__global' : $wikiId;
332 if ( $scope >= 3 && $thisSite ) {
333 $result[
"__sites:$wikiId" ] = $thisSite;
334 $keyPrefix =
"_$thisSite";
337 foreach ( $allPrefixes as $iwInfo ) {
338 $prefix = $iwInfo[
'iw_prefix'];
339 $result[
"$keyPrefix:$prefix"] = implode(
' ', [
340 $iwInfo[
'iw_local'] ?? 0, $iwInfo[
'iw_url']
344 $result[
"__list:$keyPrefix"] = implode(
' ', $list );
345 $result[
"__list:__sites"] = $wikiId;
355 private function getAllPrefixesDB( $local ) {
357 if ( $local !==
null ) {
358 $where[
'iw_local'] = (int)$local;
361 $dbr = $this->dbProvider->getReplicaDatabase();
362 $res = $dbr->newSelectQueryBuilder()
363 ->select( self::selectFields() )
364 ->from(
'interwiki' )
366 ->orderBy(
'iw_prefix' )
367 ->caller( __METHOD__ )->fetchResultSet();
370 foreach ( $res as $row ) {
371 $retval[] = (array)$row;
383 if ( $this->data !==
null ) {
384 return $this->getAllPrefixesPregenerated( $local );
386 return $this->getAllPrefixesDB( $local );
395 private static function selectFields() {
An interwiki record value object.
Base class for language-specific code.
Store key-value entries in a size-limited in-memory LRU cache.
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()
Multi-datacenter aware caching interface.