Watchlist query group.
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /**
9 *
10 */
11 define( 'MSG_LOAD_TIMEOUT', 60);
12 define( 'MSG_LOCK_TIMEOUT', 10);
13 define( 'MSG_WAIT_TIMEOUT', 10);
14 define( 'MSG_CACHE_VERSION', 1 );
15
16 /**
17 * Message cache
18 * Performs various MediaWiki namespace-related functions
19 *
20 * @package MediaWiki
21 */
22 class MessageCache {
23 var $mCache, $mUseCache, $mDisable, $mExpiry;
24 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
25 var $mExtensionMessages = array();
26 var $mInitialised = false;
27 var $mDeferred = true;
28
29 function __construct( &$memCached, $useDB, $expiry, $memcPrefix) {
30 wfProfileIn( __METHOD__ );
31
32 $this->mUseCache = !is_null( $memCached );
33 $this->mMemc = &$memCached;
34 $this->mDisable = !$useDB;
35 $this->mExpiry = $expiry;
36 $this->mDisableTransform = false;
37 $this->mMemcKey = $memcPrefix.':messages';
38 $this->mKeys = false; # initialised on demand
39 $this->mInitialised = true;
40 $this->mParser = null;
41
42 # When we first get asked for a message,
43 # then we'll fill up the cache. If we
44 # can return a cache hit, this saves
45 # some extra milliseconds
46 $this->mDeferred = true;
47
48 wfProfileOut( __METHOD__ );
49 }
50
51 function getParserOptions() {
52 if ( !$this->mParserOptions ) {
53 $this->mParserOptions = new ParserOptions;
54 }
55 return $this->mParserOptions;
56 }
57
58 /**
59 * Try to load the cache from a local file
60 */
61 function loadFromLocal( $hash ) {
62 global $wgLocalMessageCache;
63
64 $this->mCache = false;
65 if ( $wgLocalMessageCache === false ) {
66 return;
67 }
68
69 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
70
71 wfSuppressWarnings();
72 $file = fopen( $filename, 'r' );
73 wfRestoreWarnings();
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, 10000000 );
83 $this->setCache( 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;
93
94 if ( $wgLocalMessageCache === false ) {
95 return;
96 }
97
98 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
99 $oldUmask = umask( 0 );
100 wfMkdirParents( $wgLocalMessageCache, 0777 );
101 umask( $oldUmask );
102
103 $file = fopen( $filename, 'w' );
104 if ( !$file ) {
105 wfDebug( "Unable to open local cache file for writing\n" );
106 return;
107 }
108
109 fwrite( $file, $hash . $serialized );
110 fclose( $file );
111 @chmod( $filename, 0666 );
112 }
113
114 function loadFromScript( $hash ) {
115 global $wgLocalMessageCache;
116 if ( $wgLocalMessageCache === false ) {
117 return;
118 }
119
120 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
121
122 wfSuppressWarnings();
123 $file = fopen( $filename, 'r' );
124 wfRestoreWarnings();
125 if ( !$file ) {
126 return;
127 }
128 $localHash=substr(fread($file,40),8);
129 fclose($file);
130 if ($hash!=$localHash) {
131 return;
132 }
133 require("$wgLocalMessageCache/messages-" . wfWikiID());
134 $this->setCache( $this->mCache);
135 }
136
137 function saveToScript($array, $hash) {
138 global $wgLocalMessageCache;
139 if ( $wgLocalMessageCache === false ) {
140 return;
141 }
142
143 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
144 $oldUmask = umask( 0 );
145 wfMkdirParents( $wgLocalMessageCache, 0777 );
146 umask( $oldUmask );
147 $file = fopen( $filename.'.tmp', 'w');
148 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
149
150 foreach ($array as $key => $message) {
151 fwrite($file, "'". $this->escapeForScript($key).
152 "' => '" . $this->escapeForScript($message).
153 "',\n");
154 }
155 fwrite($file,");\n?>");
156 fclose($file);
157 rename($filename.'.tmp',$filename);
158 }
159
160 function escapeForScript($string) {
161 $string = str_replace( '\\', '\\\\', $string );
162 $string = str_replace( '\'', '\\\'', $string );
163 return $string;
164 }
165
166 /**
167 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
168 */
169 function setCache( $cache ) {
170 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) {
171 $this->mCache = $cache;
172 } else {
173 $this->mCache = false;
174 }
175 }
176
177 /**
178 * Loads messages either from memcached or the database, if not disabled
179 * On error, quietly switches to a fallback mode
180 * Returns false for a reportable error, true otherwise
181 */
182 function load() {
183 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
184
185 if ( $this->mDisable ) {
186 static $shownDisabled = false;
187 if ( !$shownDisabled ) {
188 wfDebug( "MessageCache::load(): disabled\n" );
189 $shownDisabled = true;
190 }
191 return true;
192 }
193 if ( !$this->mUseCache ) {
194 $this->mDeferred = false;
195 return true;
196 }
197
198 $fname = 'MessageCache::load';
199 wfProfileIn( $fname );
200 $success = true;
201
202 $this->mCache = false;
203
204 # Try local cache
205 wfProfileIn( $fname.'-fromlocal' );
206 $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
207 if ( $hash ) {
208 if ($wgLocalMessageCacheSerialized) {
209 $this->loadFromLocal( $hash );
210 } else {
211 $this->loadFromScript( $hash );
212 }
213 if ( $this->mCache ) {
214 wfDebug( "MessageCache::load(): got from local cache\n" );
215 }
216 }
217 wfProfileOut( $fname.'-fromlocal' );
218
219 # Try memcached
220 if ( !$this->mCache ) {
221 wfProfileIn( $fname.'-fromcache' );
222 $this->setCache( $this->mMemc->get( $this->mMemcKey ) );
223 if ( $this->mCache ) {
224 wfDebug( "MessageCache::load(): got from global cache\n" );
225 # Save to local cache
226 if ( $wgLocalMessageCache !== false ) {
227 $serialized = serialize( $this->mCache );
228 if ( !$hash ) {
229 $hash = md5( $serialized );
230 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
231 }
232 if ($wgLocalMessageCacheSerialized) {
233 $this->saveToLocal( $serialized,$hash );
234 } else {
235 $this->saveToScript( $this->mCache, $hash );
236 }
237 }
238 }
239 wfProfileOut( $fname.'-fromcache' );
240 }
241
242
243 # If there's nothing in memcached, load all the messages from the database
244 if ( !$this->mCache ) {
245 wfDebug( "MessageCache::load(): cache is empty\n" );
246 $this->lock();
247 # Other threads don't need to load the messages if another thread is doing it.
248 $success = $this->mMemc->add( $this->mMemcKey.'-status', "loading", MSG_LOAD_TIMEOUT );
249 if ( $success ) {
250 wfProfileIn( $fname.'-load' );
251 wfDebug( "MessageCache::load(): loading all messages from DB\n" );
252 $this->loadFromDB();
253 wfProfileOut( $fname.'-load' );
254
255 # Save in memcached
256 # Keep trying if it fails, this is kind of important
257 wfProfileIn( $fname.'-save' );
258 for ($i=0; $i<20 &&
259 !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
260 $i++ ) {
261 usleep(mt_rand(500000,1500000));
262 }
263
264 # Save to local cache
265 if ( $wgLocalMessageCache !== false ) {
266 $serialized = serialize( $this->mCache );
267 $hash = md5( $serialized );
268 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
269 if ($wgLocalMessageCacheSerialized) {
270 $this->saveToLocal( $serialized,$hash );
271 } else {
272 $this->saveToScript( $this->mCache, $hash );
273 }
274 }
275
276 wfProfileOut( $fname.'-save' );
277 if ( $i == 20 ) {
278 $this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 );
279 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
280 } else {
281 $this->mMemc->delete( $this->mMemcKey.'-status' );
282 }
283 }
284 $this->unlock();
285 }
286
287 if ( !is_array( $this->mCache ) ) {
288 wfDebug( "MessageCache::load(): unable to load cache, disabled\n" );
289 $this->mDisable = true;
290 $this->mCache = false;
291 }
292 wfProfileOut( $fname );
293 $this->mDeferred = false;
294 return $success;
295 }
296
297 /**
298 * Loads all or main part of cacheable messages from the database
299 */
300 function loadFromDB() {
301 global $wgLang, $wgMaxMsgCacheEntrySize;
302
303 wfProfileIn( __METHOD__ );
304 $dbr =& wfGetDB( DB_SLAVE );
305 $this->mCache = array();
306
307 # Load titles for all oversized pages in the MediaWiki namespace
308 $res = $dbr->select( 'page', 'page_title',
309 array(
310 'page_len > ' . intval( $wgMaxMsgCacheEntrySize ),
311 'page_is_redirect' => 0,
312 'page_namespace' => NS_MEDIAWIKI,
313 ),
314 __METHOD__ );
315 while ( $row = $dbr->fetchObject( $res ) ) {
316 $this->mCache[$row->page_title] = '!TOO BIG';
317 }
318 $dbr->freeResult( $res );
319
320 # Load text for the remaining pages
321 $res = $dbr->select( array( 'page', 'revision', 'text' ),
322 array( 'page_title', 'old_text', 'old_flags' ),
323 array(
324 'page_is_redirect' => 0,
325 'page_namespace' => NS_MEDIAWIKI,
326 'page_latest=rev_id',
327 'rev_text_id=old_id',
328 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize ) ),
329 __METHOD__ );
330
331 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
332 $this->mCache[$row->page_title] = ' ' . Revision::getRevisionText( $row );
333 }
334 $this->mCache['VERSION'] = MSG_CACHE_VERSION;
335 $dbr->freeResult( $res );
336 wfProfileOut( __METHOD__ );
337 }
338
339 /**
340 * Not really needed anymore
341 */
342 function getKeys() {
343 global $wgContLang;
344 if ( !$this->mKeys ) {
345 $this->mKeys = array();
346 $allMessages = Language::getMessagesFor( 'en' );
347 foreach ( $allMessages as $key => $unused ) {
348 $title = $wgContLang->ucfirst( $key );
349 array_push( $this->mKeys, $title );
350 }
351 }
352 return $this->mKeys;
353 }
354
355 function replace( $title, $text ) {
356 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc;
357 global $wgMaxMsgCacheEntrySize;
358
359 wfProfileIn( __METHOD__ );
360 $this->lock();
361 $this->load();
362 $parserMemc->delete(wfMemcKey('sidebar'));
363 if ( is_array( $this->mCache ) ) {
364 if ( $text === false ) {
365 # Article was deleted
366 unset( $this->mCache[$title] );
367 $this->mMemc->delete( "$this->mMemcKey:{$title}" );
368 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
369 $this->mCache[$title] = '!TOO BIG';
370 $this->mMemc->set( "$this->mMemcKey:{$title}", ' '.$text, $this->mExpiry );
371 } else {
372 $this->mCache[$title] = ' ' . $text;
373 $this->mMemc->delete( "$this->mMemcKey:{$title}" );
374 }
375 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
376
377 # Save to local cache
378 if ( $wgLocalMessageCache !== false ) {
379 $serialized = serialize( $this->mCache );
380 $hash = md5( $serialized );
381 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
382 if ($wgLocalMessageCacheSerialized) {
383 $this->saveToLocal( $serialized,$hash );
384 } else {
385 $this->saveToScript( $this->mCache, $hash );
386 }
387 }
388 }
389 $this->unlock();
390 wfProfileOut( __METHOD__ );
391 }
392
393 /**
394 * Returns success
395 * Represents a write lock on the messages key
396 */
397 function lock() {
398 if ( !$this->mUseCache ) {
399 return true;
400 }
401
402 $lockKey = $this->mMemcKey . 'lock';
403 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
404 sleep(1);
405 }
406
407 return $i >= MSG_WAIT_TIMEOUT;
408 }
409
410 function unlock() {
411 if ( !$this->mUseCache ) {
412 return;
413 }
414
415 $lockKey = $this->mMemcKey . 'lock';
416 $this->mMemc->delete( $lockKey );
417 }
418
419 /**
420 * Get a message from either the content language or the user language.
421 *
422 * @param string $key The message cache key
423 * @param bool $useDB Get the message from the DB, false to use only the localisation
424 * @param bool $forContent Get the message from the content language rather than the
425 * user language
426 * @param bool $isFullKey Specifies whether $key is a two part key "lang/msg".
427 */
428 function get( $key, $useDB = true, $forContent = true, $isFullKey = false ) {
429 global $wgContLanguageCode, $wgContLang, $wgLang;
430 if( $forContent ) {
431 $lang =& $wgContLang;
432 } else {
433 $lang =& $wgLang;
434 }
435 $langcode = $lang->getCode();
436 # If uninitialised, someone is trying to call this halfway through Setup.php
437 if( !$this->mInitialised ) {
438 return '&lt;' . htmlspecialchars($key) . '&gt;';
439 }
440 # If cache initialization was deferred, start it now.
441 if( $this->mDeferred && !$this->mDisable && $useDB ) {
442 $this->load();
443 }
444
445 $message = false;
446
447 # Try the MediaWiki namespace
448 if( !$this->mDisable && $useDB ) {
449 $title = $wgContLang->ucfirst( $key );
450 if(!$isFullKey && ($langcode != $wgContLanguageCode) ) {
451 $title .= '/' . $langcode;
452 }
453 $message = $this->getMsgFromNamespace( $title );
454 }
455 # Try the extension array
456 if( $message === false && isset( $this->mExtensionMessages[$langcode][$key] ) ) {
457 $message = $this->mExtensionMessages[$langcode][$key];
458 }
459 if ( $message === false && isset( $this->mExtensionMessages['en'][$key] ) ) {
460 $message = $this->mExtensionMessages['en'][$key];
461 }
462
463 # Try the array in the language object
464 if( $message === false ) {
465 #wfDebug( "Trying language object for message $key\n" );
466 wfSuppressWarnings();
467 $message = $lang->getMessage( $key );
468 wfRestoreWarnings();
469 if ( is_null( $message ) ) {
470 $message = false;
471 }
472 }
473
474 # Try the array of another language
475 if( $message === false && strpos( $key, '/' ) ) {
476 $message = explode( '/', $key );
477 if ( $message[1] ) {
478 wfSuppressWarnings();
479 $message = Language::getMessageFor( $message[0], $message[1] );
480 wfRestoreWarnings();
481 if ( is_null( $message ) ) {
482 $message = false;
483 }
484 } else {
485 $message = false;
486 }
487 }
488
489 # Is this a custom message? Try the default language in the db...
490 if( ($message === false || $message === '-' ) &&
491 !$this->mDisable && $useDB &&
492 !$isFullKey && ($langcode != $wgContLanguageCode) ) {
493 $message = $this->getMsgFromNamespace( $wgContLang->ucfirst( $key ) );
494 }
495
496 # Final fallback
497 if( $message === false ) {
498 return '&lt;' . htmlspecialchars($key) . '&gt;';
499 }
500
501 # Replace brace tags
502 $message = $this->transform( $message );
503 return $message;
504 }
505
506 /**
507 * Get a message from the MediaWiki namespace, with caching. The key must
508 * first be converted to two-part lang/msg form if necessary.
509 *
510 * @param string $title Message cache key with initial uppercase letter
511 */
512 function getMsgFromNamespace( $title ) {
513 $message = false;
514 $type = false;
515
516 # Try the cache
517 if( $this->mUseCache && isset( $this->mCache[$title] ) ) {
518 $entry = $this->mCache[$title];
519 $type = substr( $entry, 0, 1 );
520 if ( $type == ' ' ) {
521 return substr( $entry, 1 );
522 }
523 }
524
525 # Call message hooks, in case they are defined
526 wfRunHooks('MessagesPreLoad', array( $title, &$message ) );
527 if ( $message !== false ) {
528 return $message;
529 }
530
531 # If there is no cache entry and no placeholder, it doesn't exist
532 if ( $type != '!' && $message === false ) {
533 return false;
534 }
535
536 $memcKey = $this->mMemcKey . ':' . $title;
537
538 # Try the individual message cache
539 if ( $this->mUseCache ) {
540 $entry = $this->mMemc->get( $memcKey );
541 if ( $entry ) {
542 $type = substr( $entry, 0, 1 );
543
544 if ( $type == ' ' ) {
545 $message = substr( $entry, 1 );
546 $this->mCache[$title] = $message;
547 return $message;
548 } elseif ( $entry == '!NONEXISTENT' ) {
549 return false;
550 } else {
551 # Corrupt/obsolete entry, delete it
552 $this->mMemc->delete( $memcKey );
553 }
554
555 }
556 }
557
558 # Try loading it from the DB
559 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
560 if( $revision ) {
561 $message = $revision->getText();
562 if ($this->mUseCache) {
563 $this->mCache[$title] = ' ' . $message;
564 $this->mMemc->set( $memcKey, $message, $this->mExpiry );
565 }
566 } else {
567 # Negative caching
568 # Use some special text instead of false, because false gets converted to '' somewhere
569 $this->mMemc->set( $memcKey, '!NONEXISTENT', $this->mExpiry );
570 $this->mCache[$title] = false;
571 }
572
573 return $message;
574 }
575
576 function transform( $message ) {
577 global $wgParser;
578 if ( !$this->mParser && isset( $wgParser ) ) {
579 # Do some initialisation so that we don't have to do it twice
580 $wgParser->firstCallInit();
581 # Clone it and store it
582 $this->mParser = clone $wgParser;
583 }
584 if ( !$this->mDisableTransform && $this->mParser ) {
585 if( strpos( $message, '{{' ) !== false ) {
586 $message = $this->mParser->transformMsg( $message, $this->getParserOptions() );
587 }
588 }
589 return $message;
590 }
591
592 function disable() { $this->mDisable = true; }
593 function enable() { $this->mDisable = false; }
594 function disableTransform() { $this->mDisableTransform = true; }
595 function enableTransform() { $this->mDisableTransform = false; }
596 function setTransform( $x ) { $this->mDisableTransform = $x; }
597 function getTransform() { return $this->mDisableTransform; }
598
599 /**
600 * Add a message to the cache
601 *
602 * @param mixed $key
603 * @param mixed $value
604 * @param string $lang The messages language, English by default
605 */
606 function addMessage( $key, $value, $lang = 'en' ) {
607 $this->mExtensionMessages[$lang][$key] = $value;
608 }
609
610 /**
611 * Add an associative array of message to the cache
612 *
613 * @param array $messages An associative array of key => values to be added
614 * @param string $lang The messages language, English by default
615 */
616 function addMessages( $messages, $lang = 'en' ) {
617 wfProfileIn( __METHOD__ );
618 if ( isset( $this->mExtensionMessages[$lang] ) ) {
619 $this->mExtensionMessages[$lang] = $messages + $this->mExtensionMessages[$lang];
620 } else {
621 $this->mExtensionMessages[$lang] = $messages;
622 }
623 wfProfileOut( __METHOD__ );
624 }
625
626 /**
627 * Add a 2-D array of messages by lang. Useful for extensions.
628 * Introduced in 1.9. Please do not use it for now, for backwards compatibility.
629 *
630 * @param array $messages The array to be added
631 */
632 function addMessagesByLang( $messages ) {
633 wfProfileIn( __METHOD__ );
634 foreach ( $messages as $key => $value ) {
635 $this->addMessages( $value, $key );
636 }
637 wfProfileOut( __METHOD__ );
638 }
639
640 /**
641 * Get the extension messages for a specific language
642 *
643 * @param string $lang The messages language, English by default
644 */
645 function getExtensionMessagesFor( $lang = 'en' ) {
646 wfProfileIn( __METHOD__ );
647 $messages = array();
648 if ( isset( $this->mExtensionMessages[$lang] ) ) {
649 $messages = $this->mExtensionMessages[$lang];
650 }
651 if ( $lang != 'en' ) {
652 $messages = $messages + $this->mExtensionMessages['en'];
653 }
654 wfProfileOut( __METHOD__ );
655 return $messages;
656 }
657
658 /**
659 * Clear all stored messages. Mainly used after a mass rebuild.
660 */
661 function clear() {
662 global $wgLocalMessageCache;
663 if( $this->mUseCache ) {
664 # Global cache
665 $this->mMemc->delete( $this->mMemcKey );
666 # Invalidate all local caches
667 $this->mMemc->delete( "{$this->mMemcKey}-hash" );
668 }
669 }
670
671 static function loadAllMessages() {
672 # Some extensions will load their messages when you load their class file
673 wfLoadAllExtensions();
674 # Others will respond to this hook
675 wfRunHooks( 'LoadAllMessages' );
676 # Still others will respond to neither, they are EVIL. We sometimes need to know!
677 }
678 }
679 ?>