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
25 * MediaWiki message cache structure version.
26 * Bump this whenever the message cache format has changed.
28 define( 'MSG_CACHE_VERSION', 1 );
31 * Memcached timeout when loading a key.
32 * See MessageCache::load()
34 define( 'MSG_LOAD_TIMEOUT', 60 );
37 * Memcached timeout when locking a key for a writing operation.
38 * See MessageCache::lock()
40 define( 'MSG_LOCK_TIMEOUT', 30 );
42 * Number of times we will try to acquire a lock from Memcached.
43 * This comes in addition to MSG_LOCK_TIMEOUT.
45 define( 'MSG_WAIT_TIMEOUT', 30 );
49 * Performs various MediaWiki namespace-related functions
53 const FOR_UPDATE
= 1; // force message reload
56 * Process local cache of loaded messages that are defined in
57 * MediaWiki namespace. First array level is a language code,
58 * second level is message key and the values are either message
59 * content prefixed with space, or !NONEXISTENT for negative
65 * Should mean that database cannot be used, but check
71 * Lifetime for cache, used by object caching.
72 * Set on construction, see __construct().
77 * Message cache has its own parser which it uses to transform
80 protected $mParserOptions, $mParser;
83 * Variable for tracking which variables are already loaded
84 * @var array $mLoadedLanguages
86 protected $mLoadedLanguages = array();
89 * @var bool $mInParser
91 protected $mInParser = false;
95 /** @var WANObjectCache */
101 * @var MessageCache $instance
103 private static $instance;
106 * Get the signleton instance of this class
109 * @return MessageCache
111 public static function singleton() {
112 if ( is_null( self
::$instance ) ) {
113 global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
114 self
::$instance = new self(
115 wfGetMessageCacheStorage(),
116 $wgUseDatabaseMessages,
121 return self
::$instance;
125 * Destroy the singleton instance
129 public static function destroyInstance() {
130 self
::$instance = null;
134 * Normalize message key input
136 * @param string $key Input message key to be normalized
137 * @return string Normalized message key
139 public static function normalizeKey( $key ) {
141 $lckey = strtr( $key, ' ', '_' );
142 if ( ord( $lckey ) < 128 ) {
143 $lckey[0] = strtolower( $lckey[0] );
145 $lckey = $wgContLang->lcfirst( $lckey );
152 * @param BagOStuff $memCached A cache instance. If none, fall back to CACHE_NONE.
154 * @param int $expiry Lifetime for cache. @see $mExpiry.
156 function __construct( $memCached, $useDB, $expiry ) {
158 $memCached = wfGetCache( CACHE_NONE
);
161 $this->mMemc
= $memCached;
162 $this->mDisable
= !$useDB;
163 $this->mExpiry
= $expiry;
165 $this->wanCache
= ObjectCache
::getMainWANInstance();
169 * ParserOptions is lazy initialised.
171 * @return ParserOptions
173 function getParserOptions() {
174 if ( !$this->mParserOptions
) {
175 $this->mParserOptions
= new ParserOptions
;
176 $this->mParserOptions
->setEditSection( false );
179 return $this->mParserOptions
;
183 * Try to load the cache from a local file.
185 * @param string $hash The hash of contents, to check validity.
186 * @param string $code Optional language code, see documenation of load().
187 * @return array The cache array
189 function getLocalCache( $hash, $code ) {
190 global $wgCacheDirectory;
192 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
194 # Check file existence
195 MediaWiki\
suppressWarnings();
196 $file = fopen( $filename, 'r' );
197 MediaWiki\restoreWarnings
();
199 return false; // No cache file
202 // Check to see if the file has the hash specified
203 $localHash = fread( $file, 32 );
204 if ( $hash === $localHash ) {
205 // All good, get the rest of it
207 while ( !feof( $file ) ) {
208 $serialized .= fread( $file, 100000 );
212 return unserialize( $serialized );
216 return false; // Wrong hash
221 * Save the cache to a local file.
222 * @param string $serialized
223 * @param string $hash
224 * @param string $code
226 function saveToLocal( $serialized, $hash, $code ) {
227 global $wgCacheDirectory;
229 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
230 wfMkdirParents( $wgCacheDirectory, null, __METHOD__
); // might fail
232 MediaWiki\
suppressWarnings();
233 $file = fopen( $filename, 'w' );
234 MediaWiki\restoreWarnings
();
237 wfDebug( "Unable to open local cache file for writing\n" );
242 fwrite( $file, $hash . $serialized );
244 MediaWiki\
suppressWarnings();
245 chmod( $filename, 0666 );
246 MediaWiki\restoreWarnings
();
250 * Loads messages from caches or from database in this order:
251 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
253 * (3) from the database.
255 * When succesfully loading from (2) or (3), all higher level caches are
256 * updated for the newest version.
258 * Nothing is loaded if member variable mDisable is true, either manually
259 * set by calling code or if message loading fails (is this possible?).
261 * Returns true if cache is already populated or it was succesfully populated,
262 * or false if populating empty cache fails. Also returns true if MessageCache
265 * @param bool|string $code Language to which load messages
266 * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache
267 * @throws MWException
270 function load( $code = false, $mode = null ) {
271 global $wgUseLocalMessageCache;
273 if ( !is_string( $code ) ) {
274 # This isn't really nice, so at least make a note about it and try to
276 wfDebug( __METHOD__
. " called without providing a language code\n" );
280 # Don't do double loading...
281 if ( isset( $this->mLoadedLanguages
[$code] ) && $mode != self
::FOR_UPDATE
) {
285 # 8 lines of code just to say (once) that message cache is disabled
286 if ( $this->mDisable
) {
287 static $shownDisabled = false;
288 if ( !$shownDisabled ) {
289 wfDebug( __METHOD__
. ": disabled\n" );
290 $shownDisabled = true;
296 # Loading code starts
297 $success = false; # Keep track of success
298 $staleCache = false; # a cache array with expired data, or false if none has been loaded
299 $hashExpired = false; # whether the cluster-local validation hash is stale
300 $where = array(); # Debug info, delayed to avoid spamming debug log too much
303 # Hash of the contents is stored in memcache, to detect if local cache goes
304 # out of date (e.g. due to replace() on some other server)
305 if ( $wgUseLocalMessageCache ) {
306 list( $hash, $hashExpired ) = $this->getValidationHash( $code );
308 $cache = $this->getLocalCache( $hash, $code );
310 $where[] = 'local cache is empty or has the wrong hash';
311 } elseif ( $this->isCacheExpired( $cache ) ) {
312 $where[] = 'local cache is expired';
313 $staleCache = $cache;
314 } elseif ( $hashExpired ) {
315 $where[] = 'local cache validation key is expired';
316 $staleCache = $cache;
318 $where[] = 'got from local cache';
320 $this->mCache
[$code] = $cache;
326 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
327 # Try the global cache. If it is empty, try to acquire a lock. If
328 # the lock can't be acquired, wait for the other thread to finish
329 # and then try the global cache a second time.
330 for ( $failedAttempts = 0; $failedAttempts < 2; $failedAttempts++
) {
331 if ( $hashExpired && $staleCache ) {
332 # Do not bother fetching the whole cache blob to avoid I/O.
333 # Instead, just try to get the non-blocking $statusKey lock
334 # below, and use the local stale value if it was not acquired.
335 $where[] = 'global cache is presumed expired';
337 $cache = $this->mMemc
->get( $cacheKey );
339 $where[] = 'global cache is empty';
340 } elseif ( $this->isCacheExpired( $cache ) ) {
341 $where[] = 'global cache is expired';
342 $staleCache = $cache;
344 $where[] = 'got from global cache';
345 $this->mCache
[$code] = $cache;
346 $this->saveToCaches( $cache, 'local-only', $code );
352 # Done, no need to retry
356 # We need to call loadFromDB. Limit the concurrency to one process.
357 # This prevents the site from going down when the cache expires.
358 if ( $this->loadFromDBWithLock( $code, $where ) ) {
359 # Load from DB complete, no need to retry
362 } elseif ( $staleCache ) {
363 # Use the stale cache while some other thread constructs the new one
364 $where[] = 'using stale cache';
365 $this->mCache
[$code] = $staleCache;
368 } elseif ( $failedAttempts > 0 ) {
369 # Already retried once, still failed, so don't do another lock/unlock cycle
370 # This case will typically be hit if memcached is down, or if
371 # loadFromDB() takes longer than MSG_WAIT_TIMEOUT
372 $where[] = "could not acquire status key.";
375 $statusKey = wfMemcKey( 'messages', $code, 'status' );
376 $status = $this->mMemc
->get( $statusKey );
377 if ( $status === 'error' ) {
381 # Wait for the other thread to finish, then retry
382 $where[] = 'waited for other thread to complete';
383 $this->lock( $cacheKey );
384 $this->unlock( $cacheKey );
391 $where[] = 'loading FAILED - cache is disabled';
392 $this->mDisable
= true;
393 $this->mCache
= false;
394 # This used to throw an exception, but that led to nasty side effects like
395 # the whole wiki being instantly down if the memcached server died
397 # All good, just record the success
398 $this->mLoadedLanguages
[$code] = true;
401 $info = implode( ', ', $where );
402 wfDebugLog( 'MessageCache', __METHOD__
. ": Loading $code... $info\n" );
408 * @param string $code
409 * @param array $where List of wfDebug() comments
410 * @return bool Lock acquired and loadFromDB() called
412 protected function loadFromDBWithLock( $code, array &$where ) {
413 global $wgUseLocalMessageCache;
415 $memCache = $this->mMemc
;
417 $statusKey = wfMemcKey( 'messages', $code, 'status' );
418 if ( !$memCache->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT
) ) {
419 return false; // could not acquire lock
422 # Unlock the status key if there is an exception
423 $statusUnlocker = new ScopedCallback( function () use ( $memCache, $statusKey ) {
424 $memCache->delete( $statusKey );
427 # Now let's regenerate
428 $where[] = 'loading from database';
430 $cacheKey = wfMemcKey( 'messages', $code );
431 # Lock the cache to prevent conflicting writes
432 # If this lock fails, it doesn't really matter, it just means the
433 # write is potentially non-atomic, e.g. the results of a replace()
435 if ( $this->lock( $cacheKey ) ) {
437 $mainUnlocker = new ScopedCallback( function () use ( $that, $cacheKey ) {
438 $that->unlock( $cacheKey );
441 $mainUnlocker = null;
442 $where[] = 'could not acquire main lock';
445 $cache = $this->loadFromDB( $code );
446 $this->mCache
[$code] = $cache;
447 $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
450 ScopedCallback
::consume( $mainUnlocker );
451 ScopedCallback
::consume( $statusUnlocker );
453 if ( !$saveSuccess ) {
454 # Cache save has failed.
455 # There are two main scenarios where this could be a problem:
457 # - The cache is more than the maximum size (typically
460 # - Memcached has no space remaining in the relevant slab
461 # class. This is unlikely with recent versions of
464 # Either way, if there is a local cache, nothing bad will
465 # happen. If there is no local cache, disabling the message
466 # cache for all requests avoids incurring a loadFromDB()
467 # overhead on every request, and thus saves the wiki from
468 # complete downtime under moderate traffic conditions.
469 if ( !$wgUseLocalMessageCache ) {
470 $memCache->set( $statusKey, 'error', 60 * 5 );
471 $where[] = 'could not save cache, disabled globally for 5 minutes';
473 $where[] = "could not save global cache";
481 * Loads cacheable messages from the database. Messages bigger than
482 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
483 * on-demand from the database later.
485 * @param string $code Language code.
486 * @return array Loaded messages for storing in caches.
488 function loadFromDB( $code ) {
489 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
491 $dbr = wfGetDB( DB_SLAVE
);
496 'page_is_redirect' => 0,
497 'page_namespace' => NS_MEDIAWIKI
,
501 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
502 if ( !isset( $this->mCache
[$wgLanguageCode] ) ) {
503 $this->load( $wgLanguageCode );
505 $mostused = array_keys( $this->mCache
[$wgLanguageCode] );
506 foreach ( $mostused as $key => $value ) {
507 $mostused[$key] = "$value/$code";
511 if ( count( $mostused ) ) {
512 $conds['page_title'] = $mostused;
513 } elseif ( $code !== $wgLanguageCode ) {
514 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
516 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
517 # other than language code.
518 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
521 # Conditions to fetch oversized pages to ignore them
523 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
525 # Load titles for all oversized pages in the MediaWiki namespace
526 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__
. "($code)-big" );
527 foreach ( $res as $row ) {
528 $cache[$row->page_title
] = '!TOO BIG';
531 # Conditions to load the remaining pages with their contents
532 $smallConds = $conds;
533 $smallConds[] = 'page_latest=rev_id';
534 $smallConds[] = 'rev_text_id=old_id';
535 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
538 array( 'page', 'revision', 'text' ),
539 array( 'page_title', 'old_text', 'old_flags' ),
541 __METHOD__
. "($code)-small"
544 foreach ( $res as $row ) {
545 $text = Revision
::getRevisionText( $row );
546 if ( $text === false ) {
547 // Failed to fetch data; possible ES errors?
548 // Store a marker to fetch on-demand as a workaround...
553 . ": failed to load message page text for {$row->page_title} ($code)"
556 $entry = ' ' . $text;
558 $cache[$row->page_title
] = $entry;
561 $cache['VERSION'] = MSG_CACHE_VERSION
;
562 $cache['EXPIRY'] = wfTimestamp( TS_MW
, time() +
$this->mExpiry
);
568 * Updates cache as necessary when message page is changed
570 * @param string $title Name of the page changed.
571 * @param mixed $text New contents of the page.
573 public function replace( $title, $text ) {
574 global $wgMaxMsgCacheEntrySize, $wgContLang, $wgLanguageCode;
576 if ( $this->mDisable
) {
580 list( $msg, $code ) = $this->figureMessage( $title );
581 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
582 # Content language overrides do not use the /<code> suffix
586 $cacheKey = wfMemcKey( 'messages', $code );
587 $this->lock( $cacheKey );
588 $this->load( $code, self
::FOR_UPDATE
);
590 $titleKey = wfMemcKey( 'messages', 'individual', $title );
592 if ( $text === false ) {
593 # Article was deleted
594 $this->mCache
[$code][$title] = '!NONEXISTENT';
595 $this->mMemc
->delete( $titleKey );
596 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
598 $this->mCache
[$code][$title] = '!TOO BIG';
599 $this->mMemc
->set( $titleKey, ' ' . $text, $this->mExpiry
);
601 $this->mCache
[$code][$title] = ' ' . $text;
602 $this->mMemc
->delete( $titleKey );
606 $this->saveToCaches( $this->mCache
[$code], 'all', $code );
607 $this->unlock( $cacheKey );
608 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
610 // Also delete cached sidebar... just in case it is affected
611 $codes = array( $code );
612 if ( $code === 'en' ) {
613 // Delete all sidebars, like for example on action=purge on the
615 $codes = array_keys( Language
::fetchLanguageNames() );
618 foreach ( $codes as $code ) {
619 $sidebarKey = wfMemcKey( 'sidebar', $code );
620 $this->wanCache
->delete( $sidebarKey, 5 );
623 // Update the message in the message blob store
624 $blobStore = new MessageBlobStore();
625 $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
627 Hooks
::run( 'MessageCacheReplace', array( $title, $text ) );
631 * Is the given cache array expired due to time passing or a version change?
633 * @param array $cache
636 protected function isCacheExpired( $cache ) {
637 if ( !isset( $cache['VERSION'] ) ||
!isset( $cache['EXPIRY'] ) ) {
640 if ( $cache['VERSION'] != MSG_CACHE_VERSION
) {
643 if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
651 * Shortcut to update caches.
653 * @param array $cache Cached messages with a version.
654 * @param string $dest Either "local-only" to save to local caches only
655 * or "all" to save to all caches.
656 * @param string|bool $code Language code (default: false)
659 protected function saveToCaches( $cache, $dest, $code = false ) {
660 global $wgUseLocalMessageCache;
662 if ( $dest === 'all' ) {
663 $cacheKey = wfMemcKey( 'messages', $code );
664 $success = $this->mMemc
->set( $cacheKey, $cache );
669 # Save to local cache
670 if ( $wgUseLocalMessageCache ) {
671 $serialized = serialize( $cache );
672 $hash = md5( $serialized );
673 $this->setValidationHash( $code, $hash );
674 $this->saveToLocal( $serialized, $hash, $code );
681 * Get the md5 used to validate the local disk cache
683 * @param string $code
684 * @return array (hash or false, bool expiry status)
686 protected function getValidationHash( $code ) {
688 $value = $this->wanCache
->get(
689 wfMemcKey( 'messages', $code, 'hash' ),
691 array( wfMemcKey( 'messages', $code ) )
693 $expired = ( $curTTL === null ||
$curTTL < 0 );
695 return array( $value, $expired );
699 * Set the md5 used to validate the local disk cache
701 * @param string $code
702 * @param string $hash
704 protected function setValidationHash( $code, $hash ) {
705 $this->wanCache
->set(
706 wfMemcKey( 'messages', $code, 'hash' ),
708 WANObjectCache
::TTL_NONE
713 * Represents a write lock on the messages key.
715 * Will retry MessageCache::MSG_WAIT_TIMEOUT times, each operations having
716 * a timeout of MessageCache::MSG_LOCK_TIMEOUT.
719 * @return bool Success
721 function lock( $key ) {
722 $lockKey = $key . ':lock';
725 for ( $i = 0; $i < MSG_WAIT_TIMEOUT
&& !$acquired; $i++
) {
726 $acquired = $this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
);
731 # Fail fast if memcached is totally down
734 if ( !$this->mMemc
->set( wfMemcKey( 'test' ), 'test', 1 ) ) {
744 function unlock( $key ) {
745 $lockKey = $key . ':lock';
746 $this->mMemc
->delete( $lockKey );
750 * Get a message from either the content language or the user language.
752 * First, assemble a list of languages to attempt getting the message from. This
753 * chain begins with the requested language and its fallbacks and then continues with
754 * the content language and its fallbacks. For each language in the chain, the following
755 * process will occur (in this order):
756 * 1. If a language-specific override, i.e., [[MW:msg/lang]], is available, use that.
757 * Note: for the content language, there is no /lang subpage.
758 * 2. Fetch from the static CDB cache.
759 * 3. If available, check the database for fallback language overrides.
761 * This process provides a number of guarantees. When changing this code, make sure all
762 * of these guarantees are preserved.
763 * * If the requested language is *not* the content language, then the CDB cache for that
764 * specific language will take precedence over the root database page ([[MW:msg]]).
765 * * Fallbacks will be just that: fallbacks. A fallback language will never be reached if
766 * the message is available *anywhere* in the language for which it is a fallback.
768 * @param string $key The message key
769 * @param bool $useDB If true, look for the message in the DB, false
770 * to use only the compiled l10n cache.
771 * @param bool|string|object $langcode Code of the language to get the message for.
772 * - If string and a valid code, will create a standard language object
773 * - If string but not a valid code, will create a basic language object
774 * - If boolean and false, create object from the current users language
775 * - If boolean and true, create object from the wikis content language
776 * - If language object, use it as given
777 * @param bool $isFullKey Specifies whether $key is a two part key "msg/lang".
779 * @throws MWException When given an invalid key
780 * @return string|bool False if the message doesn't exist, otherwise the
781 * message (which can be empty)
783 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
786 if ( is_int( $key ) ) {
787 // Fix numerical strings that somehow become ints
790 } elseif ( !is_string( $key ) ) {
791 throw new MWException( 'Non-string key given' );
792 } elseif ( $key === '' ) {
793 // Shortcut: the empty key is always missing
797 // For full keys, get the language code from the key
798 $pos = strrpos( $key, '/' );
799 if ( $isFullKey && $pos !== false ) {
800 $langcode = substr( $key, $pos +
1 );
801 $key = substr( $key, 0, $pos );
804 // Normalise title-case input (with some inlining)
805 $lckey = MessageCache
::normalizeKey( $key );
807 Hooks
::run( 'MessageCache::get', array( &$lckey ) );
809 if ( ord( $lckey ) < 128 ) {
810 $uckey = ucfirst( $lckey );
812 $uckey = $wgContLang->ucfirst( $lckey );
815 // Loop through each language in the fallback list until we find something useful
816 $lang = wfGetLangObj( $langcode );
817 $message = $this->getMessageFromFallbackChain(
821 !$this->mDisable
&& $useDB
824 // If we still have no message, maybe the key was in fact a full key so try that
825 if ( $message === false ) {
826 $parts = explode( '/', $lckey );
827 // We may get calls for things that are http-urls from sidebar
828 // Let's not load nonexistent languages for those
829 // They usually have more than one slash.
830 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
831 $message = Language
::getMessageFor( $parts[0], $parts[1] );
832 if ( $message === null ) {
838 // Post-processing if the message exists
839 if ( $message !== false ) {
841 $message = str_replace(
843 # Fix for trailing whitespace, removed by textarea
845 # Fix for NBSP, converted to space by firefox
862 * Given a language, try and fetch a message from that language, then the
863 * fallbacks of that language, then the site language, then the fallbacks for the
866 * @param Language $lang Requested language
867 * @param string $lckey Lowercase key for the message
868 * @param string $uckey Uppercase key for the message
869 * @param bool $useDB Whether to use the database
871 * @see MessageCache::get
872 * @return string|bool The message, or false if not found
874 protected function getMessageFromFallbackChain( $lang, $lckey, $uckey, $useDB ) {
875 global $wgLanguageCode, $wgContLang;
877 $langcode = $lang->getCode();
880 // First try the requested language.
882 if ( $langcode === $wgLanguageCode ) {
883 // Messages created in the content language will not have the /lang extension
884 $message = $this->getMsgFromNamespace( $uckey, $langcode );
886 $message = $this->getMsgFromNamespace( "$uckey/$langcode", $langcode );
890 if ( $message !== false ) {
894 // Check the CDB cache
895 $message = $lang->getMessage( $lckey );
896 if ( $message !== null ) {
900 list( $fallbackChain, $siteFallbackChain ) =
901 Language
::getFallbacksIncludingSiteLanguage( $langcode );
903 // Next try checking the database for all of the fallback languages of the requested language.
905 foreach ( $fallbackChain as $code ) {
906 if ( $code === $wgLanguageCode ) {
907 // Messages created in the content language will not have the /lang extension
908 $message = $this->getMsgFromNamespace( $uckey, $code );
910 $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
913 if ( $message !== false ) {
914 // Found the message.
920 // Now try checking the site language.
922 $message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode );
923 if ( $message !== false ) {
928 $message = $wgContLang->getMessage( $lckey );
929 if ( $message !== null ) {
933 // Finally try the DB for the site language's fallbacks.
935 foreach ( $siteFallbackChain as $code ) {
936 $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
937 if ( $message === false && $code === $wgLanguageCode ) {
938 // Messages created in the content language will not have the /lang extension
939 $message = $this->getMsgFromNamespace( $uckey, $code );
942 if ( $message !== false ) {
943 // Found the message.
953 * Get a message from the MediaWiki namespace, with caching. The key must
954 * first be converted to two-part lang/msg form if necessary.
956 * Unlike self::get(), this function doesn't resolve fallback chains, and
957 * some callers require this behavior. LanguageConverter::parseCachedTable()
958 * and self::get() are some examples in core.
960 * @param string $title Message cache key with initial uppercase letter.
961 * @param string $code Code denoting the language to try.
962 * @return string|bool The message, or false if it does not exist or on error
964 function getMsgFromNamespace( $title, $code ) {
965 $this->load( $code );
966 if ( isset( $this->mCache
[$code][$title] ) ) {
967 $entry = $this->mCache
[$code][$title];
968 if ( substr( $entry, 0, 1 ) === ' ' ) {
969 // The message exists, so make sure a string
971 return (string)substr( $entry, 1 );
972 } elseif ( $entry === '!NONEXISTENT' ) {
974 } elseif ( $entry === '!TOO BIG' ) {
975 // Fall through and try invididual message cache below
978 // XXX: This is not cached in process cache, should it?
980 Hooks
::run( 'MessagesPreLoad', array( $title, &$message ) );
981 if ( $message !== false ) {
988 # Try the individual message cache
989 $titleKey = wfMemcKey( 'messages', 'individual', $title );
990 $entry = $this->mMemc
->get( $titleKey );
992 if ( substr( $entry, 0, 1 ) === ' ' ) {
993 $this->mCache
[$code][$title] = $entry;
995 // The message exists, so make sure a string
997 return (string)substr( $entry, 1 );
998 } elseif ( $entry === '!NONEXISTENT' ) {
999 $this->mCache
[$code][$title] = '!NONEXISTENT';
1003 # Corrupt/obsolete entry, delete it
1004 $this->mMemc
->delete( $titleKey );
1008 # Try loading it from the database
1009 $revision = Revision
::newFromTitle(
1010 Title
::makeTitle( NS_MEDIAWIKI
, $title ), false, Revision
::READ_LATEST
1013 $content = $revision->getContent();
1015 // A possibly temporary loading failure.
1018 __METHOD__
. ": failed to load message page text for {$title} ($code)"
1020 $message = null; // no negative caching
1022 // XXX: Is this the right way to turn a Content object into a message?
1023 // NOTE: $content is typically either WikitextContent, JavaScriptContent or
1024 // CssContent. MessageContent is *not* used for storing messages, it's
1025 // only used for wrapping them when needed.
1026 $message = $content->getWikitextForTransclusion();
1028 if ( $message === false ||
$message === null ) {
1031 __METHOD__
. ": message content doesn't provide wikitext "
1032 . "(content model: " . $content->getContentHandler() . ")"
1035 $message = false; // negative caching
1037 $this->mCache
[$code][$title] = ' ' . $message;
1038 $this->mMemc
->set( $titleKey, ' ' . $message, $this->mExpiry
);
1042 $message = false; // negative caching
1045 if ( $message === false ) { // negative caching
1046 $this->mCache
[$code][$title] = '!NONEXISTENT';
1047 $this->mMemc
->set( $titleKey, '!NONEXISTENT', $this->mExpiry
);
1054 * @param string $message
1055 * @param bool $interface
1056 * @param string $language Language code
1057 * @param Title $title
1060 function transform( $message, $interface = false, $language = null, $title = null ) {
1061 // Avoid creating parser if nothing to transform
1062 if ( strpos( $message, '{{' ) === false ) {
1066 if ( $this->mInParser
) {
1070 $parser = $this->getParser();
1072 $popts = $this->getParserOptions();
1073 $popts->setInterfaceMessage( $interface );
1074 $popts->setTargetLanguage( $language );
1076 $userlang = $popts->setUserLang( $language );
1077 $this->mInParser
= true;
1078 $message = $parser->transformMsg( $message, $popts, $title );
1079 $this->mInParser
= false;
1080 $popts->setUserLang( $userlang );
1089 function getParser() {
1090 global $wgParser, $wgParserConf;
1091 if ( !$this->mParser
&& isset( $wgParser ) ) {
1092 # Do some initialisation so that we don't have to do it twice
1093 $wgParser->firstCallInit();
1094 # Clone it and store it
1095 $class = $wgParserConf['class'];
1096 if ( $class == 'ParserDiffTest' ) {
1098 $this->mParser
= new $class( $wgParserConf );
1100 $this->mParser
= clone $wgParser;
1104 return $this->mParser
;
1108 * @param string $text
1109 * @param Title $title
1110 * @param bool $linestart Whether or not this is at the start of a line
1111 * @param bool $interface Whether this is an interface message
1112 * @param string $language Language code
1113 * @return ParserOutput|string
1115 public function parse( $text, $title = null, $linestart = true,
1116 $interface = false, $language = null
1118 if ( $this->mInParser
) {
1119 return htmlspecialchars( $text );
1122 $parser = $this->getParser();
1123 $popts = $this->getParserOptions();
1124 $popts->setInterfaceMessage( $interface );
1125 $popts->setTargetLanguage( $language );
1127 if ( !$title ||
!$title instanceof Title
) {
1129 wfDebugLog( 'GlobalTitleFail', __METHOD__
. ' called by ' .
1130 wfGetAllCallers( 5 ) . ' with no title set.' );
1133 // Sometimes $wgTitle isn't set either...
1135 # It's not uncommon having a null $wgTitle in scripts. See r80898
1136 # Create a ghost title in such case
1137 $title = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/title not set in ' . __METHOD__
);
1140 $this->mInParser
= true;
1141 $res = $parser->parse( $text, $title, $popts, $linestart );
1142 $this->mInParser
= false;
1147 function disable() {
1148 $this->mDisable
= true;
1152 $this->mDisable
= false;
1156 * Clear all stored messages. Mainly used after a mass rebuild.
1159 $langs = Language
::fetchLanguageNames( null, 'mw' );
1160 foreach ( array_keys( $langs ) as $code ) {
1161 # Global and local caches
1162 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
1165 $this->mLoadedLanguages
= array();
1169 * @param string $key
1172 public function figureMessage( $key ) {
1173 global $wgLanguageCode;
1175 $pieces = explode( '/', $key );
1176 if ( count( $pieces ) < 2 ) {
1177 return array( $key, $wgLanguageCode );
1180 $lang = array_pop( $pieces );
1181 if ( !Language
::fetchLanguageName( $lang, null, 'mw' ) ) {
1182 return array( $key, $wgLanguageCode );
1185 $message = implode( '/', $pieces );
1187 return array( $message, $lang );
1191 * Get all message keys stored in the message cache for a given language.
1192 * If $code is the content language code, this will return all message keys
1193 * for which MediaWiki:msgkey exists. If $code is another language code, this
1194 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
1195 * @param string $code Language code
1196 * @return array Array of message keys (strings)
1198 public function getAllMessageKeys( $code ) {
1200 $this->load( $code );
1201 if ( !isset( $this->mCache
[$code] ) ) {
1202 // Apparently load() failed
1205 // Remove administrative keys
1206 $cache = $this->mCache
[$code];
1207 unset( $cache['VERSION'] );
1208 unset( $cache['EXPIRY'] );
1209 // Remove any !NONEXISTENT keys
1210 $cache = array_diff( $cache, array( '!NONEXISTENT' ) );
1212 // Keys may appear with a capital first letter. lcfirst them.
1213 return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) );