Merge iwtransclusion branch into trunk
[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 }
155
156 if( $iwData && is_array( $iwData ) ) { // is_array is hack for old keys
157 $iw = Interwiki::loadFromArray( $iwData );
158 if( $iw ) {
159 return $iw;
160 }
161 }
162
163 $db = wfGetDB( DB_SLAVE );
164
165 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
166 __METHOD__ ) );
167 $iw = Interwiki::loadFromArray( $row );
168 if ( $iw ) {
169 $mc = array(
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
175 );
176 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
177 return $iw;
178 }
179
180 return false;
181 }
182
183 /**
184 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
185 *
186 * @param $mc Associative array: row from the interwiki table
187 * @return Boolean: whether everything was there
188 */
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'] : '';
198
199 return $iw;
200 }
201 return false;
202 }
203
204 /**
205 * Fetch all interwiki prefixes from interwiki cache
206 *
207 * @param $local If not null, limits output to local/non-local interwikis
208 * @return Array List of prefixes
209 * @since 1.19
210 */
211 protected static function getAllPrefixesCached( $local ) {
212 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
213 static $db, $site;
214
215 wfDebug( __METHOD__ . "()\n" );
216 if( !$db ) {
217 $db = CdbReader::open( $wgInterwikiCache );
218 }
219 /* Resolve site name */
220 if( $wgInterwikiScopes >= 3 && !$site ) {
221 $site = $db->get( '__sites:' . wfWikiID() );
222 if ( $site == '' ) {
223 $site = $wgInterwikiFallbackSite;
224 }
225 }
226
227 // List of interwiki sources
228 $sources = array();
229 // Global Level
230 if ( $wgInterwikiScopes >= 2 ) {
231 $sources[] = '__global';
232 }
233 // Site level
234 if ( $wgInterwikiScopes >= 3 ) {
235 $sources[] = '_' . $site;
236 }
237 $sources[] = wfWikiID();
238
239 $data = array();
240
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}" );
245 if( !$row ) {
246 continue;
247 }
248
249 list( $iw_local, $iw_url ) = explode( ' ', $row );
250
251 if ( $local !== null && $local != $iw_local ) {
252 continue;
253 }
254
255 $data[$iw_prefix] = array(
256 'iw_prefix' => $iw_prefix,
257 'iw_url' => $iw_url,
258 'iw_local' => $iw_local,
259 );
260 }
261 }
262
263 ksort( $data );
264
265 return array_values( $data );
266 }
267
268 /**
269 * Fetch all interwiki prefixes from DB
270 *
271 * @param $local If not null, limits output to local/non-local interwikis
272 * @return Array List of prefixes
273 * @since 1.19
274 */
275 protected static function getAllPrefixesDB( $local ) {
276 $db = wfGetDB( DB_SLAVE );
277
278 $where = array();
279
280 if ( $local !== null ) {
281 if ( $local == 1 ) {
282 $where['iw_local'] = 1;
283 } elseif ( $local == 0 ) {
284 $where['iw_local'] = 0;
285 }
286 }
287
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' )
291 );
292 $retval = array();
293 foreach ( $res as $row ) {
294 $retval[] = (array)$row;
295 }
296 return $retval;
297 }
298
299 /**
300 * Returns all interwiki prefixes
301 *
302 * @param $local If set, limits output to local/non-local interwikis
303 * @return Array List of prefixes
304 * @since 1.19
305 */
306 public static function getAllPrefixes( $local = null ) {
307 global $wgInterwikiCache;
308
309 if ( $wgInterwikiCache ) {
310 return self::getAllPrefixesCached( $local );
311 } else {
312 return self::getAllPrefixesDB( $local );
313 }
314 }
315
316 /**
317 * Get the URL for a particular title (or with $1 if no title given)
318 *
319 * @param $title String: what text to put for the article name
320 * @return String: the URL
321 */
322 public function getURL( $title = null ) {
323 $url = $this->mURL;
324 if( $title != null ) {
325 $url = str_replace( "$1", $title, $url );
326 }
327 return $url;
328 }
329
330 /**
331 * Get the API URL for this wiki
332 *
333 * @return String: the URL
334 */
335 public function getAPI() {
336 return $this->mAPI;
337 }
338
339 /**
340 * Get the DB name for this wiki
341 *
342 * @return String: the DB name
343 */
344 public function getWikiID() {
345 return $this->mWikiID;
346 }
347
348 /**
349 * Is this a local link from a sister project, or is
350 * it something outside, like Google
351 *
352 * @return Boolean
353 */
354 public function isLocal() {
355 return $this->mLocal;
356 }
357
358 /**
359 * Can pages from this wiki be transcluded?
360 * Still requires $wgEnableScaryTransclusion
361 *
362 * @return Boolean
363 */
364 public function isTranscludable() {
365 return $this->mTrans;
366 }
367
368 /**
369 * Get the name for the interwiki site
370 *
371 * @return String
372 */
373 public function getName() {
374 $msg = wfMessage( 'interwiki-name-' . $this->mPrefix )->inContentLanguage();
375 return !$msg->exists() ? '' : $msg;
376 }
377
378 /**
379 * Get a description for this interwiki
380 *
381 * @return String
382 */
383 public function getDescription() {
384 $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix )->inContentLanguage();
385 return !$msg->exists() ? '' : $msg;
386 }
387
388
389 /**
390 * Transclude an interwiki link.
391 */
392 public static function interwikiTransclude( $title ) {
393
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
396
397 $wikiID = $title->getTransWikiID( );
398 $transAPI = $title->getTransAPI( );
399
400 if ( $wikiID !== '') {
401
402 $finalText = self::fetchTemplateFromDB( $wikiID, $title );
403 return $finalText;
404
405 } else if( $transAPI !== '' ) {
406
407 $interwiki = $title->getInterwiki( );
408 $fullTitle = $title->getSemiPrefixedText( );
409
410 $finalText = self::fetchTemplateFromAPI( $interwiki, $transAPI, $fullTitle );
411
412 return $finalText;
413
414 }
415 return false;
416 }
417
418 /**
419 * Retrieve the wikitext of a distant page accessing the foreign DB
420 */
421 public static function fetchTemplateFromDB ( $wikiID, $title ) {
422
423 $revision = Revision::loadFromTitleForeignWiki( $wikiID, $title );
424
425 if ( $revision ) {
426 $text = $revision->getText();
427 return $text;
428 }
429
430 return false;
431 }
432
433 /**
434 * Retrieve the wikitext of a distant page using the API of the foreign wiki
435 */
436 public static function fetchTemplateFromAPI( $interwiki, $transAPI, $fullTitle ) {
437 global $wgMemc, $wgTranscludeCacheExpiry;
438
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 ) {
444 return false;
445 } else if ( $text ) {
446 return $text;
447 }
448
449 $url = wfAppendQuery(
450 $transAPI,
451 array( 'action' => 'query',
452 'titles' => $fullTitle,
453 'prop' => 'revisions',
454 'rvprop' => 'content',
455 'format' => 'json'
456 )
457 );
458
459 $get = Http::get( $url );
460 $content = FormatJson::decode( $get, true );
461
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 );
468
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 );
472
473 return $text;
474 } else {
475 $wgMemc->set( $key, array ( 'missing' => true ), $wgTranscludeCacheExpiry );
476 }
477 }
478 return false;
479 }
480
481 public static function getSubtemplatesListFromAPI ( $interwiki, $transAPI, $title ) {
482 $url = wfAppendQuery( $transAPI,
483 array( 'action' => 'query',
484 'titles' => $title,
485 'prop' => 'templates',
486 'format' => 'json'
487 )
488 );
489
490 $get = Http::get( $url );
491 $myArray = FormatJson::decode($get, true);
492
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'];
499 }
500 }
501 return $templates;
502 }
503 }
504
505 public static function cacheTemplatesFromAPI( $interwiki, $transAPI, $titles ){
506 global $wgMemc, $wgTranscludeCacheExpiry;
507
508 $outdatedTitles = array( );
509
510 foreach( $titles as $title ){
511 if ( isset ( $title['title'] ) ) {
512 $key = wfMemcKey( 'iwtransclustiontext', 'textid', $interwiki, $title['title'] );
513 $text = $wgMemc->get( $key );
514 if( !$text ){
515 $outdatedTitles[] = $title['title'];
516 }
517 }
518 }
519
520 $batches = array_chunk( $outdatedTitles, 50 );
521
522 foreach( $batches as $batch ){
523 $url = wfAppendQuery(
524 $transAPI,
525 array( 'action' => 'query',
526 'titles' => implode( '|', $batch ),
527 'prop' => 'revisions',
528 'rvprop' => 'content',
529 'format' => 'json'
530 )
531 );
532 $get = Http::get( $url );
533 $content = FormatJson::decode( $get, true );
534
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]['*'];
541 } else {
542 $text = array ( 'missing' => true );
543 }
544 $wgMemc->set( $key, $text, $wgTranscludeCacheExpiry );
545 }
546 }
547 }
548 }
549 }