* Reduced some pointless regex capture overhead
[mediawiki.git] / includes / Interwiki.php
blob5a3b655643cc34bc1f0ad69630938d59547d901b
1 <?php
2 /**
3 * @file
4 * Interwiki table entry
5 */
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)
13 class Interwiki {
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.
19 protected $mPrefix, $mURL, $mAPI, $mWikiID, $mLocal, $mTrans;
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;
30 /**
31 * Check whether an interwiki prefix exists
33 * @param $prefix String: interwiki prefix to use
34 * @return Boolean: whether it exists
36 static public function isValidInterwiki( $prefix ) {
37 $result = self::fetch( $prefix );
38 return (bool)$result;
41 /**
42 * Fetch an Interwiki object
44 * @param $prefix String: interwiki prefix to use
45 * @return Interwiki Object, or null if not valid
47 static public function fetch( $prefix ) {
48 global $wgContLang;
49 if( $prefix == '' ) {
50 return null;
52 $prefix = $wgContLang->lc( $prefix );
53 if( isset( self::$smCache[$prefix] ) ) {
54 return self::$smCache[$prefix];
56 global $wgInterwikiCache;
57 if( $wgInterwikiCache ) {
58 $iw = Interwiki::getInterwikiCached( $prefix );
59 } else {
60 $iw = Interwiki::load( $prefix );
61 if( !$iw ) {
62 $iw = false;
65 if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
66 reset( self::$smCache );
67 unset( self::$smCache[key( self::$smCache )] );
69 self::$smCache[$prefix] = $iw;
70 return $iw;
73 /**
74 * Fetch interwiki prefix data from local cache in constant database.
76 * @note More logic is explained in DefaultSettings.
78 * @param $prefix String: interwiki prefix
79 * @return Interwiki object
81 protected static function getInterwikiCached( $prefix ) {
82 $value = self::getInterwikiCacheEntry( $prefix );
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;
93 return $s;
96 /**
97 * Get entry from interwiki cache
99 * @note More logic is explained in DefaultSettings.
101 * @param $prefix String: database key
102 * @return String: the entry
104 protected static function getInterwikiCacheEntry( $prefix ) {
105 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
106 static $db, $site;
108 wfDebug( __METHOD__ . "( $prefix )\n" );
109 if( !$db ) {
110 $db = CdbReader::open( $wgInterwikiCache );
112 /* Resolve site name */
113 if( $wgInterwikiScopes >= 3 && !$site ) {
114 $site = $db->get( '__sites:' . wfWikiID() );
115 if ( $site == '' ) {
116 $site = $wgInterwikiFallbackSite;
120 $value = $db->get( wfMemcKey( $prefix ) );
121 // Site level
122 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
123 $value = $db->get( "_{$site}:{$prefix}" );
125 // Global Level
126 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
127 $value = $db->get( "__global:{$prefix}" );
129 if ( $value == 'undef' ) {
130 $value = '';
133 return $value;
137 * Load the interwiki, trying first memcached then the DB
139 * @param $prefix The interwiki prefix
140 * @return Boolean: the prefix is valid
142 protected static function load( $prefix ) {
143 global $wgMemc, $wgInterwikiExpiry;
144 $key = wfMemcKey( 'interwiki', $prefix );
145 $mc = $wgMemc->get( $key );
147 if( $mc && is_array( $mc ) ) { // is_array is hack for old keys
148 $iw = Interwiki::loadFromArray( $mc );
149 if( $iw ) {
150 return $iw;
154 $db = wfGetDB( DB_SLAVE );
156 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
157 __METHOD__ ) );
158 $iw = Interwiki::loadFromArray( $row );
159 if ( $iw ) {
160 $mc = array(
161 'iw_url' => $iw->mURL,
162 'iw_api' => $iw->mAPI,
163 'iw_local' => $iw->mLocal,
164 'iw_trans' => $iw->mTrans
166 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
167 return $iw;
170 return false;
174 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
176 * @param $mc Associative array: row from the interwiki table
177 * @return Boolean: whether everything was there
179 protected static function loadFromArray( $mc ) {
180 if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) {
181 $iw = new Interwiki();
182 $iw->mURL = $mc['iw_url'];
183 $iw->mLocal = $mc['iw_local'];
184 $iw->mTrans = $mc['iw_trans'];
185 $iw->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : '';
186 $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : '';
188 return $iw;
190 return false;
194 * Get the URL for a particular title (or with $1 if no title given)
196 * @param $title String: what text to put for the article name
197 * @return String: the URL
199 public function getURL( $title = null ) {
200 $url = $this->mURL;
201 if( $title != null ) {
202 $url = str_replace( "$1", $title, $url );
204 return $url;
208 * Get the API URL for this wiki
210 * @return String: the URL
212 public function getAPI() {
213 return $this->mAPI;
217 * Get the DB name for this wiki
219 * @return String: the DB name
221 public function getWikiID() {
222 return $this->mWikiID;
226 * Is this a local link from a sister project, or is
227 * it something outside, like Google
229 * @return Boolean
231 public function isLocal() {
232 return $this->mLocal;
236 * Can pages from this wiki be transcluded?
237 * Still requires $wgEnableScaryTransclusion
239 * @return Boolean
241 public function isTranscludable() {
242 return $this->mTrans;
246 * Get the name for the interwiki site
248 * @return String
250 public function getName() {
251 $key = 'interwiki-name-' . $this->mPrefix;
252 $msg = wfMsgForContent( $key );
253 return wfEmptyMsg( $key, $msg ) ? '' : $msg;
257 * Get a description for this interwiki
259 * @return String
261 public function getDescription() {
262 $key = 'interwiki-desc-' . $this->mPrefix;
263 $msg = wfMsgForContent( $key );
264 return wfEmptyMsg( $key, $msg ) ? '' : $msg;