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