Cache negative results for interwiki prefix checks. Also helps filecache fail-over...
[lhc/web/wiklou.git] / includes / interwiki / Interwiki.php
1 <?php
2 /**
3 * @file
4 * Interwiki table entry
5 */
6
7 /**
8 * The interwiki class
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.
13 */
14 class Interwiki {
15
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.
19
20 protected $mPrefix, $mURL, $mAPI, $mWikiID, $mLocal, $mTrans;
21
22 public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0, $trans = 0 ) {
23 $this->mPrefix = $prefix;
24 $this->mURL = $url;
25 $this->mAPI = $api;
26 $this->mWikiID = $wikiId;
27 $this->mLocal = $local;
28 $this->mTrans = $trans;
29 }
30
31 /**
32 * Check whether an interwiki prefix exists
33 *
34 * @param $prefix String: interwiki prefix to use
35 * @return Boolean: whether it exists
36 */
37 static public function isValidInterwiki( $prefix ) {
38 $result = self::fetch( $prefix );
39 return (bool)$result;
40 }
41
42 /**
43 * Fetch an Interwiki object
44 *
45 * @param $prefix String: interwiki prefix to use
46 * @return Interwiki Object, or null if not valid
47 */
48 static public function fetch( $prefix ) {
49 global $wgContLang;
50 if( $prefix == '' ) {
51 return null;
52 }
53 $prefix = $wgContLang->lc( $prefix );
54 if( isset( self::$smCache[$prefix] ) ) {
55 return self::$smCache[$prefix];
56 }
57 global $wgInterwikiCache;
58 if( $wgInterwikiCache ) {
59 $iw = Interwiki::getInterwikiCached( $prefix );
60 } else {
61 $iw = Interwiki::load( $prefix );
62 if( !$iw ) {
63 $iw = false;
64 }
65 }
66 if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
67 reset( self::$smCache );
68 unset( self::$smCache[key( self::$smCache )] );
69 }
70 self::$smCache[$prefix] = $iw;
71 return $iw;
72 }
73
74 /**
75 * Fetch interwiki prefix data from local cache in constant database.
76 *
77 * @note More logic is explained in DefaultSettings.
78 *
79 * @param $prefix String: interwiki prefix
80 * @return Interwiki object
81 */
82 protected static function getInterwikiCached( $prefix ) {
83 $value = self::getInterwikiCacheEntry( $prefix );
84
85 $s = new Interwiki( $prefix );
86 if ( $value != '' ) {
87 // Split values
88 list( $local, $url ) = explode( ' ', $value, 2 );
89 $s->mURL = $url;
90 $s->mLocal = (int)$local;
91 } else {
92 $s = false;
93 }
94 return $s;
95 }
96
97 /**
98 * Get entry from interwiki cache
99 *
100 * @note More logic is explained in DefaultSettings.
101 *
102 * @param $prefix String: database key
103 * @return String: the entry
104 */
105 protected static function getInterwikiCacheEntry( $prefix ) {
106 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
107 static $db, $site;
108
109 wfDebug( __METHOD__ . "( $prefix )\n" );
110 if( !$db ) {
111 $db = CdbReader::open( $wgInterwikiCache );
112 }
113 /* Resolve site name */
114 if( $wgInterwikiScopes >= 3 && !$site ) {
115 $site = $db->get( '__sites:' . wfWikiID() );
116 if ( $site == '' ) {
117 $site = $wgInterwikiFallbackSite;
118 }
119 }
120
121 $value = $db->get( wfMemcKey( $prefix ) );
122 // Site level
123 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
124 $value = $db->get( "_{$site}:{$prefix}" );
125 }
126 // Global Level
127 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
128 $value = $db->get( "__global:{$prefix}" );
129 }
130 if ( $value == 'undef' ) {
131 $value = '';
132 }
133
134 return $value;
135 }
136
137 /**
138 * Load the interwiki, trying first memcached then the DB
139 *
140 * @param $prefix The interwiki prefix
141 * @return Boolean: the prefix is valid
142 */
143 protected static function load( $prefix ) {
144 global $wgMemc, $wgInterwikiExpiry;
145
146 $iwData = false;
147 if ( !wfRunHooks( 'InterwikiLoadPrefix', array( $prefix, &$iwData ) ) ) {
148 return Interwiki::loadFromArray( $iwData );
149 }
150
151 if ( !$iwData ) {
152 $key = wfMemcKey( 'interwiki', $prefix );
153 $iwData = $wgMemc->get( $key );
154 if ( $iwData === '!EMPTY' ) {
155 return false; // negative cache hit
156 }
157 }
158
159 if( $iwData && is_array( $iwData ) ) { // is_array is hack for old keys
160 $iw = Interwiki::loadFromArray( $iwData );
161 if( $iw ) {
162 return $iw;
163 }
164 }
165
166 $db = wfGetDB( DB_SLAVE );
167
168 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
169 __METHOD__ ) );
170 $iw = Interwiki::loadFromArray( $row );
171 if ( $iw ) {
172 $mc = array(
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
178 );
179 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
180 return $iw;
181 } else {
182 $wgMemc->add( $key, '!EMPTY', $wgInterwikiExpiry ); // negative cache hit
183 }
184
185 return false;
186 }
187
188 /**
189 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
190 *
191 * @param $mc Associative array: row from the interwiki table
192 * @return Boolean: whether everything was there
193 */
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'] : '';
203
204 return $iw;
205 }
206 return false;
207 }
208
209 /**
210 * Fetch all interwiki prefixes from interwiki cache
211 *
212 * @param $local If not null, limits output to local/non-local interwikis
213 * @return Array List of prefixes
214 * @since 1.19
215 */
216 protected static function getAllPrefixesCached( $local ) {
217 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
218 static $db, $site;
219
220 wfDebug( __METHOD__ . "()\n" );
221 if( !$db ) {
222 $db = CdbReader::open( $wgInterwikiCache );
223 }
224 /* Resolve site name */
225 if( $wgInterwikiScopes >= 3 && !$site ) {
226 $site = $db->get( '__sites:' . wfWikiID() );
227 if ( $site == '' ) {
228 $site = $wgInterwikiFallbackSite;
229 }
230 }
231
232 // List of interwiki sources
233 $sources = array();
234 // Global Level
235 if ( $wgInterwikiScopes >= 2 ) {
236 $sources[] = '__global';
237 }
238 // Site level
239 if ( $wgInterwikiScopes >= 3 ) {
240 $sources[] = '_' . $site;
241 }
242 $sources[] = wfWikiID();
243
244 $data = array();
245
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}" );
250 if( !$row ) {
251 continue;
252 }
253
254 list( $iw_local, $iw_url ) = explode( ' ', $row );
255
256 if ( $local !== null && $local != $iw_local ) {
257 continue;
258 }
259
260 $data[$iw_prefix] = array(
261 'iw_prefix' => $iw_prefix,
262 'iw_url' => $iw_url,
263 'iw_local' => $iw_local,
264 );
265 }
266 }
267
268 ksort( $data );
269
270 return array_values( $data );
271 }
272
273 /**
274 * Fetch all interwiki prefixes from DB
275 *
276 * @param $local If not null, limits output to local/non-local interwikis
277 * @return Array List of prefixes
278 * @since 1.19
279 */
280 protected static function getAllPrefixesDB( $local ) {
281 $db = wfGetDB( DB_SLAVE );
282
283 $where = array();
284
285 if ( $local !== null ) {
286 if ( $local == 1 ) {
287 $where['iw_local'] = 1;
288 } elseif ( $local == 0 ) {
289 $where['iw_local'] = 0;
290 }
291 }
292
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' )
296 );
297 $retval = array();
298 foreach ( $res as $row ) {
299 $retval[] = (array)$row;
300 }
301 return $retval;
302 }
303
304 /**
305 * Returns all interwiki prefixes
306 *
307 * @param $local If set, limits output to local/non-local interwikis
308 * @return Array List of prefixes
309 * @since 1.19
310 */
311 public static function getAllPrefixes( $local = null ) {
312 global $wgInterwikiCache;
313
314 if ( $wgInterwikiCache ) {
315 return self::getAllPrefixesCached( $local );
316 } else {
317 return self::getAllPrefixesDB( $local );
318 }
319 }
320
321 /**
322 * Get the URL for a particular title (or with $1 if no title given)
323 *
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.
329 */
330 public function getURL( $title = null ) {
331 $url = $this->mURL;
332 if( $title !== null ) {
333 $url = str_replace( "$1", wfUrlencode( $title ), $url );
334 }
335 return $url;
336 }
337
338 /**
339 * Get the API URL for this wiki
340 *
341 * @return String: the URL
342 */
343 public function getAPI() {
344 return $this->mAPI;
345 }
346
347 /**
348 * Get the DB name for this wiki
349 *
350 * @return String: the DB name
351 */
352 public function getWikiID() {
353 return $this->mWikiID;
354 }
355
356 /**
357 * Is this a local link from a sister project, or is
358 * it something outside, like Google
359 *
360 * @return Boolean
361 */
362 public function isLocal() {
363 return $this->mLocal;
364 }
365
366 /**
367 * Can pages from this wiki be transcluded?
368 * Still requires $wgEnableScaryTransclusion
369 *
370 * @return Boolean
371 */
372 public function isTranscludable() {
373 return $this->mTrans;
374 }
375
376 /**
377 * Get the name for the interwiki site
378 *
379 * @return String
380 */
381 public function getName() {
382 $msg = wfMessage( 'interwiki-name-' . $this->mPrefix )->inContentLanguage();
383 return !$msg->exists() ? '' : $msg;
384 }
385
386 /**
387 * Get a description for this interwiki
388 *
389 * @return String
390 */
391 public function getDescription() {
392 $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix )->inContentLanguage();
393 return !$msg->exists() ? '' : $msg;
394 }
395
396
397 /**
398 * Transclude an interwiki link.
399 */
400 public static function interwikiTransclude( $title ) {
401
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
404
405 $wikiID = $title->getTransWikiID( );
406 $transAPI = $title->getTransAPI( );
407
408 if ( $wikiID !== '') {
409
410 $finalText = self::fetchTemplateFromDB( $wikiID, $title );
411 return $finalText;
412
413 } else if( $transAPI !== '' ) {
414
415 $interwiki = $title->getInterwiki( );
416 $fullTitle = $title->getSemiPrefixedText( );
417
418 $finalText = self::fetchTemplateFromAPI( $interwiki, $transAPI, $fullTitle );
419
420 return $finalText;
421
422 }
423 return false;
424 }
425
426 /**
427 * Retrieve the wikitext of a distant page accessing the foreign DB
428 */
429 public static function fetchTemplateFromDB ( $wikiID, $title ) {
430
431 $revision = Revision::loadFromTitleForeignWiki( $wikiID, $title );
432
433 if ( $revision ) {
434 $text = $revision->getText();
435 return $text;
436 }
437
438 return false;
439 }
440
441 /**
442 * Retrieve the wikitext of a distant page using the API of the foreign wiki
443 */
444 public static function fetchTemplateFromAPI( $interwiki, $transAPI, $fullTitle ) {
445 global $wgMemc, $wgTranscludeCacheExpiry;
446
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 ) {
452 return false;
453 } else if ( $text ) {
454 return $text;
455 }
456
457 $url = wfAppendQuery(
458 $transAPI,
459 array( 'action' => 'query',
460 'titles' => $fullTitle,
461 'prop' => 'revisions',
462 'rvprop' => 'content',
463 'format' => 'json'
464 )
465 );
466
467 $get = Http::get( $url );
468 $content = FormatJson::decode( $get, true );
469
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 );
476
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 );
480
481 return $text;
482 } else {
483 $wgMemc->set( $key, array ( 'missing' => true ), $wgTranscludeCacheExpiry );
484 }
485 }
486 return false;
487 }
488
489 public static function getSubtemplatesListFromAPI ( $interwiki, $transAPI, $title ) {
490 $url = wfAppendQuery( $transAPI,
491 array( 'action' => 'query',
492 'titles' => $title,
493 'prop' => 'templates',
494 'format' => 'json'
495 )
496 );
497
498 $get = Http::get( $url );
499 $myArray = FormatJson::decode($get, true);
500
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'];
507 }
508 }
509 return $templates;
510 }
511 }
512
513 public static function cacheTemplatesFromAPI( $interwiki, $transAPI, $titles ){
514 global $wgMemc, $wgTranscludeCacheExpiry;
515
516 $outdatedTitles = array( );
517
518 foreach( $titles as $title ){
519 if ( isset ( $title['title'] ) ) {
520 $key = wfMemcKey( 'iwtransclustiontext', 'textid', $interwiki, $title['title'] );
521 $text = $wgMemc->get( $key );
522 if( !$text ){
523 $outdatedTitles[] = $title['title'];
524 }
525 }
526 }
527
528 $batches = array_chunk( $outdatedTitles, 50 );
529
530 foreach( $batches as $batch ){
531 $url = wfAppendQuery(
532 $transAPI,
533 array( 'action' => 'query',
534 'titles' => implode( '|', $batch ),
535 'prop' => 'revisions',
536 'rvprop' => 'content',
537 'format' => 'json'
538 )
539 );
540 $get = Http::get( $url );
541 $content = FormatJson::decode( $get, true );
542
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]['*'];
549 } else {
550 $text = array ( 'missing' => true );
551 }
552 $wgMemc->set( $key, $text, $wgTranscludeCacheExpiry );
553 }
554 }
555 }
556 }
557 }