c5d6ac0975e89c184279322611cb318f45b34d69
10 define( 'MSG_LOAD_TIMEOUT', 60 );
11 define( 'MSG_LOCK_TIMEOUT', 10 );
12 define( 'MSG_WAIT_TIMEOUT', 10 );
13 define( 'MSG_CACHE_VERSION', 1 );
17 * Performs various MediaWiki namespace-related functions
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
30 // Should mean that database cannot be used, but check
33 /// Lifetime for cache, used by object caching
37 * Message cache has it's own parser which it uses to transform
40 protected $mParserOptions, $mParser;
42 /// Variable for tracking which variables are already loaded
43 protected $mLoadedLanguages = array();
46 * Used for automatic detection of most used messages.
48 protected $mRequestedMessages = array();
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.
55 protected static $mAdaptiveDataAge = 604800; // Is 7*24*3600
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.
63 protected static $mAdaptiveInclusionThreshold = 0.05;
68 private static $instance;
71 * Get the signleton instance of this class
74 * @return MessageCache object
76 public static function singleton() {
77 if ( is_null( self
::$instance ) ) {
78 global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
79 self
::$instance = new self( wfGetMessageCacheStorage(), $wgUseDatabaseMessages, $wgMsgCacheExpiry );
81 return self
::$instance;
85 * Destroy the singleton instance
89 public static function destroyInstance() {
90 self
::$instance = null;
93 function __construct( $memCached, $useDB, $expiry ) {
95 $memCached = wfGetCache( CACHE_NONE
);
98 $this->mMemc
= $memCached;
99 $this->mDisable
= !$useDB;
100 $this->mExpiry
= $expiry;
104 * ParserOptions is lazy initialised.
106 * @return ParserOptions
108 function getParserOptions() {
109 if ( !$this->mParserOptions
) {
110 $this->mParserOptions
= new ParserOptions
;
112 return $this->mParserOptions
;
116 * Try to load the cache from a local file.
117 * Actual format of the file depends on the $wgLocalMessageCacheSerialized
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.
124 function loadFromLocal( $hash, $code ) {
125 global $wgCacheDirectory, $wgLocalMessageCacheSerialized;
127 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
129 # Check file existence
130 wfSuppressWarnings();
131 $file = fopen( $filename, 'r' );
134 return false; // No cache file
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
143 while ( !feof( $file ) ) {
144 $serialized .= fread( $file, 100000 );
147 return $this->setCache( unserialize( $serialized ), $code );
150 return false; // Wrong hash
153 $localHash = substr( fread( $file, 40 ), 8 );
155 if ( $hash != $localHash ) {
156 return false; // Wrong hash
159 # Require overwrites the member variable or just shadows it?
160 require( $filename );
161 return $this->setCache( $this->mCache
, $code );
166 * Save the cache to a local file.
168 function saveToLocal( $serialized, $hash, $code ) {
169 global $wgCacheDirectory;
171 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
172 wfMkdirParents( $wgCacheDirectory ); // might fail
174 wfSuppressWarnings();
175 $file = fopen( $filename, 'w' );
179 wfDebug( "Unable to open local cache file for writing\n" );
183 fwrite( $file, $hash . $serialized );
185 @chmod
( $filename, 0666 );
188 function saveToScript( $array, $hash, $code ) {
189 global $wgCacheDirectory;
191 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
192 $tempFilename = $filename . '.tmp';
193 wfMkdirParents( $wgCacheDirectory ); // might fail
195 wfSuppressWarnings();
196 $file = fopen( $tempFilename, 'w' );
200 wfDebug( "Unable to open local cache file for writing\n" );
204 fwrite( $file, "<?php\n//$hash\n\n \$this->mCache = array(" );
206 foreach ( $array as $key => $message ) {
207 $key = $this->escapeForScript( $key );
208 $message = $this->escapeForScript( $message );
209 fwrite( $file, "'$key' => '$message',\n" );
212 fwrite( $file, ");\n?>" );
214 rename( $tempFilename, $filename );
217 function escapeForScript( $string ) {
218 $string = str_replace( '\\', '\\\\', $string );
219 $string = str_replace( '\'', '\\\'', $string );
224 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
228 function setCache( $cache, $code ) {
229 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION
) {
230 $this->mCache
[$code] = $cache;
238 * Loads messages from caches or from database in this order:
239 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
241 * (3) from the database.
243 * When succesfully loading from (2) or (3), all higher level caches are
244 * updated for the newest version.
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?).
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
253 * @param $code String: language to which load messages
255 function load( $code = false ) {
256 global $wgUseLocalMessageCache;
258 if( !is_string( $code ) ) {
259 # This isn't really nice, so at least make a note about it and try to
261 wfDebug( __METHOD__
. " called without providing a language code\n" );
265 # Don't do double loading...
266 if ( isset( $this->mLoadedLanguages
[$code] ) ) {
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;
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
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' );
292 $hash = $this->mMemc
->get( wfMemcKey( 'messages', $code, 'hash' ) );
294 $success = $this->loadFromLocal( $hash, $code );
295 if ( $success ) $where[] = 'got from local cache';
297 wfProfileOut( __METHOD__
. '-fromlocal' );
301 # Fails if nothing in cache, or in the wrong version.
303 wfProfileIn( __METHOD__
. '-fromcache' );
304 $cache = $this->mMemc
->get( $cacheKey );
305 $success = $this->setCache( $cache, $code );
307 $where[] = 'got from global cache';
308 $this->saveToCaches( $cache, false, $code );
310 wfProfileOut( __METHOD__
. '-fromcache' );
314 # Nothing in caches... so we need create one and store it in caches
316 $where[] = 'cache is empty';
317 $where[] = 'loading from database';
319 $this->lock( $cacheKey );
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
);
326 $cache = $this->loadFromDB( $code );
327 $success = $this->setCache( $cache, $code );
330 $success = $this->saveToCaches( $cache, true, $code );
332 $this->mMemc
->delete( $statusKey );
334 $this->mMemc
->set( $statusKey, 'error', 60 * 5 );
335 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
338 $this->unlock($cacheKey);
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;
349 # All good, just record the success
350 $info = implode( ', ', $where );
351 wfDebug( __METHOD__
. ": Loading $code... $info\n" );
352 $this->mLoadedLanguages
[$code] = true;
354 wfProfileOut( __METHOD__
);
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.
363 * @param $code String: language code.
364 * @return Array: loaded messages for storing in caches.
366 function loadFromDB( $code ) {
367 wfProfileIn( __METHOD__
);
368 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
369 $dbr = wfGetDB( DB_SLAVE
);
374 'page_is_redirect' => 0,
375 'page_namespace' => NS_MEDIAWIKI
,
379 if ( $wgAdaptiveMessageCache ) {
380 $mostused = $this->getMostUsedMessages();
381 if ( $code !== $wgLanguageCode ) {
382 foreach ( $mostused as $key => $value ) {
383 $mostused[$key] = "$value/$code";
388 if ( count( $mostused ) ) {
389 $conds['page_title'] = $mostused;
390 } elseif ( $code !== $wgLanguageCode ) {
391 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), "/$code" );
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() );
398 # Conditions to fetch oversized pages to ignore them
400 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
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';
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 );
415 array( 'page', 'revision', 'text' ),
416 array( 'page_title', 'old_text', 'old_flags' ),
418 __METHOD__
. "($code)-small"
421 foreach ( $res as $row ) {
422 $cache[$row->page_title
] = ' ' . Revision
::getRevisionText( $row );
425 foreach ( $mostused as $key ) {
426 if ( !isset( $cache[$key] ) ) {
427 $cache[$key] = '!NONEXISTENT';
431 $cache['VERSION'] = MSG_CACHE_VERSION
;
432 wfProfileOut( __METHOD__
);
437 * Updates cache as necessary when message page is changed
439 * @param $title String: name of the page changed.
440 * @param $text Mixed: new contents of the page.
442 public function replace( $title, $text ) {
443 global $wgMaxMsgCacheEntrySize;
444 wfProfileIn( __METHOD__
);
446 if ( $this->mDisable
) {
447 wfProfileOut( __METHOD__
);
451 list( $msg, $code ) = $this->figureMessage( $title );
453 $cacheKey = wfMemcKey( 'messages', $code );
454 $this->load( $code );
455 $this->lock( $cacheKey );
457 $titleKey = wfMemcKey( 'messages', 'individual', $title );
459 if ( $text === false ) {
460 # Article was deleted
461 $this->mCache
[$code][$title] = '!NONEXISTENT';
462 $this->mMemc
->delete( $titleKey );
463 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
465 $this->mCache
[$code][$title] = '!TOO BIG';
466 $this->mMemc
->set( $titleKey, ' ' . $text, $this->mExpiry
);
468 $this->mCache
[$code][$title] = ' ' . $text;
469 $this->mMemc
->delete( $titleKey );
473 $this->saveToCaches( $this->mCache
[$code], true, $code );
474 $this->unlock( $cacheKey );
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
481 $codes = array_keys( Language
::getLanguageNames() );
485 foreach ( $codes as $code ) {
486 $sidebarKey = wfMemcKey( 'sidebar', $code );
487 $parserMemc->delete( $sidebarKey );
490 // Update the message in the message blob store
492 MessageBlobStore
::updateMessage( $wgContLang->lcfirst( $msg ) );
494 wfRunHooks( 'MessageCacheReplace', array( $title, $text ) );
496 wfProfileOut( __METHOD__
);
500 * Shortcut to update caches.
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.
507 protected function saveToCaches( $cache, $memc = true, $code = false ) {
508 wfProfileIn( __METHOD__
);
509 global $wgUseLocalMessageCache, $wgLocalMessageCacheSerialized;
511 $cacheKey = wfMemcKey( 'messages', $code );
514 $success = $this->mMemc
->set( $cacheKey, $cache, $this->mExpiry
);
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 );
527 $this->saveToScript( $cache, $hash, $code );
531 wfProfileOut( __METHOD__
);
536 * Represents a write lock on the messages key
538 * @return Boolean: success
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++
) {
546 return $i >= MSG_WAIT_TIMEOUT
;
549 function unlock( $key ) {
550 $lockKey = $key . ':lock';
551 $this->mMemc
->delete( $lockKey );
555 * Get a message from either the content language or the user language.
557 * @param $key String: the message cache key
558 * @param $useDB Boolean: get the message from the DB, false to use only
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
568 * @param $isFullKey Boolean: specifies whether $key is a two part key
571 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
572 global $wgLanguageCode, $wgContLang;
574 if ( !is_string( $key ) ) {
575 throw new MWException( 'Non-string key given' );
578 if ( strval( $key ) === '' ) {
579 # Shortcut: the empty key is always missing
583 $lang = wfGetLangObj( $langcode );
585 throw new MWException( "Bad lang code $langcode given" );
588 $langcode = $lang->getCode();
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 );
598 $lckey = $wgContLang->lcfirst( $lckey );
599 $uckey = $wgContLang->ucfirst( $lckey );
603 * Record each message request, but only once per request.
604 * This information is not used unless $wgAdaptiveMessageCache
607 $this->mRequestedMessages
[$uckey] = true;
609 # Try the MediaWiki namespace
610 if( !$this->mDisable
&& $useDB ) {
612 if( !$isFullKey && ( $langcode != $wgLanguageCode ) ) {
613 $title .= '/' . $langcode;
615 $message = $this->getMsgFromNamespace( $title, $langcode );
618 # Try the array in the language object
619 if ( $message === false ) {
620 $message = $lang->getMessage( $lckey );
621 if ( is_null( $message ) ) {
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 ) ) {
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 );
648 if( $message === false ) {
653 $message = strtr( $message,
655 # Fix for trailing whitespace, removed by textarea
657 # Fix for NBSP, converted to space by firefox
658 ' ' => "\xc2\xa0",
659 ' ' => "\xc2\xa0",
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.
669 * @param $title String: Message cache key with initial uppercase letter.
670 * @param $code String: code denoting the language to try.
672 function getMsgFromNamespace( $title, $code ) {
673 global $wgAdaptiveMessageCache;
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' ) {
682 } elseif( $entry === '!TOO BIG' ) {
683 // Fall through and try invididual message cache below
686 // XXX: This is not cached in process cache, should it?
688 wfRunHooks( 'MessagesPreLoad', array( $title, &$message ) );
689 if ( $message !== false ) {
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.
699 if ( !$wgAdaptiveMessageCache ) {
704 # Try the individual message cache
705 $titleKey = wfMemcKey( 'messages', 'individual', $title );
706 $entry = $this->mMemc
->get( $titleKey );
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';
715 # Corrupt/obsolete entry, delete it
716 $this->mMemc
->delete( $titleKey );
720 # Try loading it from the database
721 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
723 $message = $revision->getText();
724 $this->mCache
[$code][$title] = ' ' . $message;
725 $this->mMemc
->set( $titleKey, ' ' . $message, $this->mExpiry
);
728 $this->mCache
[$code][$title] = '!NONEXISTENT';
729 $this->mMemc
->set( $titleKey, '!NONEXISTENT', $this->mExpiry
);
735 function transform( $message, $interface = false, $language = null, $title = null ) {
736 // Avoid creating parser if nothing to transform
737 if( strpos( $message, '{{' ) === false ) {
741 $parser = $this->getParser();
743 $popts = $this->getParserOptions();
744 $popts->setInterfaceMessage( $interface );
745 $popts->setTargetLanguage( $language );
746 $popts->setUserLang( $language );
747 $message = $parser->transformMsg( $message, $popts, $title );
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' ) {
764 $this->mParser
= new $class( $wgParserConf );
766 $this->mParser
= clone $wgParser;
769 return $this->mParser
;
773 * @param $text string
775 * @param $linestart bool
776 * @return ParserOutput
778 public function parse( $text, $title = null, $linestart = true, $interface = false, $language = null ) {
779 $parser = $this->getParser();
780 $popts = $this->getParserOptions();
783 $popts->setInterfaceMessage( true );
785 if ( $language !== null ) {
786 $popts->setTargetLanguage( $language );
789 return $parser->parse( $text, $title, $popts, $linestart );
793 $this->mDisable
= true;
797 $this->mDisable
= false;
801 * Clear all stored messages. Mainly used after a mass rebuild.
804 $langs = Language
::getLanguageNames( false );
805 foreach ( array_keys($langs) as $code ) {
807 $this->mMemc
->delete( wfMemcKey( 'messages', $code ) );
808 # Invalidate all local caches
809 $this->mMemc
->delete( wfMemcKey( 'messages', $code, 'hash' ) );
811 $this->mLoadedLanguages
= array();
814 public function figureMessage( $key ) {
815 global $wgLanguageCode;
816 $pieces = explode( '/', $key );
817 if( count( $pieces ) < 2 ) {
818 return array( $key, $wgLanguageCode );
821 $lang = array_pop( $pieces );
822 $validCodes = Language
::getLanguageNames();
823 if( !array_key_exists( $lang, $validCodes ) ) {
824 return array( $key, $wgLanguageCode );
827 $message = implode( '/', $pieces );
828 return array( $message, $lang );
831 public static function logMessages() {
832 wfProfileIn( __METHOD__
);
833 global $wgAdaptiveMessageCache;
834 if ( !$wgAdaptiveMessageCache ||
!self
::$instance instanceof MessageCache
) {
835 wfProfileOut( __METHOD__
);
839 $cachekey = wfMemckey( 'message-profiling' );
840 $cache = wfGetCache( CACHE_DB
);
841 $data = $cache->get( $cachekey );
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] );
855 $index = substr( wfTimestampNow(), 0, 8 );
856 if ( !isset( $data[$index] ) ) {
857 $data[$index] = array();
860 foreach ( self
::$instance->mRequestedMessages
as $message => $_ ) {
861 if ( !isset( $data[$index][$message] ) ) {
862 $data[$index][$message] = 0;
864 $data[$index][$message]++
;
867 $cache->set( $cachekey, $data );
868 wfProfileOut( __METHOD__
);
871 public function getMostUsedMessages() {
872 wfProfileIn( __METHOD__
);
873 $cachekey = wfMemcKey( 'message-profiling' );
874 $cache = wfGetCache( CACHE_DB
);
875 $data = $cache->get( $cachekey );
877 wfProfileOut( __METHOD__
);
883 foreach( $data as $messages ) {
884 foreach( $messages as $message => $count ) {
886 if ( !isset( $list[$key] ) ) {
889 $list[$key] +
= $count;
894 foreach ( $list as $message => $count ) {
895 if ( $count < intval( $max * self
::$mAdaptiveInclusionThreshold ) ) {
896 unset( $list[$message] );
900 wfProfileOut( __METHOD__
);
901 return array_keys( $list );