Cleanup for r42022/r42023 interwiki stuff
[mediawiki.git] / includes / Interwiki.php
blob5d27ff8a6c43e5da95187a31d386d8da0926892d
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 function __construct( $prefix = null, $url = '', $local = 0, $trans = 0 )
22 $this->mPrefix = $prefix;
23 $this->mURL = $url;
24 $this->mLocal = $local;
25 $this->mTrans = $trans;
28 /**
29 * Check whether an interwiki prefix exists
31 * @return bool Whether it exists
32 * @param $prefix string Interwiki prefix to use
34 static public function isValidInterwiki( $prefix ){
35 $result = self::fetch( $prefix );
36 return (bool)$result;
39 /**
40 * Fetch an Interwiki object
42 * @return Interwiki Object, or null if not valid
43 * @param $prefix string Interwiki prefix to use
45 static public function fetch( $prefix ) {
46 global $wgContLang;
47 if( $prefix == '' ) {
48 return null;
50 $prefix = $wgContLang->lc( $prefix );
51 if( isset( self::$smCache[$prefix] ) ){
52 return self::$smCache[$prefix];
54 global $wgInterwikiCache;
55 if ($wgInterwikiCache) {
56 return Interwiki::getInterwikiCached( $key );
58 $iw = Interwiki::load( $prefix );
59 if( !$iw ){
60 $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 $key \type{\string} Database key
76 * @return \type{\Interwiki} An interwiki object
78 protected static function getInterwikiCached( $key ) {
79 $value = getInterwikiCacheEntry( $key );
81 $s = new Interwiki( $key );
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 if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ){
91 reset( self::$smCache );
92 unset( self::$smCache[ key( self::$smCache ) ] );
94 self::$smCache[$prefix] = $s;
95 return $s;
98 /**
99 * Get entry from interwiki cache
101 * @note More logic is explained in DefaultSettings.
103 * @param $key \type{\string} Database key
104 * @return \type{\string) The entry
106 protected static function getInterwikiCacheEntry( $key ){
107 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
108 static $db, $site;
110 if( !$db ){
111 $db = dba_open( $wgInterwikiCache, 'r', 'cdb' );
113 /* Resolve site name */
114 if( $wgInterwikiScopes>=3 && !$site ) {
115 $site = dba_fetch( '__sites:' . wfWikiID(), $db );
116 if ( $site == "" ){
117 $site = $wgInterwikiFallbackSite;
121 $value = dba_fetch( wfMemcKey( $key ), $db );
122 // Site level
123 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
124 $value = dba_fetch( "_{$site}:{$key}", $db );
126 // Global Level
127 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
128 $value = dba_fetch( "__global:{$key}", $db );
130 if ( $value == 'undef' )
131 $value = '';
133 return $value;
137 * Load the interwiki, trying first memcached then the DB
139 * @param $prefix The interwiki prefix
140 * @return bool The prefix is valid
141 * @static
144 protected static function load( $prefix ) {
145 global $wgMemc;
146 $key = wfMemcKey( 'interwiki', $prefix );
147 $mc = $wgMemc->get( $key );
148 $iw = false;
149 if( $mc && is_array( $mc ) ){ // is_array is hack for old keys
150 $iw = Interwiki::loadFromArray( $mc );
151 if( $iw ){
152 return $iw;
156 $db = wfGetDB( DB_SLAVE );
158 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
159 __METHOD__ ) );
160 $iw = Interwiki::loadFromArray( $row );
161 if ( $iw ) {
162 $mc = array( 'iw_url' => $iw->mURL, 'iw_local' => $iw->mLocal, 'iw_trans' => $iw->mTrans );
163 $wgMemc->add( $key, $mc );
164 return $iw;
167 return false;
171 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
173 * @return bool Whether everything was there
174 * @param $res ResultWrapper Row from the interwiki table
175 * @static
177 protected static function loadFromArray( $mc ) {
178 if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ){
179 $iw = new Interwiki();
180 $iw->mURL = $mc['iw_url'];
181 $iw->mLocal = $mc['iw_local'];
182 $iw->mTrans = $mc['iw_trans'];
183 return $iw;
185 return false;
188 /**
189 * Get the URL for a particular title (or with $1 if no title given)
191 * @param $title string What text to put for the article name
192 * @return string The URL
194 function getURL( $title = null ){
195 $url = $this->mURL;
196 if( $title != null ){
197 $url = str_replace( "$1", $title, $url );
199 return $url;
202 function isLocal(){
203 return $this->mLocal;
206 function isTranscludable(){
207 return $this->mTrans;