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, $mLocal, $mTrans;
20 public function __construct( $prefix = null, $url = '', $local = 0, $trans = 0 ) {
21 $this->mPrefix
= $prefix;
23 $this->mLocal
= $local;
24 $this->mTrans
= $trans;
28 * Check whether an interwiki prefix exists
30 * @param $prefix String: interwiki prefix to use
31 * @return Boolean: whether it exists
33 static public function isValidInterwiki( $prefix ) {
34 $result = self
::fetch( $prefix );
39 * Fetch an Interwiki object
41 * @param $prefix String: interwiki prefix to use
42 * @return Interwiki Object, or null if not valid
44 static public function fetch( $prefix ) {
49 $prefix = $wgContLang->lc( $prefix );
50 if( isset( self
::$smCache[$prefix] ) ) {
51 return self
::$smCache[$prefix];
53 global $wgInterwikiCache;
54 if( $wgInterwikiCache ) {
55 $iw = Interwiki
::getInterwikiCached( $prefix );
57 $iw = Interwiki
::load( $prefix );
62 if( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
) {
63 reset( self
::$smCache );
64 unset( self
::$smCache[ key( self
::$smCache ) ] );
66 self
::$smCache[$prefix] = $iw;
71 * Fetch interwiki prefix data from local cache in constant database.
73 * @note More logic is explained in DefaultSettings.
75 * @param $prefix String: interwiki prefix
76 * @return Interwiki object
78 protected static function getInterwikiCached( $prefix ) {
79 $value = self
::getInterwikiCacheEntry( $prefix );
81 $s = new Interwiki( $prefix );
84 list( $local, $url ) = explode( ' ', $value, 2 );
86 $s->mLocal
= (int)$local;
94 * Get entry from interwiki cache
96 * @note More logic is explained in DefaultSettings.
98 * @param $prefix String: database key
99 * @return String: the entry
101 protected static function getInterwikiCacheEntry( $prefix ) {
102 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
105 wfDebug( __METHOD__
. "( $prefix )\n" );
107 $db = CdbReader
::open( $wgInterwikiCache );
109 /* Resolve site name */
110 if( $wgInterwikiScopes>=3 && !$site ) {
111 $site = $db->get( '__sites:' . wfWikiID() );
113 $site = $wgInterwikiFallbackSite;
117 $value = $db->get( wfMemcKey( $prefix ) );
119 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
120 $value = $db->get( "_{$site}:{$prefix}" );
123 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
124 $value = $db->get( "__global:{$prefix}" );
126 if ( $value == 'undef' )
133 * Load the interwiki, trying first memcached then the DB
135 * @param $prefix The interwiki prefix
136 * @return Boolean: the prefix is valid
138 protected static function load( $prefix ) {
139 global $wgMemc, $wgInterwikiExpiry;
140 $key = wfMemcKey( 'interwiki', $prefix );
141 $mc = $wgMemc->get( $key );
143 if( $mc && is_array( $mc ) ) { // is_array is hack for old keys
144 $iw = Interwiki
::loadFromArray( $mc );
150 $db = wfGetDB( DB_SLAVE
);
152 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
154 $iw = Interwiki
::loadFromArray( $row );
156 $mc = array( 'iw_url' => $iw->mURL
, 'iw_local' => $iw->mLocal
, 'iw_trans' => $iw->mTrans
);
157 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
165 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
167 * @param $mc Associative array: row from the interwiki table
168 * @return Boolean: whether everything was there
170 protected static function loadFromArray( $mc ) {
171 if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) {
172 $iw = new Interwiki();
173 $iw->mURL
= $mc['iw_url'];
174 $iw->mLocal
= $mc['iw_local'];
175 $iw->mTrans
= $mc['iw_trans'];
182 * Get the URL for a particular title (or with $1 if no title given)
184 * @param $title String: what text to put for the article name
185 * @return String: the URL
187 public function getURL( $title = null ) {
189 if( $title != null ) {
190 $url = str_replace( "$1", $title, $url );
196 * Is this a local link from a sister project, or is
197 * it something outside, like Google
201 public function isLocal() {
202 return $this->mLocal
;
206 * Can pages from this wiki be transcluded?
207 * Still requires $wgEnableScaryTransclusion
211 public function isTranscludable() {
212 return $this->mTrans
;
216 * Get the name for the interwiki site
220 public function getName() {
221 $key = 'interwiki-name-' . $this->mPrefix
;
222 $msg = wfMsgForContent( $key );
223 return wfEmptyMsg( $key, $msg ) ?
'' : $msg;
227 * Get a description for this interwiki
231 public function getDescription() {
232 $key = 'interwiki-desc-' . $this->mPrefix
;
233 $msg = wfMsgForContent( $key );
234 return wfEmptyMsg( $key, $msg ) ?
'' : $msg;