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