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 );
156 if( $iwData && is_array( $iwData ) ) { // is_array is hack for old keys
157 $iw = Interwiki
::loadFromArray( $iwData );
163 $db = wfGetDB( DB_SLAVE
);
165 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
167 $iw = Interwiki
::loadFromArray( $row );
170 'iw_url' => $iw->mURL
,
171 'iw_api' => $iw->mAPI
,
172 'iw_wikiid' => $iw->mWikiID
,
173 'iw_local' => $iw->mLocal
,
174 'iw_trans' => $iw->mTrans
176 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
184 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
186 * @param $mc Associative array: row from the interwiki table
187 * @return Boolean: whether everything was there
189 protected static function loadFromArray( $mc ) {
190 if( isset( $mc['iw_url'] ) ) {
191 $iw = new Interwiki();
192 $iw->mURL
= $mc['iw_url'];
193 $iw->mLocal
= isset( $mc['iw_local'] ) ?
$mc['iw_local'] : 0;
194 $iw->mTrans
= isset( $mc['iw_trans'] ) ?
$mc['iw_trans'] : 0;
195 $iw->mAPI
= isset( $mc['iw_api'] ) ?
$mc['iw_api'] :
196 $iw->mAPI
= isset( $mc['iw_api'] ) ?
$mc['iw_api'] : '';
197 $iw->mWikiID
= isset( $mc['iw_wikiid'] ) ?
$mc['iw_wikiid'] : '';
205 * Fetch all interwiki prefixes from interwiki cache
207 * @param $local If not null, limits output to local/non-local interwikis
208 * @return Array List of prefixes
211 protected static function getAllPrefixesCached( $local ) {
212 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
215 wfDebug( __METHOD__
. "()\n" );
217 $db = CdbReader
::open( $wgInterwikiCache );
219 /* Resolve site name */
220 if( $wgInterwikiScopes >= 3 && !$site ) {
221 $site = $db->get( '__sites:' . wfWikiID() );
223 $site = $wgInterwikiFallbackSite;
227 // List of interwiki sources
230 if ( $wgInterwikiScopes >= 2 ) {
231 $sources[] = '__global';
234 if ( $wgInterwikiScopes >= 3 ) {
235 $sources[] = '_' . $site;
237 $sources[] = wfWikiID();
241 foreach( $sources as $source ) {
242 $list = $db->get( "__list:{$source}" );
243 foreach ( explode( ' ', $list ) as $iw_prefix ) {
244 $row = $db->get( "{$source}:{$iw_prefix}" );
249 list( $iw_local, $iw_url ) = explode( ' ', $row );
251 if ( $local !== null && $local != $iw_local ) {
255 $data[$iw_prefix] = array(
256 'iw_prefix' => $iw_prefix,
258 'iw_local' => $iw_local,
265 return array_values( $data );
269 * Fetch all interwiki prefixes from DB
271 * @param $local If not null, limits output to local/non-local interwikis
272 * @return Array List of prefixes
275 protected static function getAllPrefixesDB( $local ) {
276 $db = wfGetDB( DB_SLAVE
);
280 if ( $local !== null ) {
282 $where['iw_local'] = 1;
283 } elseif ( $local == 0 ) {
284 $where['iw_local'] = 0;
288 $res = $db->select( 'interwiki',
289 array( 'iw_prefix', 'iw_url', 'iw_api', 'iw_wikiid', 'iw_local', 'iw_trans' ),
290 $where, __METHOD__
, array( 'ORDER BY' => 'iw_prefix' )
293 foreach ( $res as $row ) {
294 $retval[] = (array)$row;
300 * Returns all interwiki prefixes
302 * @param $local If set, limits output to local/non-local interwikis
303 * @return Array List of prefixes
306 public static function getAllPrefixes( $local = null ) {
307 global $wgInterwikiCache;
309 if ( $wgInterwikiCache ) {
310 return self
::getAllPrefixesCached( $local );
312 return self
::getAllPrefixesDB( $local );
317 * Get the URL for a particular title (or with $1 if no title given)
319 * @param $title String: what text to put for the article name
320 * @return String: the URL
322 public function getURL( $title = null ) {
324 if( $title != null ) {
325 $url = str_replace( "$1", $title, $url );
331 * Get the API URL for this wiki
333 * @return String: the URL
335 public function getAPI() {
340 * Get the DB name for this wiki
342 * @return String: the DB name
344 public function getWikiID() {
345 return $this->mWikiID
;
349 * Is this a local link from a sister project, or is
350 * it something outside, like Google
354 public function isLocal() {
355 return $this->mLocal
;
359 * Can pages from this wiki be transcluded?
360 * Still requires $wgEnableScaryTransclusion
364 public function isTranscludable() {
365 return $this->mTrans
;
369 * Get the name for the interwiki site
373 public function getName() {
374 $msg = wfMessage( 'interwiki-name-' . $this->mPrefix
)->inContentLanguage();
375 return !$msg->exists() ?
'' : $msg;
379 * Get a description for this interwiki
383 public function getDescription() {
384 $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix
)->inContentLanguage();
385 return !$msg->exists() ?
'' : $msg;
390 * Transclude an interwiki link.
392 public static function interwikiTransclude( $title ) {
394 // If we have a wikiID, we will use it to get an access to the remote database
395 // if not, we will use the API URL to retrieve the data through a HTTP Get
397 $wikiID = $title->getTransWikiID( );
398 $transAPI = $title->getTransAPI( );
400 if ( $wikiID !== '') {
402 $finalText = self
::fetchTemplateFromDB( $wikiID, $title );
405 } else if( $transAPI !== '' ) {
407 $interwiki = $title->getInterwiki( );
408 $fullTitle = $title->getSemiPrefixedText( );
410 $finalText = self
::fetchTemplateFromAPI( $interwiki, $transAPI, $fullTitle );
419 * Retrieve the wikitext of a distant page accessing the foreign DB
421 public static function fetchTemplateFromDB ( $wikiID, $title ) {
423 $revision = Revision
::loadFromTitleForeignWiki( $wikiID, $title );
426 $text = $revision->getText();
434 * Retrieve the wikitext of a distant page using the API of the foreign wiki
436 public static function fetchTemplateFromAPI( $interwiki, $transAPI, $fullTitle ) {
437 global $wgMemc, $wgTranscludeCacheExpiry;
439 $key = wfMemcKey( 'iwtransclustiontext', 'textid', $interwiki, $fullTitle );
440 $text = $wgMemc->get( $key );
441 if( is_array ( $text ) &&
442 isset ( $text['missing'] ) &&
443 $text['missing'] === true ) {
445 } else if ( $text ) {
449 $url = wfAppendQuery(
451 array( 'action' => 'query',
452 'titles' => $fullTitle,
453 'prop' => 'revisions',
454 'rvprop' => 'content',
459 $get = Http
::get( $url );
460 $content = FormatJson
::decode( $get, true );
462 if ( isset ( $content['query'] ) &&
463 isset ( $content['query']['pages'] ) ) {
464 $page = array_pop( $content['query']['pages'] );
465 if ( $page && isset( $page['revisions'][0]['*'] ) ) {
466 $text = $page['revisions'][0]['*'];
467 $wgMemc->set( $key, $text, $wgTranscludeCacheExpiry );
469 // When we cache a template, we also retrieve and cache its subtemplates
470 $subtemplates = self
::getSubtemplatesListFromAPI( $interwiki, $transAPI, $fullTitle );
471 self
::cacheTemplatesFromAPI( $interwiki, $transAPI, $subtemplates );
475 $wgMemc->set( $key, array ( 'missing' => true ), $wgTranscludeCacheExpiry );
481 public static function getSubtemplatesListFromAPI ( $interwiki, $transAPI, $title ) {
482 $url = wfAppendQuery( $transAPI,
483 array( 'action' => 'query',
485 'prop' => 'templates',
490 $get = Http
::get( $url );
491 $myArray = FormatJson
::decode($get, true);
493 $templates = array( );
494 if ( ! empty( $myArray['query'] )) {
495 if ( ! empty( $myArray['query']['pages'] )) {
496 $templates = array_pop( $myArray['query']['pages'] );
497 if ( ! empty( $templates['templates'] )) {
498 $templates = $templates['templates'];
505 public static function cacheTemplatesFromAPI( $interwiki, $transAPI, $titles ){
506 global $wgMemc, $wgTranscludeCacheExpiry;
508 $outdatedTitles = array( );
510 foreach( $titles as $title ){
511 if ( isset ( $title['title'] ) ) {
512 $key = wfMemcKey( 'iwtransclustiontext', 'textid', $interwiki, $title['title'] );
513 $text = $wgMemc->get( $key );
515 $outdatedTitles[] = $title['title'];
520 $batches = array_chunk( $outdatedTitles, 50 );
522 foreach( $batches as $batch ){
523 $url = wfAppendQuery(
525 array( 'action' => 'query',
526 'titles' => implode( '|', $batch ),
527 'prop' => 'revisions',
528 'rvprop' => 'content',
532 $get = Http
::get( $url );
533 $content = FormatJson
::decode( $get, true );
535 if ( isset ( $content['query'] ) &&
536 isset ( $content['query']['pages'] ) ) {
537 foreach( $content['query']['pages'] as $page ) {
538 $key = wfMemcKey( 'iwtransclustiontext', 'textid', $interwiki, $page['title'] );
539 if ( isset ( $page['revisions'][0]['*'] ) ) {
540 $text = $page['revisions'][0]['*'];
542 $text = array ( 'missing' => true );
544 $wgMemc->set( $key, $text, $wgTranscludeCacheExpiry );