76 private int $interwikiScopes;
79 private string $wikiId;
80 private ?
string $thisSite =
null;
81 private array $virtualDomainsMapping;
100 $this->options = $options;
102 $this->contLang = $contLang;
103 $this->wanCache = $wanCache;
104 $this->hookRunner =
new HookRunner( $hookContainer );
105 $this->dbProvider = $dbProvider;
106 $this->languageNameUtils = $languageNameUtils;
112 $this->data = is_array( $interwikiData ) ? $interwikiData :
null;
113 $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 );
215 $iwData = $this->wanCache->getWithSetCallback(
216 $this->wanCache->makeKey(
'interwiki', $prefix ),
218 function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix ) {
221 && $this->languageNameUtils->getLanguageName( $prefix )
223 $row = $this->loadFromDB( $prefix,
'virtual-interwiki-interlanguage' );
226 $row = $this->loadFromDB( $prefix );
229 if ( !$row && isset( $this->virtualDomainsMapping[
'virtual-interwiki'] ) ) {
230 $row = $this->loadFromDB( $prefix,
'virtual-interwiki' );
234 return $row ? (array)$row :
'!NONEXISTENT';
239 return is_array( $iwData ) ? $this->makeFromRow( $iwData ) : false;
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();
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'] ??
'';
271 return new Interwiki(
null,
$url, $api, $wikiId, $local, $trans );
279 private function makeFromPregen(
string $prefix,
string $value ) {
281 [ $local,
$url ] = explode(
' ', $value, 2 );
282 return new Interwiki( $prefix,
$url,
'',
'', (
int)$local );
291 private function getAllPrefixesPregenerated( $local ) {
293 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
294 $this->thisSite = $this->data[
'__sites:' . $this->wikiId]
301 if ( $this->interwikiScopes >= 2 ) {
302 $sources[] =
'__global';
305 if ( $this->interwikiScopes >= 3 ) {
306 $sources[] =
'_' . $this->thisSite;
308 $sources[] = $this->wikiId;
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;
319 [ $iw_local, $iw_url ] = explode(
' ', $row );
321 if ( $local !==
null && $local != $iw_local ) {
325 $data[$iw_prefix] = [
326 'iw_prefix' => $iw_prefix,
328 'iw_local' => $iw_local,
333 return array_values( $data );
352 array $allPrefixes,
int $scope = 1, ?
string $thisSite =
null
355 $wikiId = WikiMap::getCurrentWikiId();
356 $keyPrefix = ( $scope >= 2 ) ?
'__global' : $wikiId;
357 if ( $scope >= 3 && $thisSite ) {
358 $result[
"__sites:$wikiId" ] = $thisSite;
359 $keyPrefix =
"_$thisSite";
362 foreach ( $allPrefixes as $iwInfo ) {
363 $prefix = $iwInfo[
'iw_prefix'];
364 $result[
"$keyPrefix:$prefix"] = implode(
' ', [
365 $iwInfo[
'iw_local'] ?? 0, $iwInfo[
'iw_url']
369 $result[
"__list:$keyPrefix"] = implode(
' ', $list );
370 $result[
"__list:__sites"] = $wikiId;
380 private function getAllPrefixesDB( $local ) {
382 if ( $local !==
null ) {
383 $where[
'iw_local'] = (int)$local;
386 $dbr = $this->dbProvider->getReplicaDatabase();
387 $res = $dbr->newSelectQueryBuilder()
388 ->select( self::selectFields() )
389 ->from(
'interwiki' )
391 ->orderBy(
'iw_prefix' )
392 ->caller( __METHOD__ )->fetchResultSet();
395 foreach ( $res as $row ) {
396 $retval[] = (array)$row;
408 if ( $this->data !==
null ) {
409 return $this->getAllPrefixesPregenerated( $local );
411 return $this->getAllPrefixesDB( $local );
420 private static function selectFields() {