2 namespace MediaWiki\Interwiki
;
5 * InterwikiLookup implementing the "classic" interwiki storage (hardcoded up to MW 1.26).
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
24 use \Cdb\Exception
as CdbException
;
25 use \Cdb\Reader
as CdbReader
;
34 * InterwikiLookup implementing the "classic" interwiki storage (hardcoded up to MW 1.26).
36 * This implements two levels of caching (in-process array and a WANObjectCache)
37 * and tree storage backends (SQL, CDB, and plain PHP arrays).
39 * All information is loaded on creation when called by $this->fetch( $prefix ).
40 * All work is done on slave, because this should *never* change (except during
41 * schema updates etc, which aren't wiki-related)
45 class ClassicInterwikiLookup
implements InterwikiLookup
{
55 private $contentLanguage;
65 private $objectCacheExpiry;
68 * @var bool|array|string
75 private $interwikiScopes;
80 private $fallbackSite;
85 private $cdbReader = null;
90 private $thisSite = null;
93 * @param Language $contentLanguage Language object used to convert prefixes to lower case
94 * @param WANObjectCache $objectCache Cache for interwiki info retrieved from the database
95 * @param int $objectCacheExpiry Expiry time for $objectCache, in seconds
96 * @param bool|array|string $cdbData The path of a CDB file, or
97 * an array resembling the contents of a CDB file,
98 * or false to use the database.
99 * @param int $interwikiScopes Specify number of domains to check for messages:
100 * - 1: Just local wiki level
101 * - 2: wiki and global levels
102 * - 3: site level as well as wiki and global levels
103 * @param string $fallbackSite The code to assume for the local site,
105 function __construct(
106 Language
$contentLanguage,
107 WANObjectCache
$objectCache,
113 $this->localCache
= new MapCacheLRU( 100 );
115 $this->contentLanguage
= $contentLanguage;
116 $this->objectCache
= $objectCache;
117 $this->objectCacheExpiry
= $objectCacheExpiry;
118 $this->cdbData
= $cdbData;
119 $this->interwikiScopes
= $interwikiScopes;
120 $this->fallbackSite
= $fallbackSite;
124 * Check whether an interwiki prefix exists
126 * @param string $prefix Interwiki prefix to use
127 * @return bool Whether it exists
129 public function isValidInterwiki( $prefix ) {
130 $result = $this->fetch( $prefix );
132 return (bool)$result;
136 * Fetch an Interwiki object
138 * @param string $prefix Interwiki prefix to use
139 * @return Interwiki|null|bool
141 public function fetch( $prefix ) {
142 if ( $prefix == '' ) {
146 $prefix = $this->contentLanguage
->lc( $prefix );
147 if ( $this->localCache
->has( $prefix ) ) {
148 return $this->localCache
->get( $prefix );
151 if ( $this->cdbData
) {
152 $iw = $this->getInterwikiCached( $prefix );
154 $iw = $this->load( $prefix );
159 $this->localCache
->set( $prefix, $iw );
165 * Resets locally cached Interwiki objects. This is intended for use during testing only.
166 * This does not invalidate entries in the persistent cache, as invalidateCache() does.
169 public function resetLocalCache() {
170 $this->localCache
->clear();
174 * Purge the in-process and object cache for an interwiki prefix
175 * @param string $prefix
177 public function invalidateCache( $prefix ) {
178 $this->localCache
->clear( $prefix );
180 $key = $this->objectCache
->makeKey( 'interwiki', $prefix );
181 $this->objectCache
->delete( $key );
185 * Fetch interwiki prefix data from local cache in constant database.
187 * @note More logic is explained in DefaultSettings.
189 * @param string $prefix Interwiki prefix
192 private function getInterwikiCached( $prefix ) {
193 $value = $this->getInterwikiCacheEntry( $prefix );
197 list( $local, $url ) = explode( ' ', $value, 2 );
198 return new Interwiki( $prefix, $url, '', '', (int)$local );
205 * Get entry from interwiki cache
207 * @note More logic is explained in DefaultSettings.
209 * @param string $prefix Database key
210 * @return bool|string The interwiki entry or false if not found
212 private function getInterwikiCacheEntry( $prefix ) {
213 wfDebug( __METHOD__
. "( $prefix )\n" );
217 if ( $this->interwikiScopes
>= 3 && !$this->thisSite
) {
218 $this->thisSite
= $this->getCacheValue( '__sites:' . wfWikiID() );
219 if ( $this->thisSite
== '' ) {
220 $this->thisSite
= $this->fallbackSite
;
224 $value = $this->getCacheValue( wfMemcKey( $prefix ) );
226 if ( $value == '' && $this->interwikiScopes
>= 3 ) {
227 $value = $this->getCacheValue( "_{$this->thisSite}:{$prefix}" );
230 if ( $value == '' && $this->interwikiScopes
>= 2 ) {
231 $value = $this->getCacheValue( "__global:{$prefix}" );
233 if ( $value == 'undef' ) {
236 } catch ( CdbException
$e ) {
237 wfDebug( __METHOD__
. ": CdbException caught, error message was "
238 . $e->getMessage() );
244 private function getCacheValue( $key ) {
245 if ( $this->cdbReader
=== null ) {
246 if ( is_string( $this->cdbData
) ) {
247 $this->cdbReader
= \Cdb\Reader
::open( $this->cdbData
);
248 } elseif ( is_array( $this->cdbData
) ) {
249 $this->cdbReader
= new \Cdb\Reader\
Hash( $this->cdbData
);
251 $this->cdbReader
= false;
255 if ( $this->cdbReader
) {
256 return $this->cdbReader
->get( $key );
263 * Load the interwiki, trying first memcached then the DB
265 * @param string $prefix The interwiki prefix
266 * @return Interwiki|bool Interwiki if $prefix is valid, otherwise false
268 private function load( $prefix ) {
270 if ( !Hooks
::run( 'InterwikiLoadPrefix', [ $prefix, &$iwData ] ) ) {
271 return $this->loadFromArray( $iwData );
274 if ( is_array( $iwData ) ) {
275 $iw = $this->loadFromArray( $iwData );
277 return $iw; // handled by hook
281 $iwData = $this->objectCache
->getWithSetCallback(
282 $this->objectCache
->makeKey( 'interwiki', $prefix ),
283 $this->objectCacheExpiry
,
284 function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix ) {
285 $dbr = wfGetDB( DB_SLAVE
); // TODO: inject LoadBalancer
287 $setOpts +
= Database
::getCacheSetOptions( $dbr );
289 $row = $dbr->selectRow(
291 ClassicInterwikiLookup
::selectFields(),
292 [ 'iw_prefix' => $prefix ],
296 return $row ?
(array)$row : '!NONEXISTENT';
300 if ( is_array( $iwData ) ) {
301 return $this->loadFromArray( $iwData ) ?
: false;
308 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
310 * @param array $mc Associative array: row from the interwiki table
311 * @return Interwiki|bool Interwiki object or false if $mc['iw_url'] is not set
313 private function loadFromArray( $mc ) {
314 if ( isset( $mc['iw_url'] ) ) {
315 $url = $mc['iw_url'];
316 $local = isset( $mc['iw_local'] ) ?
$mc['iw_local'] : 0;
317 $trans = isset( $mc['iw_trans'] ) ?
$mc['iw_trans'] : 0;
318 $api = isset( $mc['iw_api'] ) ?
$mc['iw_api'] : '';
319 $wikiId = isset( $mc['iw_wikiid'] ) ?
$mc['iw_wikiid'] : '';
321 return new Interwiki( null, $url, $api, $wikiId, $local, $trans );
328 * Fetch all interwiki prefixes from interwiki cache
330 * @param null|string $local If not null, limits output to local/non-local interwikis
331 * @return array List of prefixes, where each row is an associative array
333 private function getAllPrefixesCached( $local ) {
334 wfDebug( __METHOD__
. "()\n" );
337 /* Resolve site name */
338 if ( $this->interwikiScopes
>= 3 && !$this->thisSite
) {
339 $site = $this->getCacheValue( '__sites:' . wfWikiID() );
342 $this->thisSite
= $this->fallbackSite
;
344 $this->thisSite
= $site;
348 // List of interwiki sources
351 if ( $this->interwikiScopes
>= 2 ) {
352 $sources[] = '__global';
355 if ( $this->interwikiScopes
>= 3 ) {
356 $sources[] = '_' . $this->thisSite
;
358 $sources[] = wfWikiID();
360 foreach ( $sources as $source ) {
361 $list = $this->getCacheValue( '__list:' . $source );
362 foreach ( explode( ' ', $list ) as $iw_prefix ) {
363 $row = $this->getCacheValue( "{$source}:{$iw_prefix}" );
368 list( $iw_local, $iw_url ) = explode( ' ', $row );
370 if ( $local !== null && $local != $iw_local ) {
374 $data[$iw_prefix] = [
375 'iw_prefix' => $iw_prefix,
377 'iw_local' => $iw_local,
381 } catch ( CdbException
$e ) {
382 wfDebug( __METHOD__
. ": CdbException caught, error message was "
383 . $e->getMessage() );
388 return array_values( $data );
392 * Fetch all interwiki prefixes from DB
394 * @param string|null $local If not null, limits output to local/non-local interwikis
395 * @return array[] Interwiki rows
397 private function getAllPrefixesDB( $local ) {
398 $db = wfGetDB( DB_SLAVE
); // TODO: inject DB LoadBalancer
402 if ( $local !== null ) {
404 $where['iw_local'] = 1;
405 } elseif ( $local == 0 ) {
406 $where['iw_local'] = 0;
410 $res = $db->select( 'interwiki',
411 $this->selectFields(),
412 $where, __METHOD__
, [ 'ORDER BY' => 'iw_prefix' ]
416 foreach ( $res as $row ) {
417 $retval[] = (array)$row;
424 * Returns all interwiki prefixes
426 * @param string|null $local If set, limits output to local/non-local interwikis
427 * @return array[] Interwiki rows, where each row is an associative array
429 public function getAllPrefixes( $local = null ) {
430 if ( $this->cdbData
) {
431 return $this->getAllPrefixesCached( $local );
434 return $this->getAllPrefixesDB( $local );
438 * Return the list of interwiki fields that should be selected to create
439 * a new Interwiki object.
442 private static function selectFields() {