Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / interwiki / Interwiki.php
blob4003fa885f6aa04fbef1fc7f1f399e1cee4d6804
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 {
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 protected $mPrefix, $mURL, $mAPI, $mWikiID, $mLocal, $mTrans;
36 public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0,
37 $trans = 0
38 ) {
39 $this->mPrefix = $prefix;
40 $this->mURL = $url;
41 $this->mAPI = $api;
42 $this->mWikiID = $wikiId;
43 $this->mLocal = $local;
44 $this->mTrans = $trans;
47 /**
48 * Check whether an interwiki prefix exists
50 * @param string $prefix Interwiki prefix to use
51 * @return bool Whether it exists
53 public static function isValidInterwiki( $prefix ) {
54 $result = self::fetch( $prefix );
55 return (bool)$result;
58 /**
59 * Fetch an Interwiki object
61 * @param string $prefix Interwiki prefix to use
62 * @return Interwiki|null|bool
64 public static function fetch( $prefix ) {
65 global $wgContLang;
66 if ( $prefix == '' ) {
67 return null;
69 $prefix = $wgContLang->lc( $prefix );
70 if ( isset( self::$smCache[$prefix] ) ) {
71 return self::$smCache[$prefix];
73 global $wgInterwikiCache;
74 if ( $wgInterwikiCache ) {
75 $iw = Interwiki::getInterwikiCached( $prefix );
76 } else {
77 $iw = Interwiki::load( $prefix );
78 if ( !$iw ) {
79 $iw = false;
82 if ( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
83 reset( self::$smCache );
84 unset( self::$smCache[key( self::$smCache )] );
86 self::$smCache[$prefix] = $iw;
87 return $iw;
90 /**
91 * Fetch interwiki prefix data from local cache in constant database.
93 * @note More logic is explained in DefaultSettings.
95 * @param string $prefix Interwiki prefix
96 * @return Interwiki object
98 protected static function getInterwikiCached( $prefix ) {
99 $value = self::getInterwikiCacheEntry( $prefix );
101 $s = new Interwiki( $prefix );
102 if ( $value != '' ) {
103 // Split values
104 list( $local, $url ) = explode( ' ', $value, 2 );
105 $s->mURL = $url;
106 $s->mLocal = (int)$local;
107 } else {
108 $s = false;
110 return $s;
114 * Get entry from interwiki cache
116 * @note More logic is explained in DefaultSettings.
118 * @param string $prefix Database key
119 * @return string The interwiki entry
121 protected static function getInterwikiCacheEntry( $prefix ) {
122 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
123 static $db, $site;
125 wfDebug( __METHOD__ . "( $prefix )\n" );
126 if ( !$db ) {
127 $db = CdbReader::open( $wgInterwikiCache );
129 /* Resolve site name */
130 if ( $wgInterwikiScopes >= 3 && !$site ) {
131 $site = $db->get( '__sites:' . wfWikiID() );
132 if ( $site == '' ) {
133 $site = $wgInterwikiFallbackSite;
137 $value = $db->get( wfMemcKey( $prefix ) );
138 // Site level
139 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
140 $value = $db->get( "_{$site}:{$prefix}" );
142 // Global Level
143 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
144 $value = $db->get( "__global:{$prefix}" );
146 if ( $value == 'undef' ) {
147 $value = '';
150 return $value;
154 * Load the interwiki, trying first memcached then the DB
156 * @param string $prefix The interwiki prefix
157 * @return bool If $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 array $mc Associative array: row from the interwiki table
207 * @return Interwiki|bool Interwiki object or false if $mc['iw_url'] is not set
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 null|string $local 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 string|null $local 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 string|null $local 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 string $title 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 bool
376 public function isLocal() {
377 return $this->mLocal;
381 * Can pages from this wiki be transcluded?
382 * Still requires $wgEnableScaryTransclusion
384 * @return bool
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 string[]
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'