Solving an issue in r69542
[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, $mAPI, $mWikiID, $mLocal, $mTrans;
19
20 public function __construct( $prefix = null, $url = '', $api = '', $wikiid = '', $local = 0, $trans = 0 ) {
21 $this->mPrefix = $prefix;
22 $this->mURL = $url;
23 $this->mAPI = $api;
24 $this->mWikiID = $wikiid;
25 $this->mLocal = $local;
26 $this->mTrans = $trans;
27 }
28
29 /**
30 * Check whether an interwiki prefix exists
31 *
32 * @param $prefix String: interwiki prefix to use
33 * @return Boolean: whether it exists
34 */
35 static public function isValidInterwiki( $prefix ) {
36 $result = self::fetch( $prefix );
37 return (bool)$result;
38 }
39
40 /**
41 * Fetch an Interwiki object
42 *
43 * @param $prefix String: interwiki prefix to use
44 * @return Interwiki Object, or null if not valid
45 */
46 static public function fetch( $prefix ) {
47 global $wgContLang;
48 if( $prefix == '' ) {
49 return null;
50 }
51 $prefix = $wgContLang->lc( $prefix );
52 if( isset( self::$smCache[$prefix] ) ) {
53 return self::$smCache[$prefix];
54 }
55 global $wgInterwikiCache;
56 if( $wgInterwikiCache ) {
57 $iw = Interwiki::getInterwikiCached( $prefix );
58 } else {
59 $iw = Interwiki::load( $prefix );
60 if( !$iw ) {
61 $iw = false;
62 }
63 }
64 if( self::CACHE_LIMIT && count( self::$smCache ) >= self::CACHE_LIMIT ) {
65 reset( self::$smCache );
66 unset( self::$smCache[ key( self::$smCache ) ] );
67 }
68 self::$smCache[$prefix] = $iw;
69 return $iw;
70 }
71
72 /**
73 * Fetch interwiki prefix data from local cache in constant database.
74 *
75 * @note More logic is explained in DefaultSettings.
76 *
77 * @param $prefix String: interwiki prefix
78 * @return Interwiki object
79 */
80 protected static function getInterwikiCached( $prefix ) {
81 $value = self::getInterwikiCacheEntry( $prefix );
82
83 $s = new Interwiki( $prefix );
84 if ( $value != '' ) {
85 // Split values
86 list( $local, $url ) = explode( ' ', $value, 2 );
87 $s->mURL = $url;
88 $s->mLocal = (int)$local;
89 } else {
90 $s = false;
91 }
92 return $s;
93 }
94
95 /**
96 * Get entry from interwiki cache
97 *
98 * @note More logic is explained in DefaultSettings.
99 *
100 * @param $prefix String: database key
101 * @return String: the entry
102 */
103 protected static function getInterwikiCacheEntry( $prefix ) {
104 global $wgInterwikiCache, $wgInterwikiScopes, $wgInterwikiFallbackSite;
105 static $db, $site;
106
107 wfDebug( __METHOD__ . "( $prefix )\n" );
108 if( !$db ) {
109 $db = CdbReader::open( $wgInterwikiCache );
110 }
111 /* Resolve site name */
112 if( $wgInterwikiScopes>=3 && !$site ) {
113 $site = $db->get( '__sites:' . wfWikiID() );
114 if ( $site == '' ) {
115 $site = $wgInterwikiFallbackSite;
116 }
117 }
118
119 $value = $db->get( wfMemcKey( $prefix ) );
120 // Site level
121 if ( $value == '' && $wgInterwikiScopes >= 3 ) {
122 $value = $db->get( "_{$site}:{$prefix}" );
123 }
124 // Global Level
125 if ( $value == '' && $wgInterwikiScopes >= 2 ) {
126 $value = $db->get( "__global:{$prefix}" );
127 }
128 if ( $value == 'undef' )
129 $value = '';
130
131 return $value;
132 }
133
134 /**
135 * Load the interwiki, trying first memcached then the DB
136 *
137 * @param $prefix The interwiki prefix
138 * @return Boolean: the prefix is valid
139 */
140 protected static function load( $prefix ) {
141 global $wgMemc, $wgInterwikiExpiry;
142 $key = wfMemcKey( 'interwiki', $prefix );
143 $mc = $wgMemc->get( $key );
144 $iw = false;
145 if( $mc && is_array( $mc ) ) { // is_array is hack for old keys
146 $iw = Interwiki::loadFromArray( $mc );
147 if( $iw ) {
148 return $iw;
149 }
150 }
151
152 $db = wfGetDB( DB_SLAVE );
153
154 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
155 __METHOD__ ) );
156 $iw = Interwiki::loadFromArray( $row );
157 if ( $iw ) {
158 $mc = array( 'iw_url' => $iw->mURL, 'iw_api' => $iw->mAPI, 'iw_local' => $iw->mLocal, 'iw_trans' => $iw->mTrans );
159 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
160 return $iw;
161 }
162
163 return false;
164 }
165
166 /**
167 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
168 *
169 * @param $mc Associative array: row from the interwiki table
170 * @return Boolean: whether everything was there
171 */
172 protected static function loadFromArray( $mc ) {
173 if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) {
174 $iw = new Interwiki();
175 $iw->mURL = $mc['iw_url'];
176 $iw->mLocal = $mc['iw_local'];
177 $iw->mTrans = $mc['iw_trans'];
178 $iw->mAPI = isset( $mc['iw_api'] ) ? $mc['iw_api'] : '';
179 $iw->mWikiID = isset( $mc['iw_wikiid'] ) ? $mc['iw_wikiid'] : '';
180
181 return $iw;
182 }
183 return false;
184 }
185
186 /**
187 * Get the URL for a particular title (or with $1 if no title given)
188 *
189 * @param $title String: what text to put for the article name
190 * @return String: the URL
191 */
192 public function getURL( $title = null ) {
193 $url = $this->mURL;
194 if( $title != null ) {
195 $url = str_replace( "$1", $title, $url );
196 }
197 return $url;
198 }
199
200 /**
201 * Get the API URL for this wiki
202 *
203 * @return String: the URL
204 */
205 public function getAPI( ) {
206 return $this->mAPI;
207 }
208
209 /**
210 * Get the DB name for this wiki
211 *
212 * @return String: the DB name
213 */
214 public function getWikiID( ) {
215 return $this->mWikiID;
216 }
217
218 /**
219 * Is this a local link from a sister project, or is
220 * it something outside, like Google
221 *
222 * @return Boolean
223 */
224 public function isLocal() {
225 return $this->mLocal;
226 }
227
228 /**
229 * Can pages from this wiki be transcluded?
230 * Still requires $wgEnableScaryTransclusion
231 *
232 * @return Boolean
233 */
234 public function isTranscludable() {
235 return $this->mTrans;
236 }
237
238 /**
239 * Get the name for the interwiki site
240 *
241 * @return String
242 */
243 public function getName() {
244 $key = 'interwiki-name-' . $this->mPrefix;
245 $msg = wfMsgForContent( $key );
246 return wfEmptyMsg( $key, $msg ) ? '' : $msg;
247 }
248
249 /**
250 * Get a description for this interwiki
251 *
252 * @return String
253 */
254 public function getDescription() {
255 $key = 'interwiki-desc-' . $this->mPrefix;
256 $msg = wfMsgForContent( $key );
257 return wfEmptyMsg( $key, $msg ) ? '' : $msg;
258 }
259 }