52 public const CONSTRUCTOR_OPTIONS = [
68 private $loadBalancer;
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->loadBalancer = $loadBalancer;
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 ) {
221 $row =
$dbr->selectRow(
223 self::selectFields(),
224 [
'iw_prefix' => $prefix ],
228 return $row ? (array)$row :
'!NONEXISTENT';
233 return is_array( $iwData ) ? $this->makeFromRow( $iwData ) : false;
240 private function makeFromRow( array $row ) {
241 $url = $row[
'iw_url'];
242 $local = $row[
'iw_local'] ?? 0;
243 $trans = $row[
'iw_trans'] ?? 0;
244 $api = $row[
'iw_api'] ??
'';
245 $wikiId = $row[
'iw_wikiid'] ??
'';
247 return new Interwiki(
null, $url, $api, $wikiId, $local, $trans );
255 private function makeFromPregen(
string $prefix,
string $value ) {
257 [ $local, $url ] = explode(
' ', $value, 2 );
258 return new Interwiki( $prefix, $url,
'',
'', (
int)$local );
267 private function getAllPrefixesPregenerated( $local ) {
269 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
270 $this->thisSite = $this->data[
'__sites:' . $this->wikiId]
277 if ( $this->interwikiScopes >= 2 ) {
278 $sources[] =
'__global';
281 if ( $this->interwikiScopes >= 3 ) {
282 $sources[] =
'_' . $this->thisSite;
284 $sources[] = $this->wikiId;
287 foreach ( $sources as
$source ) {
288 $list = $this->data[
'__list:' .
$source] ??
'';
289 foreach ( explode(
' ', $list ) as $iw_prefix ) {
290 $row = $this->data[
"{$source}:{$iw_prefix}"] ??
null;
295 [ $iw_local, $iw_url ] = explode(
' ', $row );
297 if ( $local !==
null && $local != $iw_local ) {
301 $data[$iw_prefix] = [
302 'iw_prefix' => $iw_prefix,
304 'iw_local' => $iw_local,
309 return array_values( $data );
328 array $allPrefixes,
int $scope = 1, ?
string $thisSite =
null
331 $wikiId = WikiMap::getCurrentWikiId();
332 $keyPrefix = ( $scope >= 2 ) ?
'__global' : $wikiId;
333 if ( $scope >= 3 && $thisSite ) {
334 $result[
"__sites:$wikiId" ] = $thisSite;
335 $keyPrefix =
"_$thisSite";
338 foreach ( $allPrefixes as $iwInfo ) {
339 $prefix = $iwInfo[
'iw_prefix'];
340 $result[
"$keyPrefix:$prefix"] = implode(
' ', [
341 $iwInfo[
'iw_local'] ?? 0, $iwInfo[
'iw_url']
345 $result[
"__list:$keyPrefix"] = implode(
' ', $list );
346 $result[
"__list:__sites"] = $wikiId;
356 private function getAllPrefixesDB( $local ) {
358 if ( $local !==
null ) {
359 $where[
'iw_local'] = (int)$local;
364 self::selectFields(),
365 $where, __METHOD__, [
'ORDER BY' =>
'iw_prefix' ]
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() {