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 * @return bool Whether it exists
31 * @param $prefix string Interwiki prefix to use
33 static public function isValidInterwiki( $prefix ) {
34 $result = self
::fetch( $prefix );
39 * Fetch an Interwiki object
41 * @return Interwiki Object, or null if not valid
42 * @param $prefix string Interwiki prefix to use
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 \type{\string} Interwiki prefix
76 * @return \type{\Interwiki} An 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 \type{\string} Database key
99 * @return \type{\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 bool The prefix is valid
139 protected static function load( $prefix ) {
140 global $wgMemc, $wgInterwikiExpiry;
141 $key = wfMemcKey( 'interwiki', $prefix );
142 $mc = $wgMemc->get( $key );
144 if( $mc && is_array( $mc ) ) { // is_array is hack for old keys
145 $iw = Interwiki
::loadFromArray( $mc );
151 $db = wfGetDB( DB_SLAVE
);
153 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
155 $iw = Interwiki
::loadFromArray( $row );
157 $mc = array( 'iw_url' => $iw->mURL
, 'iw_local' => $iw->mLocal
, 'iw_trans' => $iw->mTrans
);
158 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
166 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
168 * @return bool Whether everything was there
169 * @param $res ResultWrapper Row from the interwiki table
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'];
184 * Get the URL for a particular title (or with $1 if no title given)
186 * @param $title string What text to put for the article name
187 * @return string The URL
189 public function getURL( $title = null ) {
191 if( $title != null ) {
192 $url = str_replace( "$1", $title, $url );
198 * Is this a local link from a sister project, or is
199 * it something outside, like Google
202 public function isLocal() {
203 return $this->mLocal
;
207 * Can pages from this wiki be transcluded?
208 * Still requires $wgEnableScaryTransclusion
211 public function isTranscludable() {
212 return $this->mTrans
;
216 * Get the name for the interwiki site
219 public function getName() {
220 $key = 'interwiki-name-' . $this->mPrefix
;
221 $msg = wfMsgForContent( $key );
222 return wfEmptyMsg( $key, $msg ) ?
'' : $msg;
226 * Get a description for this interwiki
229 public function getDescription() {
230 $key = 'interwiki-desc-' . $this->mPrefix
;
231 $msg = wfMsgForContent( $key );
232 return wfEmptyMsg( $key, $msg ) ?
'' : $msg;