123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- /**
- * @file
- * Interwiki table entry
- */
- /**
- * 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)
- */
- class Interwiki {
- // 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;
- function __construct( $prefix = null, $url = '', $local = 0, $trans = 0 )
- {
- $this->mPrefix = $prefix;
- $this->mURL = $url;
- $this->mLocal = $local;
- $this->mTrans = $trans;
- }
- /**
- * Check whether an interwiki prefix exists
- *
- * @return bool Whether it exists
- * @param $prefix string Interwiki prefix to use
- */
- static public function isValidInterwiki( $prefix ){
- $result = self::fetch( $prefix );
- return (bool)$result;
- }
- /**
- * Fetch an Interwiki object
- *
- * @return Interwiki Object, or null if not valid
- * @param $prefix string Interwiki prefix to use
- */
- static public function fetch( $prefix ) {
- global $wgContLang;
- if( $prefix == '' ) {
- return null;
- }
- $prefix = $wgContLang->lc( $prefix );
- if( isset( self::$smCache[$prefix] ) ){
- return self::$smCache[$prefix];
- }
- global $wgInterwikiCache;
- 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 ){
- reset( self::$smCache );
- unset( self::$smCache[ key( self::$smCache ) ] );
- }
- self::$smCache[$prefix] = $iw;
- return $iw;
- }
-
- /**
- * Fetch interwiki prefix data from local cache in constant database.
- *
- * @note More logic is explained in DefaultSettings.
- *
- * @param $prefix \type{\string} Interwiki prefix
- * @return \type{\Interwiki} An interwiki object
- */
- protected static function getInterwikiCached( $prefix ) {
- $value = self::getInterwikiCacheEntry( $prefix );
-
- $s = new Interwiki( $prefix );
- if ( $value != '' ) {
- // Split values
- list( $local, $url ) = explode( ' ', $value, 2 );
- $s->mURL = $url;
- $s->mLocal = (int)$local;
- }else{
- $s = false;
- }
- return $s;
- }
-
- /**
- * Get entry from interwiki cache
- *
- * @note More logic is explained in DefaultSettings.
- *
- * @param $prefix \type{\string} Database key
- * @return \type{\string) The entry
- */
- protected static function getInterwikiCacheEntry( $prefix ){
- global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
- static $db, $site;
- wfDebug( __METHOD__ . "( $prefix )\n" );
- if( !$db ){
- $db = dba_open( $wgInterwikiCache, 'r', 'cdb' );
- }
- /* Resolve site name */
- if( $wgInterwikiScopes>=3 && !$site ) {
- $site = dba_fetch( '__sites:' . wfWikiID(), $db );
- if ( $site == "" ){
- $site = $wgInterwikiFallbackSite;
- }
- }
-
- $value = dba_fetch( wfMemcKey( $prefix ), $db );
- // Site level
- if ( $value == '' && $wgInterwikiScopes >= 3 ) {
- $value = dba_fetch( "_{$site}:{$prefix}", $db );
- }
- // Global Level
- if ( $value == '' && $wgInterwikiScopes >= 2 ) {
- $value = dba_fetch( "__global:{$prefix}", $db );
- }
- if ( $value == 'undef' )
- $value = '';
-
- return $value;
- }
- /**
- * Load the interwiki, trying first memcached then the DB
- *
- * @param $prefix The interwiki prefix
- * @return bool The prefix is valid
- * @static
- *
- */
- protected static function load( $prefix ) {
- global $wgMemc, $wgInterwikiExpiry;
- global $wgLanguageNames;
- $key = wfMemcKey( 'interwiki', $prefix );
- $mc = $wgMemc->get( $key );
- $iw = false;
- if( $mc && is_array( $mc ) ){ // is_array is hack for old keys
- $iw = Interwiki::loadFromArray( $mc );
- if( $iw ){
- return $iw;
- }
- }
- # $$$ Sean: This is a hack to get the interwiki links working
- # but only accept a prefix that is a valid language
- if ( isset($prefix, $wgLanguageNames[$prefix]) && '' != $wgLanguageNames[$prefix] ) {
- $iw = new Interwiki();
- $iw->mURL = $prefix . '.wiki';
- $iw->mLocal = 0;
- $iw->mTrans = 0;
- } else {
- $iw = false;
- }
- /*
- $db = wfGetDB( DB_SLAVE );
- $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
- __METHOD__ ) );
- $iw = Interwiki::loadFromArray( $row );
- */
- # $$$ end of hack
- if ( $iw ) {
- $mc = array( 'iw_url' => $iw->mURL, '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, Database::fetchRow, etc)
- *
- * @return bool Whether everything was there
- * @param $res ResultWrapper Row from the interwiki table
- * @static
- */
- 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'];
- return $iw;
- }
- return false;
- }
-
- /**
- * 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
- */
- function getURL( $title = null ){
- $url = $this->mURL;
- if( $title != null ){
- $url = str_replace( "$1", $title, $url );
- }
- return $url;
- }
-
- function isLocal(){
- return $this->mLocal;
- }
-
- function isTranscludable(){
- return $this->mTrans;
- }
- }
|