Clarified FileBackend::doOperations() docs a bit more.
[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 if ( !$db ) {
127 $db = CdbReader::open( $wgInterwikiCache );
128 }
129 /* Resolve site name */
130 if ( $wgInterwikiScopes >= 3 && !$site ) {
131 $site = $db->get( '__sites:' . wfWikiID() );
132 if ( $site == '' ) {
133 $site = $wgInterwikiFallbackSite;
134 }
135 }
136
137 $value = $db->get( wfMemcKey( $prefix ) );
138 // Site level
139 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
140 $value = $db->get( "_{$site}:{$prefix}" );
141 }
142 // Global Level
143 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
144 $value = $db->get( "__global:{$prefix}" );
145 }
146 if ( $value == 'undef' ) {
147 $value = '';
148 }
149
150 return $value;
151 }
152
153 /**
154 * Load the interwiki, trying first memcached then the DB
155 *
156 * @param string $prefix The interwiki prefix
157 * @return bool If $prefix is valid
158 */
159 protected static function load( $prefix ) {
160 global $wgMemc, $wgInterwikiExpiry;
161
162 $iwData = false;
163 if ( !wfRunHooks( 'InterwikiLoadPrefix', array( $prefix, &$iwData ) ) ) {
164 return Interwiki::loadFromArray( $iwData );
165 }
166
167 if ( !$iwData ) {
168 $key = wfMemcKey( 'interwiki', $prefix );
169 $iwData = $wgMemc->get( $key );
170 if ( $iwData === '!NONEXISTENT' ) {
171 return false; // negative cache hit
172 }
173 }
174
175 if ( $iwData && is_array( $iwData ) ) { // is_array is hack for old keys
176 $iw = Interwiki::loadFromArray( $iwData );
177 if ( $iw ) {
178 return $iw;
179 }
180 }
181
182 $db = wfGetDB( DB_SLAVE );
183
184 $row = $db->fetchRow( $db->select( 'interwiki', self::selectFields(), array( 'iw_prefix' => $prefix ),
185 __METHOD__ ) );
186 $iw = Interwiki::loadFromArray( $row );
187 if ( $iw ) {
188 $mc = array(
189 'iw_url' => $iw->mURL,
190 'iw_api' => $iw->mAPI,
191 'iw_local' => $iw->mLocal,
192 'iw_trans' => $iw->mTrans
193 );
194 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
195 return $iw;
196 } else {
197 $wgMemc->add( $key, '!NONEXISTENT', $wgInterwikiExpiry ); // negative cache hit
198 }
199
200 return false;
201 }
202
203 /**
204 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
205 *
206 * @param array $mc Associative array: row from the interwiki table
207 * @return Interwiki|bool Interwiki object or false if $mc['iw_url'] is not set
208 */
209 protected static function loadFromArray( $mc ) {
210 if ( isset( $mc['iw_url'] ) ) {
211 $iw = new Interwiki();
212 $iw->mURL = $mc['iw_url'];
213 $iw->mLocal = isset( $mc['iw_local'] ) ? $mc['iw_local'] : 0;
214 $iw->mTrans = isset( $mc['iw_trans'] ) ? $mc['iw_trans'] : 0;
215 $iw->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : '';
216 $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : '';
217
218 return $iw;
219 }
220 return false;
221 }
222
223 /**
224 * Fetch all interwiki prefixes from interwiki cache
225 *
226 * @param null|string $local If not null, limits output to local/non-local interwikis
227 * @return array List of prefixes
228 * @since 1.19
229 */
230 protected static function getAllPrefixesCached( $local ) {
231 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
232 static $db, $site;
233
234 wfDebug( __METHOD__ . "()\n" );
235 if ( !$db ) {
236 $db = CdbReader::open( $wgInterwikiCache );
237 }
238 /* Resolve site name */
239 if ( $wgInterwikiScopes >= 3 && !$site ) {
240 $site = $db->get( '__sites:' . wfWikiID() );
241 if ( $site == '' ) {
242 $site = $wgInterwikiFallbackSite;
243 }
244 }
245
246 // List of interwiki sources
247 $sources = array();
248 // Global Level
249 if ( $wgInterwikiScopes >= 2 ) {
250 $sources[] = '__global';
251 }
252 // Site level
253 if ( $wgInterwikiScopes >= 3 ) {
254 $sources[] = '_' . $site;
255 }
256 $sources[] = wfWikiID();
257
258 $data = array();
259
260 foreach ( $sources as $source ) {
261 $list = $db->get( "__list:{$source}" );
262 foreach ( explode( ' ', $list ) as $iw_prefix ) {
263 $row = $db->get( "{$source}:{$iw_prefix}" );
264 if ( !$row ) {
265 continue;
266 }
267
268 list( $iw_local, $iw_url ) = explode( ' ', $row );
269
270 if ( $local !== null && $local != $iw_local ) {
271 continue;
272 }
273
274 $data[$iw_prefix] = array(
275 'iw_prefix' => $iw_prefix,
276 'iw_url' => $iw_url,
277 'iw_local' => $iw_local,
278 );
279 }
280 }
281
282 ksort( $data );
283
284 return array_values( $data );
285 }
286
287 /**
288 * Fetch all interwiki prefixes from DB
289 *
290 * @param string|null $local If not null, limits output to local/non-local interwikis
291 * @return array List of prefixes
292 * @since 1.19
293 */
294 protected static function getAllPrefixesDB( $local ) {
295 $db = wfGetDB( DB_SLAVE );
296
297 $where = array();
298
299 if ( $local !== null ) {
300 if ( $local == 1 ) {
301 $where['iw_local'] = 1;
302 } elseif ( $local == 0 ) {
303 $where['iw_local'] = 0;
304 }
305 }
306
307 $res = $db->select( 'interwiki',
308 self::selectFields(),
309 $where, __METHOD__, array( 'ORDER BY' => 'iw_prefix' )
310 );
311 $retval = array();
312 foreach ( $res as $row ) {
313 $retval[] = (array)$row;
314 }
315 return $retval;
316 }
317
318 /**
319 * Returns all interwiki prefixes
320 *
321 * @param string|null $local If set, limits output to local/non-local interwikis
322 * @return array List of prefixes
323 * @since 1.19
324 */
325 public static function getAllPrefixes( $local = null ) {
326 global $wgInterwikiCache;
327
328 if ( $wgInterwikiCache ) {
329 return self::getAllPrefixesCached( $local );
330 } else {
331 return self::getAllPrefixesDB( $local );
332 }
333 }
334
335 /**
336 * Get the URL for a particular title (or with $1 if no title given)
337 *
338 * @param string $title What text to put for the article name
339 * @return string The URL
340 * @note Prior to 1.19 The getURL with an argument was broken.
341 * If you if you use this arg in an extension that supports MW earlier
342 * than 1.19 please wfUrlencode and substitute $1 on your own.
343 */
344 public function getURL( $title = null ) {
345 $url = $this->mURL;
346 if ( $title !== null ) {
347 $url = str_replace( "$1", wfUrlencode( $title ), $url );
348 }
349 return $url;
350 }
351
352 /**
353 * Get the API URL for this wiki
354 *
355 * @return string The URL
356 */
357 public function getAPI() {
358 return $this->mAPI;
359 }
360
361 /**
362 * Get the DB name for this wiki
363 *
364 * @return string The DB name
365 */
366 public function getWikiID() {
367 return $this->mWikiID;
368 }
369
370 /**
371 * Is this a local link from a sister project, or is
372 * it something outside, like Google
373 *
374 * @return bool
375 */
376 public function isLocal() {
377 return $this->mLocal;
378 }
379
380 /**
381 * Can pages from this wiki be transcluded?
382 * Still requires $wgEnableScaryTransclusion
383 *
384 * @return bool
385 */
386 public function isTranscludable() {
387 return $this->mTrans;
388 }
389
390 /**
391 * Get the name for the interwiki site
392 *
393 * @return string
394 */
395 public function getName() {
396 $msg = wfMessage( 'interwiki-name-' . $this->mPrefix )->inContentLanguage();
397 return !$msg->exists() ? '' : $msg;
398 }
399
400 /**
401 * Get a description for this interwiki
402 *
403 * @return string
404 */
405 public function getDescription() {
406 $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix )->inContentLanguage();
407 return !$msg->exists() ? '' : $msg;
408 }
409
410 /**
411 * Return the list of interwiki fields that should be selected to create
412 * a new Interwiki object.
413 * @return string[]
414 */
415 public static function selectFields() {
416 return array(
417 'iw_prefix',
418 'iw_url',
419 'iw_api',
420 'iw_wikiid',
421 'iw_local',
422 'iw_trans'
423 );
424 }
425 }