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 ){
36 $prefix = $wgContLang->lc( $prefix );
37 if( isset( self
::$smCache[$prefix] ) ){
40 global $wgInterwikiCache;
41 if ($wgInterwikiCache) {
42 return Interwiki
::isValidInterwikiCached( $key );
44 $iw = Interwiki
::load( $prefix );
48 if( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
){
49 reset( self
::$smCache );
50 unset( self
::$smCache[ key( self
::$smCache ) ] );
52 self
::$smCache[$prefix] = $iw;
57 * Fetch an Interwiki object
59 * @return Interwiki Object, or null if not valid
60 * @param $prefix string Interwiki prefix to use
62 static public function fetch( $prefix ) {
64 $prefix = $wgContLang->lc( $prefix );
65 if( isset( self
::$smCache[$prefix] ) ){
66 return self
::$smCache[$prefix];
68 global $wgInterwikiCache;
69 if ($wgInterwikiCache) {
70 return Interwiki
::getInterwikiCached( $key );
72 $iw = Interwiki
::load( $prefix );
76 if( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
){
77 reset( self
::$smCache );
78 unset( self
::$smCache[ key( self
::$smCache ) ] );
80 self
::$smCache[$prefix] = $iw;
85 * Fetch interwiki prefix data from local cache in constant database.
87 * @note More logic is explained in DefaultSettings.
89 * @param $key \type{\string} Database key
90 * @return \type{\Interwiki} An interwiki object
92 protected static function getInterwikiCached( $key ) {
93 $value = getInterwikiCacheEntry( $key );
95 $s = new Interwiki( $key );
98 list( $local, $url ) = explode( ' ', $value, 2 );
100 $s->mLocal
= (int)$local;
104 if( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
){
105 reset( self
::$smCache );
106 unset( self
::$smCache[ key( self
::$smCache ) ] );
108 self
::$smCache[$prefix] = $s;
113 * Check whether an interwiki is in the cache
115 * @note More logic is explained in DefaultSettings.
117 * @param $key \type{\string} Database key
118 * @return \type{\bool} Whether it exists
120 protected static function isValidInterwikiCached( $key ) {
121 $value = getInterwikiCacheEntry( $key );
126 * Get entry from interwiki cache
128 * @note More logic is explained in DefaultSettings.
130 * @param $key \type{\string} Database key
131 * @return \type{\string) The entry
133 protected static function getInterwikiCacheEntry( $key ){
134 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
138 $db = dba_open( $wgInterwikiCache, 'r', 'cdb' );
140 /* Resolve site name */
141 if( $wgInterwikiScopes>=3 && !$site ) {
142 $site = dba_fetch( '__sites:' . wfWikiID(), $db );
144 $site = $wgInterwikiFallbackSite;
148 $value = dba_fetch( wfMemcKey( $key ), $db );
150 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
151 $value = dba_fetch( "_{$site}:{$key}", $db );
154 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
155 $value = dba_fetch( "__global:{$key}", $db );
157 if ( $value == 'undef' )
164 * Load the interwiki, trying first memcached then the DB
166 * @param $prefix The interwiki prefix
167 * @return bool The prefix is valid
171 protected static function load( $prefix ) {
173 $key = wfMemcKey( 'interwiki', $prefix );
174 $mc = $wgMemc->get( $key );
176 if( $mc && is_array( $mc ) ){ // is_array is hack for old keys
177 $iw = Interwiki
::loadFromArray( $mc );
181 $db = wfGetDB( DB_SLAVE
);
183 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
185 $iw = Interwiki
::loadFromArray( $row );
187 $mc = array( 'iw_url' => $iw->mURL
, 'iw_local' => $iw->mLocal
, 'iw_trans' => $iw->mTrans
);
188 $wgMemc->add( $key, $mc );
196 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
198 * @return bool Whether everything was there
199 * @param $res ResultWrapper Row from the interwiki table
202 protected static function loadFromArray( $mc ) {
203 if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ){
204 $iw = new Interwiki();
205 $iw->mURL
= $mc['iw_url'];
206 $iw->mLocal
= $mc['iw_local'];
207 $iw->mTrans
= $mc['iw_trans'];
214 * Get the URL for a particular title (or with $1 if no title given)
216 * @param $title string What text to put for the article name
217 * @return string The URL
219 function getURL( $title = null ){
221 if( $title != null ){
222 $url = str_replace( "$1", $title, $url );
228 return $this->mLocal
;
231 function isTranscludable(){
232 return $this->mTrans
;