Merge "PostgreSQL: Port update-keys.sql to PostgreSQL"
[lhc/web/wiklou.git] / includes / interwiki / Interwiki.php
1 <?php
2 /**
3 * Interwiki table entry.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22 use \Cdb\Exception as CdbException;
23 use \Cdb\Reader as CdbReader;
24
25 /**
26 * The interwiki class
27 * All information is loaded on creation when called by Interwiki::fetch( $prefix ).
28 * All work is done on slave, because this should *never* change (except during
29 * schema updates etc, which aren't wiki-related)
30 */
31 class Interwiki {
32 // Cache - removes oldest entry when it hits limit
33 protected static $smCache = array();
34 const CACHE_LIMIT = 100; // 0 means unlimited, any other value is max number of entries.
35
36 /** @var string The interwiki prefix, (e.g. "Meatball", or the language prefix "de") */
37 protected $mPrefix;
38
39 /** @var string The URL of the wiki, with "$1" as a placeholder for an article name. */
40 protected $mURL;
41
42 /** @var string The URL of the file api.php */
43 protected $mAPI;
44
45 /** @var string The name of the database (for a connection to be established
46 * with wfGetLB( 'wikiid' ))
47 */
48 protected $mWikiID;
49
50 /** @var bool Whether the wiki is in this project */
51 protected $mLocal;
52
53 /** @var bool Whether interwiki transclusions are allowed */
54 protected $mTrans;
55
56 public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0,
57 $trans = 0
58 ) {
59 $this->mPrefix = $prefix;
60 $this->mURL = $url;
61 $this->mAPI = $api;
62 $this->mWikiID = $wikiId;
63 $this->mLocal = $local;
64 $this->mTrans = $trans;
65 }
66
67 /**
68 * Check whether an interwiki prefix exists
69 *
70 * @param string $prefix Interwiki prefix to use
71 * @return bool Whether it exists
72 */
73 public static function isValidInterwiki( $prefix ) {
74 $result = self::fetch( $prefix );
75
76 return (bool)$result;
77 }
78
79 /**
80 * Fetch an Interwiki object
81 *
82 * @param string $prefix Interwiki prefix to use
83 * @return Interwiki|null|bool
84 */
85 public static function fetch( $prefix ) {
86 global $wgContLang;
87
88 if ( $prefix == '' ) {
89 return null;
90 }
91
92 $prefix = $wgContLang->lc( $prefix );
93 if ( isset( self::$smCache[$prefix] ) ) {
94 return self::$smCache[$prefix];
95 }
96
97 global $wgInterwikiCache;
98 if ( $wgInterwikiCache ) {
99 $iw = Interwiki::getInterwikiCached( $prefix );
100 } else {
101 $iw = Interwiki::load( $prefix );
102 if ( !$iw ) {
103 $iw = false;
104 }
105 }
106
107 if ( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
108 reset( self::$smCache );
109 unset( self::$smCache[key( self::$smCache )] );
110 }
111
112 self::$smCache[$prefix] = $iw;
113
114 return $iw;
115 }
116
117 /**
118 * Fetch interwiki prefix data from local cache in constant database.
119 *
120 * @note More logic is explained in DefaultSettings.
121 *
122 * @param string $prefix Interwiki prefix
123 * @return Interwiki
124 */
125 protected static function getInterwikiCached( $prefix ) {
126 $value = self::getInterwikiCacheEntry( $prefix );
127
128 $s = new Interwiki( $prefix );
129 if ( $value != '' ) {
130 // Split values
131 list( $local, $url ) = explode( ' ', $value, 2 );
132 $s->mURL = $url;
133 $s->mLocal = (int)$local;
134 } else {
135 $s = false;
136 }
137
138 return $s;
139 }
140
141 /**
142 * Get entry from interwiki cache
143 *
144 * @note More logic is explained in DefaultSettings.
145 *
146 * @param string $prefix Database key
147 * @return string The interwiki entry
148 */
149 protected static function getInterwikiCacheEntry( $prefix ) {
150 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
151 static $db, $site;
152
153 wfDebug( __METHOD__ . "( $prefix )\n" );
154 $value = false;
155 try {
156 if ( !$db ) {
157 $db = CdbReader::open( $wgInterwikiCache );
158 }
159 /* Resolve site name */
160 if ( $wgInterwikiScopes >= 3 && !$site ) {
161 $site = $db->get( '__sites:' . wfWikiID() );
162 if ( $site == '' ) {
163 $site = $wgInterwikiFallbackSite;
164 }
165 }
166
167 $value = $db->get( wfMemcKey( $prefix ) );
168 // Site level
169 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
170 $value = $db->get( "_{$site}:{$prefix}" );
171 }
172 // Global Level
173 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
174 $value = $db->get( "__global:{$prefix}" );
175 }
176 if ( $value == 'undef' ) {
177 $value = '';
178 }
179 } catch ( CdbException $e ) {
180 wfDebug( __METHOD__ . ": CdbException caught, error message was "
181 . $e->getMessage() );
182 }
183
184 return $value;
185 }
186
187 /**
188 * Load the interwiki, trying first memcached then the DB
189 *
190 * @param string $prefix The interwiki prefix
191 * @return Interwiki|bool Interwiki if $prefix is valid, otherwise false
192 */
193 protected static function load( $prefix ) {
194 global $wgMemc, $wgInterwikiExpiry;
195
196 $iwData = array();
197 if ( !wfRunHooks( 'InterwikiLoadPrefix', array( $prefix, &$iwData ) ) ) {
198 return Interwiki::loadFromArray( $iwData );
199 }
200
201 if ( !$iwData ) {
202 $key = wfMemcKey( 'interwiki', $prefix );
203 $iwData = $wgMemc->get( $key );
204 if ( $iwData === '!NONEXISTENT' ) {
205 // negative cache hit
206 return false;
207 }
208 }
209
210 // is_array is hack for old keys
211 if ( $iwData && is_array( $iwData ) ) {
212 $iw = Interwiki::loadFromArray( $iwData );
213 if ( $iw ) {
214 return $iw;
215 }
216 }
217
218 $db = wfGetDB( DB_SLAVE );
219
220 $row = $db->fetchRow( $db->select(
221 'interwiki',
222 self::selectFields(),
223 array( 'iw_prefix' => $prefix ),
224 __METHOD__
225 ) );
226
227 $iw = Interwiki::loadFromArray( $row );
228 if ( $iw ) {
229 $mc = array(
230 'iw_url' => $iw->mURL,
231 'iw_api' => $iw->mAPI,
232 'iw_local' => $iw->mLocal,
233 'iw_trans' => $iw->mTrans
234 );
235 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
236
237 return $iw;
238 }
239
240 // negative cache hit
241 $wgMemc->add( $key, '!NONEXISTENT', $wgInterwikiExpiry );
242
243 return false;
244 }
245
246 /**
247 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
248 *
249 * @param array $mc Associative array: row from the interwiki table
250 * @return Interwiki|bool Interwiki object or false if $mc['iw_url'] is not set
251 */
252 protected static function loadFromArray( $mc ) {
253 if ( isset( $mc['iw_url'] ) ) {
254 $iw = new Interwiki();
255 $iw->mURL = $mc['iw_url'];
256 $iw->mLocal = isset( $mc['iw_local'] ) ? $mc['iw_local'] : 0;
257 $iw->mTrans = isset( $mc['iw_trans'] ) ? $mc['iw_trans'] : 0;
258 $iw->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : '';
259 $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : '';
260
261 return $iw;
262 }
263
264 return false;
265 }
266
267 /**
268 * Fetch all interwiki prefixes from interwiki cache
269 *
270 * @param null|string $local If not null, limits output to local/non-local interwikis
271 * @return array List of prefixes
272 * @since 1.19
273 */
274 protected static function getAllPrefixesCached( $local ) {
275 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
276 static $db, $site;
277
278 wfDebug( __METHOD__ . "()\n" );
279 $data = array();
280 try {
281 if ( !$db ) {
282 $db = CdbReader::open( $wgInterwikiCache );
283 }
284 /* Resolve site name */
285 if ( $wgInterwikiScopes >= 3 && !$site ) {
286 $site = $db->get( '__sites:' . wfWikiID() );
287 if ( $site == '' ) {
288 $site = $wgInterwikiFallbackSite;
289 }
290 }
291
292 // List of interwiki sources
293 $sources = array();
294 // Global Level
295 if ( $wgInterwikiScopes >= 2 ) {
296 $sources[] = '__global';
297 }
298 // Site level
299 if ( $wgInterwikiScopes >= 3 ) {
300 $sources[] = '_' . $site;
301 }
302 $sources[] = wfWikiID();
303
304 foreach ( $sources as $source ) {
305 $list = $db->get( "__list:{$source}" );
306 foreach ( explode( ' ', $list ) as $iw_prefix ) {
307 $row = $db->get( "{$source}:{$iw_prefix}" );
308 if ( !$row ) {
309 continue;
310 }
311
312 list( $iw_local, $iw_url ) = explode( ' ', $row );
313
314 if ( $local !== null && $local != $iw_local ) {
315 continue;
316 }
317
318 $data[$iw_prefix] = array(
319 'iw_prefix' => $iw_prefix,
320 'iw_url' => $iw_url,
321 'iw_local' => $iw_local,
322 );
323 }
324 }
325 } catch ( CdbException $e ) {
326 wfDebug( __METHOD__ . ": CdbException caught, error message was "
327 . $e->getMessage() );
328 }
329
330 ksort( $data );
331
332 return array_values( $data );
333 }
334
335 /**
336 * Fetch all interwiki prefixes from DB
337 *
338 * @param string|null $local If not null, limits output to local/non-local interwikis
339 * @return array List of prefixes
340 * @since 1.19
341 */
342 protected static function getAllPrefixesDB( $local ) {
343 $db = wfGetDB( DB_SLAVE );
344
345 $where = array();
346
347 if ( $local !== null ) {
348 if ( $local == 1 ) {
349 $where['iw_local'] = 1;
350 } elseif ( $local == 0 ) {
351 $where['iw_local'] = 0;
352 }
353 }
354
355 $res = $db->select( 'interwiki',
356 self::selectFields(),
357 $where, __METHOD__, array( 'ORDER BY' => 'iw_prefix' )
358 );
359
360 $retval = array();
361 foreach ( $res as $row ) {
362 $retval[] = (array)$row;
363 }
364
365 return $retval;
366 }
367
368 /**
369 * Returns all interwiki prefixes
370 *
371 * @param string|null $local If set, limits output to local/non-local interwikis
372 * @return array List of prefixes
373 * @since 1.19
374 */
375 public static function getAllPrefixes( $local = null ) {
376 global $wgInterwikiCache;
377
378 if ( $wgInterwikiCache ) {
379 return self::getAllPrefixesCached( $local );
380 }
381
382 return self::getAllPrefixesDB( $local );
383 }
384
385 /**
386 * Get the URL for a particular title (or with $1 if no title given)
387 *
388 * @param string $title What text to put for the article name
389 * @return string The URL
390 * @note Prior to 1.19 The getURL with an argument was broken.
391 * If you if you use this arg in an extension that supports MW earlier
392 * than 1.19 please wfUrlencode and substitute $1 on your own.
393 */
394 public function getURL( $title = null ) {
395 $url = $this->mURL;
396 if ( $title !== null ) {
397 $url = str_replace( "$1", wfUrlencode( $title ), $url );
398 }
399
400 return $url;
401 }
402
403 /**
404 * Get the API URL for this wiki
405 *
406 * @return string The URL
407 */
408 public function getAPI() {
409 return $this->mAPI;
410 }
411
412 /**
413 * Get the DB name for this wiki
414 *
415 * @return string The DB name
416 */
417 public function getWikiID() {
418 return $this->mWikiID;
419 }
420
421 /**
422 * Is this a local link from a sister project, or is
423 * it something outside, like Google
424 *
425 * @return bool
426 */
427 public function isLocal() {
428 return $this->mLocal;
429 }
430
431 /**
432 * Can pages from this wiki be transcluded?
433 * Still requires $wgEnableScaryTransclusion
434 *
435 * @return bool
436 */
437 public function isTranscludable() {
438 return $this->mTrans;
439 }
440
441 /**
442 * Get the name for the interwiki site
443 *
444 * @return string
445 */
446 public function getName() {
447 $msg = wfMessage( 'interwiki-name-' . $this->mPrefix )->inContentLanguage();
448
449 return !$msg->exists() ? '' : $msg;
450 }
451
452 /**
453 * Get a description for this interwiki
454 *
455 * @return string
456 */
457 public function getDescription() {
458 $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix )->inContentLanguage();
459
460 return !$msg->exists() ? '' : $msg;
461 }
462
463 /**
464 * Return the list of interwiki fields that should be selected to create
465 * a new Interwiki object.
466 * @return string[]
467 */
468 public static function selectFields() {
469 return array(
470 'iw_prefix',
471 'iw_url',
472 'iw_api',
473 'iw_wikiid',
474 'iw_local',
475 'iw_trans'
476 );
477 }
478 }