3 * Localisation messages cache.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
27 define( 'MSG_LOAD_TIMEOUT', 60 );
28 define( 'MSG_LOCK_TIMEOUT', 10 );
29 define( 'MSG_WAIT_TIMEOUT', 10 );
30 define( 'MSG_CACHE_VERSION', 1 );
34 * Performs various MediaWiki namespace-related functions
39 * Process local cache of loaded messages that are defined in
40 * MediaWiki namespace. First array level is a language code,
41 * second level is message key and the values are either message
42 * content prefixed with space, or !NONEXISTENT for negative
47 // Should mean that database cannot be used, but check
50 /// Lifetime for cache, used by object caching
54 * Message cache has it's own parser which it uses to transform
57 protected $mParserOptions, $mParser;
59 /// Variable for tracking which variables are already loaded
60 protected $mLoadedLanguages = array();
63 * Used for automatic detection of most used messages.
65 protected $mRequestedMessages = array();
68 * How long the message request counts are stored. Longer period gives
69 * better sample, but also takes longer to adapt changes. The counts
70 * are aggregrated per day, regardless of the value of this variable.
72 protected static $mAdaptiveDataAge = 604800; // Is 7*24*3600
75 * Filter the tail of less used messages that are requested more seldom
76 * than this factor times the number of request of most requested message.
77 * These messages are not loaded in the default set, but are still cached
78 * individually on demand with the normal cache expiry time.
80 protected static $mAdaptiveInclusionThreshold = 0.05;
87 private static $instance;
92 protected $mInParser = false;
95 * Get the signleton instance of this class
98 * @return MessageCache object
100 public static function singleton() {
101 if ( is_null( self
::$instance ) ) {
102 global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
103 self
::$instance = new self( wfGetMessageCacheStorage(), $wgUseDatabaseMessages, $wgMsgCacheExpiry );
105 return self
::$instance;
109 * Destroy the singleton instance
113 public static function destroyInstance() {
114 self
::$instance = null;
117 function __construct( $memCached, $useDB, $expiry ) {
119 $memCached = wfGetCache( CACHE_NONE
);
122 $this->mMemc
= $memCached;
123 $this->mDisable
= !$useDB;
124 $this->mExpiry
= $expiry;
128 * ParserOptions is lazy initialised.
130 * @return ParserOptions
132 function getParserOptions() {
133 if ( !$this->mParserOptions
) {
134 $this->mParserOptions
= new ParserOptions
;
136 return $this->mParserOptions
;
140 * Try to load the cache from a local file.
141 * Actual format of the file depends on the $wgLocalMessageCacheSerialized
144 * @param $hash String: the hash of contents, to check validity.
145 * @param $code Mixed: Optional language code, see documenation of load().
146 * @return bool on failure.
148 function loadFromLocal( $hash, $code ) {
149 global $wgCacheDirectory, $wgLocalMessageCacheSerialized;
151 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
153 # Check file existence
154 wfSuppressWarnings();
155 $file = fopen( $filename, 'r' );
158 return false; // No cache file
161 if ( $wgLocalMessageCacheSerialized ) {
162 // Check to see if the file has the hash specified
163 $localHash = fread( $file, 32 );
164 if ( $hash === $localHash ) {
165 // All good, get the rest of it
167 while ( !feof( $file ) ) {
168 $serialized .= fread( $file, 100000 );
171 return $this->setCache( unserialize( $serialized ), $code );
174 return false; // Wrong hash
177 $localHash = substr( fread( $file, 40 ), 8 );
179 if ( $hash != $localHash ) {
180 return false; // Wrong hash
183 # Require overwrites the member variable or just shadows it?
184 require( $filename );
185 return $this->setCache( $this->mCache
, $code );
190 * Save the cache to a local file.
192 function saveToLocal( $serialized, $hash, $code ) {
193 global $wgCacheDirectory;
195 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
196 wfMkdirParents( $wgCacheDirectory, null, __METHOD__
); // might fail
198 wfSuppressWarnings();
199 $file = fopen( $filename, 'w' );
203 wfDebug( "Unable to open local cache file for writing\n" );
207 fwrite( $file, $hash . $serialized );
209 wfSuppressWarnings();
210 chmod( $filename, 0666 );
214 function saveToScript( $array, $hash, $code ) {
215 global $wgCacheDirectory;
217 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
218 $tempFilename = $filename . '.tmp';
219 wfMkdirParents( $wgCacheDirectory, null, __METHOD__
); // might fail
221 wfSuppressWarnings();
222 $file = fopen( $tempFilename, 'w' );
226 wfDebug( "Unable to open local cache file for writing\n" );
230 fwrite( $file, "<?php\n//$hash\n\n \$this->mCache = array(" );
232 foreach ( $array as $key => $message ) {
233 $key = $this->escapeForScript( $key );
234 $message = $this->escapeForScript( $message );
235 fwrite( $file, "'$key' => '$message',\n" );
238 fwrite( $file, ");\n?>" );
240 rename( $tempFilename, $filename );
243 function escapeForScript( $string ) {
244 $string = str_replace( '\\', '\\\\', $string );
245 $string = str_replace( '\'', '\\\'', $string );
250 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
254 function setCache( $cache, $code ) {
255 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION
) {
256 $this->mCache
[$code] = $cache;
264 * Loads messages from caches or from database in this order:
265 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
267 * (3) from the database.
269 * When succesfully loading from (2) or (3), all higher level caches are
270 * updated for the newest version.
272 * Nothing is loaded if member variable mDisable is true, either manually
273 * set by calling code or if message loading fails (is this possible?).
275 * Returns true if cache is already populated or it was succesfully populated,
276 * or false if populating empty cache fails. Also returns true if MessageCache
279 * @param $code String: language to which load messages
282 function load( $code = false ) {
283 global $wgUseLocalMessageCache;
285 if( !is_string( $code ) ) {
286 # This isn't really nice, so at least make a note about it and try to
288 wfDebug( __METHOD__
. " called without providing a language code\n" );
292 # Don't do double loading...
293 if ( isset( $this->mLoadedLanguages
[$code] ) ) {
297 # 8 lines of code just to say (once) that message cache is disabled
298 if ( $this->mDisable
) {
299 static $shownDisabled = false;
300 if ( !$shownDisabled ) {
301 wfDebug( __METHOD__
. ": disabled\n" );
302 $shownDisabled = true;
307 # Loading code starts
308 wfProfileIn( __METHOD__
);
309 $success = false; # Keep track of success
310 $where = array(); # Debug info, delayed to avoid spamming debug log too much
311 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
314 # Hash of the contents is stored in memcache, to detect if local cache goes
315 # out of date (due to update in other thread?)
316 if ( $wgUseLocalMessageCache ) {
317 wfProfileIn( __METHOD__
. '-fromlocal' );
319 $hash = $this->mMemc
->get( wfMemcKey( 'messages', $code, 'hash' ) );
321 $success = $this->loadFromLocal( $hash, $code );
322 if ( $success ) $where[] = 'got from local cache';
324 wfProfileOut( __METHOD__
. '-fromlocal' );
328 # Fails if nothing in cache, or in the wrong version.
330 wfProfileIn( __METHOD__
. '-fromcache' );
331 $cache = $this->mMemc
->get( $cacheKey );
332 $success = $this->setCache( $cache, $code );
334 $where[] = 'got from global cache';
335 $this->saveToCaches( $cache, false, $code );
337 wfProfileOut( __METHOD__
. '-fromcache' );
341 # Nothing in caches... so we need create one and store it in caches
343 $where[] = 'cache is empty';
344 $where[] = 'loading from database';
346 $this->lock( $cacheKey );
348 # Limit the concurrency of loadFromDB to a single process
349 # This prevents the site from going down when the cache expires
350 $statusKey = wfMemcKey( 'messages', $code, 'status' );
351 $success = $this->mMemc
->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT
);
353 $cache = $this->loadFromDB( $code );
354 $success = $this->setCache( $cache, $code );
357 $success = $this->saveToCaches( $cache, true, $code );
359 $this->mMemc
->delete( $statusKey );
361 $this->mMemc
->set( $statusKey, 'error', 60 * 5 );
362 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
365 $this->unlock($cacheKey);
369 # Bad luck... this should not happen
370 $where[] = 'loading FAILED - cache is disabled';
371 $info = implode( ', ', $where );
372 wfDebug( __METHOD__
. ": Loading $code... $info\n" );
373 $this->mDisable
= true;
374 $this->mCache
= false;
376 # All good, just record the success
377 $info = implode( ', ', $where );
378 wfDebug( __METHOD__
. ": Loading $code... $info\n" );
379 $this->mLoadedLanguages
[$code] = true;
381 wfProfileOut( __METHOD__
);
386 * Loads cacheable messages from the database. Messages bigger than
387 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
388 * on-demand from the database later.
390 * @param $code String: language code.
391 * @return Array: loaded messages for storing in caches.
393 function loadFromDB( $code ) {
394 wfProfileIn( __METHOD__
);
395 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
396 $dbr = wfGetDB( DB_SLAVE
);
401 'page_is_redirect' => 0,
402 'page_namespace' => NS_MEDIAWIKI
,
406 if ( $wgAdaptiveMessageCache ) {
407 $mostused = $this->getMostUsedMessages();
408 if ( $code !== $wgLanguageCode ) {
409 foreach ( $mostused as $key => $value ) {
410 $mostused[$key] = "$value/$code";
415 if ( count( $mostused ) ) {
416 $conds['page_title'] = $mostused;
417 } elseif ( $code !== $wgLanguageCode ) {
418 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), "/$code" );
420 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
421 # other than language code.
422 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
425 # Conditions to fetch oversized pages to ignore them
427 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
429 # Load titles for all oversized pages in the MediaWiki namespace
430 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__
. "($code)-big" );
431 foreach ( $res as $row ) {
432 $cache[$row->page_title
] = '!TOO BIG';
435 # Conditions to load the remaining pages with their contents
436 $smallConds = $conds;
437 $smallConds[] = 'page_latest=rev_id';
438 $smallConds[] = 'rev_text_id=old_id';
439 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
442 array( 'page', 'revision', 'text' ),
443 array( 'page_title', 'old_text', 'old_flags' ),
445 __METHOD__
. "($code)-small"
448 foreach ( $res as $row ) {
449 $text = Revision
::getRevisionText( $row );
450 if( $text === false ) {
451 // Failed to fetch data; possible ES errors?
452 // Store a marker to fetch on-demand as a workaround...
454 wfDebugLog( 'MessageCache', __METHOD__
. ": failed to load message page text for {$row->page_title} ($code)" );
456 $entry = ' ' . $text;
458 $cache[$row->page_title
] = $entry;
461 foreach ( $mostused as $key ) {
462 if ( !isset( $cache[$key] ) ) {
463 $cache[$key] = '!NONEXISTENT';
467 $cache['VERSION'] = MSG_CACHE_VERSION
;
468 wfProfileOut( __METHOD__
);
473 * Updates cache as necessary when message page is changed
475 * @param $title String: name of the page changed.
476 * @param $text Mixed: new contents of the page.
478 public function replace( $title, $text ) {
479 global $wgMaxMsgCacheEntrySize;
480 wfProfileIn( __METHOD__
);
482 if ( $this->mDisable
) {
483 wfProfileOut( __METHOD__
);
487 list( $msg, $code ) = $this->figureMessage( $title );
489 $cacheKey = wfMemcKey( 'messages', $code );
490 $this->load( $code );
491 $this->lock( $cacheKey );
493 $titleKey = wfMemcKey( 'messages', 'individual', $title );
495 if ( $text === false ) {
496 # Article was deleted
497 $this->mCache
[$code][$title] = '!NONEXISTENT';
498 $this->mMemc
->delete( $titleKey );
499 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
501 $this->mCache
[$code][$title] = '!TOO BIG';
502 $this->mMemc
->set( $titleKey, ' ' . $text, $this->mExpiry
);
504 $this->mCache
[$code][$title] = ' ' . $text;
505 $this->mMemc
->delete( $titleKey );
509 $this->saveToCaches( $this->mCache
[$code], true, $code );
510 $this->unlock( $cacheKey );
512 // Also delete cached sidebar... just in case it is affected
513 $codes = array( $code );
514 if ( $code === 'en' ) {
515 // Delete all sidebars, like for example on action=purge on the
517 $codes = array_keys( Language
::fetchLanguageNames() );
521 foreach ( $codes as $code ) {
522 $sidebarKey = wfMemcKey( 'sidebar', $code );
523 $wgMemc->delete( $sidebarKey );
526 // Update the message in the message blob store
528 MessageBlobStore
::updateMessage( $wgContLang->lcfirst( $msg ) );
530 wfRunHooks( 'MessageCacheReplace', array( $title, $text ) );
532 wfProfileOut( __METHOD__
);
536 * Shortcut to update caches.
538 * @param $cache Array: cached messages with a version.
539 * @param $memc Bool: Wether to update or not memcache.
540 * @param $code String: Language code.
541 * @return bool on somekind of error.
543 protected function saveToCaches( $cache, $memc = true, $code = false ) {
544 wfProfileIn( __METHOD__
);
545 global $wgUseLocalMessageCache, $wgLocalMessageCacheSerialized;
547 $cacheKey = wfMemcKey( 'messages', $code );
550 $success = $this->mMemc
->set( $cacheKey, $cache, $this->mExpiry
);
555 # Save to local cache
556 if ( $wgUseLocalMessageCache ) {
557 $serialized = serialize( $cache );
558 $hash = md5( $serialized );
559 $this->mMemc
->set( wfMemcKey( 'messages', $code, 'hash' ), $hash, $this->mExpiry
);
560 if ($wgLocalMessageCacheSerialized) {
561 $this->saveToLocal( $serialized, $hash, $code );
563 $this->saveToScript( $cache, $hash, $code );
567 wfProfileOut( __METHOD__
);
572 * Represents a write lock on the messages key
576 * @return Boolean: success
578 function lock( $key ) {
579 $lockKey = $key . ':lock';
580 for ( $i = 0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
584 return $i >= MSG_WAIT_TIMEOUT
;
587 function unlock( $key ) {
588 $lockKey = $key . ':lock';
589 $this->mMemc
->delete( $lockKey );
593 * Get a message from either the content language or the user language.
595 * @param $key String: the message cache key
596 * @param $useDB Boolean: get the message from the DB, false to use only
598 * @param $langcode String: code of the language to get the message for, if
599 * it is a valid code create a language for that language,
600 * if it is a string but not a valid code then make a basic
601 * language object, if it is a false boolean then use the
602 * current users language (as a fallback for the old
603 * parameter functionality), or if it is a true boolean
604 * then use the wikis content language (also as a
606 * @param $isFullKey Boolean: specifies whether $key is a two part key
609 * @return string|bool
611 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
612 global $wgLanguageCode, $wgContLang;
614 if ( is_int( $key ) ) {
615 // "Non-string key given" exception sometimes happens for numerical strings that become ints somewhere on their way here
616 $key = strval( $key );
619 if ( !is_string( $key ) ) {
620 throw new MWException( 'Non-string key given' );
623 if ( strval( $key ) === '' ) {
624 # Shortcut: the empty key is always missing
628 $lang = wfGetLangObj( $langcode );
630 throw new MWException( "Bad lang code $langcode given" );
633 $langcode = $lang->getCode();
637 # Normalise title-case input (with some inlining)
638 $lckey = str_replace( ' ', '_', $key );
639 if ( ord( $key ) < 128 ) {
640 $lckey[0] = strtolower( $lckey[0] );
641 $uckey = ucfirst( $lckey );
643 $lckey = $wgContLang->lcfirst( $lckey );
644 $uckey = $wgContLang->ucfirst( $lckey );
648 * Record each message request, but only once per request.
649 * This information is not used unless $wgAdaptiveMessageCache
652 $this->mRequestedMessages
[$uckey] = true;
654 # Try the MediaWiki namespace
655 if( !$this->mDisable
&& $useDB ) {
657 if( !$isFullKey && ( $langcode != $wgLanguageCode ) ) {
658 $title .= '/' . $langcode;
660 $message = $this->getMsgFromNamespace( $title, $langcode );
663 # Try the array in the language object
664 if ( $message === false ) {
665 $message = $lang->getMessage( $lckey );
666 if ( is_null( $message ) ) {
671 # Try the array of another language
672 if( $message === false ) {
673 $parts = explode( '/', $lckey );
674 # We may get calls for things that are http-urls from sidebar
675 # Let's not load nonexistent languages for those
676 # They usually have more than one slash.
677 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
678 $message = Language
::getMessageFor( $parts[0], $parts[1] );
679 if ( is_null( $message ) ) {
685 # Is this a custom message? Try the default language in the db...
686 if( ( $message === false ||
$message === '-' ) &&
687 !$this->mDisable
&& $useDB &&
688 !$isFullKey && ( $langcode != $wgLanguageCode ) ) {
689 $message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode );
693 if( $message === false ) {
698 $message = strtr( $message,
700 # Fix for trailing whitespace, removed by textarea
702 # Fix for NBSP, converted to space by firefox
703 ' ' => "\xc2\xa0",
704 ' ' => "\xc2\xa0",
711 * Get a message from the MediaWiki namespace, with caching. The key must
712 * first be converted to two-part lang/msg form if necessary.
714 * @param $title String: Message cache key with initial uppercase letter.
715 * @param $code String: code denoting the language to try.
717 * @return string|bool False on failure
719 function getMsgFromNamespace( $title, $code ) {
720 global $wgAdaptiveMessageCache;
722 $this->load( $code );
723 if ( isset( $this->mCache
[$code][$title] ) ) {
724 $entry = $this->mCache
[$code][$title];
725 if ( substr( $entry, 0, 1 ) === ' ' ) {
726 return substr( $entry, 1 );
727 } elseif ( $entry === '!NONEXISTENT' ) {
729 } elseif( $entry === '!TOO BIG' ) {
730 // Fall through and try invididual message cache below
733 // XXX: This is not cached in process cache, should it?
735 wfRunHooks( 'MessagesPreLoad', array( $title, &$message ) );
736 if ( $message !== false ) {
741 * If message cache is in normal mode, it is guaranteed
742 * (except bugs) that there is always entry (or placeholder)
743 * in the cache if message exists. Thus we can do minor
744 * performance improvement and return false early.
746 if ( !$wgAdaptiveMessageCache ) {
751 # Try the individual message cache
752 $titleKey = wfMemcKey( 'messages', 'individual', $title );
753 $entry = $this->mMemc
->get( $titleKey );
755 if ( substr( $entry, 0, 1 ) === ' ' ) {
756 $this->mCache
[$code][$title] = $entry;
757 return substr( $entry, 1 );
758 } elseif ( $entry === '!NONEXISTENT' ) {
759 $this->mCache
[$code][$title] = '!NONEXISTENT';
762 # Corrupt/obsolete entry, delete it
763 $this->mMemc
->delete( $titleKey );
767 # Try loading it from the database
768 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
770 $message = $revision->getText();
771 if ($message === false) {
772 // A possibly temporary loading failure.
773 wfDebugLog( 'MessageCache', __METHOD__
. ": failed to load message page text for {$title} ($code)" );
775 $this->mCache
[$code][$title] = ' ' . $message;
776 $this->mMemc
->set( $titleKey, ' ' . $message, $this->mExpiry
);
780 $this->mCache
[$code][$title] = '!NONEXISTENT';
781 $this->mMemc
->set( $titleKey, '!NONEXISTENT', $this->mExpiry
);
788 * @param $message string
789 * @param $interface bool
791 * @param $title Title
794 function transform( $message, $interface = false, $language = null, $title = null ) {
795 // Avoid creating parser if nothing to transform
796 if( strpos( $message, '{{' ) === false ) {
800 if ( $this->mInParser
) {
804 $parser = $this->getParser();
806 $popts = $this->getParserOptions();
807 $popts->setInterfaceMessage( $interface );
808 $popts->setTargetLanguage( $language );
810 $userlang = $popts->setUserLang( $language );
811 $this->mInParser
= true;
812 $message = $parser->transformMsg( $message, $popts, $title );
813 $this->mInParser
= false;
814 $popts->setUserLang( $userlang );
822 function getParser() {
823 global $wgParser, $wgParserConf;
824 if ( !$this->mParser
&& isset( $wgParser ) ) {
825 # Do some initialisation so that we don't have to do it twice
826 $wgParser->firstCallInit();
827 # Clone it and store it
828 $class = $wgParserConf['class'];
829 if ( $class == 'Parser_DiffTest' ) {
831 $this->mParser
= new $class( $wgParserConf );
833 $this->mParser
= clone $wgParser;
836 return $this->mParser
;
840 * @param $text string
841 * @param $title Title
842 * @param $linestart bool
843 * @param $interface bool
845 * @return ParserOutput
847 public function parse( $text, $title = null, $linestart = true, $interface = false, $language = null ) {
848 if ( $this->mInParser
) {
849 return htmlspecialchars( $text );
852 $parser = $this->getParser();
853 $popts = $this->getParserOptions();
854 $popts->setInterfaceMessage( $interface );
855 $popts->setTargetLanguage( $language );
857 wfProfileIn( __METHOD__
);
858 if ( !$title ||
!$title instanceof Title
) {
862 // Sometimes $wgTitle isn't set either...
864 # It's not uncommon having a null $wgTitle in scripts. See r80898
865 # Create a ghost title in such case
866 $title = Title
::newFromText( 'Dwimmerlaik' );
869 $this->mInParser
= true;
870 $res = $parser->parse( $text, $title, $popts, $linestart );
871 $this->mInParser
= false;
873 wfProfileOut( __METHOD__
);
878 $this->mDisable
= true;
882 $this->mDisable
= false;
886 * Clear all stored messages. Mainly used after a mass rebuild.
889 $langs = Language
::fetchLanguageNames( null, 'mw' );
890 foreach ( array_keys($langs) as $code ) {
892 $this->mMemc
->delete( wfMemcKey( 'messages', $code ) );
893 # Invalidate all local caches
894 $this->mMemc
->delete( wfMemcKey( 'messages', $code, 'hash' ) );
896 $this->mLoadedLanguages
= array();
903 public function figureMessage( $key ) {
904 global $wgLanguageCode;
905 $pieces = explode( '/', $key );
906 if( count( $pieces ) < 2 ) {
907 return array( $key, $wgLanguageCode );
910 $lang = array_pop( $pieces );
911 if( !Language
::fetchLanguageName( $lang, null, 'mw' ) ) {
912 return array( $key, $wgLanguageCode );
915 $message = implode( '/', $pieces );
916 return array( $message, $lang );
919 public static function logMessages() {
920 wfProfileIn( __METHOD__
);
921 global $wgAdaptiveMessageCache;
922 if ( !$wgAdaptiveMessageCache ||
!self
::$instance instanceof MessageCache
) {
923 wfProfileOut( __METHOD__
);
927 $cachekey = wfMemckey( 'message-profiling' );
928 $cache = wfGetCache( CACHE_DB
);
929 $data = $cache->get( $cachekey );
935 $age = self
::$mAdaptiveDataAge;
936 $filterDate = substr( wfTimestamp( TS_MW
, time() - $age ), 0, 8 );
937 foreach ( array_keys( $data ) as $key ) {
938 if ( $key < $filterDate ) {
939 unset( $data[$key] );
943 $index = substr( wfTimestampNow(), 0, 8 );
944 if ( !isset( $data[$index] ) ) {
945 $data[$index] = array();
948 foreach ( self
::$instance->mRequestedMessages
as $message => $_ ) {
949 if ( !isset( $data[$index][$message] ) ) {
950 $data[$index][$message] = 0;
952 $data[$index][$message]++
;
955 $cache->set( $cachekey, $data );
956 wfProfileOut( __METHOD__
);
962 public function getMostUsedMessages() {
963 wfProfileIn( __METHOD__
);
964 $cachekey = wfMemcKey( 'message-profiling' );
965 $cache = wfGetCache( CACHE_DB
);
966 $data = $cache->get( $cachekey );
968 wfProfileOut( __METHOD__
);
974 foreach( $data as $messages ) {
975 foreach( $messages as $message => $count ) {
977 if ( !isset( $list[$key] ) ) {
980 $list[$key] +
= $count;
985 foreach ( $list as $message => $count ) {
986 if ( $count < intval( $max * self
::$mAdaptiveInclusionThreshold ) ) {
987 unset( $list[$message] );
991 wfProfileOut( __METHOD__
);
992 return array_keys( $list );
996 * Get all message keys stored in the message cache for a given language.
997 * If $code is the content language code, this will return all message keys
998 * for which MediaWiki:msgkey exists. If $code is another language code, this
999 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
1000 * @param $code string
1001 * @return array of message keys (strings)
1003 public function getAllMessageKeys( $code ) {
1005 $this->load( $code );
1006 if ( !isset( $this->mCache
[$code] ) ) {
1007 // Apparently load() failed
1010 $cache = $this->mCache
[$code]; // Copy the cache
1011 unset( $cache['VERSION'] ); // Remove the VERSION key
1012 $cache = array_diff( $cache, array( '!NONEXISTENT' ) ); // Remove any !NONEXISTENT keys
1013 // Keys may appear with a capital first letter. lcfirst them.
1014 return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) );