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 - removed in LRU order 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 * Fetch an Interwiki object
31 * @return Interwiki Object, or null if not valid
32 * @param $prefix string Interwiki prefix to use
34 static public function fetch( $prefix ) {
35 if( isset( self
::$smCache[$prefix] ) ){
36 return self
::$smCache[$prefix];
38 global $wgInterwikiCache;
39 if ($wgInterwikiCache) {
40 return Interwiki
::getInterwikiCached( $key );
43 if(! $iw->load( $prefix ) ){
46 if( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
){
47 array_shift( self
::$smCache );
49 self
::$smCache[$prefix] = &$iw;
54 * Fetch interwiki prefix data from local cache in constant database.
56 * @note More logic is explained in DefaultSettings.
58 * @param $key \type{\string} Database key
59 * @return \type{\string} URL of interwiki site
61 protected static function getInterwikiCached( $key ) {
62 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
66 $db=dba_open($wgInterwikiCache,'r','cdb');
67 /* Resolve site name */
68 if ($wgInterwikiScopes>=3 and !$site) {
69 $site = dba_fetch('__sites:' . wfWikiID(), $db);
71 $site = $wgInterwikiFallbackSite;
73 $value = dba_fetch( wfMemcKey( $key ), $db);
74 if ($value=='' and $wgInterwikiScopes>=3) {
76 $value = dba_fetch("_{$site}:{$key}", $db);
78 if ($value=='' and $wgInterwikiScopes>=2) {
80 $value = dba_fetch("__global:{$key}", $db);
84 $s = new Interwiki( $key );
86 list( $local, $url ) = explode( ' ', $value, 2 );
88 $s->mLocal
= (int)$local;
92 if( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
){
93 array_shift( self
::$smCache );
95 self
::$smCache[$prefix] = &$s;
105 $db = wfGetDB( DB_SLAVE
);
110 * Load interwiki from the DB
112 * @param $prefix The interwiki prefix
113 * @return bool The prefix is valid
116 function load( $prefix ) {
118 $key = wfMemcKey( 'interwiki', $prefix );
119 $mc = $wgMemc->get( $key );
120 if( $mc && is_array( $mc ) && $this->loadFromArray( $mc ) ){ // is_array is hack for old keys
121 wfDebug("Succeeded\n");
125 $db =& $this->getDB();
126 $res = $db->resultObject( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
128 if ( $this->loadFromResult( $res ) ) {
129 $mc = array( 'url' => $this->mURL
, 'local' => $this->mLocal
, 'trans' => $this->mTrans
);
130 $wgMemc->add( $key, $mc );
137 * Fill in member variables from an array (e.g. memcached result)
139 * @return bool Whether everything was there
140 * @param $res ResultWrapper Row from the interwiki table
142 function loadFromArray( $mc ) {
143 if( isset( $mc['url'] ) && isset( $mc['local'] ) && isset( $mc['trans'] ) ){
144 $this->mURL
= $mc['url'];
145 $this->mLocal
= $mc['local'];
146 $this->mTrans
= $mc['trans'];
153 * Fill in member variables from a result wrapper
155 * @return bool Whether there was a row there
156 * @param $res ResultWrapper Row from the interwiki table
158 function loadFromResult( ResultWrapper
$res ) {
160 if ( 0 != $res->numRows() ) {
162 $row = $res->fetchObject();
163 $this->initFromRow( $row );
171 * Given a database row from the interwiki table, initialize
174 * @param $row ResultWrapper A row from the interwiki table
176 function initFromRow( $row ) {
177 $this->mPrefix
= $row->iw_prefix
;
178 $this->mURL
= $row->iw_url
;
179 $this->mLocal
= $row->iw_local
;
180 $this->mTrans
= $row->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 function getURL( $title = null ){
191 if( $title != null ){
192 $url = str_replace( "$1", $title, $url );
198 return $this->mLocal
;
201 function isTranscludable(){
202 return $this->mTrans
;