78 private $interwikiScopes;
84 private $thisSite =
null;
101 $this->options = $options;
103 $this->contLang = $contLang;
104 $this->wanCache = $wanCache;
105 $this->hookRunner =
new HookRunner( $hookContainer );
106 $this->dbProvider = $dbProvider;
112 $this->data = is_array( $interwikiData ) ? $interwikiData :
null;
113 $this->wikiId = $options->
get(
'wikiId' );
122 $iw = $this->
fetch( $prefix );
132 if ( $prefix ===
null || $prefix ===
'' ) {
136 $prefix = $this->contLang->lc( $prefix );
138 return $this->instances->getWithSetCallback(
140 function () use ( $prefix ) {
141 return $this->load( $prefix );
156 $this->instances->clear( $prefix );
158 $key = $this->wanCache->makeKey(
'interwiki', $prefix );
159 $this->wanCache->delete( $key );
168 private function getPregenValue(
string $prefix ) {
170 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
171 $this->thisSite = $this->data[
'__sites:' . $this->wikiId]
175 $value = $this->data[$this->wikiId .
':' . $prefix] ??
false;
177 if ( $value ===
false && $this->interwikiScopes >= 3 ) {
178 $value = $this->data[
"_{$this->thisSite}:{$prefix}"] ??
false;
181 if ( $value ===
false && $this->interwikiScopes >= 2 ) {
182 $value = $this->data[
"__global:{$prefix}"] ??
false;
197 private function load( $prefix ) {
198 if ( $this->data !==
null ) {
199 $value = $this->getPregenValue( $prefix );
200 return $value ? $this->makeFromPregen( $prefix, $value ) : false;
204 $abort = !$this->hookRunner->onInterwikiLoadPrefix( $prefix, $iwData );
205 if ( isset( $iwData[
'iw_url'] ) ) {
207 return $this->makeFromRow( $iwData );
215 $iwData = $this->wanCache->getWithSetCallback(
216 $this->wanCache->makeKey(
'interwiki', $prefix ),
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();
226 return $row ? (array)$row :
'!NONEXISTENT';
231 return is_array( $iwData ) ? $this->makeFromRow( $iwData ) : false;
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'] ??
'';
245 return new Interwiki(
null,
$url, $api, $wikiId, $local, $trans );
253 private function makeFromPregen(
string $prefix,
string $value ) {
255 [ $local,
$url ] = explode(
' ', $value, 2 );
265 private function getAllPrefixesPregenerated( $local ) {
267 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
268 $this->thisSite = $this->data[
'__sites:' . $this->wikiId]
275 if ( $this->interwikiScopes >= 2 ) {
276 $sources[] =
'__global';
279 if ( $this->interwikiScopes >= 3 ) {
280 $sources[] =
'_' . $this->thisSite;
282 $sources[] = $this->wikiId;
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;
293 [ $iw_local, $iw_url ] = explode(
' ', $row );
295 if ( $local !==
null && $local != $iw_local ) {
299 $data[$iw_prefix] = [
300 'iw_prefix' => $iw_prefix,
302 'iw_local' => $iw_local,
307 return array_values( $data );
326 array $allPrefixes,
int $scope = 1, ?
string $thisSite =
null
329 $wikiId = WikiMap::getCurrentWikiId();
330 $keyPrefix = ( $scope >= 2 ) ?
'__global' : $wikiId;
331 if ( $scope >= 3 && $thisSite ) {
332 $result[
"__sites:$wikiId" ] = $thisSite;
333 $keyPrefix =
"_$thisSite";
336 foreach ( $allPrefixes as $iwInfo ) {
337 $prefix = $iwInfo[
'iw_prefix'];
338 $result[
"$keyPrefix:$prefix"] = implode(
' ', [
339 $iwInfo[
'iw_local'] ?? 0, $iwInfo[
'iw_url']
343 $result[
"__list:$keyPrefix"] = implode(
' ', $list );
344 $result[
"__list:__sites"] = $wikiId;
354 private function getAllPrefixesDB( $local ) {
356 if ( $local !==
null ) {
357 $where[
'iw_local'] = (int)$local;
360 $dbr = $this->dbProvider->getReplicaDatabase();
361 $res = $dbr->newSelectQueryBuilder()
362 ->select( self::selectFields() )
363 ->from(
'interwiki' )
365 ->orderBy(
'iw_prefix' )
366 ->caller( __METHOD__ )->fetchResultSet();
369 foreach ( $res as $row ) {
370 $retval[] = (array)$row;
382 if ( $this->data !==
null ) {
383 return $this->getAllPrefixesPregenerated( $local );
385 return $this->getAllPrefixesDB( $local );
394 private static function selectFields() {