* Removed leftover token check of unhide=1 that was removed elsewhere
[mediawiki.git] / includes / Interwiki.php
blob3c71f6eeceb6d5edd0281c365cbdde9f3548ad9b
1 <?php
2 /**
3 * @file
4 * Interwiki table entry
5 */
7 /**
8 * The interwiki class
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)
12 class Interwiki {
14 // Cache - removes oldest entry 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 public function __construct( $prefix = null, $url = '', $local = 0, $trans = 0 ) {
21 $this->mPrefix = $prefix;
22 $this->mURL = $url;
23 $this->mLocal = $local;
24 $this->mTrans = $trans;
27 /**
28 * Check whether an interwiki prefix exists
30 * @param $prefix String: interwiki prefix to use
31 * @return Boolean: whether it exists
33 static public function isValidInterwiki( $prefix ) {
34 $result = self::fetch( $prefix );
35 return (bool)$result;
38 /**
39 * Fetch an Interwiki object
41 * @param $prefix String: interwiki prefix to use
42 * @return Interwiki Object, or null if not valid
44 static public function fetch( $prefix ) {
45 global $wgContLang;
46 if( $prefix == '' ) {
47 return null;
49 $prefix = $wgContLang->lc( $prefix );
50 if( isset( self::$smCache[$prefix] ) ) {
51 return self::$smCache[$prefix];
53 global $wgInterwikiCache;
54 if( $wgInterwikiCache ) {
55 $iw = Interwiki::getInterwikiCached( $prefix );
56 } else {
57 $iw = Interwiki::load( $prefix );
58 if( !$iw ) {
59 $iw = false;
62 if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
63 reset( self::$smCache );
64 unset( self::$smCache[ key( self::$smCache ) ] );
66 self::$smCache[$prefix] = $iw;
67 return $iw;
70 /**
71 * Fetch interwiki prefix data from local cache in constant database.
73 * @note More logic is explained in DefaultSettings.
75 * @param $prefix String: interwiki prefix
76 * @return Interwiki object
78 protected static function getInterwikiCached( $prefix ) {
79 $value = self::getInterwikiCacheEntry( $prefix );
81 $s = new Interwiki( $prefix );
82 if ( $value != '' ) {
83 // Split values
84 list( $local, $url ) = explode( ' ', $value, 2 );
85 $s->mURL = $url;
86 $s->mLocal = (int)$local;
87 } else {
88 $s = false;
90 return $s;
93 /**
94 * Get entry from interwiki cache
96 * @note More logic is explained in DefaultSettings.
98 * @param $prefix String: database key
99 * @return String: the entry
101 protected static function getInterwikiCacheEntry( $prefix ) {
102 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
103 static $db, $site;
105 wfDebug( __METHOD__ . "( $prefix )\n" );
106 if( !$db ) {
107 $db = CdbReader::open( $wgInterwikiCache );
109 /* Resolve site name */
110 if( $wgInterwikiScopes>=3 && !$site ) {
111 $site = $db->get( '__sites:' . wfWikiID() );
112 if ( $site == '' ) {
113 $site = $wgInterwikiFallbackSite;
117 $value = $db->get( wfMemcKey( $prefix ) );
118 // Site level
119 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
120 $value = $db->get( "_{$site}:{$prefix}" );
122 // Global Level
123 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
124 $value = $db->get( "__global:{$prefix}" );
126 if ( $value == 'undef' )
127 $value = '';
129 return $value;
133 * Load the interwiki, trying first memcached then the DB
135 * @param $prefix The interwiki prefix
136 * @return Boolean: the prefix is valid
138 protected static function load( $prefix ) {
139 global $wgMemc, $wgInterwikiExpiry;
140 $key = wfMemcKey( 'interwiki', $prefix );
141 $mc = $wgMemc->get( $key );
142 $iw = false;
143 if( $mc && is_array( $mc ) ) { // is_array is hack for old keys
144 $iw = Interwiki::loadFromArray( $mc );
145 if( $iw ) {
146 return $iw;
150 $db = wfGetDB( DB_SLAVE );
152 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
153 __METHOD__ ) );
154 $iw = Interwiki::loadFromArray( $row );
155 if ( $iw ) {
156 $mc = array( 'iw_url' => $iw->mURL, 'iw_local' => $iw->mLocal, 'iw_trans' => $iw->mTrans );
157 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
158 return $iw;
161 return false;
165 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
167 * @param $mc Associative array: row from the interwiki table
168 * @return Boolean: whether everything was there
170 protected static function loadFromArray( $mc ) {
171 if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) {
172 $iw = new Interwiki();
173 $iw->mURL = $mc['iw_url'];
174 $iw->mLocal = $mc['iw_local'];
175 $iw->mTrans = $mc['iw_trans'];
176 return $iw;
178 return false;
182 * Get the URL for a particular title (or with $1 if no title given)
184 * @param $title String: what text to put for the article name
185 * @return String: the URL
187 public function getURL( $title = null ) {
188 $url = $this->mURL;
189 if( $title != null ) {
190 $url = str_replace( "$1", $title, $url );
192 return $url;
196 * Is this a local link from a sister project, or is
197 * it something outside, like Google
199 * @return Boolean
201 public function isLocal() {
202 return $this->mLocal;
206 * Can pages from this wiki be transcluded?
207 * Still requires $wgEnableScaryTransclusion
209 * @return Boolean
211 public function isTranscludable() {
212 return $this->mTrans;
216 * Get the name for the interwiki site
218 * @return String
220 public function getName() {
221 $key = 'interwiki-name-' . $this->mPrefix;
222 $msg = wfMsgForContent( $key );
223 return wfEmptyMsg( $key, $msg ) ? '' : $msg;
227 * Get a description for this interwiki
229 * @return String
231 public function getDescription() {
232 $key = 'interwiki-desc-' . $this->mPrefix;
233 $msg = wfMsgForContent( $key );
234 return wfEmptyMsg( $key, $msg ) ? '' : $msg;