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 return Interwiki
::getInterwikiCached( $key );
58 $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 $key \type{\string} Database key
76 * @return \type{\Interwiki} An interwiki object
78 protected static function getInterwikiCached( $key ) {
79 $value = getInterwikiCacheEntry( $key );
81 $s = new Interwiki( $key );
84 list( $local, $url ) = explode( ' ', $value, 2 );
86 $s->mLocal
= (int)$local;
90 if( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
){
91 reset( self
::$smCache );
92 unset( self
::$smCache[ key( self
::$smCache ) ] );
94 self
::$smCache[$prefix] = $s;
99 * Get entry from interwiki cache
101 * @note More logic is explained in DefaultSettings.
103 * @param $key \type{\string} Database key
104 * @return \type{\string) The entry
106 protected static function getInterwikiCacheEntry( $key ){
107 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
111 $db = dba_open( $wgInterwikiCache, 'r', 'cdb' );
113 /* Resolve site name */
114 if( $wgInterwikiScopes>=3 && !$site ) {
115 $site = dba_fetch( '__sites:' . wfWikiID(), $db );
117 $site = $wgInterwikiFallbackSite;
121 $value = dba_fetch( wfMemcKey( $key ), $db );
123 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
124 $value = dba_fetch( "_{$site}:{$key}", $db );
127 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
128 $value = dba_fetch( "__global:{$key}", $db );
130 if ( $value == 'undef' )
137 * Load the interwiki, trying first memcached then the DB
139 * @param $prefix The interwiki prefix
140 * @return bool The prefix is valid
144 protected static function load( $prefix ) {
146 $key = wfMemcKey( 'interwiki', $prefix );
147 $mc = $wgMemc->get( $key );
149 if( $mc && is_array( $mc ) ){ // is_array is hack for old keys
150 $iw = Interwiki
::loadFromArray( $mc );
156 $db = wfGetDB( DB_SLAVE
);
158 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
160 $iw = Interwiki
::loadFromArray( $row );
162 $mc = array( 'iw_url' => $iw->mURL
, 'iw_local' => $iw->mLocal
, 'iw_trans' => $iw->mTrans
);
163 $wgMemc->add( $key, $mc );
171 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
173 * @return bool Whether everything was there
174 * @param $res ResultWrapper Row from the interwiki table
177 protected static function loadFromArray( $mc ) {
178 if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ){
179 $iw = new Interwiki();
180 $iw->mURL
= $mc['iw_url'];
181 $iw->mLocal
= $mc['iw_local'];
182 $iw->mTrans
= $mc['iw_trans'];
189 * Get the URL for a particular title (or with $1 if no title given)
191 * @param $title string What text to put for the article name
192 * @return string The URL
194 function getURL( $title = null ){
196 if( $title != null ){
197 $url = str_replace( "$1", $title, $url );
203 return $this->mLocal
;
206 function isTranscludable(){
207 return $this->mTrans
;