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)
12 * This class also contains the functions that allow interwiki templates transclusion.
16 // Cache - removes oldest entry when it hits limit
17 protected static $smCache = array();
18 const CACHE_LIMIT
= 100; // 0 means unlimited, any other value is max number of entries.
20 protected $mPrefix, $mURL, $mAPI, $mWikiID, $mLocal, $mTrans;
22 public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0, $trans = 0 ) {
23 $this->mPrefix
= $prefix;
26 $this->mWikiID
= $wikiId;
27 $this->mLocal
= $local;
28 $this->mTrans
= $trans;
32 * Check whether an interwiki prefix exists
34 * @param $prefix String: interwiki prefix to use
35 * @return Boolean: whether it exists
37 static public function isValidInterwiki( $prefix ) {
38 $result = self
::fetch( $prefix );
43 * Fetch an Interwiki object
45 * @param $prefix String: interwiki prefix to use
46 * @return Interwiki Object, or null if not valid
48 static public function fetch( $prefix ) {
53 $prefix = $wgContLang->lc( $prefix );
54 if( isset( self
::$smCache[$prefix] ) ) {
55 return self
::$smCache[$prefix];
57 global $wgInterwikiCache;
58 if( $wgInterwikiCache ) {
59 $iw = Interwiki
::getInterwikiCached( $prefix );
61 $iw = Interwiki
::load( $prefix );
66 if( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
) {
67 reset( self
::$smCache );
68 unset( self
::$smCache[key( self
::$smCache )] );
70 self
::$smCache[$prefix] = $iw;
75 * Fetch interwiki prefix data from local cache in constant database.
77 * @note More logic is explained in DefaultSettings.
79 * @param $prefix String: interwiki prefix
80 * @return Interwiki object
82 protected static function getInterwikiCached( $prefix ) {
83 $value = self
::getInterwikiCacheEntry( $prefix );
85 $s = new Interwiki( $prefix );
88 list( $local, $url ) = explode( ' ', $value, 2 );
90 $s->mLocal
= (int)$local;
98 * Get entry from interwiki cache
100 * @note More logic is explained in DefaultSettings.
102 * @param $prefix String: database key
103 * @return String: the entry
105 protected static function getInterwikiCacheEntry( $prefix ) {
106 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
109 wfDebug( __METHOD__
. "( $prefix )\n" );
111 $db = CdbReader
::open( $wgInterwikiCache );
113 /* Resolve site name */
114 if( $wgInterwikiScopes >= 3 && !$site ) {
115 $site = $db->get( '__sites:' . wfWikiID() );
117 $site = $wgInterwikiFallbackSite;
121 $value = $db->get( wfMemcKey( $prefix ) );
123 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
124 $value = $db->get( "_{$site}:{$prefix}" );
127 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
128 $value = $db->get( "__global:{$prefix}" );
130 if ( $value == 'undef' ) {
138 * Load the interwiki, trying first memcached then the DB
140 * @param $prefix 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 === '!EMPTY' ) {
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_wikiid' => $iw->mWikiID
,
176 'iw_local' => $iw->mLocal
,
177 'iw_trans' => $iw->mTrans
179 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
182 $wgMemc->add( $key, '!EMPTY', $wgInterwikiExpiry ); // negative cache hit
189 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
191 * @param $mc Associative array: row from the interwiki table
192 * @return Boolean: whether everything was there
194 protected static function loadFromArray( $mc ) {
195 if( isset( $mc['iw_url'] ) ) {
196 $iw = new Interwiki();
197 $iw->mURL
= $mc['iw_url'];
198 $iw->mLocal
= isset( $mc['iw_local'] ) ?
$mc['iw_local'] : 0;
199 $iw->mTrans
= isset( $mc['iw_trans'] ) ?
$mc['iw_trans'] : 0;
200 $iw->mAPI
= isset( $mc['iw_api'] ) ?
$mc['iw_api'] :
201 $iw->mAPI
= isset( $mc['iw_api'] ) ?
$mc['iw_api'] : '';
202 $iw->mWikiID
= isset( $mc['iw_wikiid'] ) ?
$mc['iw_wikiid'] : '';
210 * Fetch all interwiki prefixes from interwiki cache
212 * @param $local If not null, limits output to local/non-local interwikis
213 * @return Array List of prefixes
216 protected static function getAllPrefixesCached( $local ) {
217 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
220 wfDebug( __METHOD__
. "()\n" );
222 $db = CdbReader
::open( $wgInterwikiCache );
224 /* Resolve site name */
225 if( $wgInterwikiScopes >= 3 && !$site ) {
226 $site = $db->get( '__sites:' . wfWikiID() );
228 $site = $wgInterwikiFallbackSite;
232 // List of interwiki sources
235 if ( $wgInterwikiScopes >= 2 ) {
236 $sources[] = '__global';
239 if ( $wgInterwikiScopes >= 3 ) {
240 $sources[] = '_' . $site;
242 $sources[] = wfWikiID();
246 foreach( $sources as $source ) {
247 $list = $db->get( "__list:{$source}" );
248 foreach ( explode( ' ', $list ) as $iw_prefix ) {
249 $row = $db->get( "{$source}:{$iw_prefix}" );
254 list( $iw_local, $iw_url ) = explode( ' ', $row );
256 if ( $local !== null && $local != $iw_local ) {
260 $data[$iw_prefix] = array(
261 'iw_prefix' => $iw_prefix,
263 'iw_local' => $iw_local,
270 return array_values( $data );
274 * Fetch all interwiki prefixes from DB
276 * @param $local If not null, limits output to local/non-local interwikis
277 * @return Array List of prefixes
280 protected static function getAllPrefixesDB( $local ) {
281 $db = wfGetDB( DB_SLAVE
);
285 if ( $local !== null ) {
287 $where['iw_local'] = 1;
288 } elseif ( $local == 0 ) {
289 $where['iw_local'] = 0;
293 $res = $db->select( 'interwiki',
294 array( 'iw_prefix', 'iw_url', 'iw_api', 'iw_wikiid', 'iw_local', 'iw_trans' ),
295 $where, __METHOD__
, array( 'ORDER BY' => 'iw_prefix' )
298 foreach ( $res as $row ) {
299 $retval[] = (array)$row;
305 * Returns all interwiki prefixes
307 * @param $local If set, limits output to local/non-local interwikis
308 * @return Array List of prefixes
311 public static function getAllPrefixes( $local = null ) {
312 global $wgInterwikiCache;
314 if ( $wgInterwikiCache ) {
315 return self
::getAllPrefixesCached( $local );
317 return self
::getAllPrefixesDB( $local );
322 * Get the URL for a particular title (or with $1 if no title given)
324 * @param $title String: what text to put for the article name
325 * @return String: the URL
326 * @note Prior to 1.19 The getURL with an argument was broken.
327 * If you if you use this arg in an extension that supports MW earlier
328 * than 1.19 please wfUrlencode and substitute $1 on your own.
330 public function getURL( $title = null ) {
332 if( $title !== null ) {
333 $url = str_replace( "$1", wfUrlencode( $title ), $url );
339 * Get the API URL for this wiki
341 * @return String: the URL
343 public function getAPI() {
348 * Get the DB name for this wiki
350 * @return String: the DB name
352 public function getWikiID() {
353 return $this->mWikiID
;
357 * Is this a local link from a sister project, or is
358 * it something outside, like Google
362 public function isLocal() {
363 return $this->mLocal
;
367 * Can pages from this wiki be transcluded?
368 * Still requires $wgEnableScaryTransclusion
372 public function isTranscludable() {
373 return $this->mTrans
;
377 * Get the name for the interwiki site
381 public function getName() {
382 $msg = wfMessage( 'interwiki-name-' . $this->mPrefix
)->inContentLanguage();
383 return !$msg->exists() ?
'' : $msg;
387 * Get a description for this interwiki
391 public function getDescription() {
392 $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix
)->inContentLanguage();
393 return !$msg->exists() ?
'' : $msg;
398 * Transclude an interwiki link.
400 public static function interwikiTransclude( $title ) {
402 // If we have a wikiID, we will use it to get an access to the remote database
403 // if not, we will use the API URL to retrieve the data through a HTTP Get
405 $wikiID = $title->getTransWikiID( );
406 $transAPI = $title->getTransAPI( );
408 if ( $wikiID !== '') {
410 $finalText = self
::fetchTemplateFromDB( $wikiID, $title );
413 } else if( $transAPI !== '' ) {
415 $interwiki = $title->getInterwiki( );
416 $fullTitle = $title->getSemiPrefixedText( );
418 $finalText = self
::fetchTemplateFromAPI( $interwiki, $transAPI, $fullTitle );
427 * Retrieve the wikitext of a distant page accessing the foreign DB
429 public static function fetchTemplateFromDB ( $wikiID, $title ) {
431 $revision = Revision
::loadFromTitleForeignWiki( $wikiID, $title );
434 $text = $revision->getText();
442 * Retrieve the wikitext of a distant page using the API of the foreign wiki
444 public static function fetchTemplateFromAPI( $interwiki, $transAPI, $fullTitle ) {
445 global $wgMemc, $wgTranscludeCacheExpiry;
447 $key = wfMemcKey( 'iwtransclustiontext', 'textid', $interwiki, $fullTitle );
448 $text = $wgMemc->get( $key );
449 if( is_array ( $text ) &&
450 isset ( $text['missing'] ) &&
451 $text['missing'] === true ) {
453 } else if ( $text ) {
457 $url = wfAppendQuery(
459 array( 'action' => 'query',
460 'titles' => $fullTitle,
461 'prop' => 'revisions',
462 'rvprop' => 'content',
467 $get = Http
::get( $url );
468 $content = FormatJson
::decode( $get, true );
470 if ( isset ( $content['query'] ) &&
471 isset ( $content['query']['pages'] ) ) {
472 $page = array_pop( $content['query']['pages'] );
473 if ( $page && isset( $page['revisions'][0]['*'] ) ) {
474 $text = $page['revisions'][0]['*'];
475 $wgMemc->set( $key, $text, $wgTranscludeCacheExpiry );
477 // When we cache a template, we also retrieve and cache its subtemplates
478 $subtemplates = self
::getSubtemplatesListFromAPI( $interwiki, $transAPI, $fullTitle );
479 self
::cacheTemplatesFromAPI( $interwiki, $transAPI, $subtemplates );
483 $wgMemc->set( $key, array ( 'missing' => true ), $wgTranscludeCacheExpiry );
489 public static function getSubtemplatesListFromAPI ( $interwiki, $transAPI, $title ) {
490 $url = wfAppendQuery( $transAPI,
491 array( 'action' => 'query',
493 'prop' => 'templates',
498 $get = Http
::get( $url );
499 $myArray = FormatJson
::decode($get, true);
501 $templates = array( );
502 if ( ! empty( $myArray['query'] )) {
503 if ( ! empty( $myArray['query']['pages'] )) {
504 $templates = array_pop( $myArray['query']['pages'] );
505 if ( ! empty( $templates['templates'] )) {
506 $templates = $templates['templates'];
513 public static function cacheTemplatesFromAPI( $interwiki, $transAPI, $titles ){
514 global $wgMemc, $wgTranscludeCacheExpiry;
516 $outdatedTitles = array( );
518 foreach( $titles as $title ){
519 if ( isset ( $title['title'] ) ) {
520 $key = wfMemcKey( 'iwtransclustiontext', 'textid', $interwiki, $title['title'] );
521 $text = $wgMemc->get( $key );
523 $outdatedTitles[] = $title['title'];
528 $batches = array_chunk( $outdatedTitles, 50 );
530 foreach( $batches as $batch ){
531 $url = wfAppendQuery(
533 array( 'action' => 'query',
534 'titles' => implode( '|', $batch ),
535 'prop' => 'revisions',
536 'rvprop' => 'content',
540 $get = Http
::get( $url );
541 $content = FormatJson
::decode( $get, true );
543 if ( isset ( $content['query'] ) &&
544 isset ( $content['query']['pages'] ) ) {
545 foreach( $content['query']['pages'] as $page ) {
546 $key = wfMemcKey( 'iwtransclustiontext', 'textid', $interwiki, $page['title'] );
547 if ( isset ( $page['revisions'][0]['*'] ) ) {
548 $text = $page['revisions'][0]['*'];
550 $text = array ( 'missing' => true );
552 $wgMemc->set( $key, $text, $wgTranscludeCacheExpiry );