Slight fix for r47781: remove useless if($index) conditional: $index is always set...
[lhc/web/wiklou.git] / includes / Interwiki.php
1 <?php
2 /**
3 * @file
4 * Interwiki table entry
5 */
6
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)
11 */
12 class Interwiki {
13
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.
17
18 protected $mPrefix, $mURL, $mLocal, $mTrans;
19
20 function __construct( $prefix = null, $url = '', $local = 0, $trans = 0 )
21 {
22 $this->mPrefix = $prefix;
23 $this->mURL = $url;
24 $this->mLocal = $local;
25 $this->mTrans = $trans;
26 }
27
28 /**
29 * Check whether an interwiki prefix exists
30 *
31 * @return bool Whether it exists
32 * @param $prefix string Interwiki prefix to use
33 */
34 static public function isValidInterwiki( $prefix ){
35 $result = self::fetch( $prefix );
36 return (bool)$result;
37 }
38
39 /**
40 * Fetch an Interwiki object
41 *
42 * @return Interwiki Object, or null if not valid
43 * @param $prefix string Interwiki prefix to use
44 */
45 static public function fetch( $prefix ) {
46 global $wgContLang;
47 if( $prefix == '' ) {
48 return null;
49 }
50 $prefix = $wgContLang->lc( $prefix );
51 if( isset( self::$smCache[$prefix] ) ){
52 return self::$smCache[$prefix];
53 }
54 global $wgInterwikiCache;
55 if ($wgInterwikiCache) {
56 $iw = Interwiki::getInterwikiCached( $prefix );
57 } else {
58 $iw = Interwiki::load( $prefix );
59 if( !$iw ){
60 $iw = false;
61 }
62 }
63 if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ){
64 reset( self::$smCache );
65 unset( self::$smCache[ key( self::$smCache ) ] );
66 }
67 self::$smCache[$prefix] = $iw;
68 return $iw;
69 }
70
71 /**
72 * Fetch interwiki prefix data from local cache in constant database.
73 *
74 * @note More logic is explained in DefaultSettings.
75 *
76 * @param $prefix \type{\string} Interwiki prefix
77 * @return \type{\Interwiki} An interwiki object
78 */
79 protected static function getInterwikiCached( $prefix ) {
80 $value = self::getInterwikiCacheEntry( $prefix );
81
82 $s = new Interwiki( $prefix );
83 if ( $value != '' ) {
84 // Split values
85 list( $local, $url ) = explode( ' ', $value, 2 );
86 $s->mURL = $url;
87 $s->mLocal = (int)$local;
88 }else{
89 $s = false;
90 }
91 return $s;
92 }
93
94 /**
95 * Get entry from interwiki cache
96 *
97 * @note More logic is explained in DefaultSettings.
98 *
99 * @param $prefix \type{\string} Database key
100 * @return \type{\string) The entry
101 */
102 protected static function getInterwikiCacheEntry( $prefix ){
103 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
104 static $db, $site;
105
106 wfDebug( __METHOD__ . "( $prefix )\n" );
107 if( !$db ){
108 $db = dba_open( $wgInterwikiCache, 'r', 'cdb' );
109 }
110 /* Resolve site name */
111 if( $wgInterwikiScopes>=3 && !$site ) {
112 $site = dba_fetch( '__sites:' . wfWikiID(), $db );
113 if ( $site == "" ){
114 $site = $wgInterwikiFallbackSite;
115 }
116 }
117
118 $value = dba_fetch( wfMemcKey( $prefix ), $db );
119 // Site level
120 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
121 $value = dba_fetch( "_{$site}:{$prefix}", $db );
122 }
123 // Global Level
124 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
125 $value = dba_fetch( "__global:{$prefix}", $db );
126 }
127 if ( $value == 'undef' )
128 $value = '';
129
130 return $value;
131 }
132
133 /**
134 * Load the interwiki, trying first memcached then the DB
135 *
136 * @param $prefix The interwiki prefix
137 * @return bool The prefix is valid
138 * @static
139 *
140 */
141 protected static function load( $prefix ) {
142 global $wgMemc, $wgInterwikiExpiry;
143 $key = wfMemcKey( 'interwiki', $prefix );
144 $mc = $wgMemc->get( $key );
145 $iw = false;
146 if( $mc && is_array( $mc ) ){ // is_array is hack for old keys
147 $iw = Interwiki::loadFromArray( $mc );
148 if( $iw ){
149 return $iw;
150 }
151 }
152
153 $db = wfGetDB( DB_SLAVE );
154
155 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
156 __METHOD__ ) );
157 $iw = Interwiki::loadFromArray( $row );
158 if ( $iw ) {
159 $mc = array( 'iw_url' => $iw->mURL, 'iw_local' => $iw->mLocal, 'iw_trans' => $iw->mTrans );
160 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
161 return $iw;
162 }
163
164 return false;
165 }
166
167 /**
168 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
169 *
170 * @return bool Whether everything was there
171 * @param $res ResultWrapper Row from the interwiki table
172 * @static
173 */
174 protected static function loadFromArray( $mc ) {
175 if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ){
176 $iw = new Interwiki();
177 $iw->mURL = $mc['iw_url'];
178 $iw->mLocal = $mc['iw_local'];
179 $iw->mTrans = $mc['iw_trans'];
180 return $iw;
181 }
182 return false;
183 }
184
185 /**
186 * Get the URL for a particular title (or with $1 if no title given)
187 *
188 * @param $title string What text to put for the article name
189 * @return string The URL
190 */
191 function getURL( $title = null ){
192 $url = $this->mURL;
193 if( $title != null ){
194 $url = str_replace( "$1", $title, $url );
195 }
196 return $url;
197 }
198
199 function isLocal(){
200 return $this->mLocal;
201 }
202
203 function isTranscludable(){
204 return $this->mTrans;
205 }
206
207 }