Fix 2714 : backlink from special:whatlinkshere was hard set as 'existing'
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /** */
9 require_once( 'Revision.php' );
10
11 /**
12 *
13 */
14 define( 'MSG_LOAD_TIMEOUT', 60);
15 define( 'MSG_LOCK_TIMEOUT', 10);
16 define( 'MSG_WAIT_TIMEOUT', 10);
17
18 /**
19 * Message cache
20 * Performs various useful MediaWiki namespace-related functions
21 *
22 * @package MediaWiki
23 */
24 class MessageCache {
25 var $mCache, $mUseCache, $mDisable, $mExpiry;
26 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
27 var $mExtensionMessages = array();
28 var $mInitialised = false;
29 var $mDeferred = true;
30
31 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
32 $fname = 'MessageCache::initialise';
33 wfProfileIn( $fname );
34
35 $this->mUseCache = !is_null( $memCached );
36 $this->mMemc = &$memCached;
37 $this->mDisable = !$useDB;
38 $this->mExpiry = $expiry;
39 $this->mDisableTransform = false;
40 $this->mMemcKey = $memcPrefix.':messages';
41 $this->mKeys = false; # initialised on demand
42 $this->mInitialised = true;
43
44 wfProfileIn( $fname.'-parseropt' );
45 $this->mParserOptions = ParserOptions::newFromUser( $u=NULL );
46 wfProfileOut( $fname.'-parseropt' );
47 wfProfileIn( $fname.'-parser' );
48 $this->mParser = new Parser;
49 wfProfileOut( $fname.'-parser' );
50
51 # When we first get asked for a message,
52 # then we'll fill up the cache. If we
53 # can return a cache hit, this saves
54 # some extra milliseconds
55 $this->mDeferred = true;
56
57 wfProfileOut( $fname );
58 }
59
60 /**
61 * Try to load the cache from a local file
62 */
63 function loadFromLocal( $hash ) {
64 global $wgLocalMessageCache, $wgDBname;
65
66 $this->mCache = false;
67 if ( $wgLocalMessageCache === false ) {
68 return;
69 }
70
71 $filename = "$wgLocalMessageCache/messages-$wgDBname";
72
73 $file = fopen( $filename, 'r' );
74 if ( !$file ) {
75 return;
76 }
77
78 // Check to see if the file has the hash specified
79 $localHash = fread( $file, 32 );
80 if ( $hash == $localHash ) {
81 // All good, get the rest of it
82 $serialized = fread( $file, 1000000 );
83 $this->mCache = unserialize( $serialized );
84 }
85 fclose( $file );
86 }
87
88 /**
89 * Save the cache to a local file
90 */
91 function saveToLocal( $serialized, $hash ) {
92 global $wgLocalMessageCache, $wgDBname;
93
94 if ( $wgLocalMessageCache === false ) {
95 return;
96 }
97
98 $filename = "$wgLocalMessageCache/messages-$wgDBname";
99 wfMkdirParents( $wgLocalMessageCache, 0777 );
100
101 $file = fopen( $filename, 'w' );
102 if ( !$file ) {
103 wfDebug( "Unable to open local cache file for writing\n" );
104 return;
105 }
106
107 fwrite( $file, $hash . $serialized );
108 fclose( $file );
109 }
110
111
112 /**
113 * Loads messages either from memcached or the database, if not disabled
114 * On error, quietly switches to a fallback mode
115 * Returns false for a reportable error, true otherwise
116 */
117 function load() {
118 global $wgLocalMessageCache;
119
120 if ( $this->mDisable ) {
121 static $shownDisabled = false;
122 if ( !$shownDisabled ) {
123 wfDebug( "MessageCache::load(): disabled\n" );
124 $shownDisabled = true;
125 }
126 return true;
127 }
128 $fname = 'MessageCache::load';
129 wfProfileIn( $fname );
130 $success = true;
131
132 if ( $this->mUseCache ) {
133 $this->mCache = false;
134
135 # Try local cache
136 wfProfileIn( $fname.'-fromlocal' );
137 $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
138 if ( $hash ) {
139 $this->loadFromLocal( $hash );
140 }
141 wfProfileOut( $fname.'-fromlocal' );
142
143 # Try memcached
144 if ( !$this->mCache ) {
145 wfProfileIn( $fname.'-fromcache' );
146 $this->mCache = $this->mMemc->get( $this->mMemcKey );
147
148 # Save to local cache
149 if ( $wgLocalMessageCache !== false ) {
150 $serialized = serialize( $this->mCache );
151 if ( !$hash ) {
152 $hash = md5( $serialized );
153 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
154 }
155 $this->saveToLocal( $serialized, $hash );
156 }
157 wfProfileOut( $fname.'-fromcache' );
158 }
159
160
161 # If there's nothing in memcached, load all the messages from the database
162 if ( !$this->mCache ) {
163 wfDebug( "MessageCache::load(): loading all messages\n" );
164 $this->lock();
165 # Other threads don't need to load the messages if another thread is doing it.
166 $success = $this->mMemc->add( $this->mMemcKey.'-status', "loading", MSG_LOAD_TIMEOUT );
167 if ( $success ) {
168 wfProfileIn( $fname.'-load' );
169 $this->loadFromDB();
170 wfProfileOut( $fname.'-load' );
171
172 # Save in memcached
173 # Keep trying if it fails, this is kind of important
174 wfProfileIn( $fname.'-save' );
175 for ($i=0; $i<20 &&
176 !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
177 $i++ ) {
178 usleep(mt_rand(500000,1500000));
179 }
180
181 # Save to local cache
182 if ( $wgLocalMessageCache !== false ) {
183 $serialized = serialize( $this->mCache );
184 $hash = md5( $serialized );
185 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
186 $this->saveToLocal( $serialized, $hash );
187 }
188
189 wfProfileOut( $fname.'-save' );
190 if ( $i == 20 ) {
191 $this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 );
192 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
193 }
194 }
195 $this->unlock();
196 }
197
198 if ( !is_array( $this->mCache ) ) {
199 wfDebug( "MessageCache::load(): individual message mode\n" );
200 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
201 # Causing too much DB load, disabling -- TS
202 $this->mDisable = true;
203 /*
204 if ( $this->mCache == "loading" ) {
205 $this->mUseCache = false;
206 } elseif ( $this->mCache == "error" ) {
207 $this->mUseCache = false;
208 $success = false;
209 } else {
210 $this->mDisable = true;
211 $success = false;
212 }*/
213 $this->mCache = false;
214 }
215 }
216 wfProfileOut( $fname );
217 $this->mDeferred = false;
218 return $success;
219 }
220
221 /**
222 * Loads all or main part of cacheable messages from the database
223 */
224 function loadFromDB() {
225 global $wgAllMessagesEn, $wgLang;
226
227 $fname = 'MessageCache::loadFromDB';
228 $dbr =& wfGetDB( DB_SLAVE );
229 if ( !$dbr ) {
230 wfDebugDieBacktrace( 'Invalid database object' );
231 }
232 $conditions = array( 'page_is_redirect' => 0,
233 'page_namespace' => NS_MEDIAWIKI);
234 $res = $dbr->select( array( 'page', 'revision', 'text' ),
235 array( 'page_title', 'old_text', 'old_flags' ),
236 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI.' AND page_latest=rev_id AND rev_text_id=old_id',
237 $fname
238 );
239
240 $this->mCache = array();
241 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
242 $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
243 }
244
245 # Negative caching
246 # Go through the language array and the extension array and make a note of
247 # any keys missing from the cache
248 foreach ( $wgAllMessagesEn as $key => $value ) {
249 $uckey = $wgLang->ucfirst( $key );
250 if ( !array_key_exists( $uckey, $this->mCache ) ) {
251 $this->mCache[$uckey] = false;
252 }
253 }
254 foreach ( $this->mExtensionMessages as $key => $value ) {
255 $uckey = $wgLang->ucfirst( $key );
256 if ( !array_key_exists( $uckey, $this->mCache ) ) {
257 $this->mCache[$uckey] = false;
258 }
259 }
260
261 $dbr->freeResult( $res );
262 }
263
264 /**
265 * Not really needed anymore
266 */
267 function getKeys() {
268 global $wgAllMessagesEn, $wgContLang;
269 if ( !$this->mKeys ) {
270 $this->mKeys = array();
271 foreach ( $wgAllMessagesEn as $key => $value ) {
272 $title = $wgContLang->ucfirst( $key );
273 array_push( $this->mKeys, $title );
274 }
275 }
276 return $this->mKeys;
277 }
278
279 /**
280 * @deprecated
281 */
282 function isCacheable( $key ) {
283 return true;
284 }
285
286 function replace( $title, $text ) {
287 global $wgLocalMessageCache;
288
289 $this->lock();
290 $this->load();
291 if ( is_array( $this->mCache ) ) {
292 $this->mCache[$title] = $text;
293 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
294
295 # Save to local cache
296 if ( $wgLocalMessageCache !== false ) {
297 $serialized = serialize( $this->mCache );
298 $hash = md5( $serialized );
299 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
300 $this->saveToLocal( $serialized, $hash );
301 }
302
303
304 }
305 $this->unlock();
306 }
307
308 /**
309 * Returns success
310 * Represents a write lock on the messages key
311 */
312 function lock() {
313 if ( !$this->mUseCache ) {
314 return true;
315 }
316
317 $lockKey = $this->mMemcKey . 'lock';
318 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
319 sleep(1);
320 }
321
322 return $i >= MSG_WAIT_TIMEOUT;
323 }
324
325 function unlock() {
326 if ( !$this->mUseCache ) {
327 return;
328 }
329
330 $lockKey = $this->mMemcKey . 'lock';
331 $this->mMemc->delete( $lockKey );
332 }
333
334 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
335 global $wgContLanguageCode;
336 if( $forcontent ) {
337 global $wgContLang;
338 $lang =& $wgContLang;
339 $langcode = $wgContLanguageCode;
340 } else {
341 global $wgLang, $wgLanguageCode;
342 $lang =& $wgLang;
343 $langcode = $wgLanguageCode;
344 }
345 # If uninitialised, someone is trying to call this halfway through Setup.php
346 if( !$this->mInitialised ) {
347 return '&lt;' . htmlspecialchars($key) . '&gt;';
348 }
349 # If cache initialization was deferred, start it now.
350 if( $this->mDeferred && !$this->mDisable && $useDB ) {
351 $this->load();
352 }
353
354 $message = false;
355 if( !$this->mDisable && $useDB ) {
356 $title = $lang->ucfirst( $key );
357 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
358 $title .= '/' . $langcode;
359 }
360 $message = $this->getFromCache( $title );
361 }
362 # Try the extension array
363 if( $message === false && array_key_exists( $key, $this->mExtensionMessages ) ) {
364 $message = $this->mExtensionMessages[$key];
365 }
366
367 # Try the array in the language object
368 if( $message === false ) {
369 wfSuppressWarnings();
370 $message = $lang->getMessage( $key );
371 wfRestoreWarnings();
372 if ( is_null( $message ) ) {
373 $message = false;
374 }
375 }
376
377 # Try the English array
378 if( $message === false && $langcode != 'en' ) {
379 wfSuppressWarnings();
380 $message = Language::getMessage( $key );
381 wfRestoreWarnings();
382 if ( is_null( $message ) ) {
383 $message = false;
384 }
385 }
386
387 # Is this a custom message? Try the default language in the db...
388 if( $message === false &&
389 !$this->mDisable && $useDB &&
390 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
391 $message = $this->getFromCache( $lang->ucfirst( $key ) );
392 }
393
394 # Final fallback
395 if( $message === false ) {
396 return '&lt;' . htmlspecialchars($key) . '&gt;';
397 }
398
399 # Replace brace tags
400 $message = $this->transform( $message );
401 return $message;
402 }
403
404 function getFromCache( $title ) {
405 $message = false;
406
407 # Try the cache
408 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
409 return $this->mCache[$title];
410 }
411
412 # Try individual message cache
413 if ( $this->mUseCache ) {
414 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
415 if ( $message == '###NONEXISTENT###' ) {
416 return false;
417 } elseif( !is_null( $message ) ) {
418 $this->mCache[$title] = $message;
419 return $message;
420 } else {
421 $message = false;
422 }
423 }
424
425 # If it wasn't in the cache, load each message from the DB individually
426 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
427 if( $revision ) {
428 $message = $revision->getText();
429 if ($this->mUseCache) {
430 $this->mCache[$title]=$message;
431 /* individual messages may be often
432 recached until proper purge code exists
433 */
434 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
435 }
436 } else {
437 # Negative caching
438 # Use some special text instead of false, because false gets converted to '' somewhere
439 $this->mMemc->set( $this->mMemcKey . ':' . $title, '###NONEXISTENT###', $this->mExpiry );
440 }
441
442 return $message;
443 }
444
445 function transform( $message ) {
446 if( !$this->mDisableTransform ) {
447 if( strpos( $message, '{{' ) !== false ) {
448 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
449 }
450 }
451 return $message;
452 }
453
454 function disable() { $this->mDisable = true; }
455 function enable() { $this->mDisable = false; }
456 function disableTransform() { $this->mDisableTransform = true; }
457 function enableTransform() { $this->mDisableTransform = false; }
458 function setTransform( $x ) { $this->mDisableTransform = $x; }
459 function getTransform() { return $this->mDisableTransform; }
460
461 /**
462 * Add a message to the cache
463 *
464 * @param mixed $key
465 * @param mixed $value
466 */
467 function addMessage( $key, $value ) {
468 $this->mExtensionMessages[$key] = $value;
469 }
470
471 /**
472 * Add an associative array of message to the cache
473 *
474 * @param array $messages An associative array of key => values to be added
475 */
476 function addMessages( $messages ) {
477 foreach ( $messages as $key => $value ) {
478 $this->addMessage( $key, $value );
479 }
480 }
481
482 /**
483 * Clear all stored messages. Mainly used after a mass rebuild.
484 */
485 function clear() {
486 if( $this->mUseCache ) {
487 $this->mMemc->delete( $this->mMemcKey );
488 }
489 }
490 }
491 ?>