3 * InterwikiLookup implementing the "classic" interwiki storage (hardcoded up to MW 1.26).
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Interwiki
;
25 use Cdb\Exception
as CdbException
;
26 use Cdb\Reader
as CdbReader
;
33 use Wikimedia\Rdbms\Database
;
36 * InterwikiLookup implementing the "classic" interwiki storage (hardcoded up to MW 1.26).
38 * This implements two levels of caching (in-process array and a WANObjectCache)
39 * and tree storage backends (SQL, CDB, and plain PHP arrays).
41 * All information is loaded on creation when called by $this->fetch( $prefix ).
42 * All work is done on replica DB, because this should *never* change (except during
43 * schema updates etc, which aren't wiki-related)
47 class ClassicInterwikiLookup
implements InterwikiLookup
{
67 private $objectCacheExpiry;
70 * @var bool|array|string
77 private $interwikiScopes;
82 private $fallbackSite;
87 private $cdbReader = null;
92 private $thisSite = null;
95 * @param Language $contLang Language object used to convert prefixes to lower case
96 * @param WANObjectCache $objectCache Cache for interwiki info retrieved from the database
97 * @param int $objectCacheExpiry Expiry time for $objectCache, in seconds
98 * @param bool|array|string $cdbData The path of a CDB file, or
99 * an array resembling the contents of a CDB file,
100 * or false to use the database.
101 * @param int $interwikiScopes Specify number of domains to check for messages:
102 * - 1: Just local wiki level
103 * - 2: wiki and global levels
104 * - 3: site level as well as wiki and global levels
105 * @param string $fallbackSite The code to assume for the local site,
107 function __construct(
109 WANObjectCache
$objectCache,
115 $this->localCache
= new MapCacheLRU( 100 );
117 $this->contLang
= $contLang;
118 $this->objectCache
= $objectCache;
119 $this->objectCacheExpiry
= $objectCacheExpiry;
120 $this->cdbData
= $cdbData;
121 $this->interwikiScopes
= $interwikiScopes;
122 $this->fallbackSite
= $fallbackSite;
126 * Check whether an interwiki prefix exists
128 * @param string $prefix Interwiki prefix to use
129 * @return bool Whether it exists
131 public function isValidInterwiki( $prefix ) {
132 $result = $this->fetch( $prefix );
134 return (bool)$result;
138 * Fetch an Interwiki object
140 * @param string $prefix Interwiki prefix to use
141 * @return Interwiki|null|bool
143 public function fetch( $prefix ) {
144 if ( $prefix == '' ) {
148 $prefix = $this->contLang
->lc( $prefix );
149 if ( $this->localCache
->has( $prefix ) ) {
150 return $this->localCache
->get( $prefix );
153 if ( $this->cdbData
) {
154 $iw = $this->getInterwikiCached( $prefix );
156 $iw = $this->load( $prefix );
161 $this->localCache
->set( $prefix, $iw );
167 * Resets locally cached Interwiki objects. This is intended for use during testing only.
168 * This does not invalidate entries in the persistent cache, as invalidateCache() does.
171 public function resetLocalCache() {
172 $this->localCache
->clear();
176 * Purge the in-process and object cache for an interwiki prefix
177 * @param string $prefix
179 public function invalidateCache( $prefix ) {
180 $this->localCache
->clear( $prefix );
182 $key = $this->objectCache
->makeKey( 'interwiki', $prefix );
183 $this->objectCache
->delete( $key );
187 * Fetch interwiki prefix data from local cache in constant database.
189 * @note More logic is explained in DefaultSettings.
191 * @param string $prefix Interwiki prefix
192 * @return Interwiki|false
194 private function getInterwikiCached( $prefix ) {
195 $value = $this->getInterwikiCacheEntry( $prefix );
199 list( $local, $url ) = explode( ' ', $value, 2 );
200 return new Interwiki( $prefix, $url, '', '', (int)$local );
207 * Get entry from interwiki cache
209 * @note More logic is explained in DefaultSettings.
211 * @param string $prefix Database key
212 * @return bool|string The interwiki entry or false if not found
214 private function getInterwikiCacheEntry( $prefix ) {
215 wfDebug( __METHOD__
. "( $prefix )\n" );
217 $wikiId = WikiMap
::getWikiIdFromDbDomain( WikiMap
::getCurrentWikiDbDomain() );
222 if ( $this->interwikiScopes
>= 3 && !$this->thisSite
) {
223 $this->thisSite
= $this->getCacheValue( '__sites:' . $wikiId );
224 if ( $this->thisSite
== '' ) {
225 $this->thisSite
= $this->fallbackSite
;
229 $value = $this->getCacheValue( $wikiId . ':' . $prefix );
231 if ( $value == '' && $this->interwikiScopes
>= 3 ) {
232 $value = $this->getCacheValue( "_{$this->thisSite}:{$prefix}" );
235 if ( $value == '' && $this->interwikiScopes
>= 2 ) {
236 $value = $this->getCacheValue( "__global:{$prefix}" );
238 if ( $value == 'undef' ) {
241 } catch ( CdbException
$e ) {
242 wfDebug( __METHOD__
. ": CdbException caught, error message was "
243 . $e->getMessage() );
249 private function getCacheValue( $key ) {
250 if ( $this->cdbReader
=== null ) {
251 if ( is_string( $this->cdbData
) ) {
252 $this->cdbReader
= \Cdb\Reader
::open( $this->cdbData
);
253 } elseif ( is_array( $this->cdbData
) ) {
254 $this->cdbReader
= new \Cdb\Reader\
Hash( $this->cdbData
);
256 $this->cdbReader
= false;
260 if ( $this->cdbReader
) {
261 return $this->cdbReader
->get( $key );
268 * Load the interwiki, trying first memcached then the DB
270 * @param string $prefix The interwiki prefix
271 * @return Interwiki|bool Interwiki if $prefix is valid, otherwise false
273 private function load( $prefix ) {
275 if ( !Hooks
::run( 'InterwikiLoadPrefix', [ $prefix, &$iwData ] ) ) {
276 return $this->loadFromArray( $iwData );
279 if ( is_array( $iwData ) ) {
280 $iw = $this->loadFromArray( $iwData );
282 return $iw; // handled by hook
287 $iwData = $this->objectCache
->getWithSetCallback(
288 $this->objectCache
->makeKey( 'interwiki', $prefix ),
289 $this->objectCacheExpiry
,
290 function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix, $fname ) {
291 $dbr = wfGetDB( DB_REPLICA
); // TODO: inject LoadBalancer
293 $setOpts +
= Database
::getCacheSetOptions( $dbr );
295 $row = $dbr->selectRow(
297 self
::selectFields(),
298 [ 'iw_prefix' => $prefix ],
302 return $row ?
(array)$row : '!NONEXISTENT';
306 if ( is_array( $iwData ) ) {
307 return $this->loadFromArray( $iwData ) ?
: false;
314 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
316 * @param array $mc Associative array: row from the interwiki table
317 * @return Interwiki|bool Interwiki object or false if $mc['iw_url'] is not set
319 private function loadFromArray( $mc ) {
320 if ( isset( $mc['iw_url'] ) ) {
321 $url = $mc['iw_url'];
322 $local = $mc['iw_local'] ??
0;
323 $trans = $mc['iw_trans'] ??
0;
324 $api = $mc['iw_api'] ??
'';
325 $wikiId = $mc['iw_wikiid'] ??
'';
327 return new Interwiki( null, $url, $api, $wikiId, $local, $trans );
334 * Fetch all interwiki prefixes from interwiki cache
336 * @param null|string $local If not null, limits output to local/non-local interwikis
337 * @return array List of prefixes, where each row is an associative array
339 private function getAllPrefixesCached( $local ) {
340 wfDebug( __METHOD__
. "()\n" );
342 $wikiId = WikiMap
::getWikiIdFromDbDomain( WikiMap
::getCurrentWikiDbDomain() );
346 /* Resolve site name */
347 if ( $this->interwikiScopes
>= 3 && !$this->thisSite
) {
348 $site = $this->getCacheValue( '__sites:' . $wikiId );
351 $this->thisSite
= $this->fallbackSite
;
353 $this->thisSite
= $site;
357 // List of interwiki sources
360 if ( $this->interwikiScopes
>= 2 ) {
361 $sources[] = '__global';
364 if ( $this->interwikiScopes
>= 3 ) {
365 $sources[] = '_' . $this->thisSite
;
367 $sources[] = $wikiId;
369 foreach ( $sources as $source ) {
370 $list = $this->getCacheValue( '__list:' . $source );
371 foreach ( explode( ' ', $list ) as $iw_prefix ) {
372 $row = $this->getCacheValue( "{$source}:{$iw_prefix}" );
377 list( $iw_local, $iw_url ) = explode( ' ', $row );
379 if ( $local !== null && $local != $iw_local ) {
383 $data[$iw_prefix] = [
384 'iw_prefix' => $iw_prefix,
386 'iw_local' => $iw_local,
390 } catch ( CdbException
$e ) {
391 wfDebug( __METHOD__
. ": CdbException caught, error message was "
392 . $e->getMessage() );
395 return array_values( $data );
399 * Fetch all interwiki prefixes from DB
401 * @param string|null $local If not null, limits output to local/non-local interwikis
402 * @return array[] Interwiki rows
404 private function getAllPrefixesDB( $local ) {
405 $db = wfGetDB( DB_REPLICA
); // TODO: inject DB LoadBalancer
409 if ( $local !== null ) {
411 $where['iw_local'] = 1;
412 } elseif ( $local == 0 ) {
413 $where['iw_local'] = 0;
417 $res = $db->select( 'interwiki',
418 self
::selectFields(),
419 $where, __METHOD__
, [ 'ORDER BY' => 'iw_prefix' ]
423 foreach ( $res as $row ) {
424 $retval[] = (array)$row;
431 * Returns all interwiki prefixes
433 * @param string|null $local If set, limits output to local/non-local interwikis
434 * @return array[] Interwiki rows, where each row is an associative array
436 public function getAllPrefixes( $local = null ) {
437 if ( $this->cdbData
) {
438 return $this->getAllPrefixesCached( $local );
441 return $this->getAllPrefixesDB( $local );
445 * Return the list of interwiki fields that should be selected to create
446 * a new Interwiki object.
449 private static function selectFields() {