25use Cdb\Exception as CdbException;
26use Cdb\Reader as CdbReader;
131 $this->hookRunner =
new HookRunner( $hookContainer );
146 $result = $this->
fetch( $prefix );
148 return (
bool)$result;
158 if ( $prefix ==
'' ) {
162 $prefix = $this->contLang->lc( $prefix );
163 if ( $this->localCache->has( $prefix ) ) {
164 return $this->localCache->get( $prefix );
167 if ( $this->cdbData ) {
170 $iw = $this->
load( $prefix );
175 $this->localCache->set( $prefix, $iw );
186 $this->localCache->clear();
194 $this->localCache->clear( $prefix );
196 $key = $this->objectCache->makeKey(
'interwiki', $prefix );
197 $this->objectCache->delete( $key );
213 list( $local, $url ) = explode(
' ', $value, 2 );
214 return new Interwiki( $prefix, $url,
'',
'', (
int)$local );
229 wfDebug( __METHOD__ .
"( $prefix )" );
231 $wikiId = WikiMap::getCurrentWikiId();
236 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
237 $this->thisSite = $this->
getCacheValue(
'__sites:' . $wikiId );
238 if ( $this->thisSite ==
'' ) {
245 if ( $value ==
'' && $this->interwikiScopes >= 3 ) {
246 $value = $this->
getCacheValue(
"_{$this->thisSite}:{$prefix}" );
249 if ( $value ==
'' && $this->interwikiScopes >= 2 ) {
252 if ( $value ==
'undef' ) {
255 }
catch ( CdbException $e ) {
256 wfDebug( __METHOD__ .
": CdbException caught, error message was "
257 . $e->getMessage() );
264 if ( $this->cdbReader ===
null ) {
265 if ( is_string( $this->cdbData ) ) {
266 $this->cdbReader = \Cdb\Reader::open( $this->cdbData );
267 } elseif ( is_array( $this->cdbData ) ) {
268 $this->cdbReader = new \Cdb\Reader\Hash( $this->cdbData );
270 $this->cdbReader =
false;
274 if ( $this->cdbReader ) {
275 return $this->cdbReader->get( $key );
287 private function load( $prefix ) {
289 if ( !$this->hookRunner->onInterwikiLoadPrefix( $prefix, $iwData ) ) {
293 if ( is_array( $iwData ) ) {
301 $iwData = $this->objectCache->getWithSetCallback(
302 $this->objectCache->makeKey(
'interwiki', $prefix ),
303 $this->objectCacheExpiry,
304 function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix, $fname ) {
307 $setOpts += Database::getCacheSetOptions(
$dbr );
309 $row =
$dbr->selectRow(
311 self::selectFields(),
312 [
'iw_prefix' => $prefix ],
316 return $row ? (array)$row :
'!NONEXISTENT';
320 if ( is_array( $iwData ) ) {
334 if ( isset( $mc[
'iw_url'] ) ) {
335 $url = $mc[
'iw_url'];
336 $local = $mc[
'iw_local'] ?? 0;
337 $trans = $mc[
'iw_trans'] ?? 0;
338 $api = $mc[
'iw_api'] ??
'';
339 $wikiId = $mc[
'iw_wikiid'] ??
'';
341 return new Interwiki(
null, $url, $api, $wikiId, $local, $trans );
356 $wikiId = WikiMap::getCurrentWikiId();
361 if ( $this->interwikiScopes >= 3 && !$this->thisSite ) {
367 $this->thisSite = $site;
374 if ( $this->interwikiScopes >= 2 ) {
375 $sources[] =
'__global';
378 if ( $this->interwikiScopes >= 3 ) {
381 $sources[] = $wikiId;
383 foreach ( $sources as
$source ) {
385 foreach ( explode(
' ', $list ) as $iw_prefix ) {
391 list( $iw_local, $iw_url ) = explode(
' ', $row );
393 if ( $local !==
null && $local != $iw_local ) {
397 $data[$iw_prefix] = [
398 'iw_prefix' => $iw_prefix,
400 'iw_local' => $iw_local,
404 }
catch ( CdbException $e ) {
405 wfDebug( __METHOD__ .
": CdbException caught, error message was "
406 . $e->getMessage() );
409 return array_values( $data );
426 array $allPrefixes,
int $scope = 1, ?
string $thisSite =
null
429 $wikiId = WikiMap::getCurrentWikiId();
430 $keyPrefix = ( $scope >= 2 ) ?
'__global' : $wikiId;
432 $result[
"__sites:$wikiId" ] =
$thisSite;
433 $keyPrefix =
"_$thisSite";
436 foreach ( $allPrefixes as $iwInfo ) {
437 $prefix = $iwInfo[
'iw_prefix'];
438 $result[
"$keyPrefix:$prefix"] = implode(
' ', [
439 $iwInfo[
'iw_local'] ?? 0, $iwInfo[
'iw_url']
443 $result[
"__list:$keyPrefix"] = implode(
' ', $list );
444 $result[
"__list:__sites"] = $wikiId;
455 $db = $this->loadBalancer->getConnectionRef(
DB_REPLICA );
459 if ( $local !==
null ) {
461 $where[
'iw_local'] = 1;
462 } elseif ( $local == 0 ) {
463 $where[
'iw_local'] = 0;
467 $res = $db->select(
'interwiki',
468 self::selectFields(),
469 $where, __METHOD__, [
'ORDER BY' =>
'iw_prefix' ]
473 foreach (
$res as $row ) {
474 $retval[] = (array)$row;
487 if ( $this->cdbData ) {
488 return $this->getAllPrefixesCached( $local );
491 return $this->getAllPrefixesDB( $local );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Value object for representing interwiki records.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Handles a simple LRU key/value map with a maximum number of entries.
Multi-datacenter aware caching interface.
Helper tools for dealing with other locally-hosted wikis.