4 * Interwiki table entry
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 schema updates etc, which arent wiki-related)
14 // Cache - removes oldest entry when it hits limit
15 protected static $smCache = array();
16 const CACHE_LIMIT
= 100; // 0 means unlimited, any other value is max number of entries.
18 protected $mPrefix, $mURL, $mAPI, $mWikiID, $mLocal, $mTrans;
20 public function __construct( $prefix = null, $url = '', $api = '', $wikiid = '', $local = 0, $trans = 0 ) {
21 $this->mPrefix
= $prefix;
24 $this->mWikiID
= $wikiid;
25 $this->mLocal
= $local;
26 $this->mTrans
= $trans;
30 * Check whether an interwiki prefix exists
32 * @param $prefix String: interwiki prefix to use
33 * @return Boolean: whether it exists
35 static public function isValidInterwiki( $prefix ) {
36 $result = self
::fetch( $prefix );
41 * Fetch an Interwiki object
43 * @param $prefix String: interwiki prefix to use
44 * @return Interwiki Object, or null if not valid
46 static public function fetch( $prefix ) {
51 $prefix = $wgContLang->lc( $prefix );
52 if( isset( self
::$smCache[$prefix] ) ) {
53 return self
::$smCache[$prefix];
55 global $wgInterwikiCache;
56 if( $wgInterwikiCache ) {
57 $iw = Interwiki
::getInterwikiCached( $prefix );
59 $iw = Interwiki
::load( $prefix );
64 if( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
) {
65 reset( self
::$smCache );
66 unset( self
::$smCache[ key( self
::$smCache ) ] );
68 self
::$smCache[$prefix] = $iw;
73 * Fetch interwiki prefix data from local cache in constant database.
75 * @note More logic is explained in DefaultSettings.
77 * @param $prefix String: interwiki prefix
78 * @return Interwiki object
80 protected static function getInterwikiCached( $prefix ) {
81 $value = self
::getInterwikiCacheEntry( $prefix );
83 $s = new Interwiki( $prefix );
86 list( $local, $url ) = explode( ' ', $value, 2 );
88 $s->mLocal
= (int)$local;
96 * Get entry from interwiki cache
98 * @note More logic is explained in DefaultSettings.
100 * @param $prefix String: database key
101 * @return String: the entry
103 protected static function getInterwikiCacheEntry( $prefix ) {
104 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
107 wfDebug( __METHOD__
. "( $prefix )\n" );
109 $db = CdbReader
::open( $wgInterwikiCache );
111 /* Resolve site name */
112 if( $wgInterwikiScopes>=3 && !$site ) {
113 $site = $db->get( '__sites:' . wfWikiID() );
115 $site = $wgInterwikiFallbackSite;
119 $value = $db->get( wfMemcKey( $prefix ) );
121 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
122 $value = $db->get( "_{$site}:{$prefix}" );
125 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
126 $value = $db->get( "__global:{$prefix}" );
128 if ( $value == 'undef' )
135 * Load the interwiki, trying first memcached then the DB
137 * @param $prefix The interwiki prefix
138 * @return Boolean: the prefix is valid
140 protected static function load( $prefix ) {
141 global $wgMemc, $wgInterwikiExpiry;
142 $key = wfMemcKey( 'interwiki', $prefix );
143 $mc = $wgMemc->get( $key );
145 if( $mc && is_array( $mc ) ) { // is_array is hack for old keys
146 $iw = Interwiki
::loadFromArray( $mc );
152 $db = wfGetDB( DB_SLAVE
);
154 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
156 $iw = Interwiki
::loadFromArray( $row );
158 $mc = array( 'iw_url' => $iw->mURL
, 'iw_api' => $iw->mAPI
, 'iw_local' => $iw->mLocal
, 'iw_trans' => $iw->mTrans
);
159 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
167 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
169 * @param $mc Associative array: row from the interwiki table
170 * @return Boolean: whether everything was there
172 protected static function loadFromArray( $mc ) {
173 if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) {
174 $iw = new Interwiki();
175 $iw->mURL
= $mc['iw_url'];
176 $iw->mLocal
= $mc['iw_local'];
177 $iw->mTrans
= $mc['iw_trans'];
178 $iw->mAPI
= isset( $mc['iw_api'] ) ?
$mc['iw_api'] : '';
179 $iw->mWikiID
= isset( $mc['iw_wikiid'] ) ?
$mc['iw_wikiid'] : '';
187 * Get the URL for a particular title (or with $1 if no title given)
189 * @param $title String: what text to put for the article name
190 * @return String: the URL
192 public function getURL( $title = null ) {
194 if( $title != null ) {
195 $url = str_replace( "$1", $title, $url );
201 * Get the API URL for this wiki
203 * @return String: the URL
205 public function getAPI( ) {
210 * Get the DB name for this wiki
212 * @return String: the DB name
214 public function getWikiID( ) {
215 return $this->mWikiID
;
219 * Is this a local link from a sister project, or is
220 * it something outside, like Google
224 public function isLocal() {
225 return $this->mLocal
;
229 * Can pages from this wiki be transcluded?
230 * Still requires $wgEnableScaryTransclusion
234 public function isTranscludable() {
235 return $this->mTrans
;
239 * Get the name for the interwiki site
243 public function getName() {
244 $key = 'interwiki-name-' . $this->mPrefix
;
245 $msg = wfMsgForContent( $key );
246 return wfEmptyMsg( $key, $msg ) ?
'' : $msg;
250 * Get a description for this interwiki
254 public function getDescription() {
255 $key = 'interwiki-desc-' . $this->mPrefix
;
256 $msg = wfMsgForContent( $key );
257 return wfEmptyMsg( $key, $msg ) ?
'' : $msg;