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 = array();
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
= $local;
64 $this->mTrans
= $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 * Purge the cache for an interwiki prefix
119 * @param string $prefix
122 public static function invalidateCache( $prefix ) {
123 $cache = ObjectCache
::getMainWANInstance();
124 $key = wfMemcKey( 'interwiki', $prefix );
125 $cache->delete( $key );
129 * Fetch interwiki prefix data from local cache in constant database.
131 * @note More logic is explained in DefaultSettings.
133 * @param string $prefix Interwiki prefix
136 protected static function getInterwikiCached( $prefix ) {
137 $value = self
::getInterwikiCacheEntry( $prefix );
139 $s = new Interwiki( $prefix );
142 list( $local, $url ) = explode( ' ', $value, 2 );
144 $s->mLocal
= (int)$local;
153 * Get entry from interwiki cache
155 * @note More logic is explained in DefaultSettings.
157 * @param string $prefix Database key
158 * @return bool|string The interwiki entry or false if not found
160 protected static function getInterwikiCacheEntry( $prefix ) {
161 global $wgInterwikiScopes, $wgInterwikiFallbackSite;
164 wfDebug( __METHOD__
. "( $prefix )\n" );
168 if ( $wgInterwikiScopes >= 3 && !$site ) {
169 $site = self
::getCacheValue( '__sites:' . wfWikiID() );
171 $site = $wgInterwikiFallbackSite;
175 $value = self
::getCacheValue( wfMemcKey( $prefix ) );
177 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
178 $value = self
::getCacheValue( "_{$site}:{$prefix}" );
181 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
182 $value = self
::getCacheValue( "__global:{$prefix}" );
184 if ( $value == 'undef' ) {
187 } catch ( CdbException
$e ) {
188 wfDebug( __METHOD__
. ": CdbException caught, error message was "
189 . $e->getMessage() );
195 private static function getCacheValue( $key ) {
196 global $wgInterwikiCache;
198 if ( $reader === null ) {
199 $reader = is_array( $wgInterwikiCache ) ?
false : CdbReader
::open( $wgInterwikiCache );
202 return $reader->get( $key );
204 return isset( $wgInterwikiCache[$key] ) ?
$wgInterwikiCache[$key] : false;
209 * Load the interwiki, trying first memcached then the DB
211 * @param string $prefix The interwiki prefix
212 * @return Interwiki|bool Interwiki if $prefix is valid, otherwise false
214 protected static function load( $prefix ) {
215 global $wgInterwikiExpiry;
218 if ( !Hooks
::run( 'InterwikiLoadPrefix', array( $prefix, &$iwData ) ) ) {
219 return Interwiki
::loadFromArray( $iwData );
222 if ( is_array( $iwData ) ) {
223 $iw = Interwiki
::loadFromArray( $iwData );
225 return $iw; // handled by hook
229 $iwData = ObjectCache
::getMainWANInstance()->getWithSetCallback(
230 wfMemcKey( 'interwiki', $prefix ),
232 function ( $oldValue, &$ttl, array &$setOpts ) use ( $prefix ) {
233 $dbr = wfGetDB( DB_SLAVE
);
235 $setOpts +
= Database
::getCacheSetOptions( $dbr );
237 $row = $dbr->selectRow(
239 Interwiki
::selectFields(),
240 array( 'iw_prefix' => $prefix ),
244 return $row ?
(array)$row : '!NONEXISTENT';
248 if ( is_array( $iwData ) ) {
249 return Interwiki
::loadFromArray( $iwData ) ?
: false;
256 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
258 * @param array $mc Associative array: row from the interwiki table
259 * @return Interwiki|bool Interwiki object or false if $mc['iw_url'] is not set
261 protected static function loadFromArray( $mc ) {
262 if ( isset( $mc['iw_url'] ) ) {
263 $iw = new Interwiki();
264 $iw->mURL
= $mc['iw_url'];
265 $iw->mLocal
= isset( $mc['iw_local'] ) ?
$mc['iw_local'] : 0;
266 $iw->mTrans
= isset( $mc['iw_trans'] ) ?
$mc['iw_trans'] : 0;
267 $iw->mAPI
= isset( $mc['iw_api'] ) ?
$mc['iw_api'] : '';
268 $iw->mWikiID
= isset( $mc['iw_wikiid'] ) ?
$mc['iw_wikiid'] : '';
277 * Fetch all interwiki prefixes from interwiki cache
279 * @param null|string $local If not null, limits output to local/non-local interwikis
280 * @return array List of prefixes
283 protected static function getAllPrefixesCached( $local ) {
284 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
287 wfDebug( __METHOD__
. "()\n" );
291 $db = CdbReader
::open( $wgInterwikiCache );
293 /* Resolve site name */
294 if ( $wgInterwikiScopes >= 3 && !$site ) {
295 $site = $db->get( '__sites:' . wfWikiID() );
297 $site = $wgInterwikiFallbackSite;
301 // List of interwiki sources
304 if ( $wgInterwikiScopes >= 2 ) {
305 $sources[] = '__global';
308 if ( $wgInterwikiScopes >= 3 ) {
309 $sources[] = '_' . $site;
311 $sources[] = wfWikiID();
313 foreach ( $sources as $source ) {
314 $list = $db->get( "__list:{$source}" );
315 foreach ( explode( ' ', $list ) as $iw_prefix ) {
316 $row = $db->get( "{$source}:{$iw_prefix}" );
321 list( $iw_local, $iw_url ) = explode( ' ', $row );
323 if ( $local !== null && $local != $iw_local ) {
327 $data[$iw_prefix] = array(
328 'iw_prefix' => $iw_prefix,
330 'iw_local' => $iw_local,
334 } catch ( CdbException
$e ) {
335 wfDebug( __METHOD__
. ": CdbException caught, error message was "
336 . $e->getMessage() );
341 return array_values( $data );
345 * Fetch all interwiki prefixes from DB
347 * @param string|null $local If not null, limits output to local/non-local interwikis
348 * @return array List of prefixes
351 protected static function getAllPrefixesDB( $local ) {
352 $db = wfGetDB( DB_SLAVE
);
356 if ( $local !== null ) {
358 $where['iw_local'] = 1;
359 } elseif ( $local == 0 ) {
360 $where['iw_local'] = 0;
364 $res = $db->select( 'interwiki',
365 self
::selectFields(),
366 $where, __METHOD__
, array( 'ORDER BY' => 'iw_prefix' )
370 foreach ( $res as $row ) {
371 $retval[] = (array)$row;
378 * Returns all interwiki prefixes
380 * @param string|null $local If set, limits output to local/non-local interwikis
381 * @return array List of prefixes
384 public static function getAllPrefixes( $local = null ) {
385 global $wgInterwikiCache;
387 if ( $wgInterwikiCache ) {
388 return self
::getAllPrefixesCached( $local );
391 return self
::getAllPrefixesDB( $local );
395 * Get the URL for a particular title (or with $1 if no title given)
397 * @param string $title What text to put for the article name
398 * @return string The URL
399 * @note Prior to 1.19 The getURL with an argument was broken.
400 * If you if you use this arg in an extension that supports MW earlier
401 * than 1.19 please wfUrlencode and substitute $1 on your own.
403 public function getURL( $title = null ) {
405 if ( $title !== null ) {
406 $url = str_replace( "$1", wfUrlencode( $title ), $url );
413 * Get the API URL for this wiki
415 * @return string The URL
417 public function getAPI() {
422 * Get the DB name for this wiki
424 * @return string The DB name
426 public function getWikiID() {
427 return $this->mWikiID
;
431 * Is this a local link from a sister project, or is
432 * it something outside, like Google
436 public function isLocal() {
437 return $this->mLocal
;
441 * Can pages from this wiki be transcluded?
442 * Still requires $wgEnableScaryTransclusion
446 public function isTranscludable() {
447 return $this->mTrans
;
451 * Get the name for the interwiki site
455 public function getName() {
456 $msg = wfMessage( 'interwiki-name-' . $this->mPrefix
)->inContentLanguage();
458 return !$msg->exists() ?
'' : $msg;
462 * Get a description for this interwiki
466 public function getDescription() {
467 $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix
)->inContentLanguage();
469 return !$msg->exists() ?
'' : $msg;
473 * Return the list of interwiki fields that should be selected to create
474 * a new Interwiki object.
477 public static function selectFields() {