3 * Interwiki table entry.
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
22 use \Cdb\Exception
as CdbException
;
23 use \Cdb\Reader
as CdbReader
;
27 * All information is loaded on creation when called by Interwiki::fetch( $prefix ).
28 * All work is done on slave, because this should *never* change (except during
29 * schema updates etc, which aren't wiki-related)
32 // Cache - removes oldest entry when it hits limit
33 protected static $smCache = [];
34 const CACHE_LIMIT
= 100; // 0 means unlimited, any other value is max number of entries.
36 /** @var string The interwiki prefix, (e.g. "Meatball", or the language prefix "de") */
39 /** @var string The URL of the wiki, with "$1" as a placeholder for an article name. */
42 /** @var string The URL of the file api.php */
45 /** @var string The name of the database (for a connection to be established
46 * with wfGetLB( 'wikiid' ))
50 /** @var bool Whether the wiki is in this project */
53 /** @var bool Whether interwiki transclusions are allowed */
56 public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0,
59 $this->mPrefix
= $prefix;
62 $this->mWikiID
= $wikiId;
63 $this->mLocal
= (bool)$local;
64 $this->mTrans
= (bool)$trans;
68 * Check whether an interwiki prefix exists
70 * @param string $prefix Interwiki prefix to use
71 * @return bool Whether it exists
73 public static function isValidInterwiki( $prefix ) {
74 $result = self
::fetch( $prefix );
80 * Fetch an Interwiki object
82 * @param string $prefix Interwiki prefix to use
83 * @return Interwiki|null|bool
85 public static function fetch( $prefix ) {
88 if ( $prefix == '' ) {
92 $prefix = $wgContLang->lc( $prefix );
93 if ( isset( self
::$smCache[$prefix] ) ) {
94 return self
::$smCache[$prefix];
97 global $wgInterwikiCache;
98 if ( $wgInterwikiCache ) {
99 $iw = Interwiki
::getInterwikiCached( $prefix );
101 $iw = Interwiki
::load( $prefix );
107 if ( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
) {
108 reset( self
::$smCache );
109 unset( self
::$smCache[key( self
::$smCache )] );
112 self
::$smCache[$prefix] = $iw;
118 * Resets locally cached Interwiki objects. This is intended for use during testing only.
119 * This does not invalidate entries in the persistent cache, as invalidateCache() does.
122 public static function resetLocalCache() {
123 static::$smCache = [];
127 * Purge the cache (local and persistent) for an interwiki prefix.
128 * @param string $prefix
131 public static function invalidateCache( $prefix ) {
132 $cache = ObjectCache
::getMainWANInstance();
133 $key = wfMemcKey( 'interwiki', $prefix );
134 $cache->delete( $key );
135 unset( static::$smCache[$prefix] );
139 * Fetch interwiki prefix data from local cache in constant database.
141 * @note More logic is explained in DefaultSettings.
143 * @param string $prefix Interwiki prefix
146 protected static function getInterwikiCached( $prefix ) {
147 $value = self
::getInterwikiCacheEntry( $prefix );
149 $s = new Interwiki( $prefix );
152 list( $local, $url ) = explode( ' ', $value, 2 );
154 $s->mLocal
= (bool)$local;
163 * Get entry from interwiki cache
165 * @note More logic is explained in DefaultSettings.
167 * @param string $prefix Database key
168 * @return bool|string The interwiki entry or false if not found
170 protected static function getInterwikiCacheEntry( $prefix ) {
171 global $wgInterwikiScopes, $wgInterwikiFallbackSite;
177 if ( $wgInterwikiScopes >= 3 && !$site ) {
178 $site = self
::getCacheValue( '__sites:' . wfWikiID() );
180 $site = $wgInterwikiFallbackSite;
184 $value = self
::getCacheValue( wfMemcKey( $prefix ) );
186 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
187 $value = self
::getCacheValue( "_{$site}:{$prefix}" );
190 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
191 $value = self
::getCacheValue( "__global:{$prefix}" );
193 if ( $value == 'undef' ) {
196 } catch ( CdbException
$e ) {
197 wfDebug( __METHOD__
. ": CdbException caught, error message was "
198 . $e->getMessage() );
204 private static function getCacheValue( $key ) {
205 global $wgInterwikiCache;
207 if ( $reader === null ) {
208 $reader = is_array( $wgInterwikiCache ) ?
false : CdbReader
::open( $wgInterwikiCache );
211 return $reader->get( $key );
213 return isset( $wgInterwikiCache[$key] ) ?
$wgInterwikiCache[$key] : false;
218 * Load the interwiki, trying first memcached then the DB
220 * @param string $prefix The interwiki prefix
221 * @return Interwiki|bool Interwiki if $prefix is valid, otherwise false
223 protected static function load( $prefix ) {
224 global $wgInterwikiExpiry;
227 if ( !Hooks
::run( 'InterwikiLoadPrefix', [ $prefix, &$iwData ] ) ) {
228 return Interwiki
::loadFromArray( $iwData );
231 if ( is_array( $iwData ) ) {
232 $iw = Interwiki
::loadFromArray( $iwData );
234 return $iw; // handled by hook
238 $iwData = ObjectCache
::getMainWANInstance()->getWithSetCallback(
239 wfMemcKey( 'interwiki', $prefix ),
241 function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix ) {
242 $dbr = wfGetDB( DB_SLAVE
);
244 $setOpts +
= Database
::getCacheSetOptions( $dbr );
246 $row = $dbr->selectRow(
248 Interwiki
::selectFields(),
249 [ 'iw_prefix' => $prefix ],
253 return $row ?
(array)$row : '!NONEXISTENT';
257 if ( is_array( $iwData ) ) {
258 return Interwiki
::loadFromArray( $iwData ) ?
: false;
265 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
267 * @param array $mc Associative array: row from the interwiki table
268 * @return Interwiki|bool Interwiki object or false if $mc['iw_url'] is not set
270 protected static function loadFromArray( $mc ) {
271 if ( isset( $mc['iw_url'] ) ) {
272 $iw = new Interwiki();
273 $iw->mURL
= $mc['iw_url'];
274 $iw->mLocal
= isset( $mc['iw_local'] ) ?
(bool)$mc['iw_local'] : false;
275 $iw->mTrans
= isset( $mc['iw_trans'] ) ?
(bool)$mc['iw_trans'] : false;
276 $iw->mAPI
= isset( $mc['iw_api'] ) ?
$mc['iw_api'] : '';
277 $iw->mWikiID
= isset( $mc['iw_wikiid'] ) ?
$mc['iw_wikiid'] : '';
286 * Fetch all interwiki prefixes from interwiki cache
288 * @param null|string $local If not null, limits output to local/non-local interwikis
289 * @return array List of prefixes
292 protected static function getAllPrefixesCached( $local ) {
293 global $wgInterwikiScopes, $wgInterwikiFallbackSite;
296 wfDebug( __METHOD__
. "()\n" );
299 /* Resolve site name */
300 if ( $wgInterwikiScopes >= 3 && !$site ) {
301 $site = self
::getCacheValue( '__sites:' . wfWikiID() );
304 $site = $wgInterwikiFallbackSite;
308 // List of interwiki sources
311 if ( $wgInterwikiScopes >= 2 ) {
312 $sources[] = '__global';
315 if ( $wgInterwikiScopes >= 3 ) {
316 $sources[] = '_' . $site;
318 $sources[] = wfWikiID();
320 foreach ( $sources as $source ) {
321 $list = self
::getCacheValue( '__list:' . $source );
322 foreach ( explode( ' ', $list ) as $iw_prefix ) {
323 $row = self
::getCacheValue( "{$source}:{$iw_prefix}" );
328 list( $iw_local, $iw_url ) = explode( ' ', $row );
330 if ( $local !== null && $local != $iw_local ) {
334 $data[$iw_prefix] = [
335 'iw_prefix' => $iw_prefix,
337 'iw_local' => $iw_local,
341 } catch ( CdbException
$e ) {
342 wfDebug( __METHOD__
. ": CdbException caught, error message was "
343 . $e->getMessage() );
348 return array_values( $data );
352 * Fetch all interwiki prefixes from DB
354 * @param string|null $local If not null, limits output to local/non-local interwikis
355 * @return array List of prefixes
358 protected static function getAllPrefixesDB( $local ) {
359 $db = wfGetDB( DB_SLAVE
);
363 if ( $local !== null ) {
365 $where['iw_local'] = 1;
366 } elseif ( $local == 0 ) {
367 $where['iw_local'] = 0;
371 $res = $db->select( 'interwiki',
372 self
::selectFields(),
373 $where, __METHOD__
, [ 'ORDER BY' => 'iw_prefix' ]
377 foreach ( $res as $row ) {
378 $retval[] = (array)$row;
385 * Returns all interwiki prefixes
387 * @param string|null $local If set, limits output to local/non-local interwikis
388 * @return array List of prefixes
391 public static function getAllPrefixes( $local = null ) {
392 global $wgInterwikiCache;
394 if ( $wgInterwikiCache ) {
395 return self
::getAllPrefixesCached( $local );
398 return self
::getAllPrefixesDB( $local );
402 * Get the URL for a particular title (or with $1 if no title given)
404 * @param string $title What text to put for the article name
405 * @return string The URL
406 * @note Prior to 1.19 The getURL with an argument was broken.
407 * If you if you use this arg in an extension that supports MW earlier
408 * than 1.19 please wfUrlencode and substitute $1 on your own.
410 public function getURL( $title = null ) {
412 if ( $title !== null ) {
413 $url = str_replace( "$1", wfUrlencode( $title ), $url );
420 * Get the API URL for this wiki
422 * @return string The URL
424 public function getAPI() {
429 * Get the DB name for this wiki
431 * @return string The DB name
433 public function getWikiID() {
434 return $this->mWikiID
;
438 * Is this a local link from a sister project, or is
439 * it something outside, like Google
443 public function isLocal() {
444 return $this->mLocal
;
448 * Can pages from this wiki be transcluded?
449 * Still requires $wgEnableScaryTransclusion
453 public function isTranscludable() {
454 return $this->mTrans
;
458 * Get the name for the interwiki site
462 public function getName() {
463 $msg = wfMessage( 'interwiki-name-' . $this->mPrefix
)->inContentLanguage();
465 return !$msg->exists() ?
'' : $msg->text();
469 * Get a description for this interwiki
473 public function getDescription() {
474 $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix
)->inContentLanguage();
476 return !$msg->exists() ?
'' : $msg->text();
480 * Return the list of interwiki fields that should be selected to create
481 * a new Interwiki object.
484 public static function selectFields() {