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 );
44 if( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
){
45 array_shift( self
::$smCache );
47 self
::$smCache[$prefix] = &$iw;
52 * Fetch interwiki prefix data from local cache in constant database.
54 * @note More logic is explained in DefaultSettings.
56 * @param $key \type{\string} Database key
57 * @return \type{\string} URL of interwiki site
59 protected static function getInterwikiCached( $key ) {
60 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
64 $db=dba_open($wgInterwikiCache,'r','cdb');
65 /* Resolve site name */
66 if ($wgInterwikiScopes>=3 and !$site) {
67 $site = dba_fetch('__sites:' . wfWikiID(), $db);
69 $site = $wgInterwikiFallbackSite;
71 $value = dba_fetch( wfMemcKey( $key ), $db);
72 if ($value=='' and $wgInterwikiScopes>=3) {
74 $value = dba_fetch("_{$site}:{$key}", $db);
76 if ($value=='' and $wgInterwikiScopes>=2) {
78 $value = dba_fetch("__global:{$key}", $db);
82 $s = new Interwiki( $key );
84 list( $local, $url ) = explode( ' ', $value, 2 );
86 $s->mLocal
= (int)$local;
88 if( self
::CACHE_LIMIT
&& count( self
::$smCache ) >= self
::CACHE_LIMIT
){
89 array_shift( self
::$smCache );
91 self
::$smCache[$prefix] = &$s;
96 * Clear all member variables in the current object. Does not clear
97 * the block from the DB.
101 $this->mLocal
= $this->mTrans
= 0;
102 $this->mPrefix
= null;
111 $db = wfGetDB( DB_SLAVE
);
116 * Load interwiki from the DB
118 * @param $prefix The interwiki prefix
119 * @return bool The prefix is valid
122 function load( $prefix ) {
124 $key = wfMemcKey( 'interwiki', $prefix );
125 $mc = $wgMemc->get( $key );
126 if( $mc && is_array( $mc ) ){ // is_array is hack for old keys
127 if( $this->loadFromArray( $mc ) ){
128 wfDebug("Succeeded\n");
132 $db =& $this->getDB();
134 $res = $db->resultObject( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
136 if ( $this->loadFromResult( $res ) ) {
137 $mc = array( 'url' => $this->mURL
, 'local' => $this->mLocal
, 'trans' => $this->mTrans
);
138 $wgMemc->add( $key, $mc );
149 * Fill in member variables from an array (e.g. memcached result)
151 * @return bool Whether everything was there
152 * @param $res ResultWrapper Row from the interwiki table
154 function loadFromArray( $mc ) {
155 if( isset( $mc['url'] ) && isset( $mc['local'] ) && isset( $mc['trans'] ) ){
156 $this->mURL
= $mc['url'];
157 $this->mLocal
= $mc['local'];
158 $this->mTrans
= $mc['trans'];
165 * Fill in member variables from a result wrapper
167 * @return bool Whether there was a row there
168 * @param $res ResultWrapper Row from the interwiki table
170 function loadFromResult( ResultWrapper
$res ) {
172 if ( 0 != $res->numRows() ) {
174 $row = $res->fetchObject();
175 $this->initFromRow( $row );
183 * Given a database row from the interwiki table, initialize
186 * @param $row ResultWrapper A row from the interwiki table
188 function initFromRow( $row ) {
189 $this->mPrefix
= $row->iw_prefix
;
190 $this->mURL
= $row->iw_url
;
191 $this->mLocal
= $row->iw_local
;
192 $this->mTrans
= $row->iw_trans
;
196 * Get the URL for a particular title (or with $1 if no title given)
198 * @param $title string What text to put for the article name
199 * @return string The URL
201 function getURL( $title = null ){
203 if( $title != null ){
204 $url = str_replace( "$1", $title, $url );
210 return $this->mLocal
;
213 function isTranscludable(){
214 return $this->mTrans
;