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
25 * All information is loaded on creation when called by Interwiki::fetch( $prefix ).
26 * All work is done on slave, because this should *never* change (except during
27 * schema updates etc, which aren't wiki-related)
30 // Cache - removes oldest entry when it hits limit
31 protected static $smCache = array();
32 const CACHE_LIMIT
= 100; // 0 means unlimited, any other value is max number of entries.
34 /** @var string The interwiki prefix, (e.g. "Meatball", or the language prefix "de") */
37 /** @var string The URL of the wiki, with "$1" as a placeholder for an article name. */
40 /** @var string The URL of the file api.php */
43 /** @var string The name of the database (for a connection to be established
44 * with wfGetLB( 'wikiid' ))
48 /** @var bool whether the wiki is in this project */
51 /** @var bool Whether interwiki transclusions are allowed */
54 public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0,
57 $this->mPrefix
= $prefix;
60 $this->mWikiID
= $wikiId;
61 $this->mLocal
= $local;
62 $this->mTrans
= $trans;
66 * Check whether an interwiki prefix exists
68 * @param string $prefix Interwiki prefix to use
69 * @return bool Whether it exists
71 public static function isValidInterwiki( $prefix ) {
72 $result = self
::fetch( $prefix );
78 * Fetch an Interwiki object
80 * @param string $prefix Interwiki prefix to use
81 * @return Interwiki|null|bool
83 public static function fetch( $prefix ) {
86 if ( $prefix == '' ) {
90 $prefix = $wgContLang->lc( $prefix );
91 if ( isset( self
::$smCache[$prefix] ) ) {
92 return self
::$smCache[$prefix];
95 global $wgInterwikiCache;
96 if ( $wgInterwikiCache ) {
97 $iw = Interwiki
::getInterwikiCached( $prefix );
99 $iw = Interwiki
::load( $prefix );
105 if ( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
) {
106 reset( self
::$smCache );
107 unset( self
::$smCache[key( self
::$smCache )] );
110 self
::$smCache[$prefix] = $iw;
116 * Fetch interwiki prefix data from local cache in constant database.
118 * @note More logic is explained in DefaultSettings.
120 * @param string $prefix Interwiki prefix
121 * @return Interwiki object
123 protected static function getInterwikiCached( $prefix ) {
124 $value = self
::getInterwikiCacheEntry( $prefix );
126 $s = new Interwiki( $prefix );
127 if ( $value != '' ) {
129 list( $local, $url ) = explode( ' ', $value, 2 );
131 $s->mLocal
= (int)$local;
140 * Get entry from interwiki cache
142 * @note More logic is explained in DefaultSettings.
144 * @param string $prefix Database key
145 * @return string The interwiki entry
147 protected static function getInterwikiCacheEntry( $prefix ) {
148 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
151 wfDebug( __METHOD__
. "( $prefix )\n" );
155 $db = CdbReader
::open( $wgInterwikiCache );
157 /* Resolve site name */
158 if ( $wgInterwikiScopes >= 3 && !$site ) {
159 $site = $db->get( '__sites:' . wfWikiID() );
161 $site = $wgInterwikiFallbackSite;
165 $value = $db->get( wfMemcKey( $prefix ) );
167 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
168 $value = $db->get( "_{$site}:{$prefix}" );
171 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
172 $value = $db->get( "__global:{$prefix}" );
174 if ( $value == 'undef' ) {
177 } catch ( CdbException
$e ) {
178 wfDebug( __METHOD__
. ": CdbException caught, error message was "
179 . $e->getMessage() );
186 * Load the interwiki, trying first memcached then the DB
188 * @param string $prefix The interwiki prefix
189 * @return Interwiki|bool Interwiki if $prefix is valid, otherwise false
191 protected static function load( $prefix ) {
192 global $wgMemc, $wgInterwikiExpiry;
195 if ( !wfRunHooks( 'InterwikiLoadPrefix', array( $prefix, &$iwData ) ) ) {
196 return Interwiki
::loadFromArray( $iwData );
200 $key = wfMemcKey( 'interwiki', $prefix );
201 $iwData = $wgMemc->get( $key );
202 if ( $iwData === '!NONEXISTENT' ) {
203 // negative cache hit
208 // is_array is hack for old keys
209 if ( $iwData && is_array( $iwData ) ) {
210 $iw = Interwiki
::loadFromArray( $iwData );
216 $db = wfGetDB( DB_SLAVE
);
218 $row = $db->fetchRow( $db->select(
220 self
::selectFields(),
221 array( 'iw_prefix' => $prefix ),
225 $iw = Interwiki
::loadFromArray( $row );
228 'iw_url' => $iw->mURL
,
229 'iw_api' => $iw->mAPI
,
230 'iw_local' => $iw->mLocal
,
231 'iw_trans' => $iw->mTrans
233 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
238 // negative cache hit
239 $wgMemc->add( $key, '!NONEXISTENT', $wgInterwikiExpiry );
245 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
247 * @param array $mc Associative array: row from the interwiki table
248 * @return Interwiki|bool Interwiki object or false if $mc['iw_url'] is not set
250 protected static function loadFromArray( $mc ) {
251 if ( isset( $mc['iw_url'] ) ) {
252 $iw = new Interwiki();
253 $iw->mURL
= $mc['iw_url'];
254 $iw->mLocal
= isset( $mc['iw_local'] ) ?
$mc['iw_local'] : 0;
255 $iw->mTrans
= isset( $mc['iw_trans'] ) ?
$mc['iw_trans'] : 0;
256 $iw->mAPI
= isset( $mc['iw_api'] ) ?
$mc['iw_api'] : '';
257 $iw->mWikiID
= isset( $mc['iw_wikiid'] ) ?
$mc['iw_wikiid'] : '';
266 * Fetch all interwiki prefixes from interwiki cache
268 * @param null|string $local If not null, limits output to local/non-local interwikis
269 * @return array List of prefixes
272 protected static function getAllPrefixesCached( $local ) {
273 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
276 wfDebug( __METHOD__
. "()\n" );
280 $db = CdbReader
::open( $wgInterwikiCache );
282 /* Resolve site name */
283 if ( $wgInterwikiScopes >= 3 && !$site ) {
284 $site = $db->get( '__sites:' . wfWikiID() );
286 $site = $wgInterwikiFallbackSite;
290 // List of interwiki sources
293 if ( $wgInterwikiScopes >= 2 ) {
294 $sources[] = '__global';
297 if ( $wgInterwikiScopes >= 3 ) {
298 $sources[] = '_' . $site;
300 $sources[] = wfWikiID();
302 foreach ( $sources as $source ) {
303 $list = $db->get( "__list:{$source}" );
304 foreach ( explode( ' ', $list ) as $iw_prefix ) {
305 $row = $db->get( "{$source}:{$iw_prefix}" );
310 list( $iw_local, $iw_url ) = explode( ' ', $row );
312 if ( $local !== null && $local != $iw_local ) {
316 $data[$iw_prefix] = array(
317 'iw_prefix' => $iw_prefix,
319 'iw_local' => $iw_local,
323 } catch ( CdbException
$e ) {
324 wfDebug( __METHOD__
. ": CdbException caught, error message was "
325 . $e->getMessage() );
330 return array_values( $data );
334 * Fetch all interwiki prefixes from DB
336 * @param string|null $local If not null, limits output to local/non-local interwikis
337 * @return array List of prefixes
340 protected static function getAllPrefixesDB( $local ) {
341 $db = wfGetDB( DB_SLAVE
);
345 if ( $local !== null ) {
347 $where['iw_local'] = 1;
348 } elseif ( $local == 0 ) {
349 $where['iw_local'] = 0;
353 $res = $db->select( 'interwiki',
354 self
::selectFields(),
355 $where, __METHOD__
, array( 'ORDER BY' => 'iw_prefix' )
359 foreach ( $res as $row ) {
360 $retval[] = (array)$row;
367 * Returns all interwiki prefixes
369 * @param string|null $local If set, limits output to local/non-local interwikis
370 * @return array List of prefixes
373 public static function getAllPrefixes( $local = null ) {
374 global $wgInterwikiCache;
376 if ( $wgInterwikiCache ) {
377 return self
::getAllPrefixesCached( $local );
380 return self
::getAllPrefixesDB( $local );
384 * Get the URL for a particular title (or with $1 if no title given)
386 * @param string $title What text to put for the article name
387 * @return string The URL
388 * @note Prior to 1.19 The getURL with an argument was broken.
389 * If you if you use this arg in an extension that supports MW earlier
390 * than 1.19 please wfUrlencode and substitute $1 on your own.
392 public function getURL( $title = null ) {
394 if ( $title !== null ) {
395 $url = str_replace( "$1", wfUrlencode( $title ), $url );
402 * Get the API URL for this wiki
404 * @return string The URL
406 public function getAPI() {
411 * Get the DB name for this wiki
413 * @return string The DB name
415 public function getWikiID() {
416 return $this->mWikiID
;
420 * Is this a local link from a sister project, or is
421 * it something outside, like Google
425 public function isLocal() {
426 return $this->mLocal
;
430 * Can pages from this wiki be transcluded?
431 * Still requires $wgEnableScaryTransclusion
435 public function isTranscludable() {
436 return $this->mTrans
;
440 * Get the name for the interwiki site
444 public function getName() {
445 $msg = wfMessage( 'interwiki-name-' . $this->mPrefix
)->inContentLanguage();
447 return !$msg->exists() ?
'' : $msg;
451 * Get a description for this interwiki
455 public function getDescription() {
456 $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix
)->inContentLanguage();
458 return !$msg->exists() ?
'' : $msg;
462 * Return the list of interwiki fields that should be selected to create
463 * a new Interwiki object.
466 public static function selectFields() {