77 private int $interwikiScopes;
80 private string $wikiId;
81 private ?
string $thisSite =
null;
82 private array $virtualDomainsMapping;
101 $this->options = $options;
103 $this->contLang = $contLang;
104 $this->wanCache = $wanCache;
105 $this->hookRunner =
new HookRunner( $hookContainer );
106 $this->dbProvider = $dbProvider;
107 $this->languageNameUtils = $languageNameUtils;
113 $this->data = is_array( $interwikiData ) ? $interwikiData :
null;
114 $this->wikiId = $options->
get(
'wikiId' );
124 $iw = $this->
fetch( $prefix );
134 if ( $prefix ===
null || $prefix ===
'' ) {
138 $prefix = $this->contLang->lc( $prefix );
140 return $this->instances->getWithSetCallback(
142 function () use ( $prefix ) {
143 return $this->load( $prefix );
158 $this->instances->clear( $prefix );
160 $key = $this->wanCache->makeKey(
'interwiki', $prefix );
161 $this->wanCache->delete( $key );
170 private function getPregenValue(
string $prefix ) {
172 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
173 $this->thisSite = $this->data[
'__sites:' . $this->wikiId]
177 $value = $this->data[$this->wikiId .
':' . $prefix] ??
false;
179 if ( $value ===
false && $this->interwikiScopes >= 3 ) {
180 $value = $this->data[
"_{$this->thisSite}:{$prefix}"] ??
false;
183 if ( $value ===
false && $this->interwikiScopes >= 2 ) {
184 $value = $this->data[
"__global:{$prefix}"] ??
false;
199 private function load( $prefix ) {
200 if ( $this->data !==
null ) {
201 $value = $this->getPregenValue( $prefix );
202 return $value ? $this->makeFromPregen( $prefix, $value ) : false;
206 $abort = !$this->hookRunner->onInterwikiLoadPrefix( $prefix, $iwData );
207 if ( isset( $iwData[
'iw_url'] ) ) {
209 return $this->makeFromRow( $iwData );
216 $iwData = $this->wanCache->getWithSetCallback(
217 $this->wanCache->makeKey(
'interwiki', $prefix ),
219 function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix ) {
222 && $this->languageNameUtils->getLanguageName( $prefix )
224 $row = $this->loadFromDB( $prefix,
'virtual-interwiki-interlanguage' );
227 $row = $this->loadFromDB( $prefix );
230 if ( !$row && isset( $this->virtualDomainsMapping[
'virtual-interwiki'] ) ) {
231 $row = $this->loadFromDB( $prefix,
'virtual-interwiki' );
235 return $row ? (array)$row :
'!NONEXISTENT';
240 return is_array( $iwData ) ? $this->makeFromRow( $iwData ) : false;
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();
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'] ??
'';
272 return new Interwiki(
null,
$url, $api, $wikiId, $local, $trans );
280 private function makeFromPregen(
string $prefix,
string $value ) {
282 [ $local,
$url ] = explode(
' ', $value, 2 );
292 private function getAllPrefixesPregenerated( $local ) {
294 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
295 $this->thisSite = $this->data[
'__sites:' . $this->wikiId]
302 if ( $this->interwikiScopes >= 2 ) {
303 $sources[] =
'__global';
306 if ( $this->interwikiScopes >= 3 ) {
307 $sources[] =
'_' . $this->thisSite;
309 $sources[] = $this->wikiId;
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;
320 [ $iw_local, $iw_url ] = explode(
' ', $row );
322 if ( $local !==
null && $local != $iw_local ) {
326 $data[$iw_prefix] = [
327 'iw_prefix' => $iw_prefix,
329 'iw_local' => $iw_local,
334 return array_values( $data );
353 array $allPrefixes,
int $scope = 1, ?
string $thisSite =
null
356 $wikiId = WikiMap::getCurrentWikiId();
357 $keyPrefix = ( $scope >= 2 ) ?
'__global' : $wikiId;
358 if ( $scope >= 3 && $thisSite ) {
359 $result[
"__sites:$wikiId" ] = $thisSite;
360 $keyPrefix =
"_$thisSite";
363 foreach ( $allPrefixes as $iwInfo ) {
364 $prefix = $iwInfo[
'iw_prefix'];
365 $result[
"$keyPrefix:$prefix"] = implode(
' ', [
366 $iwInfo[
'iw_local'] ?? 0, $iwInfo[
'iw_url']
370 $result[
"__list:$keyPrefix"] = implode(
' ', $list );
371 $result[
"__list:__sites"] = $wikiId;
381 private function getAllPrefixesDB( $local ) {
383 if ( $local !==
null ) {
384 $where[
'iw_local'] = (int)$local;
387 $dbr = $this->dbProvider->getReplicaDatabase();
388 $res = $dbr->newSelectQueryBuilder()
389 ->select( self::selectFields() )
390 ->from(
'interwiki' )
392 ->orderBy(
'iw_prefix' )
393 ->caller( __METHOD__ )->fetchResultSet();
396 foreach ( $res as $row ) {
397 $retval[] = (array)$row;
409 if ( $this->data !==
null ) {
410 return $this->getAllPrefixesPregenerated( $local );
412 return $this->getAllPrefixesDB( $local );
421 private static function selectFields() {