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 function __construct( $prefix = null, $url = '', $local = 0, $trans = 0 )
22 $this->mPrefix
= $prefix;
24 $this->mLocal
= $local;
25 $this->mTrans
= $trans;
29 * Check whether an interwiki prefix exists
31 * @return bool Whether it exists
32 * @param $prefix string Interwiki prefix to use
34 static public function isValidInterwiki( $prefix ){
35 $result = self
::fetch( $prefix );
40 * Fetch an Interwiki object
42 * @return Interwiki Object, or null if not valid
43 * @param $prefix string Interwiki prefix to use
45 static public function fetch( $prefix ) {
50 $prefix = $wgContLang->lc( $prefix );
51 if( isset( self
::$smCache[$prefix] ) ){
52 return self
::$smCache[$prefix];
54 global $wgInterwikiCache;
55 if ($wgInterwikiCache) {
56 $iw = Interwiki
::getInterwikiCached( $prefix );
58 $iw = Interwiki
::load( $prefix );
63 if( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
){
64 reset( self
::$smCache );
65 unset( self
::$smCache[ key( self
::$smCache ) ] );
67 self
::$smCache[$prefix] = $iw;
72 * Fetch interwiki prefix data from local cache in constant database.
74 * @note More logic is explained in DefaultSettings.
76 * @param $prefix \type{\string} Interwiki prefix
77 * @return \type{\Interwiki} An interwiki object
79 protected static function getInterwikiCached( $prefix ) {
80 $value = self
::getInterwikiCacheEntry( $prefix );
82 $s = new Interwiki( $prefix );
85 list( $local, $url ) = explode( ' ', $value, 2 );
87 $s->mLocal
= (int)$local;
95 * Get entry from interwiki cache
97 * @note More logic is explained in DefaultSettings.
99 * @param $prefix \type{\string} Database key
100 * @return \type{\string) The entry
102 protected static function getInterwikiCacheEntry( $prefix ){
103 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
106 wfDebug( __METHOD__
. "( $prefix )\n" );
108 $db = dba_open( $wgInterwikiCache, 'r', 'cdb' );
110 /* Resolve site name */
111 if( $wgInterwikiScopes>=3 && !$site ) {
112 $site = dba_fetch( '__sites:' . wfWikiID(), $db );
114 $site = $wgInterwikiFallbackSite;
118 $value = dba_fetch( wfMemcKey( $prefix ), $db );
120 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
121 $value = dba_fetch( "_{$site}:{$prefix}", $db );
124 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
125 $value = dba_fetch( "__global:{$prefix}", $db );
127 if ( $value == 'undef' )
134 * Load the interwiki, trying first memcached then the DB
136 * @param $prefix The interwiki prefix
137 * @return bool The prefix is valid
141 protected static function load( $prefix ) {
142 global $wgMemc, $wgInterwikiExpiry;
143 $key = wfMemcKey( 'interwiki', $prefix );
144 $mc = $wgMemc->get( $key );
146 if( $mc && is_array( $mc ) ){ // is_array is hack for old keys
147 $iw = Interwiki
::loadFromArray( $mc );
153 $db = wfGetDB( DB_SLAVE
);
155 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
157 $iw = Interwiki
::loadFromArray( $row );
159 $mc = array( 'iw_url' => $iw->mURL
, 'iw_local' => $iw->mLocal
, 'iw_trans' => $iw->mTrans
);
160 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
168 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
170 * @return bool Whether everything was there
171 * @param $res ResultWrapper Row from the interwiki table
174 protected static function loadFromArray( $mc ) {
175 if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ){
176 $iw = new Interwiki();
177 $iw->mURL
= $mc['iw_url'];
178 $iw->mLocal
= $mc['iw_local'];
179 $iw->mTrans
= $mc['iw_trans'];
186 * Get the URL for a particular title (or with $1 if no title given)
188 * @param $title string What text to put for the article name
189 * @return string The URL
191 function getURL( $title = null ){
193 if( $title != null ){
194 $url = str_replace( "$1", $title, $url );
200 return $this->mLocal
;
203 function isTranscludable(){
204 return $this->mTrans
;