Merge capitallinks message fixes from REL1_4
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 */
6
7 require_once( 'Revision.php' );
8
9 /**
10 *
11 */
12 define( 'MSG_LOAD_TIMEOUT', 60);
13 define( 'MSG_LOCK_TIMEOUT', 10);
14 define( 'MSG_WAIT_TIMEOUT', 10);
15
16 /**
17 * Message cache
18 * Performs various useful MediaWiki namespace-related functions
19 *
20 * @package MediaWiki
21 */
22 class MessageCache
23 {
24 var $mCache, $mUseCache, $mDisable, $mExpiry;
25 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
26 var $mExtensionMessages;
27 var $mInitialised = false;
28 var $mDeferred = true;
29
30 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
31 $fname = 'MessageCache::initialise';
32 wfProfileIn( $fname );
33
34 $this->mUseCache = !is_null( $memCached );
35 $this->mMemc = &$memCached;
36 $this->mDisable = !$useDB;
37 $this->mExpiry = $expiry;
38 $this->mDisableTransform = false;
39 $this->mMemcKey = $memcPrefix.':messages';
40 $this->mKeys = false; # initialised on demand
41 $this->mInitialised = true;
42
43 wfProfileIn( $fname.'-parseropt' );
44 $this->mParserOptions = ParserOptions::newFromUser( $u=NULL );
45 wfProfileOut( $fname.'-parseropt' );
46 wfProfileIn( $fname.'-parser' );
47 $this->mParser = new Parser;
48 wfProfileOut( $fname.'-parser' );
49
50 # When we first get asked for a message,
51 # then we'll fill up the cache. If we
52 # can return a cache hit, this saves
53 # some extra milliseconds
54 $this->mDeferred = true;
55
56 wfProfileOut( $fname );
57 }
58
59 /**
60 * Loads messages either from memcached or the database, if not disabled
61 * On error, quietly switches to a fallback mode
62 * Returns false for a reportable error, true otherwise
63 */
64 function load() {
65 global $wgAllMessagesEn;
66
67 if ( $this->mDisable ) {
68 wfDebug( "MessageCache::load(): disabled\n" );
69 return true;
70 }
71 $fname = 'MessageCache::load';
72 wfProfileIn( $fname );
73 $success = true;
74
75 if ( $this->mUseCache ) {
76 wfProfileIn( $fname.'-fromcache' );
77 $this->mCache = $this->mMemc->get( $this->mMemcKey );
78 wfProfileOut( $fname.'-fromcache' );
79
80 # If there's nothing in memcached, load all the messages from the database
81 if ( !$this->mCache ) {
82 wfDebug( "MessageCache::load(): loading all messages\n" );
83 $this->lock();
84 # Other threads don't need to load the messages if another thread is doing it.
85 $success = $this->mMemc->add( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
86 if ( $success ) {
87 wfProfileIn( $fname.'-load' );
88 $this->loadFromDB();
89 wfProfileOut( $fname.'-load' );
90 # Save in memcached
91 # Keep trying if it fails, this is kind of important
92 wfProfileIn( $fname.'-save' );
93 for ( $i=0; $i<20 && !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ); $i++ ) {
94 usleep(mt_rand(500000,1500000));
95 }
96 wfProfileOut( $fname.'-save' );
97 if ( $i == 20 ) {
98 $this->mMemc->set( $this->mMemcKey, 'error', 86400 );
99 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
100 }
101 }
102 $this->unlock();
103 }
104
105 if ( !is_array( $this->mCache ) ) {
106 wfDebug( "MessageCache::load(): individual message mode\n" );
107 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
108 # Causing too much DB load, disabling -- TS
109 $this->mDisable = true;
110 /*
111 if ( $this->mCache == "loading" ) {
112 $this->mUseCache = false;
113 } elseif ( $this->mCache == "error" ) {
114 $this->mUseCache = false;
115 $success = false;
116 } else {
117 $this->mDisable = true;
118 $success = false;
119 }*/
120 $this->mCache = false;
121 }
122 }
123 wfProfileOut( $fname );
124 $this->mDeferred = false;
125 return $success;
126 }
127
128 /**
129 * Loads all or main part of cacheable messages from the database
130 */
131 function loadFromDB() {
132 global $wgPartialMessageCache;
133 $fname = 'MessageCache::loadFromDB';
134 $dbr =& wfGetDB( DB_SLAVE );
135 $conditions = array( 'page_is_redirect' => 0,
136 'page_namespace' => NS_MEDIAWIKI);
137 if ($wgPartialMessageCache) {
138 wfDebugDieBacktrace( "Confused about how this works." );
139 if (is_array($wgPartialMessageCache)) {
140 $conditions['page_title']=$wgPartialMessageCache;
141 } else {
142 require_once("MessageCacheHints.php");
143 $conditions['page_title']=MessageCacheHints::get();
144 }
145 }
146 $res = $dbr->select( array( 'page', 'text' ),
147 array( 'page_title', 'old_text', 'old_flags' ),
148 'page_is_redirect=0 AND page_namespace = '.NS_MEDIAWIKI.' AND page_latest = old_id',
149 $fname
150 );
151
152 $this->mCache = array();
153 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
154 $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
155 }
156
157 $dbr->freeResult( $res );
158 }
159
160 /**
161 * Not really needed anymore
162 */
163 function getKeys() {
164 global $wgAllMessagesEn, $wgContLang;
165 if ( !$this->mKeys ) {
166 $this->mKeys = array();
167 foreach ( $wgAllMessagesEn as $key => $value ) {
168 $title = $wgContLang->ucfirst( $key );
169 array_push( $this->mKeys, $title );
170 }
171 }
172 return $this->mKeys;
173 }
174
175 /**
176 * Obsolete
177 */
178 function isCacheable( $key ) {
179 return true;
180 }
181
182 function replace( $title, $text ) {
183 $this->lock();
184 $this->load();
185 if ( is_array( $this->mCache ) ) {
186 $this->mCache[$title] = $text;
187 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
188 }
189 $this->unlock();
190 }
191
192 /**
193 * Returns success
194 * Represents a write lock on the messages key
195 */
196 function lock() {
197 if ( !$this->mUseCache ) {
198 return true;
199 }
200
201 $lockKey = $this->mMemcKey . 'lock';
202 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
203 sleep(1);
204 }
205
206 return $i >= MSG_WAIT_TIMEOUT;
207 }
208
209 function unlock() {
210 if ( !$this->mUseCache ) {
211 return;
212 }
213
214 $lockKey = $this->mMemcKey . 'lock';
215 $this->mMemc->delete( $lockKey );
216 }
217
218 function get( $key, $useDB, $forcontent=true ) {
219 global $wgContLanguageCode;
220 if( $forcontent ) {
221 global $wgContLang;
222 $lang =& $wgContLang;
223 $langcode = $wgContLanguageCode;
224 } else {
225 global $wgLang, $wgLanguageCode;
226 $lang =& $wgLang;
227 $langcode = $wgLanguageCode;
228 }
229 # If uninitialised, someone is trying to call this halfway through Setup.php
230 if( !$this->mInitialised ) {
231 return "&lt;$key&gt;";
232 }
233 # If cache initialization was deferred, start it now.
234 if( $this->mDeferred ) {
235 $this->load();
236 }
237
238 $message = false;
239 if( !$this->mDisable && $useDB ) {
240 $title = $lang->ucfirst( $key );
241 if( $langcode != $wgContLanguageCode ) {
242 $title .= '/' . $langcode;
243 }
244
245 # Try the cache
246 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
247 $message = $this->mCache[$title];
248 }
249
250 if ( !$message && $this->mUseCache ) {
251 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
252 if( $message ) {
253 $this->mCache[$title] = $message;
254 }
255 }
256
257 # If it wasn't in the cache, load each message from the DB individually
258 if ( !$message ) {
259 $dbr =& wfGetDB( DB_SLAVE );
260 $result = $dbr->selectRow( array( 'page', 'text' ),
261 array( 'old_flags', 'old_text' ),
262 'page_namespace=' . NS_MEDIAWIKI .
263 ' AND page_title=' . $dbr->addQuotes( $title ) .
264 ' AND page_latest=old_id',
265 'MessageCache::get' );
266 if ( $result ) {
267 $message = Revision::getRevisionText( $result );
268 if ($this->mUseCache) {
269 $this->mCache[$title]=$message;
270 /* individual messages may be often
271 recached until proper purge code exists
272 */
273 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
274 }
275 }
276 }
277 }
278 # Try the extension array
279 if( !$message ) {
280 $message = @$this->mExtensionMessages[$key];
281 }
282
283 # Try the array in the language object
284 if( !$message ) {
285 wfSuppressWarnings();
286 $message = $lang->getMessage( $key );
287 wfRestoreWarnings();
288 }
289
290 # Try the English array
291 if( !$message && $langcode != 'en' ) {
292 wfSuppressWarnings();
293 $message = Language::getMessage( $key );
294 wfRestoreWarnings();
295 }
296
297 # Final fallback
298 if( !$message ) {
299 $message = "&lt;$key&gt;";
300 }
301
302 # Replace brace tags
303 $message = $this->transform( $message );
304 return $message;
305 }
306
307 function transform( $message ) {
308 if( !$this->mDisableTransform ) {
309 if( strpos( $message, '{{' ) !== false ) {
310 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
311 }
312 }
313 return $message;
314 }
315
316 function disable() { $this->mDisable = true; }
317 function enable() { $this->mDisable = false; }
318 function disableTransform() { $this->mDisableTransform = true; }
319 function enableTransform() { $this->mDisableTransform = false; }
320
321 function addMessage( $key, $value ) {
322 $this->mExtensionMessages[$key] = $value;
323 }
324
325 function addMessages( $messages ) {
326 foreach ( $messages as $key => $value ) {
327 $this->mExtensionMessages[$key] = $value;
328 }
329 }
330
331 /**
332 * Clear all stored messages. Mainly used after a mass rebuild.
333 */
334 function clear() {
335 if( $this->mUseCache ) {
336 $this->mMemc->delete( $this->mMemcKey );
337 }
338 }
339 }
340 ?>