52 private LanguageNameUtils $languageNameUtils;
62 private int $interwikiScopes;
65 private string $wikiId;
66 private ?
string $thisSite =
null;
67 private array $virtualDomainsMapping;
83 LanguageNameUtils $languageNameUtils
86 $this->options = $options;
88 $this->contLang = $contLang;
89 $this->wanCache = $wanCache;
90 $this->hookRunner =
new HookRunner( $hookContainer );
91 $this->dbProvider = $dbProvider;
92 $this->languageNameUtils = $languageNameUtils;
98 $this->data = is_array( $interwikiData ) ? $interwikiData :
null;
99 $this->wikiId = $options->
get(
'wikiId' );
109 $iw = $this->
fetch( $prefix );
119 if ( $prefix ===
null || $prefix ===
'' ) {
123 $prefix = $this->contLang->lc( $prefix );
125 return $this->instances->getWithSetCallback(
127 function () use ( $prefix ) {
128 return $this->load( $prefix );
143 $this->instances->clear( $prefix );
145 $key = $this->wanCache->makeKey(
'interwiki', $prefix );
146 $this->wanCache->delete( $key );
155 private function getPregenValue(
string $prefix ) {
157 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
158 $this->thisSite = $this->data[
'__sites:' . $this->wikiId]
162 $value = $this->data[$this->wikiId .
':' . $prefix] ??
false;
164 if ( $value ===
false && $this->interwikiScopes >= 3 ) {
165 $value = $this->data[
"_{$this->thisSite}:{$prefix}"] ??
false;
168 if ( $value ===
false && $this->interwikiScopes >= 2 ) {
169 $value = $this->data[
"__global:{$prefix}"] ??
false;
184 private function load( $prefix ) {
185 if ( $this->data !==
null ) {
186 $value = $this->getPregenValue( $prefix );
187 return $value ? $this->makeFromPregen( $prefix, $value ) : false;
191 $abort = !$this->hookRunner->onInterwikiLoadPrefix( $prefix, $iwData );
192 if ( isset( $iwData[
'iw_url'] ) ) {
194 return $this->makeFromRow( $iwData );
201 $iwData = $this->wanCache->getWithSetCallback(
202 $this->wanCache->makeKey(
'interwiki', $prefix ),
204 function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix ) {
207 && $this->languageNameUtils->getLanguageName( $prefix )
209 $row = $this->loadFromDB( $prefix,
'virtual-interwiki-interlanguage' );
212 $row = $this->loadFromDB( $prefix );
215 if ( !$row && isset( $this->virtualDomainsMapping[
'virtual-interwiki'] ) ) {
216 $row = $this->loadFromDB( $prefix,
'virtual-interwiki' );
220 return $row ? (array)$row :
'!NONEXISTENT';
225 return is_array( $iwData ) ? $this->makeFromRow( $iwData ) : false;
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();
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'] ??
'';
257 return new Interwiki(
null,
$url, $api, $wikiId, $local, $trans );
265 private function makeFromPregen(
string $prefix,
string $value ) {
267 [ $local,
$url ] = explode(
' ', $value, 2 );
268 return new Interwiki( $prefix,
$url,
'',
'', (
int)$local );
277 private function getAllPrefixesPregenerated( $local ) {
279 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
280 $this->thisSite = $this->data[
'__sites:' . $this->wikiId]
287 if ( $this->interwikiScopes >= 2 ) {
288 $sources[] =
'__global';
291 if ( $this->interwikiScopes >= 3 ) {
292 $sources[] =
'_' . $this->thisSite;
294 $sources[] = $this->wikiId;
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;
305 [ $iw_local, $iw_url ] = explode(
' ', $row );
307 if ( $local !==
null && $local != $iw_local ) {
311 $data[$iw_prefix] = [
312 'iw_prefix' => $iw_prefix,
314 'iw_local' => $iw_local,
319 return array_values( $data );
338 array $allPrefixes,
int $scope = 1, ?
string $thisSite =
null
341 $wikiId = WikiMap::getCurrentWikiId();
342 $keyPrefix = ( $scope >= 2 ) ?
'__global' : $wikiId;
343 if ( $scope >= 3 && $thisSite ) {
344 $result[
"__sites:$wikiId" ] = $thisSite;
345 $keyPrefix =
"_$thisSite";
348 foreach ( $allPrefixes as $iwInfo ) {
349 $prefix = $iwInfo[
'iw_prefix'];
350 $result[
"$keyPrefix:$prefix"] = implode(
' ', [
351 $iwInfo[
'iw_local'] ?? 0, $iwInfo[
'iw_url']
355 $result[
"__list:$keyPrefix"] = implode(
' ', $list );
356 $result[
"__list:__sites"] = $wikiId;
366 private function getAllPrefixesDB( $local ) {
368 if ( $local !==
null ) {
369 $where[
'iw_local'] = (int)$local;
372 $dbr = $this->dbProvider->getReplicaDatabase();
373 $res = $dbr->newSelectQueryBuilder()
374 ->select( self::selectFields() )
375 ->from(
'interwiki' )
377 ->orderBy(
'iw_prefix' )
378 ->caller( __METHOD__ )->fetchResultSet();
381 foreach ( $res as $row ) {
382 $retval[] = (array)$row;
394 if ( $this->data !==
null ) {
395 return $this->getAllPrefixesPregenerated( $local );
397 return $this->getAllPrefixesDB( $local );
406 private static function selectFields() {