1ba0f106d8345ab94f55f84cfaec88fc441d1d09
[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 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 }
154
155 if( $iwData && is_array( $iwData ) ) { // is_array is hack for old keys
156 $iw = Interwiki::loadFromArray( $iwData );
157 if( $iw ) {
158 return $iw;
159 }
160 }
161
162 $db = wfGetDB( DB_SLAVE );
163
164 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
165 __METHOD__ ) );
166 $iw = Interwiki::loadFromArray( $row );
167 if ( $iw ) {
168 $mc = array(
169 'iw_url' => $iw->mURL,
170 'iw_api' => $iw->mAPI,
171 'iw_local' => $iw->mLocal,
172 'iw_trans' => $iw->mTrans
173 );
174 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
175 return $iw;
176 }
177
178 return false;
179 }
180
181 /**
182 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
183 *
184 * @param $mc Associative array: row from the interwiki table
185 * @return Boolean: whether everything was there
186 */
187 protected static function loadFromArray( $mc ) {
188 if( isset( $mc['iw_url'] ) ) {
189 $iw = new Interwiki();
190 $iw->mURL = $mc['iw_url'];
191 $iw->mLocal = isset( $mc['iw_local'] ) ? $mc['iw_local'] : 0;
192 $iw->mTrans = isset( $mc['iw_trans'] ) ? $mc['iw_trans'] : 0;
193 $iw->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : '';
194 $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : '';
195
196 return $iw;
197 }
198 return false;
199 }
200
201 /**
202 * Fetch all interwiki prefixes from interwiki cache
203 *
204 * @param $local If not null, limits output to local/non-local interwikis
205 * @return Array List of prefixes
206 * @since 1.19
207 */
208 protected static function getAllPrefixesCached( $local ) {
209 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
210 static $db, $site;
211
212 wfDebug( __METHOD__ . "()\n" );
213 if( !$db ) {
214 $db = CdbReader::open( $wgInterwikiCache );
215 }
216 /* Resolve site name */
217 if( $wgInterwikiScopes >= 3 && !$site ) {
218 $site = $db->get( '__sites:' . wfWikiID() );
219 if ( $site == '' ) {
220 $site = $wgInterwikiFallbackSite;
221 }
222 }
223
224 // List of interwiki sources
225 $sources = array();
226 // Global Level
227 if ( $wgInterwikiScopes >= 2 ) {
228 $sources[] = '__global';
229 }
230 // Site level
231 if ( $wgInterwikiScopes >= 3 ) {
232 $sources[] = '_' . $site;
233 }
234 $sources[] = wfWikiID();
235
236 $data = array();
237
238 foreach( $sources as $source ) {
239 $list = $db->get( "__list:{$source}" );
240 foreach ( explode( ' ', $list ) as $iw_prefix ) {
241 $row = $db->get( "{$source}:{$iw_prefix}" );
242 if( !$row ) {
243 continue;
244 }
245
246 list( $iw_local, $iw_url ) = explode( ' ', $row );
247
248 if ( $local !== null && $local != $iw_local ) {
249 continue;
250 }
251
252 $data[$iw_prefix] = array(
253 'iw_prefix' => $iw_prefix,
254 'iw_url' => $iw_url,
255 'iw_local' => $iw_local,
256 );
257 }
258 }
259
260 ksort( $data );
261
262 return array_values( $data );
263 }
264
265 /**
266 * Fetch all interwiki prefixes from DB
267 *
268 * @param $local If not null, limits output to local/non-local interwikis
269 * @return Array List of prefixes
270 * @since 1.19
271 */
272 protected static function getAllPrefixesDB( $local ) {
273 $db = wfGetDB( DB_SLAVE );
274
275 $where = array();
276
277 if ( $local !== null ) {
278 if ( $local == 1 ) {
279 $where['iw_local'] = 1;
280 } elseif ( $local == 0 ) {
281 $where['iw_local'] = 0;
282 }
283 }
284
285 $res = $db->select( 'interwiki',
286 array( 'iw_prefix', 'iw_url', 'iw_api', 'iw_wikiid', 'iw_local', 'iw_trans' ),
287 $where, __METHOD__, array( 'ORDER BY' => 'iw_prefix' )
288 );
289 $retval = array();
290 foreach ( $res as $row ) {
291 $retval[] = (array)$row;
292 }
293 return $retval;
294 }
295
296 /**
297 * Returns all interwiki prefixes
298 *
299 * @param $local If set, limits output to local/non-local interwikis
300 * @return Array List of prefixes
301 * @since 1.19
302 */
303 public static function getAllPrefixes( $local = null ) {
304 global $wgInterwikiCache;
305
306 if ( $wgInterwikiCache ) {
307 return self::getAllPrefixesCached( $local );
308 } else {
309 return self::getAllPrefixesDB( $local );
310 }
311 }
312
313 /**
314 * Get the URL for a particular title (or with $1 if no title given)
315 *
316 * @param $title String: what text to put for the article name
317 * @return String: the URL
318 * @note Prior to 1.19 getURL did not urlencode the $title, if you use this
319 * arg in an extension that supports MW earlier than 1.19 please ensure
320 * you wfUrlencode it when installed in earlier versions of MW.
321 */
322 public function getURL( $title = null ) {
323 $url = $this->mURL;
324 if( $title != null ) {
325 $url = str_replace( "$1", wfUrlencode( $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 }