API: fix copyright symbol, coding style cleanup, more braces
[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 * @param $prefix String: interwiki prefix to use
31 * @return Boolean: whether it exists
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 * @param $prefix String: interwiki prefix to use
42 * @return Interwiki Object, or null if not valid
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 String: interwiki prefix
76 * @return 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 String: database key
99 * @return 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 Boolean: the prefix is valid
137 */
138 protected static function load( $prefix ) {
139 global $wgMemc, $wgInterwikiExpiry;
140 $key = wfMemcKey( 'interwiki', $prefix );
141 $mc = $wgMemc->get( $key );
142 $iw = false;
143 if( $mc && is_array( $mc ) ) { // is_array is hack for old keys
144 $iw = Interwiki::loadFromArray( $mc );
145 if( $iw ) {
146 return $iw;
147 }
148 }
149
150 $db = wfGetDB( DB_SLAVE );
151
152 $row = $db->fetchRow( $db->select( 'interwiki', '*', array( 'iw_prefix' => $prefix ),
153 __METHOD__ ) );
154 $iw = Interwiki::loadFromArray( $row );
155 if ( $iw ) {
156 $mc = array( 'iw_url' => $iw->mURL, 'iw_local' => $iw->mLocal, 'iw_trans' => $iw->mTrans );
157 $wgMemc->add( $key, $mc, $wgInterwikiExpiry );
158 return $iw;
159 }
160
161 return false;
162 }
163
164 /**
165 * Fill in member variables from an array (e.g. memcached result, Database::fetchRow, etc)
166 *
167 * @param $mc Associative array: row from the interwiki table
168 * @return Boolean: whether everything was there
169 */
170 protected static function loadFromArray( $mc ) {
171 if( isset( $mc['iw_url'] ) && isset( $mc['iw_local'] ) && isset( $mc['iw_trans'] ) ) {
172 $iw = new Interwiki();
173 $iw->mURL = $mc['iw_url'];
174 $iw->mLocal = $mc['iw_local'];
175 $iw->mTrans = $mc['iw_trans'];
176 return $iw;
177 }
178 return false;
179 }
180
181 /**
182 * Get the URL for a particular title (or with $1 if no title given)
183 *
184 * @param $title String: what text to put for the article name
185 * @return String: the URL
186 */
187 public function getURL( $title = null ) {
188 $url = $this->mURL;
189 if( $title != null ) {
190 $url = str_replace( "$1", $title, $url );
191 }
192 return $url;
193 }
194
195 /**
196 * Is this a local link from a sister project, or is
197 * it something outside, like Google
198 *
199 * @return Boolean
200 */
201 public function isLocal() {
202 return $this->mLocal;
203 }
204
205 /**
206 * Can pages from this wiki be transcluded?
207 * Still requires $wgEnableScaryTransclusion
208 *
209 * @return Boolean
210 */
211 public function isTranscludable() {
212 return $this->mTrans;
213 }
214
215 /**
216 * Get the name for the interwiki site
217 *
218 * @return String
219 */
220 public function getName() {
221 $key = 'interwiki-name-' . $this->mPrefix;
222 $msg = wfMsgForContent( $key );
223 return wfEmptyMsg( $key, $msg ) ? '' : $msg;
224 }
225
226 /**
227 * Get a description for this interwiki
228 *
229 * @return String
230 */
231 public function getDescription() {
232 $key = 'interwiki-desc-' . $this->mPrefix;
233 $msg = wfMsgForContent( $key );
234 return wfEmptyMsg( $key, $msg ) ? '' : $msg;
235 }
236 }