Merge "wfProfileOut() for new return added in c6396 (c4e407c)"
[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
31 // Cache - removes oldest entry when it hits limit
32 protected static $smCache = array();
33 const CACHE_LIMIT = 100; // 0 means unlimited, any other value is max number of entries.
34
35 protected $mPrefix, $mURL, $mAPI, $mWikiID, $mLocal, $mTrans;
36
37 public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0, $trans = 0 ) {
38 $this->mPrefix = $prefix;
39 $this->mURL = $url;
40 $this->mAPI = $api;
41 $this->mWikiID = $wikiId;
42 $this->mLocal = $local;
43 $this->mTrans = $trans;
44 }
45
46 /**
47 * Check whether an interwiki prefix exists
48 *
49 * @param $prefix String: interwiki prefix to use
50 * @return Boolean: whether it exists
51 */
52 static public function isValidInterwiki( $prefix ) {
53 $result = self::fetch( $prefix );
54 return (bool)$result;
55 }
56
57 /**
58 * Fetch an Interwiki object
59 *
60 * @param $prefix String: interwiki prefix to use
61 * @return Interwiki Object, or null if not valid
62 */
63 static public function fetch( $prefix ) {
64 global $wgContLang;
65 if( $prefix == '' ) {
66 return null;
67 }
68 $prefix = $wgContLang->lc( $prefix );
69 if( isset( self::$smCache[$prefix] ) ) {
70 return self::$smCache[$prefix];
71 }
72 global $wgInterwikiCache;
73 if( $wgInterwikiCache ) {
74 $iw = Interwiki::getInterwikiCached( $prefix );
75 } else {
76 $iw = Interwiki::load( $prefix );
77 if( !$iw ) {
78 $iw = false;
79 }
80 }
81 if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
82 reset( self::$smCache );
83 unset( self::$smCache[key( self::$smCache )] );
84 }
85 self::$smCache[$prefix] = $iw;
86 return $iw;
87 }
88
89 /**
90 * Fetch interwiki prefix data from local cache in constant database.
91 *
92 * @note More logic is explained in DefaultSettings.
93 *
94 * @param $prefix String: interwiki prefix
95 * @return Interwiki object
96 */
97 protected static function getInterwikiCached( $prefix ) {
98 $value = self::getInterwikiCacheEntry( $prefix );
99
100 $s = new Interwiki( $prefix );
101 if ( $value != '' ) {
102 // Split values
103 list( $local, $url ) = explode( ' ', $value, 2 );
104 $s->mURL = $url;
105 $s->mLocal = (int)$local;
106 } else {
107 $s = false;
108 }
109 return $s;
110 }
111
112 /**
113 * Get entry from interwiki cache
114 *
115 * @note More logic is explained in DefaultSettings.
116 *
117 * @param $prefix String: database key
118 * @return String: the entry
119 */
120 protected static function getInterwikiCacheEntry( $prefix ) {
121 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
122 static $db, $site;
123
124 wfDebug( __METHOD__ . "( $prefix )\n" );
125 if( !$db ) {
126 $db = CdbReader::open( $wgInterwikiCache );
127 }
128 /* Resolve site name */
129 if( $wgInterwikiScopes >= 3 && !$site ) {
130 $site = $db->get( '__sites:' . wfWikiID() );
131 if ( $site == '' ) {
132 $site = $wgInterwikiFallbackSite;
133 }
134 }
135
136 $value = $db->get( wfMemcKey( $prefix ) );
137 // Site level
138 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
139 $value = $db->get( "_{$site}:{$prefix}" );
140 }
141 // Global Level
142 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
143 $value = $db->get( "__global:{$prefix}" );
144 }
145 if ( $value == 'undef' ) {
146 $value = '';
147 }
148
149
150 return $value;
151 }
152
153 /**
154 * Load the interwiki, trying first memcached then the DB
155 *
156 * @param $prefix string The interwiki prefix
157 * @return Boolean: the 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 $mc array Associative array: row from the interwiki table
207 * @return Boolean|Interwiki whether everything was there
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 $local null|string 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 $local string|null 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 $local string|null 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 $title String: 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 Boolean
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 Boolean
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 array
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 }