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