00001 <?php
00012 class Interwiki {
00013
00014
00015 protected static $smCache = array();
00016 const CACHE_LIMIT = 100;
00017
00018 protected $mPrefix, $mURL, $mLocal, $mTrans;
00019
00020 public function __construct( $prefix = null, $url = '', $local = 0, $trans = 0 ) {
00021 $this->mPrefix = $prefix;
00022 $this->mURL = $url;
00023 $this->mLocal = $local;
00024 $this->mTrans = $trans;
00025 }
00026
00033 static public function isValidInterwiki( $prefix ) {
00034 $result = self::fetch( $prefix );
00035 return (bool)$result;
00036 }
00037
00044 static public function fetch( $prefix ) {
00045 global $wgContLang;
00046 if( $prefix == '' ) {
00047 return null;
00048 }
00049 $prefix = $wgContLang->lc( $prefix );
00050 if( isset( self::$smCache[$prefix] ) ) {
00051 return self::$smCache[$prefix];
00052 }
00053 global $wgInterwikiCache;
00054 if( $wgInterwikiCache ) {
00055 $iw = Interwiki::getInterwikiCached( $prefix );
00056 } else {
00057 $iw = Interwiki::load( $prefix );
00058 if( !$iw ) {
00059 $iw = false;
00060 }
00061 }
00062 if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
00063 reset( self::$smCache );
00064 unset( self::$smCache[ key( self::$smCache ) ] );
00065 }
00066 self::$smCache[$prefix] = $iw;
00067 return $iw;
00068 }
00069
00078 protected static function getInterwikiCached( $prefix ) {
00079 $value = self::getInterwikiCacheEntry( $prefix );
00080
00081 $s = new Interwiki( $prefix );
00082 if ( $value != '' ) {
00083
00084 list( $local, $url ) = explode( ' ', $value, 2 );
00085 $s->mURL = $url;
00086 $s->mLocal = (int)$local;
00087 } else {
00088 $s = false;
00089 }
00090 return $s;
00091 }
00092
00101 protected static function getInterwikiCacheEntry( $prefix ) {
00102 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
00103 static $db, $site;
00104
00105 wfDebug( __METHOD__ . "( $prefix )\n" );
00106 if( !$db ) {
00107 $db = CdbReader::open( $wgInterwikiCache );
00108 }
00109
00110 if( $wgInterwikiScopes>=3 && !$site ) {
00111 $site = $db->get( '__sites:' . wfWikiID() );
00112 if ( $site == '' ) {
00113 $site = $wgInterwikiFallbackSite;
00114 }
00115 }
00116
00117 $value = $db->get( wfMemcKey( $prefix ) );
00118
00119 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
00120 $value = $db->get( "_{$site}:{$prefix}" );
00121 }
00122
00123 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
00124 $value = $db->get( "__global:{$prefix}" );
00125 }
00126 if ( $value == 'undef' )
00127 $value = '';
00128
00129 return $value;
00130 }
00131
00138 protected static function load( $prefix ) {
00139 global $wgMemc, $wgInterwikiExpiry;
00140 $key = wfMemcKey( 'interwiki', $prefix );
00141 $mc = $wgMemc->get( $key );
00142 $iw = false;
00143 if( $mc && is_array( $mc ) ) {
00144 $iw = Interwiki::loadFromArray( $mc );
00145 if( $iw ) {
00146 return $iw;
00147 }
00148 }
00149
00150 $db = wfGetDB( DB_SLAVE );
00151
00152 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
00153 __METHOD__ ) );
00154 $iw = Interwiki::loadFromArray( $row );
00155 if ( $iw ) {
00156 $mc = array( 'iw_url' => $iw->mURL, 'iw_local' => $iw->mLocal, 'iw_trans' => $iw->mTrans );
00157 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
00158 return $iw;
00159 }
00160
00161 return false;
00162 }
00163
00170 protected static function loadFromArray( $mc ) {
00171 if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) {
00172 $iw = new Interwiki();
00173 $iw->mURL = $mc['iw_url'];
00174 $iw->mLocal = $mc['iw_local'];
00175 $iw->mTrans = $mc['iw_trans'];
00176 return $iw;
00177 }
00178 return false;
00179 }
00180
00187 public function getURL( $title = null ) {
00188 $url = $this->mURL;
00189 if( $title != null ) {
00190 $url = str_replace( "$1", $title, $url );
00191 }
00192 return $url;
00193 }
00194
00201 public function isLocal() {
00202 return $this->mLocal;
00203 }
00204
00211 public function isTranscludable() {
00212 return $this->mTrans;
00213 }
00214
00220 public function getName() {
00221 $key = 'interwiki-name-' . $this->mPrefix;
00222 $msg = wfMsgForContent( $key );
00223 return wfEmptyMsg( $key, $msg ) ? '' : $msg;
00224 }
00225
00231 public function getDescription() {
00232 $key = 'interwiki-desc-' . $this->mPrefix;
00233 $msg = wfMsgForContent( $key );
00234 return wfEmptyMsg( $key, $msg ) ? '' : $msg;
00235 }
00236 }