Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / interwiki / Interwiki.php
blobeacf9a87cd5f14c7af30c0b5b5ac88b1159e2772
1 <?php
2 /**
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
20 * @file
23 /**
24 * The interwiki class
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)
29 class Interwiki {
31 // Cache - removes oldest entry when it hits limit
32 protected static $smCache = array();
33 const CACHE_LIMIT = 100; // 0 means unlimited, any other value is max number of entries.
35 protected $mPrefix, $mURL, $mAPI, $mWikiID, $mLocal, $mTrans;
37 public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0, $trans = 0 ) {
38 $this->mPrefix = $prefix;
39 $this->mURL = $url;
40 $this->mAPI = $api;
41 $this->mWikiID = $wikiId;
42 $this->mLocal = $local;
43 $this->mTrans = $trans;
46 /**
47 * Check whether an interwiki prefix exists
49 * @param $prefix String: interwiki prefix to use
50 * @return Boolean: whether it exists
52 static public function isValidInterwiki( $prefix ) {
53 $result = self::fetch( $prefix );
54 return (bool)$result;
57 /**
58 * Fetch an Interwiki object
60 * @param $prefix String: interwiki prefix to use
61 * @return Interwiki|null|bool
63 static public function fetch( $prefix ) {
64 global $wgContLang;
65 if( $prefix == '' ) {
66 return null;
68 $prefix = $wgContLang->lc( $prefix );
69 if( isset( self::$smCache[$prefix] ) ) {
70 return self::$smCache[$prefix];
72 global $wgInterwikiCache;
73 if( $wgInterwikiCache ) {
74 $iw = Interwiki::getInterwikiCached( $prefix );
75 } else {
76 $iw = Interwiki::load( $prefix );
77 if( !$iw ) {
78 $iw = false;
81 if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
82 reset( self::$smCache );
83 unset( self::$smCache[key( self::$smCache )] );
85 self::$smCache[$prefix] = $iw;
86 return $iw;
89 /**
90 * Fetch interwiki prefix data from local cache in constant database.
92 * @note More logic is explained in DefaultSettings.
94 * @param $prefix String: interwiki prefix
95 * @return Interwiki object
97 protected static function getInterwikiCached( $prefix ) {
98 $value = self::getInterwikiCacheEntry( $prefix );
100 $s = new Interwiki( $prefix );
101 if ( $value != '' ) {
102 // Split values
103 list( $local, $url ) = explode( ' ', $value, 2 );
104 $s->mURL = $url;
105 $s->mLocal = (int)$local;
106 } else {
107 $s = false;
109 return $s;
113 * Get entry from interwiki cache
115 * @note More logic is explained in DefaultSettings.
117 * @param $prefix String: database key
118 * @return String: the entry
120 protected static function getInterwikiCacheEntry( $prefix ) {
121 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
122 static $db, $site;
124 wfDebug( __METHOD__ . "( $prefix )\n" );
125 if( !$db ) {
126 $db = CdbReader::open( $wgInterwikiCache );
128 /* Resolve site name */
129 if( $wgInterwikiScopes >= 3 && !$site ) {
130 $site = $db->get( '__sites:' . wfWikiID() );
131 if ( $site == '' ) {
132 $site = $wgInterwikiFallbackSite;
136 $value = $db->get( wfMemcKey( $prefix ) );
137 // Site level
138 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
139 $value = $db->get( "_{$site}:{$prefix}" );
141 // Global Level
142 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
143 $value = $db->get( "__global:{$prefix}" );
145 if ( $value == 'undef' ) {
146 $value = '';
150 return $value;
154 * Load the interwiki, trying first memcached then the DB
156 * @param $prefix string The interwiki prefix
157 * @return Boolean: the prefix is valid
159 protected static function load( $prefix ) {
160 global $wgMemc, $wgInterwikiExpiry;
162 $iwData = false;
163 if ( !wfRunHooks( 'InterwikiLoadPrefix', array( $prefix, &$iwData ) ) ) {
164 return Interwiki::loadFromArray( $iwData );
167 if ( !$iwData ) {
168 $key = wfMemcKey( 'interwiki', $prefix );
169 $iwData = $wgMemc->get( $key );
170 if ( $iwData === '!NONEXISTENT' ) {
171 return false; // negative cache hit
175 if( $iwData && is_array( $iwData ) ) { // is_array is hack for old keys
176 $iw = Interwiki::loadFromArray( $iwData );
177 if( $iw ) {
178 return $iw;
182 $db = wfGetDB( DB_SLAVE );
184 $row = $db->fetchRow( $db->select( 'interwiki', self::selectFields(), array( 'iw_prefix' => $prefix ),
185 __METHOD__ ) );
186 $iw = Interwiki::loadFromArray( $row );
187 if ( $iw ) {
188 $mc = array(
189 'iw_url' => $iw->mURL,
190 'iw_api' => $iw->mAPI,
191 'iw_local' => $iw->mLocal,
192 'iw_trans' => $iw->mTrans
194 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
195 return $iw;
196 } else {
197 $wgMemc->add( $key, '!NONEXISTENT', $wgInterwikiExpiry ); // negative cache hit
200 return false;
204 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
206 * @param $mc array Associative array: row from the interwiki table
207 * @return Boolean|Interwiki whether everything was there
209 protected static function loadFromArray( $mc ) {
210 if( isset( $mc['iw_url'] ) ) {
211 $iw = new Interwiki();
212 $iw->mURL = $mc['iw_url'];
213 $iw->mLocal = isset( $mc['iw_local'] ) ? $mc['iw_local'] : 0;
214 $iw->mTrans = isset( $mc['iw_trans'] ) ? $mc['iw_trans'] : 0;
215 $iw->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : '';
216 $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : '';
218 return $iw;
220 return false;
224 * Fetch all interwiki prefixes from interwiki cache
226 * @param $local null|string If not null, limits output to local/non-local interwikis
227 * @return Array List of prefixes
228 * @since 1.19
230 protected static function getAllPrefixesCached( $local ) {
231 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
232 static $db, $site;
234 wfDebug( __METHOD__ . "()\n" );
235 if( !$db ) {
236 $db = CdbReader::open( $wgInterwikiCache );
238 /* Resolve site name */
239 if( $wgInterwikiScopes >= 3 && !$site ) {
240 $site = $db->get( '__sites:' . wfWikiID() );
241 if ( $site == '' ) {
242 $site = $wgInterwikiFallbackSite;
246 // List of interwiki sources
247 $sources = array();
248 // Global Level
249 if ( $wgInterwikiScopes >= 2 ) {
250 $sources[] = '__global';
252 // Site level
253 if ( $wgInterwikiScopes >= 3 ) {
254 $sources[] = '_' . $site;
256 $sources[] = wfWikiID();
258 $data = array();
260 foreach( $sources as $source ) {
261 $list = $db->get( "__list:{$source}" );
262 foreach ( explode( ' ', $list ) as $iw_prefix ) {
263 $row = $db->get( "{$source}:{$iw_prefix}" );
264 if( !$row ) {
265 continue;
268 list( $iw_local, $iw_url ) = explode( ' ', $row );
270 if ( $local !== null && $local != $iw_local ) {
271 continue;
274 $data[$iw_prefix] = array(
275 'iw_prefix' => $iw_prefix,
276 'iw_url' => $iw_url,
277 'iw_local' => $iw_local,
282 ksort( $data );
284 return array_values( $data );
288 * Fetch all interwiki prefixes from DB
290 * @param $local string|null If not null, limits output to local/non-local interwikis
291 * @return Array List of prefixes
292 * @since 1.19
294 protected static function getAllPrefixesDB( $local ) {
295 $db = wfGetDB( DB_SLAVE );
297 $where = array();
299 if ( $local !== null ) {
300 if ( $local == 1 ) {
301 $where['iw_local'] = 1;
302 } elseif ( $local == 0 ) {
303 $where['iw_local'] = 0;
307 $res = $db->select( 'interwiki',
308 self::selectFields(),
309 $where, __METHOD__, array( 'ORDER BY' => 'iw_prefix' )
311 $retval = array();
312 foreach ( $res as $row ) {
313 $retval[] = (array)$row;
315 return $retval;
319 * Returns all interwiki prefixes
321 * @param $local string|null If set, limits output to local/non-local interwikis
322 * @return Array List of prefixes
323 * @since 1.19
325 public static function getAllPrefixes( $local = null ) {
326 global $wgInterwikiCache;
328 if ( $wgInterwikiCache ) {
329 return self::getAllPrefixesCached( $local );
330 } else {
331 return self::getAllPrefixesDB( $local );
336 * Get the URL for a particular title (or with $1 if no title given)
338 * @param $title String: what text to put for the article name
339 * @return String: the URL
340 * @note Prior to 1.19 The getURL with an argument was broken.
341 * If you if you use this arg in an extension that supports MW earlier
342 * than 1.19 please wfUrlencode and substitute $1 on your own.
344 public function getURL( $title = null ) {
345 $url = $this->mURL;
346 if( $title !== null ) {
347 $url = str_replace( "$1", wfUrlencode( $title ), $url );
349 return $url;
353 * Get the API URL for this wiki
355 * @return String: the URL
357 public function getAPI() {
358 return $this->mAPI;
362 * Get the DB name for this wiki
364 * @return String: the DB name
366 public function getWikiID() {
367 return $this->mWikiID;
371 * Is this a local link from a sister project, or is
372 * it something outside, like Google
374 * @return Boolean
376 public function isLocal() {
377 return $this->mLocal;
381 * Can pages from this wiki be transcluded?
382 * Still requires $wgEnableScaryTransclusion
384 * @return Boolean
386 public function isTranscludable() {
387 return $this->mTrans;
391 * Get the name for the interwiki site
393 * @return String
395 public function getName() {
396 $msg = wfMessage( 'interwiki-name-' . $this->mPrefix )->inContentLanguage();
397 return !$msg->exists() ? '' : $msg;
401 * Get a description for this interwiki
403 * @return String
405 public function getDescription() {
406 $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix )->inContentLanguage();
407 return !$msg->exists() ? '' : $msg;
411 * Return the list of interwiki fields that should be selected to create
412 * a new interwiki object.
413 * @return array
415 public static function selectFields() {
416 return array(
417 'iw_prefix',
418 'iw_url',
419 'iw_api',
420 'iw_wikiid',
421 'iw_local',
422 'iw_trans'