4 * Interwiki table entry
9 * All information is loaded on creation when called by Interwiki::fetch( $prefix ).
10 * All work is done on slave, because this should *never* change (except during
11 * schema updates etc, which aren't wiki-related)
15 // Cache - removes oldest entry when it hits limit
16 protected static $smCache = array();
17 const CACHE_LIMIT
= 100; // 0 means unlimited, any other value is max number of entries.
19 protected $mPrefix, $mURL, $mAPI, $mWikiID, $mLocal, $mTrans;
21 public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0, $trans = 0 ) {
22 $this->mPrefix
= $prefix;
25 $this->mWikiID
= $wikiId;
26 $this->mLocal
= $local;
27 $this->mTrans
= $trans;
31 * Check whether an interwiki prefix exists
33 * @param $prefix String: interwiki prefix to use
34 * @return Boolean: whether it exists
36 static public function isValidInterwiki( $prefix ) {
37 $result = self
::fetch( $prefix );
42 * Fetch an Interwiki object
44 * @param $prefix String: interwiki prefix to use
45 * @return Interwiki Object, or null if not valid
47 static public function fetch( $prefix ) {
52 $prefix = $wgContLang->lc( $prefix );
53 if( isset( self
::$smCache[$prefix] ) ) {
54 return self
::$smCache[$prefix];
56 global $wgInterwikiCache;
57 if( $wgInterwikiCache ) {
58 $iw = Interwiki
::getInterwikiCached( $prefix );
60 $iw = Interwiki
::load( $prefix );
65 if( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
) {
66 reset( self
::$smCache );
67 unset( self
::$smCache[key( self
::$smCache )] );
69 self
::$smCache[$prefix] = $iw;
74 * Fetch interwiki prefix data from local cache in constant database.
76 * @note More logic is explained in DefaultSettings.
78 * @param $prefix String: interwiki prefix
79 * @return Interwiki object
81 protected static function getInterwikiCached( $prefix ) {
82 $value = self
::getInterwikiCacheEntry( $prefix );
84 $s = new Interwiki( $prefix );
87 list( $local, $url ) = explode( ' ', $value, 2 );
89 $s->mLocal
= (int)$local;
97 * Get entry from interwiki cache
99 * @note More logic is explained in DefaultSettings.
101 * @param $prefix String: database key
102 * @return String: the entry
104 protected static function getInterwikiCacheEntry( $prefix ) {
105 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
108 wfDebug( __METHOD__
. "( $prefix )\n" );
110 $db = CdbReader
::open( $wgInterwikiCache );
112 /* Resolve site name */
113 if( $wgInterwikiScopes >= 3 && !$site ) {
114 $site = $db->get( '__sites:' . wfWikiID() );
116 $site = $wgInterwikiFallbackSite;
120 $value = $db->get( wfMemcKey( $prefix ) );
122 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
123 $value = $db->get( "_{$site}:{$prefix}" );
126 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
127 $value = $db->get( "__global:{$prefix}" );
129 if ( $value == 'undef' ) {
138 * Load the interwiki, trying first memcached then the DB
140 * @param $prefix string The interwiki prefix
141 * @return Boolean: the prefix is valid
143 protected static function load( $prefix ) {
144 global $wgMemc, $wgInterwikiExpiry;
147 if ( !wfRunHooks( 'InterwikiLoadPrefix', array( $prefix, &$iwData ) ) ) {
148 return Interwiki
::loadFromArray( $iwData );
152 $key = wfMemcKey( 'interwiki', $prefix );
153 $iwData = $wgMemc->get( $key );
154 if ( $iwData === '!NONEXISTENT' ) {
155 return false; // negative cache hit
159 if( $iwData && is_array( $iwData ) ) { // is_array is hack for old keys
160 $iw = Interwiki
::loadFromArray( $iwData );
166 $db = wfGetDB( DB_SLAVE
);
168 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
170 $iw = Interwiki
::loadFromArray( $row );
173 'iw_url' => $iw->mURL
,
174 'iw_api' => $iw->mAPI
,
175 'iw_local' => $iw->mLocal
,
176 'iw_trans' => $iw->mTrans
178 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
181 $wgMemc->add( $key, '!NONEXISTENT', $wgInterwikiExpiry ); // negative cache hit
188 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
190 * @param $mc array Associative array: row from the interwiki table
191 * @return Boolean|Interwiki whether everything was there
193 protected static function loadFromArray( $mc ) {
194 if( isset( $mc['iw_url'] ) ) {
195 $iw = new Interwiki();
196 $iw->mURL
= $mc['iw_url'];
197 $iw->mLocal
= isset( $mc['iw_local'] ) ?
$mc['iw_local'] : 0;
198 $iw->mTrans
= isset( $mc['iw_trans'] ) ?
$mc['iw_trans'] : 0;
199 $iw->mAPI
= isset( $mc['iw_api'] ) ?
$mc['iw_api'] : '';
200 $iw->mWikiID
= isset( $mc['iw_wikiid'] ) ?
$mc['iw_wikiid'] : '';
208 * Fetch all interwiki prefixes from interwiki cache
210 * @param $local null|string If not null, limits output to local/non-local interwikis
211 * @return Array List of prefixes
214 protected static function getAllPrefixesCached( $local ) {
215 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
218 wfDebug( __METHOD__
. "()\n" );
220 $db = CdbReader
::open( $wgInterwikiCache );
222 /* Resolve site name */
223 if( $wgInterwikiScopes >= 3 && !$site ) {
224 $site = $db->get( '__sites:' . wfWikiID() );
226 $site = $wgInterwikiFallbackSite;
230 // List of interwiki sources
233 if ( $wgInterwikiScopes >= 2 ) {
234 $sources[] = '__global';
237 if ( $wgInterwikiScopes >= 3 ) {
238 $sources[] = '_' . $site;
240 $sources[] = wfWikiID();
244 foreach( $sources as $source ) {
245 $list = $db->get( "__list:{$source}" );
246 foreach ( explode( ' ', $list ) as $iw_prefix ) {
247 $row = $db->get( "{$source}:{$iw_prefix}" );
252 list( $iw_local, $iw_url ) = explode( ' ', $row );
254 if ( $local !== null && $local != $iw_local ) {
258 $data[$iw_prefix] = array(
259 'iw_prefix' => $iw_prefix,
261 'iw_local' => $iw_local,
268 return array_values( $data );
272 * Fetch all interwiki prefixes from DB
274 * @param $local string|null If not null, limits output to local/non-local interwikis
275 * @return Array List of prefixes
278 protected static function getAllPrefixesDB( $local ) {
279 $db = wfGetDB( DB_SLAVE
);
283 if ( $local !== null ) {
285 $where['iw_local'] = 1;
286 } elseif ( $local == 0 ) {
287 $where['iw_local'] = 0;
291 $res = $db->select( 'interwiki',
292 array( 'iw_prefix', 'iw_url', 'iw_api', 'iw_wikiid', 'iw_local', 'iw_trans' ),
293 $where, __METHOD__
, array( 'ORDER BY' => 'iw_prefix' )
296 foreach ( $res as $row ) {
297 $retval[] = (array)$row;
303 * Returns all interwiki prefixes
305 * @param $local string|null If set, limits output to local/non-local interwikis
306 * @return Array List of prefixes
309 public static function getAllPrefixes( $local = null ) {
310 global $wgInterwikiCache;
312 if ( $wgInterwikiCache ) {
313 return self
::getAllPrefixesCached( $local );
315 return self
::getAllPrefixesDB( $local );
320 * Get the URL for a particular title (or with $1 if no title given)
322 * @param $title String: what text to put for the article name
323 * @return String: the URL
324 * @note Prior to 1.19 The getURL with an argument was broken.
325 * If you if you use this arg in an extension that supports MW earlier
326 * than 1.19 please wfUrlencode and substitute $1 on your own.
328 public function getURL( $title = null ) {
330 if( $title !== null ) {
331 $url = str_replace( "$1", wfUrlencode( $title ), $url );
337 * Get the API URL for this wiki
339 * @return String: the URL
341 public function getAPI() {
346 * Get the DB name for this wiki
348 * @return String: the DB name
350 public function getWikiID() {
351 return $this->mWikiID
;
355 * Is this a local link from a sister project, or is
356 * it something outside, like Google
360 public function isLocal() {
361 return $this->mLocal
;
365 * Can pages from this wiki be transcluded?
366 * Still requires $wgEnableScaryTransclusion
370 public function isTranscludable() {
371 return $this->mTrans
;
375 * Get the name for the interwiki site
379 public function getName() {
380 $msg = wfMessage( 'interwiki-name-' . $this->mPrefix
)->inContentLanguage();
381 return !$msg->exists() ?
'' : $msg;
385 * Get a description for this interwiki
389 public function getDescription() {
390 $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix
)->inContentLanguage();
391 return !$msg->exists() ?
'' : $msg;