/**
* The interwiki class
* All information is loaded on creation when called by Interwiki::fetch( $prefix ).
- * All work is done on slave, because this should *never* change (except during schema updates etc, which arent wiki-related)
+ * All work is done on slave, because this should *never* change (except during
+ * schema updates etc, which aren't wiki-related)
*/
class Interwiki {
- // Cache - removed in LRU order when it hits limit
+ // Cache - removes oldest entry when it hits limit
protected static $smCache = array();
const CACHE_LIMIT = 100; // 0 means unlimited, any other value is max number of entries.
- protected $mPrefix, $mURL, $mLocal, $mTrans;
+ protected $mPrefix, $mURL, $mAPI, $mWikiID, $mLocal, $mTrans;
- function __construct( $prefix = null, $url = '', $local = 0, $trans = 0 )
- {
+ public function __construct( $prefix = null, $url = '', $api = '', $wikiId = '', $local = 0, $trans = 0 ) {
$this->mPrefix = $prefix;
$this->mURL = $url;
+ $this->mAPI = $api;
+ $this->mWikiID = $wikiId;
$this->mLocal = $local;
$this->mTrans = $trans;
}
+ /**
+ * Check whether an interwiki prefix exists
+ *
+ * @param $prefix String: interwiki prefix to use
+ * @return Boolean: whether it exists
+ */
+ static public function isValidInterwiki( $prefix ) {
+ $result = self::fetch( $prefix );
+ return (bool)$result;
+ }
+
/**
* Fetch an Interwiki object
- *
+ *
+ * @param $prefix String: interwiki prefix to use
* @return Interwiki Object, or null if not valid
- * @param $prefix string Interwiki prefix to use
*/
static public function fetch( $prefix ) {
- if( isset( self::$smCache[$prefix] ) ){
+ global $wgContLang;
+ if( $prefix == '' ) {
+ return null;
+ }
+ $prefix = $wgContLang->lc( $prefix );
+ if( isset( self::$smCache[$prefix] ) ) {
return self::$smCache[$prefix];
}
global $wgInterwikiCache;
- if ($wgInterwikiCache) {
- return Interwiki::getInterwikiCached( $key );
- }
- $iw = new Interwiki;
- if(! $iw->load( $prefix ) ){
- return false;
+ if( $wgInterwikiCache ) {
+ $iw = Interwiki::getInterwikiCached( $prefix );
+ } else {
+ $iw = Interwiki::load( $prefix );
+ if( !$iw ) {
+ $iw = false;
+ }
}
- if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ){
- array_shift( self::$smCache );
+ if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
+ reset( self::$smCache );
+ unset( self::$smCache[key( self::$smCache )] );
}
- self::$smCache[$prefix] = &$iw;
+ self::$smCache[$prefix] = $iw;
return $iw;
}
-
+
/**
* Fetch interwiki prefix data from local cache in constant database.
*
* @note More logic is explained in DefaultSettings.
*
- * @param $key \type{\string} Database key
- * @return \type{\string} URL of interwiki site
+ * @param $prefix String: interwiki prefix
+ * @return Interwiki object
*/
- protected static function getInterwikiCached( $key ) {
- global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
- static $db, $site;
+ protected static function getInterwikiCached( $prefix ) {
+ $value = self::getInterwikiCacheEntry( $prefix );
- if (!$db)
- $db=dba_open($wgInterwikiCache,'r','cdb');
- /* Resolve site name */
- if ($wgInterwikiScopes>=3 and !$site) {
- $site = dba_fetch('__sites:' . wfWikiID(), $db);
- if ($site=="")
- $site = $wgInterwikiFallbackSite;
- }
- $value = dba_fetch( wfMemcKey( $key ), $db);
- if ($value=='' and $wgInterwikiScopes>=3) {
- /* try site-level */
- $value = dba_fetch("_{$site}:{$key}", $db);
- }
- if ($value=='' and $wgInterwikiScopes>=2) {
- /* try globals */
- $value = dba_fetch("__global:{$key}", $db);
- }
- if ($value=='undef')
- $value='';
- $s = new Interwiki( $key );
+ $s = new Interwiki( $prefix );
if ( $value != '' ) {
+ // Split values
list( $local, $url ) = explode( ' ', $value, 2 );
$s->mURL = $url;
$s->mLocal = (int)$local;
- }else{
- return false;
- }
- if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ){
- array_shift( self::$smCache );
+ } else {
+ $s = false;
}
- self::$smCache[$prefix] = &$s;
return $s;
}
/**
- * Get the DB object
+ * Get entry from interwiki cache
+ *
+ * @note More logic is explained in DefaultSettings.
*
- * @return Database
+ * @param $prefix String: database key
+ * @return String: the entry
*/
- function &getDB(){
- $db = wfGetDB( DB_SLAVE );
- return $db;
+ protected static function getInterwikiCacheEntry( $prefix ) {
+ global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
+ static $db, $site;
+
+ wfDebug( __METHOD__ . "( $prefix )\n" );
+ if( !$db ) {
+ $db = CdbReader::open( $wgInterwikiCache );
+ }
+ /* Resolve site name */
+ if( $wgInterwikiScopes >= 3 && !$site ) {
+ $site = $db->get( '__sites:' . wfWikiID() );
+ if ( $site == '' ) {
+ $site = $wgInterwikiFallbackSite;
+ }
+ }
+
+ $value = $db->get( wfMemcKey( $prefix ) );
+ // Site level
+ if ( $value == '' && $wgInterwikiScopes >= 3 ) {
+ $value = $db->get( "_{$site}:{$prefix}" );
+ }
+ // Global Level
+ if ( $value == '' && $wgInterwikiScopes >= 2 ) {
+ $value = $db->get( "__global:{$prefix}" );
+ }
+ if ( $value == 'undef' ) {
+ $value = '';
+ }
+
+ return $value;
}
/**
- * Load interwiki from the DB
+ * Load the interwiki, trying first memcached then the DB
*
* @param $prefix The interwiki prefix
- * @return bool The prefix is valid
- *
+ * @return Boolean: the prefix is valid
*/
- function load( $prefix ) {
- global $wgMemc;
- $key = wfMemcKey( 'interwiki', $prefix );
- $mc = $wgMemc->get( $key );
- if( $mc && is_array( $mc ) && $this->loadFromArray( $mc ) ){ // is_array is hack for old keys
- wfDebug("Succeeded\n");
- return true;
- }
-
- $db =& $this->getDB();
- $res = $db->resultObject( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
+ protected static function load( $prefix ) {
+ global $wgMemc, $wgInterwikiExpiry;
+
+ $iwData = false;
+ if ( !wfRunHooks( 'InterwikiLoadPrefix', array( $prefix, &$iwData ) ) ) {
+ return Interwiki::loadFromArray( $iwData );
+ }
+
+ if ( !$iwData ) {
+ $key = wfMemcKey( 'interwiki', $prefix );
+ $iwData = $wgMemc->get( $key );
+ }
+
+ if( $iwData && is_array( $iwData ) ) { // is_array is hack for old keys
+ $iw = Interwiki::loadFromArray( $iwData );
+ if( $iw ) {
+ return $iw;
+ }
+ }
+
+ $db = wfGetDB( DB_SLAVE );
+
+ $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
__METHOD__ ) );
- if ( $this->loadFromResult( $res ) ) {
- $mc = array( 'url' => $this->mURL, 'local' => $this->mLocal, 'trans' => $this->mTrans );
- $wgMemc->add( $key, $mc );
- return true;
+ $iw = Interwiki::loadFromArray( $row );
+ if ( $iw ) {
+ $mc = array(
+ 'iw_url' => $iw->mURL,
+ 'iw_api' => $iw->mAPI,
+ 'iw_local' => $iw->mLocal,
+ 'iw_trans' => $iw->mTrans
+ );
+ $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
+ return $iw;
}
+
return false;
}
/**
- * Fill in member variables from an array (e.g. memcached result)
+ * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
*
- * @return bool Whether everything was there
- * @param $res ResultWrapper Row from the interwiki table
+ * @param $mc Associative array: row from the interwiki table
+ * @return Boolean: whether everything was there
*/
- function loadFromArray( $mc ) {
- if( isset( $mc['url'] ) && isset( $mc['local'] ) && isset( $mc['trans'] ) ){
- $this->mURL = $mc['url'];
- $this->mLocal = $mc['local'];
- $this->mTrans = $mc['trans'];
- return true;
+ protected static function loadFromArray( $mc ) {
+ if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) {
+ $iw = new Interwiki();
+ $iw->mURL = $mc['iw_url'];
+ $iw->mLocal = $mc['iw_local'];
+ $iw->mTrans = $mc['iw_trans'];
+ $iw->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : '';
+ $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : '';
+
+ return $iw;
}
return false;
}
-
+
/**
- * Fill in member variables from a result wrapper
+ * Get the URL for a particular title (or with $1 if no title given)
*
- * @return bool Whether there was a row there
- * @param $res ResultWrapper Row from the interwiki table
+ * @param $title String: what text to put for the article name
+ * @return String: the URL
*/
- function loadFromResult( ResultWrapper $res ) {
- $ret = false;
- if ( 0 != $res->numRows() ) {
- # Get first entry
- $row = $res->fetchObject();
- $this->initFromRow( $row );
- $ret = true;
- }
- $res->free();
- return $ret;
+ public function getURL( $title = null ) {
+ $url = $this->mURL;
+ if( $title != null ) {
+ $url = str_replace( "$1", $title, $url );
+ }
+ return $url;
}
/**
- * Given a database row from the interwiki table, initialize
- * member variables
+ * Get the API URL for this wiki
*
- * @param $row ResultWrapper A row from the interwiki table
+ * @return String: the URL
*/
- function initFromRow( $row ) {
- $this->mPrefix = $row->iw_prefix;
- $this->mURL = $row->iw_url;
- $this->mLocal = $row->iw_local;
- $this->mTrans = $row->iw_trans;
+ public function getAPI() {
+ return $this->mAPI;
}
-
- /**
- * Get the URL for a particular title (or with $1 if no title given)
- *
- * @param $title string What text to put for the article name
- * @return string The URL
+
+ /**
+ * Get the DB name for this wiki
+ *
+ * @return String: the DB name
*/
- function getURL( $title = null ){
- $url = $this->mURL;
- if( $title != null ){
- $url = str_replace( "$1", $title, $url );
- }
- return $url;
+ public function getWikiID() {
+ return $this->mWikiID;
}
-
- function isLocal(){
+
+ /**
+ * Is this a local link from a sister project, or is
+ * it something outside, like Google
+ *
+ * @return Boolean
+ */
+ public function isLocal() {
return $this->mLocal;
}
-
- function isTranscludable(){
+
+ /**
+ * Can pages from this wiki be transcluded?
+ * Still requires $wgEnableScaryTransclusion
+ *
+ * @return Boolean
+ */
+ public function isTranscludable() {
return $this->mTrans;
}
+ /**
+ * Get the name for the interwiki site
+ *
+ * @return String
+ */
+ public function getName() {
+ $msg = wfMessage( 'interwiki-name-' . $this->mPrefix )->inContentLanguage();
+ return !$msg->exists() ? '' : $msg;
+ }
+
+ /**
+ * Get a description for this interwiki
+ *
+ * @return String
+ */
+ public function getDescription() {
+ $msg = wfMessage( 'interwiki-desc-' . $this->mPrefix )->inContentLanguage();
+ return !$msg->exists() ? '' : $msg;
+ }
}