c5d6ac0975e89c184279322611cb318f45b34d69
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 * @file
4 * @ingroup Cache
5 */
6
7 /**
8 *
9 */
10 define( 'MSG_LOAD_TIMEOUT', 60 );
11 define( 'MSG_LOCK_TIMEOUT', 10 );
12 define( 'MSG_WAIT_TIMEOUT', 10 );
13 define( 'MSG_CACHE_VERSION', 1 );
14
15 /**
16 * Message cache
17 * Performs various MediaWiki namespace-related functions
18 * @ingroup Cache
19 */
20 class MessageCache {
21 /**
22 * Process local cache of loaded messages that are defined in
23 * MediaWiki namespace. First array level is a language code,
24 * second level is message key and the values are either message
25 * content prefixed with space, or !NONEXISTENT for negative
26 * caching.
27 */
28 protected $mCache;
29
30 // Should mean that database cannot be used, but check
31 protected $mDisable;
32
33 /// Lifetime for cache, used by object caching
34 protected $mExpiry;
35
36 /**
37 * Message cache has it's own parser which it uses to transform
38 * messages.
39 */
40 protected $mParserOptions, $mParser;
41
42 /// Variable for tracking which variables are already loaded
43 protected $mLoadedLanguages = array();
44
45 /**
46 * Used for automatic detection of most used messages.
47 */
48 protected $mRequestedMessages = array();
49
50 /**
51 * How long the message request counts are stored. Longer period gives
52 * better sample, but also takes longer to adapt changes. The counts
53 * are aggregrated per day, regardless of the value of this variable.
54 */
55 protected static $mAdaptiveDataAge = 604800; // Is 7*24*3600
56
57 /**
58 * Filter the tail of less used messages that are requested more seldom
59 * than this factor times the number of request of most requested message.
60 * These messages are not loaded in the default set, but are still cached
61 * individually on demand with the normal cache expiry time.
62 */
63 protected static $mAdaptiveInclusionThreshold = 0.05;
64
65 /**
66 * Singleton instance
67 */
68 private static $instance;
69
70 /**
71 * Get the signleton instance of this class
72 *
73 * @since 1.18
74 * @return MessageCache object
75 */
76 public static function singleton() {
77 if ( is_null( self::$instance ) ) {
78 global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
79 self::$instance = new self( wfGetMessageCacheStorage(), $wgUseDatabaseMessages, $wgMsgCacheExpiry );
80 }
81 return self::$instance;
82 }
83
84 /**
85 * Destroy the singleton instance
86 *
87 * @since 1.18
88 */
89 public static function destroyInstance() {
90 self::$instance = null;
91 }
92
93 function __construct( $memCached, $useDB, $expiry ) {
94 if ( !$memCached ) {
95 $memCached = wfGetCache( CACHE_NONE );
96 }
97
98 $this->mMemc = $memCached;
99 $this->mDisable = !$useDB;
100 $this->mExpiry = $expiry;
101 }
102
103 /**
104 * ParserOptions is lazy initialised.
105 *
106 * @return ParserOptions
107 */
108 function getParserOptions() {
109 if ( !$this->mParserOptions ) {
110 $this->mParserOptions = new ParserOptions;
111 }
112 return $this->mParserOptions;
113 }
114
115 /**
116 * Try to load the cache from a local file.
117 * Actual format of the file depends on the $wgLocalMessageCacheSerialized
118 * setting.
119 *
120 * @param $hash String: the hash of contents, to check validity.
121 * @param $code Mixed: Optional language code, see documenation of load().
122 * @return false on failure.
123 */
124 function loadFromLocal( $hash, $code ) {
125 global $wgCacheDirectory, $wgLocalMessageCacheSerialized;
126
127 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
128
129 # Check file existence
130 wfSuppressWarnings();
131 $file = fopen( $filename, 'r' );
132 wfRestoreWarnings();
133 if ( !$file ) {
134 return false; // No cache file
135 }
136
137 if ( $wgLocalMessageCacheSerialized ) {
138 // Check to see if the file has the hash specified
139 $localHash = fread( $file, 32 );
140 if ( $hash === $localHash ) {
141 // All good, get the rest of it
142 $serialized = '';
143 while ( !feof( $file ) ) {
144 $serialized .= fread( $file, 100000 );
145 }
146 fclose( $file );
147 return $this->setCache( unserialize( $serialized ), $code );
148 } else {
149 fclose( $file );
150 return false; // Wrong hash
151 }
152 } else {
153 $localHash = substr( fread( $file, 40 ), 8 );
154 fclose( $file );
155 if ( $hash != $localHash ) {
156 return false; // Wrong hash
157 }
158
159 # Require overwrites the member variable or just shadows it?
160 require( $filename );
161 return $this->setCache( $this->mCache, $code );
162 }
163 }
164
165 /**
166 * Save the cache to a local file.
167 */
168 function saveToLocal( $serialized, $hash, $code ) {
169 global $wgCacheDirectory;
170
171 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
172 wfMkdirParents( $wgCacheDirectory ); // might fail
173
174 wfSuppressWarnings();
175 $file = fopen( $filename, 'w' );
176 wfRestoreWarnings();
177
178 if ( !$file ) {
179 wfDebug( "Unable to open local cache file for writing\n" );
180 return;
181 }
182
183 fwrite( $file, $hash . $serialized );
184 fclose( $file );
185 @chmod( $filename, 0666 );
186 }
187
188 function saveToScript( $array, $hash, $code ) {
189 global $wgCacheDirectory;
190
191 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
192 $tempFilename = $filename . '.tmp';
193 wfMkdirParents( $wgCacheDirectory ); // might fail
194
195 wfSuppressWarnings();
196 $file = fopen( $tempFilename, 'w' );
197 wfRestoreWarnings();
198
199 if ( !$file ) {
200 wfDebug( "Unable to open local cache file for writing\n" );
201 return;
202 }
203
204 fwrite( $file, "<?php\n//$hash\n\n \$this->mCache = array(" );
205
206 foreach ( $array as $key => $message ) {
207 $key = $this->escapeForScript( $key );
208 $message = $this->escapeForScript( $message );
209 fwrite( $file, "'$key' => '$message',\n" );
210 }
211
212 fwrite( $file, ");\n?>" );
213 fclose( $file);
214 rename( $tempFilename, $filename );
215 }
216
217 function escapeForScript( $string ) {
218 $string = str_replace( '\\', '\\\\', $string );
219 $string = str_replace( '\'', '\\\'', $string );
220 return $string;
221 }
222
223 /**
224 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
225 *
226 * @return bool
227 */
228 function setCache( $cache, $code ) {
229 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) {
230 $this->mCache[$code] = $cache;
231 return true;
232 } else {
233 return false;
234 }
235 }
236
237 /**
238 * Loads messages from caches or from database in this order:
239 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
240 * (2) memcached
241 * (3) from the database.
242 *
243 * When succesfully loading from (2) or (3), all higher level caches are
244 * updated for the newest version.
245 *
246 * Nothing is loaded if member variable mDisable is true, either manually
247 * set by calling code or if message loading fails (is this possible?).
248 *
249 * Returns true if cache is already populated or it was succesfully populated,
250 * or false if populating empty cache fails. Also returns true if MessageCache
251 * is disabled.
252 *
253 * @param $code String: language to which load messages
254 */
255 function load( $code = false ) {
256 global $wgUseLocalMessageCache;
257
258 if( !is_string( $code ) ) {
259 # This isn't really nice, so at least make a note about it and try to
260 # fall back
261 wfDebug( __METHOD__ . " called without providing a language code\n" );
262 $code = 'en';
263 }
264
265 # Don't do double loading...
266 if ( isset( $this->mLoadedLanguages[$code] ) ) {
267 return true;
268 }
269
270 # 8 lines of code just to say (once) that message cache is disabled
271 if ( $this->mDisable ) {
272 static $shownDisabled = false;
273 if ( !$shownDisabled ) {
274 wfDebug( __METHOD__ . ": disabled\n" );
275 $shownDisabled = true;
276 }
277 return true;
278 }
279
280 # Loading code starts
281 wfProfileIn( __METHOD__ );
282 $success = false; # Keep track of success
283 $where = array(); # Debug info, delayed to avoid spamming debug log too much
284 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
285
286 # (1) local cache
287 # Hash of the contents is stored in memcache, to detect if local cache goes
288 # out of date (due to update in other thread?)
289 if ( $wgUseLocalMessageCache ) {
290 wfProfileIn( __METHOD__ . '-fromlocal' );
291
292 $hash = $this->mMemc->get( wfMemcKey( 'messages', $code, 'hash' ) );
293 if ( $hash ) {
294 $success = $this->loadFromLocal( $hash, $code );
295 if ( $success ) $where[] = 'got from local cache';
296 }
297 wfProfileOut( __METHOD__ . '-fromlocal' );
298 }
299
300 # (2) memcache
301 # Fails if nothing in cache, or in the wrong version.
302 if ( !$success ) {
303 wfProfileIn( __METHOD__ . '-fromcache' );
304 $cache = $this->mMemc->get( $cacheKey );
305 $success = $this->setCache( $cache, $code );
306 if ( $success ) {
307 $where[] = 'got from global cache';
308 $this->saveToCaches( $cache, false, $code );
309 }
310 wfProfileOut( __METHOD__ . '-fromcache' );
311 }
312
313 # (3)
314 # Nothing in caches... so we need create one and store it in caches
315 if ( !$success ) {
316 $where[] = 'cache is empty';
317 $where[] = 'loading from database';
318
319 $this->lock( $cacheKey );
320
321 # Limit the concurrency of loadFromDB to a single process
322 # This prevents the site from going down when the cache expires
323 $statusKey = wfMemcKey( 'messages', $code, 'status' );
324 $success = $this->mMemc->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT );
325 if ( $success ) {
326 $cache = $this->loadFromDB( $code );
327 $success = $this->setCache( $cache, $code );
328 }
329 if ( $success ) {
330 $success = $this->saveToCaches( $cache, true, $code );
331 if ( $success ) {
332 $this->mMemc->delete( $statusKey );
333 } else {
334 $this->mMemc->set( $statusKey, 'error', 60 * 5 );
335 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
336 }
337 }
338 $this->unlock($cacheKey);
339 }
340
341 if ( !$success ) {
342 # Bad luck... this should not happen
343 $where[] = 'loading FAILED - cache is disabled';
344 $info = implode( ', ', $where );
345 wfDebug( __METHOD__ . ": Loading $code... $info\n" );
346 $this->mDisable = true;
347 $this->mCache = false;
348 } else {
349 # All good, just record the success
350 $info = implode( ', ', $where );
351 wfDebug( __METHOD__ . ": Loading $code... $info\n" );
352 $this->mLoadedLanguages[$code] = true;
353 }
354 wfProfileOut( __METHOD__ );
355 return $success;
356 }
357
358 /**
359 * Loads cacheable messages from the database. Messages bigger than
360 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
361 * on-demand from the database later.
362 *
363 * @param $code String: language code.
364 * @return Array: loaded messages for storing in caches.
365 */
366 function loadFromDB( $code ) {
367 wfProfileIn( __METHOD__ );
368 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
369 $dbr = wfGetDB( DB_SLAVE );
370 $cache = array();
371
372 # Common conditions
373 $conds = array(
374 'page_is_redirect' => 0,
375 'page_namespace' => NS_MEDIAWIKI,
376 );
377
378 $mostused = array();
379 if ( $wgAdaptiveMessageCache ) {
380 $mostused = $this->getMostUsedMessages();
381 if ( $code !== $wgLanguageCode ) {
382 foreach ( $mostused as $key => $value ) {
383 $mostused[$key] = "$value/$code";
384 }
385 }
386 }
387
388 if ( count( $mostused ) ) {
389 $conds['page_title'] = $mostused;
390 } elseif ( $code !== $wgLanguageCode ) {
391 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), "/$code" );
392 } else {
393 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
394 # other than language code.
395 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
396 }
397
398 # Conditions to fetch oversized pages to ignore them
399 $bigConds = $conds;
400 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
401
402 # Load titles for all oversized pages in the MediaWiki namespace
403 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__ . "($code)-big" );
404 foreach ( $res as $row ) {
405 $cache[$row->page_title] = '!TOO BIG';
406 }
407
408 # Conditions to load the remaining pages with their contents
409 $smallConds = $conds;
410 $smallConds[] = 'page_latest=rev_id';
411 $smallConds[] = 'rev_text_id=old_id';
412 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
413
414 $res = $dbr->select(
415 array( 'page', 'revision', 'text' ),
416 array( 'page_title', 'old_text', 'old_flags' ),
417 $smallConds,
418 __METHOD__ . "($code)-small"
419 );
420
421 foreach ( $res as $row ) {
422 $cache[$row->page_title] = ' ' . Revision::getRevisionText( $row );
423 }
424
425 foreach ( $mostused as $key ) {
426 if ( !isset( $cache[$key] ) ) {
427 $cache[$key] = '!NONEXISTENT';
428 }
429 }
430
431 $cache['VERSION'] = MSG_CACHE_VERSION;
432 wfProfileOut( __METHOD__ );
433 return $cache;
434 }
435
436 /**
437 * Updates cache as necessary when message page is changed
438 *
439 * @param $title String: name of the page changed.
440 * @param $text Mixed: new contents of the page.
441 */
442 public function replace( $title, $text ) {
443 global $wgMaxMsgCacheEntrySize;
444 wfProfileIn( __METHOD__ );
445
446 if ( $this->mDisable ) {
447 wfProfileOut( __METHOD__ );
448 return;
449 }
450
451 list( $msg, $code ) = $this->figureMessage( $title );
452
453 $cacheKey = wfMemcKey( 'messages', $code );
454 $this->load( $code );
455 $this->lock( $cacheKey );
456
457 $titleKey = wfMemcKey( 'messages', 'individual', $title );
458
459 if ( $text === false ) {
460 # Article was deleted
461 $this->mCache[$code][$title] = '!NONEXISTENT';
462 $this->mMemc->delete( $titleKey );
463 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
464 # Check for size
465 $this->mCache[$code][$title] = '!TOO BIG';
466 $this->mMemc->set( $titleKey, ' ' . $text, $this->mExpiry );
467 } else {
468 $this->mCache[$code][$title] = ' ' . $text;
469 $this->mMemc->delete( $titleKey );
470 }
471
472 # Update caches
473 $this->saveToCaches( $this->mCache[$code], true, $code );
474 $this->unlock( $cacheKey );
475
476 // Also delete cached sidebar... just in case it is affected
477 $codes = array( $code );
478 if ( $code === 'en' ) {
479 // Delete all sidebars, like for example on action=purge on the
480 // sidebar messages
481 $codes = array_keys( Language::getLanguageNames() );
482 }
483
484 global $parserMemc;
485 foreach ( $codes as $code ) {
486 $sidebarKey = wfMemcKey( 'sidebar', $code );
487 $parserMemc->delete( $sidebarKey );
488 }
489
490 // Update the message in the message blob store
491 global $wgContLang;
492 MessageBlobStore::updateMessage( $wgContLang->lcfirst( $msg ) );
493
494 wfRunHooks( 'MessageCacheReplace', array( $title, $text ) );
495
496 wfProfileOut( __METHOD__ );
497 }
498
499 /**
500 * Shortcut to update caches.
501 *
502 * @param $cache Array: cached messages with a version.
503 * @param $memc Bool: Wether to update or not memcache.
504 * @param $code String: Language code.
505 * @return False on somekind of error.
506 */
507 protected function saveToCaches( $cache, $memc = true, $code = false ) {
508 wfProfileIn( __METHOD__ );
509 global $wgUseLocalMessageCache, $wgLocalMessageCacheSerialized;
510
511 $cacheKey = wfMemcKey( 'messages', $code );
512
513 if ( $memc ) {
514 $success = $this->mMemc->set( $cacheKey, $cache, $this->mExpiry );
515 } else {
516 $success = true;
517 }
518
519 # Save to local cache
520 if ( $wgUseLocalMessageCache ) {
521 $serialized = serialize( $cache );
522 $hash = md5( $serialized );
523 $this->mMemc->set( wfMemcKey( 'messages', $code, 'hash' ), $hash, $this->mExpiry );
524 if ($wgLocalMessageCacheSerialized) {
525 $this->saveToLocal( $serialized, $hash, $code );
526 } else {
527 $this->saveToScript( $cache, $hash, $code );
528 }
529 }
530
531 wfProfileOut( __METHOD__ );
532 return $success;
533 }
534
535 /**
536 * Represents a write lock on the messages key
537 *
538 * @return Boolean: success
539 */
540 function lock( $key ) {
541 $lockKey = $key . ':lock';
542 for ( $i = 0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
543 sleep( 1 );
544 }
545
546 return $i >= MSG_WAIT_TIMEOUT;
547 }
548
549 function unlock( $key ) {
550 $lockKey = $key . ':lock';
551 $this->mMemc->delete( $lockKey );
552 }
553
554 /**
555 * Get a message from either the content language or the user language.
556 *
557 * @param $key String: the message cache key
558 * @param $useDB Boolean: get the message from the DB, false to use only
559 * the localisation
560 * @param $langcode String: code of the language to get the message for, if
561 * it is a valid code create a language for that language,
562 * if it is a string but not a valid code then make a basic
563 * language object, if it is a false boolean then use the
564 * current users language (as a fallback for the old
565 * parameter functionality), or if it is a true boolean
566 * then use the wikis content language (also as a
567 * fallback).
568 * @param $isFullKey Boolean: specifies whether $key is a two part key
569 * "msg/lang".
570 */
571 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
572 global $wgLanguageCode, $wgContLang;
573
574 if ( !is_string( $key ) ) {
575 throw new MWException( 'Non-string key given' );
576 }
577
578 if ( strval( $key ) === '' ) {
579 # Shortcut: the empty key is always missing
580 return false;
581 }
582
583 $lang = wfGetLangObj( $langcode );
584 if ( !$lang ) {
585 throw new MWException( "Bad lang code $langcode given" );
586 }
587
588 $langcode = $lang->getCode();
589
590 $message = false;
591
592 # Normalise title-case input (with some inlining)
593 $lckey = str_replace( ' ', '_', $key );
594 if ( ord( $key ) < 128 ) {
595 $lckey[0] = strtolower( $lckey[0] );
596 $uckey = ucfirst( $lckey );
597 } else {
598 $lckey = $wgContLang->lcfirst( $lckey );
599 $uckey = $wgContLang->ucfirst( $lckey );
600 }
601
602 /**
603 * Record each message request, but only once per request.
604 * This information is not used unless $wgAdaptiveMessageCache
605 * is enabled.
606 */
607 $this->mRequestedMessages[$uckey] = true;
608
609 # Try the MediaWiki namespace
610 if( !$this->mDisable && $useDB ) {
611 $title = $uckey;
612 if( !$isFullKey && ( $langcode != $wgLanguageCode ) ) {
613 $title .= '/' . $langcode;
614 }
615 $message = $this->getMsgFromNamespace( $title, $langcode );
616 }
617
618 # Try the array in the language object
619 if ( $message === false ) {
620 $message = $lang->getMessage( $lckey );
621 if ( is_null( $message ) ) {
622 $message = false;
623 }
624 }
625
626 # Try the array of another language
627 if( $message === false ) {
628 $parts = explode( '/', $lckey );
629 # We may get calls for things that are http-urls from sidebar
630 # Let's not load nonexistent languages for those
631 # They usually have more than one slash.
632 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
633 $message = Language::getMessageFor( $parts[0], $parts[1] );
634 if ( is_null( $message ) ) {
635 $message = false;
636 }
637 }
638 }
639
640 # Is this a custom message? Try the default language in the db...
641 if( ( $message === false || $message === '-' ) &&
642 !$this->mDisable && $useDB &&
643 !$isFullKey && ( $langcode != $wgLanguageCode ) ) {
644 $message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode );
645 }
646
647 # Final fallback
648 if( $message === false ) {
649 return false;
650 }
651
652 # Fix whitespace
653 $message = strtr( $message,
654 array(
655 # Fix for trailing whitespace, removed by textarea
656 '&#32;' => ' ',
657 # Fix for NBSP, converted to space by firefox
658 '&nbsp;' => "\xc2\xa0",
659 '&#160;' => "\xc2\xa0",
660 ) );
661
662 return $message;
663 }
664
665 /**
666 * Get a message from the MediaWiki namespace, with caching. The key must
667 * first be converted to two-part lang/msg form if necessary.
668 *
669 * @param $title String: Message cache key with initial uppercase letter.
670 * @param $code String: code denoting the language to try.
671 */
672 function getMsgFromNamespace( $title, $code ) {
673 global $wgAdaptiveMessageCache;
674
675 $this->load( $code );
676 if ( isset( $this->mCache[$code][$title] ) ) {
677 $entry = $this->mCache[$code][$title];
678 if ( substr( $entry, 0, 1 ) === ' ' ) {
679 return substr( $entry, 1 );
680 } elseif ( $entry === '!NONEXISTENT' ) {
681 return false;
682 } elseif( $entry === '!TOO BIG' ) {
683 // Fall through and try invididual message cache below
684 }
685 } else {
686 // XXX: This is not cached in process cache, should it?
687 $message = false;
688 wfRunHooks( 'MessagesPreLoad', array( $title, &$message ) );
689 if ( $message !== false ) {
690 return $message;
691 }
692
693 /**
694 * If message cache is in normal mode, it is guaranteed
695 * (except bugs) that there is always entry (or placeholder)
696 * in the cache if message exists. Thus we can do minor
697 * performance improvement and return false early.
698 */
699 if ( !$wgAdaptiveMessageCache ) {
700 return false;
701 }
702 }
703
704 # Try the individual message cache
705 $titleKey = wfMemcKey( 'messages', 'individual', $title );
706 $entry = $this->mMemc->get( $titleKey );
707 if ( $entry ) {
708 if ( substr( $entry, 0, 1 ) === ' ' ) {
709 $this->mCache[$code][$title] = $entry;
710 return substr( $entry, 1 );
711 } elseif ( $entry === '!NONEXISTENT' ) {
712 $this->mCache[$code][$title] = '!NONEXISTENT';
713 return false;
714 } else {
715 # Corrupt/obsolete entry, delete it
716 $this->mMemc->delete( $titleKey );
717 }
718 }
719
720 # Try loading it from the database
721 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
722 if ( $revision ) {
723 $message = $revision->getText();
724 $this->mCache[$code][$title] = ' ' . $message;
725 $this->mMemc->set( $titleKey, ' ' . $message, $this->mExpiry );
726 } else {
727 $message = false;
728 $this->mCache[$code][$title] = '!NONEXISTENT';
729 $this->mMemc->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
730 }
731
732 return $message;
733 }
734
735 function transform( $message, $interface = false, $language = null, $title = null ) {
736 // Avoid creating parser if nothing to transform
737 if( strpos( $message, '{{' ) === false ) {
738 return $message;
739 }
740
741 $parser = $this->getParser();
742 if ( $parser ) {
743 $popts = $this->getParserOptions();
744 $popts->setInterfaceMessage( $interface );
745 $popts->setTargetLanguage( $language );
746 $popts->setUserLang( $language );
747 $message = $parser->transformMsg( $message, $popts, $title );
748 }
749 return $message;
750 }
751
752 /**
753 * @return Parser
754 */
755 function getParser() {
756 global $wgParser, $wgParserConf;
757 if ( !$this->mParser && isset( $wgParser ) ) {
758 # Do some initialisation so that we don't have to do it twice
759 $wgParser->firstCallInit();
760 # Clone it and store it
761 $class = $wgParserConf['class'];
762 if ( $class == 'Parser_DiffTest' ) {
763 # Uncloneable
764 $this->mParser = new $class( $wgParserConf );
765 } else {
766 $this->mParser = clone $wgParser;
767 }
768 }
769 return $this->mParser;
770 }
771
772 /**
773 * @param $text string
774 * @param $title
775 * @param $linestart bool
776 * @return ParserOutput
777 */
778 public function parse( $text, $title = null, $linestart = true, $interface = false, $language = null ) {
779 $parser = $this->getParser();
780 $popts = $this->getParserOptions();
781
782 if ( $interface ) {
783 $popts->setInterfaceMessage( true );
784 }
785 if ( $language !== null ) {
786 $popts->setTargetLanguage( $language );
787 }
788
789 return $parser->parse( $text, $title, $popts, $linestart );
790 }
791
792 function disable() {
793 $this->mDisable = true;
794 }
795
796 function enable() {
797 $this->mDisable = false;
798 }
799
800 /**
801 * Clear all stored messages. Mainly used after a mass rebuild.
802 */
803 function clear() {
804 $langs = Language::getLanguageNames( false );
805 foreach ( array_keys($langs) as $code ) {
806 # Global cache
807 $this->mMemc->delete( wfMemcKey( 'messages', $code ) );
808 # Invalidate all local caches
809 $this->mMemc->delete( wfMemcKey( 'messages', $code, 'hash' ) );
810 }
811 $this->mLoadedLanguages = array();
812 }
813
814 public function figureMessage( $key ) {
815 global $wgLanguageCode;
816 $pieces = explode( '/', $key );
817 if( count( $pieces ) < 2 ) {
818 return array( $key, $wgLanguageCode );
819 }
820
821 $lang = array_pop( $pieces );
822 $validCodes = Language::getLanguageNames();
823 if( !array_key_exists( $lang, $validCodes ) ) {
824 return array( $key, $wgLanguageCode );
825 }
826
827 $message = implode( '/', $pieces );
828 return array( $message, $lang );
829 }
830
831 public static function logMessages() {
832 wfProfileIn( __METHOD__ );
833 global $wgAdaptiveMessageCache;
834 if ( !$wgAdaptiveMessageCache || !self::$instance instanceof MessageCache ) {
835 wfProfileOut( __METHOD__ );
836 return;
837 }
838
839 $cachekey = wfMemckey( 'message-profiling' );
840 $cache = wfGetCache( CACHE_DB );
841 $data = $cache->get( $cachekey );
842
843 if ( !$data ) {
844 $data = array();
845 }
846
847 $age = self::$mAdaptiveDataAge;
848 $filterDate = substr( wfTimestamp( TS_MW, time() - $age ), 0, 8 );
849 foreach ( array_keys( $data ) as $key ) {
850 if ( $key < $filterDate ) {
851 unset( $data[$key] );
852 }
853 }
854
855 $index = substr( wfTimestampNow(), 0, 8 );
856 if ( !isset( $data[$index] ) ) {
857 $data[$index] = array();
858 }
859
860 foreach ( self::$instance->mRequestedMessages as $message => $_ ) {
861 if ( !isset( $data[$index][$message] ) ) {
862 $data[$index][$message] = 0;
863 }
864 $data[$index][$message]++;
865 }
866
867 $cache->set( $cachekey, $data );
868 wfProfileOut( __METHOD__ );
869 }
870
871 public function getMostUsedMessages() {
872 wfProfileIn( __METHOD__ );
873 $cachekey = wfMemcKey( 'message-profiling' );
874 $cache = wfGetCache( CACHE_DB );
875 $data = $cache->get( $cachekey );
876 if ( !$data ) {
877 wfProfileOut( __METHOD__ );
878 return array();
879 }
880
881 $list = array();
882
883 foreach( $data as $messages ) {
884 foreach( $messages as $message => $count ) {
885 $key = $message;
886 if ( !isset( $list[$key] ) ) {
887 $list[$key] = 0;
888 }
889 $list[$key] += $count;
890 }
891 }
892
893 $max = max( $list );
894 foreach ( $list as $message => $count ) {
895 if ( $count < intval( $max * self::$mAdaptiveInclusionThreshold ) ) {
896 unset( $list[$message] );
897 }
898 }
899
900 wfProfileOut( __METHOD__ );
901 return array_keys( $list );
902 }
903
904 }