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