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 * @param BagOStuff $memCached A cache instance. If none, fall back to CACHE_NONE.
136 * @param int $expiry Lifetime for cache. @see $mExpiry.
138 function __construct( $memCached, $useDB, $expiry ) {
140 $memCached = wfGetCache( CACHE_NONE
);
143 $this->mMemc
= $memCached;
144 $this->mDisable
= !$useDB;
145 $this->mExpiry
= $expiry;
147 $this->wanCache
= ObjectCache
::getMainWANInstance();
151 * ParserOptions is lazy initialised.
153 * @return ParserOptions
155 function getParserOptions() {
156 if ( !$this->mParserOptions
) {
157 $this->mParserOptions
= new ParserOptions
;
158 $this->mParserOptions
->setEditSection( false );
161 return $this->mParserOptions
;
165 * Try to load the cache from a local file.
167 * @param string $hash The hash of contents, to check validity.
168 * @param string $code Optional language code, see documenation of load().
169 * @return array The cache array
171 function getLocalCache( $hash, $code ) {
172 global $wgCacheDirectory;
174 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
176 # Check file existence
177 MediaWiki\
suppressWarnings();
178 $file = fopen( $filename, 'r' );
179 MediaWiki\restoreWarnings
();
181 return false; // No cache file
184 // Check to see if the file has the hash specified
185 $localHash = fread( $file, 32 );
186 if ( $hash === $localHash ) {
187 // All good, get the rest of it
189 while ( !feof( $file ) ) {
190 $serialized .= fread( $file, 100000 );
194 return unserialize( $serialized );
198 return false; // Wrong hash
203 * Save the cache to a local file.
204 * @param string $serialized
205 * @param string $hash
206 * @param string $code
208 function saveToLocal( $serialized, $hash, $code ) {
209 global $wgCacheDirectory;
211 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
212 wfMkdirParents( $wgCacheDirectory, null, __METHOD__
); // might fail
214 MediaWiki\
suppressWarnings();
215 $file = fopen( $filename, 'w' );
216 MediaWiki\restoreWarnings
();
219 wfDebug( "Unable to open local cache file for writing\n" );
224 fwrite( $file, $hash . $serialized );
226 MediaWiki\
suppressWarnings();
227 chmod( $filename, 0666 );
228 MediaWiki\restoreWarnings
();
232 * Loads messages from caches or from database in this order:
233 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
235 * (3) from the database.
237 * When succesfully loading from (2) or (3), all higher level caches are
238 * updated for the newest version.
240 * Nothing is loaded if member variable mDisable is true, either manually
241 * set by calling code or if message loading fails (is this possible?).
243 * Returns true if cache is already populated or it was succesfully populated,
244 * or false if populating empty cache fails. Also returns true if MessageCache
247 * @param bool|string $code Language to which load messages
248 * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache
249 * @throws MWException
252 function load( $code = false, $mode = null ) {
253 global $wgUseLocalMessageCache;
255 if ( !is_string( $code ) ) {
256 # This isn't really nice, so at least make a note about it and try to
258 wfDebug( __METHOD__
. " called without providing a language code\n" );
262 # Don't do double loading...
263 if ( isset( $this->mLoadedLanguages
[$code] ) && $mode != self
::FOR_UPDATE
) {
267 # 8 lines of code just to say (once) that message cache is disabled
268 if ( $this->mDisable
) {
269 static $shownDisabled = false;
270 if ( !$shownDisabled ) {
271 wfDebug( __METHOD__
. ": disabled\n" );
272 $shownDisabled = true;
278 # Loading code starts
279 $success = false; # Keep track of success
280 $staleCache = false; # a cache array with expired data, or false if none has been loaded
281 $hashExpired = false; # whether the cluster-local validation hash is stale
282 $where = array(); # Debug info, delayed to avoid spamming debug log too much
285 # Hash of the contents is stored in memcache, to detect if local cache goes
286 # out of date (e.g. due to replace() on some other server)
287 if ( $wgUseLocalMessageCache ) {
288 list( $hash, $hashExpired ) = $this->getValidationHash( $code );
290 $cache = $this->getLocalCache( $hash, $code );
292 $where[] = 'local cache is empty or has the wrong hash';
293 } elseif ( $this->isCacheExpired( $cache ) ) {
294 $where[] = 'local cache is expired';
295 $staleCache = $cache;
296 } elseif ( $hashExpired ) {
297 $where[] = 'local cache validation key is expired';
298 $staleCache = $cache;
300 $where[] = 'got from local cache';
302 $this->mCache
[$code] = $cache;
308 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
309 # Try the global cache. If it is empty, try to acquire a lock. If
310 # the lock can't be acquired, wait for the other thread to finish
311 # and then try the global cache a second time.
312 for ( $failedAttempts = 0; $failedAttempts < 2; $failedAttempts++
) {
313 if ( $hashExpired && $staleCache ) {
314 # Do not bother fetching the whole cache blob to avoid I/O.
315 # Instead, just try to get the non-blocking $statusKey lock
316 # below, and use the local stale value if it was not acquired.
317 $where[] = 'global cache is presumed expired';
319 $cache = $this->mMemc
->get( $cacheKey );
321 $where[] = 'global cache is empty';
322 } elseif ( $this->isCacheExpired( $cache ) ) {
323 $where[] = 'global cache is expired';
324 $staleCache = $cache;
326 $where[] = 'got from global cache';
327 $this->mCache
[$code] = $cache;
328 $this->saveToCaches( $cache, 'local-only', $code );
334 # Done, no need to retry
338 # We need to call loadFromDB. Limit the concurrency to one process.
339 # This prevents the site from going down when the cache expires.
340 if ( $this->loadFromDBWithLock( $code, $where ) ) {
341 # Load from DB complete, no need to retry
344 } elseif ( $staleCache ) {
345 # Use the stale cache while some other thread constructs the new one
346 $where[] = 'using stale cache';
347 $this->mCache
[$code] = $staleCache;
350 } elseif ( $failedAttempts > 0 ) {
351 # Already retried once, still failed, so don't do another lock/unlock cycle
352 # This case will typically be hit if memcached is down, or if
353 # loadFromDB() takes longer than MSG_WAIT_TIMEOUT
354 $where[] = "could not acquire status key.";
357 $statusKey = wfMemcKey( 'messages', $code, 'status' );
358 $status = $this->mMemc
->get( $statusKey );
359 if ( $status === 'error' ) {
363 # Wait for the other thread to finish, then retry
364 $where[] = 'waited for other thread to complete';
365 $this->lock( $cacheKey );
366 $this->unlock( $cacheKey );
373 $where[] = 'loading FAILED - cache is disabled';
374 $this->mDisable
= true;
375 $this->mCache
= false;
376 # This used to throw an exception, but that led to nasty side effects like
377 # the whole wiki being instantly down if the memcached server died
379 # All good, just record the success
380 $this->mLoadedLanguages
[$code] = true;
383 $info = implode( ', ', $where );
384 wfDebugLog( 'MessageCache', __METHOD__
. ": Loading $code... $info\n" );
390 * @param string $code
391 * @param array $where List of wfDebug() comments
392 * @return bool Lock acquired and loadFromDB() called
394 protected function loadFromDBWithLock( $code, array &$where ) {
395 global $wgUseLocalMessageCache;
397 $memCache = $this->mMemc
;
399 $statusKey = wfMemcKey( 'messages', $code, 'status' );
400 if ( !$memCache->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT
) ) {
401 return false; // could not acquire lock
404 # Unlock the status key if there is an exception
405 $statusUnlocker = new ScopedCallback( function () use ( $memCache, $statusKey ) {
406 $memCache->delete( $statusKey );
409 # Now let's regenerate
410 $where[] = 'loading from database';
412 $cacheKey = wfMemcKey( 'messages', $code );
413 # Lock the cache to prevent conflicting writes
414 # If this lock fails, it doesn't really matter, it just means the
415 # write is potentially non-atomic, e.g. the results of a replace()
417 if ( $this->lock( $cacheKey ) ) {
419 $mainUnlocker = new ScopedCallback( function () use ( $that, $cacheKey ) {
420 $that->unlock( $cacheKey );
423 $mainUnlocker = null;
424 $where[] = 'could not acquire main lock';
427 $cache = $this->loadFromDB( $code );
428 $this->mCache
[$code] = $cache;
429 $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
432 ScopedCallback
::consume( $mainUnlocker );
433 ScopedCallback
::consume( $statusUnlocker );
435 if ( !$saveSuccess ) {
436 # Cache save has failed.
437 # There are two main scenarios where this could be a problem:
439 # - The cache is more than the maximum size (typically
442 # - Memcached has no space remaining in the relevant slab
443 # class. This is unlikely with recent versions of
446 # Either way, if there is a local cache, nothing bad will
447 # happen. If there is no local cache, disabling the message
448 # cache for all requests avoids incurring a loadFromDB()
449 # overhead on every request, and thus saves the wiki from
450 # complete downtime under moderate traffic conditions.
451 if ( !$wgUseLocalMessageCache ) {
452 $memCache->set( $statusKey, 'error', 60 * 5 );
453 $where[] = 'could not save cache, disabled globally for 5 minutes';
455 $where[] = "could not save global cache";
463 * Loads cacheable messages from the database. Messages bigger than
464 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
465 * on-demand from the database later.
467 * @param string $code Language code.
468 * @return array Loaded messages for storing in caches.
470 function loadFromDB( $code ) {
471 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
473 $dbr = wfGetDB( DB_SLAVE
);
478 'page_is_redirect' => 0,
479 'page_namespace' => NS_MEDIAWIKI
,
483 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
484 if ( !isset( $this->mCache
[$wgLanguageCode] ) ) {
485 $this->load( $wgLanguageCode );
487 $mostused = array_keys( $this->mCache
[$wgLanguageCode] );
488 foreach ( $mostused as $key => $value ) {
489 $mostused[$key] = "$value/$code";
493 if ( count( $mostused ) ) {
494 $conds['page_title'] = $mostused;
495 } elseif ( $code !== $wgLanguageCode ) {
496 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
498 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
499 # other than language code.
500 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
503 # Conditions to fetch oversized pages to ignore them
505 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
507 # Load titles for all oversized pages in the MediaWiki namespace
508 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__
. "($code)-big" );
509 foreach ( $res as $row ) {
510 $cache[$row->page_title
] = '!TOO BIG';
513 # Conditions to load the remaining pages with their contents
514 $smallConds = $conds;
515 $smallConds[] = 'page_latest=rev_id';
516 $smallConds[] = 'rev_text_id=old_id';
517 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
520 array( 'page', 'revision', 'text' ),
521 array( 'page_title', 'old_text', 'old_flags' ),
523 __METHOD__
. "($code)-small"
526 foreach ( $res as $row ) {
527 $text = Revision
::getRevisionText( $row );
528 if ( $text === false ) {
529 // Failed to fetch data; possible ES errors?
530 // Store a marker to fetch on-demand as a workaround...
535 . ": failed to load message page text for {$row->page_title} ($code)"
538 $entry = ' ' . $text;
540 $cache[$row->page_title
] = $entry;
543 $cache['VERSION'] = MSG_CACHE_VERSION
;
544 $cache['EXPIRY'] = wfTimestamp( TS_MW
, time() +
$this->mExpiry
);
550 * Updates cache as necessary when message page is changed
552 * @param string $title Name of the page changed.
553 * @param mixed $text New contents of the page.
555 public function replace( $title, $text ) {
556 global $wgMaxMsgCacheEntrySize, $wgContLang, $wgLanguageCode;
558 if ( $this->mDisable
) {
562 list( $msg, $code ) = $this->figureMessage( $title );
563 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
564 # Content language overrides do not use the /<code> suffix
568 $cacheKey = wfMemcKey( 'messages', $code );
569 $this->lock( $cacheKey );
570 $this->load( $code, self
::FOR_UPDATE
);
572 $titleKey = wfMemcKey( 'messages', 'individual', $title );
574 if ( $text === false ) {
575 # Article was deleted
576 $this->mCache
[$code][$title] = '!NONEXISTENT';
577 $this->mMemc
->delete( $titleKey );
578 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
580 $this->mCache
[$code][$title] = '!TOO BIG';
581 $this->mMemc
->set( $titleKey, ' ' . $text, $this->mExpiry
);
583 $this->mCache
[$code][$title] = ' ' . $text;
584 $this->mMemc
->delete( $titleKey );
588 $this->saveToCaches( $this->mCache
[$code], 'all', $code );
589 $this->unlock( $cacheKey );
590 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
592 // Also delete cached sidebar... just in case it is affected
593 $codes = array( $code );
594 if ( $code === 'en' ) {
595 // Delete all sidebars, like for example on action=purge on the
597 $codes = array_keys( Language
::fetchLanguageNames() );
600 foreach ( $codes as $code ) {
601 $sidebarKey = wfMemcKey( 'sidebar', $code );
602 $this->wanCache
->delete( $sidebarKey, 5 );
605 // Update the message in the message blob store
606 $blobStore = new MessageBlobStore();
607 $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
609 Hooks
::run( 'MessageCacheReplace', array( $title, $text ) );
613 * Is the given cache array expired due to time passing or a version change?
615 * @param array $cache
618 protected function isCacheExpired( $cache ) {
619 if ( !isset( $cache['VERSION'] ) ||
!isset( $cache['EXPIRY'] ) ) {
622 if ( $cache['VERSION'] != MSG_CACHE_VERSION
) {
625 if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
633 * Shortcut to update caches.
635 * @param array $cache Cached messages with a version.
636 * @param string $dest Either "local-only" to save to local caches only
637 * or "all" to save to all caches.
638 * @param string|bool $code Language code (default: false)
641 protected function saveToCaches( $cache, $dest, $code = false ) {
642 global $wgUseLocalMessageCache;
644 if ( $dest === 'all' ) {
645 $cacheKey = wfMemcKey( 'messages', $code );
646 $success = $this->mMemc
->set( $cacheKey, $cache );
651 # Save to local cache
652 if ( $wgUseLocalMessageCache ) {
653 $serialized = serialize( $cache );
654 $hash = md5( $serialized );
655 $this->setValidationHash( $code, $hash );
656 $this->saveToLocal( $serialized, $hash, $code );
663 * Get the md5 used to validate the local disk cache
665 * @param string $code
666 * @return array (hash or false, bool expiry status)
668 protected function getValidationHash( $code ) {
670 $value = $this->wanCache
->get(
671 wfMemcKey( 'messages', $code, 'hash' ),
673 array( wfMemcKey( 'messages', $code ) )
675 $expired = ( $curTTL === null ||
$curTTL < 0 );
677 return array( $value, $expired );
681 * Set the md5 used to validate the local disk cache
683 * @param string $code
684 * @param string $hash
686 protected function setValidationHash( $code, $hash ) {
687 $this->wanCache
->set(
688 wfMemcKey( 'messages', $code, 'hash' ),
690 WANObjectCache
::TTL_NONE
695 * Represents a write lock on the messages key.
697 * Will retry MessageCache::MSG_WAIT_TIMEOUT times, each operations having
698 * a timeout of MessageCache::MSG_LOCK_TIMEOUT.
701 * @return bool Success
703 function lock( $key ) {
704 $lockKey = $key . ':lock';
707 for ( $i = 0; $i < MSG_WAIT_TIMEOUT
&& !$acquired; $i++
) {
708 $acquired = $this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
);
713 # Fail fast if memcached is totally down
716 if ( !$this->mMemc
->set( wfMemcKey( 'test' ), 'test', 1 ) ) {
726 function unlock( $key ) {
727 $lockKey = $key . ':lock';
728 $this->mMemc
->delete( $lockKey );
732 * Get a message from either the content language or the user language.
734 * First, assemble a list of languages to attempt getting the message from. This
735 * chain begins with the requested language and its fallbacks and then continues with
736 * the content language and its fallbacks. For each language in the chain, the following
737 * process will occur (in this order):
738 * 1. If a language-specific override, i.e., [[MW:msg/lang]], is available, use that.
739 * Note: for the content language, there is no /lang subpage.
740 * 2. Fetch from the static CDB cache.
741 * 3. If available, check the database for fallback language overrides.
743 * This process provides a number of guarantees. When changing this code, make sure all
744 * of these guarantees are preserved.
745 * * If the requested language is *not* the content language, then the CDB cache for that
746 * specific language will take precedence over the root database page ([[MW:msg]]).
747 * * Fallbacks will be just that: fallbacks. A fallback language will never be reached if
748 * the message is available *anywhere* in the language for which it is a fallback.
750 * @param string $key The message key
751 * @param bool $useDB If true, look for the message in the DB, false
752 * to use only the compiled l10n cache.
753 * @param bool|string|object $langcode Code of the language to get the message for.
754 * - If string and a valid code, will create a standard language object
755 * - If string but not a valid code, will create a basic language object
756 * - If boolean and false, create object from the current users language
757 * - If boolean and true, create object from the wikis content language
758 * - If language object, use it as given
759 * @param bool $isFullKey Specifies whether $key is a two part key "msg/lang".
761 * @throws MWException When given an invalid key
762 * @return string|bool False if the message doesn't exist, otherwise the
763 * message (which can be empty)
765 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
768 if ( is_int( $key ) ) {
769 // Fix numerical strings that somehow become ints
772 } elseif ( !is_string( $key ) ) {
773 throw new MWException( 'Non-string key given' );
774 } elseif ( $key === '' ) {
775 // Shortcut: the empty key is always missing
779 // For full keys, get the language code from the key
780 $pos = strrpos( $key, '/' );
781 if ( $isFullKey && $pos !== false ) {
782 $langcode = substr( $key, $pos +
1 );
783 $key = substr( $key, 0, $pos );
786 // Normalise title-case input (with some inlining)
787 $lckey = strtr( $key, ' ', '_' );
788 if ( ord( $lckey ) < 128 ) {
789 $lckey[0] = strtolower( $lckey[0] );
791 $lckey = $wgContLang->lcfirst( $lckey );
794 Hooks
::run( 'MessageCache::get', array( &$lckey ) );
796 if ( ord( $lckey ) < 128 ) {
797 $uckey = ucfirst( $lckey );
799 $uckey = $wgContLang->ucfirst( $lckey );
802 // Loop through each language in the fallback list until we find something useful
803 $lang = wfGetLangObj( $langcode );
804 $message = $this->getMessageFromFallbackChain(
808 !$this->mDisable
&& $useDB
811 // If we still have no message, maybe the key was in fact a full key so try that
812 if ( $message === false ) {
813 $parts = explode( '/', $lckey );
814 // We may get calls for things that are http-urls from sidebar
815 // Let's not load nonexistent languages for those
816 // They usually have more than one slash.
817 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
818 $message = Language
::getMessageFor( $parts[0], $parts[1] );
819 if ( $message === null ) {
825 // Post-processing if the message exists
826 if ( $message !== false ) {
828 $message = str_replace(
830 # Fix for trailing whitespace, removed by textarea
832 # Fix for NBSP, converted to space by firefox
849 * Given a language, try and fetch a message from that language, then the
850 * fallbacks of that language, then the site language, then the fallbacks for the
853 * @param Language $lang Requested language
854 * @param string $lckey Lowercase key for the message
855 * @param string $uckey Uppercase key for the message
856 * @param bool $useDB Whether to use the database
858 * @see MessageCache::get
859 * @return string|bool The message, or false if not found
861 protected function getMessageFromFallbackChain( $lang, $lckey, $uckey, $useDB ) {
862 global $wgLanguageCode, $wgContLang;
864 $langcode = $lang->getCode();
867 // First try the requested language.
869 if ( $langcode === $wgLanguageCode ) {
870 // Messages created in the content language will not have the /lang extension
871 $message = $this->getMsgFromNamespace( $uckey, $langcode );
873 $message = $this->getMsgFromNamespace( "$uckey/$langcode", $langcode );
877 if ( $message !== false ) {
881 // Check the CDB cache
882 $message = $lang->getMessage( $lckey );
883 if ( $message !== null ) {
887 list( $fallbackChain, $siteFallbackChain ) =
888 Language
::getFallbacksIncludingSiteLanguage( $langcode );
890 // Next try checking the database for all of the fallback languages of the requested language.
892 foreach ( $fallbackChain as $code ) {
893 if ( $code === $wgLanguageCode ) {
894 // Messages created in the content language will not have the /lang extension
895 $message = $this->getMsgFromNamespace( $uckey, $code );
897 $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
900 if ( $message !== false ) {
901 // Found the message.
907 // Now try checking the site language.
909 $message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode );
910 if ( $message !== false ) {
915 $message = $wgContLang->getMessage( $lckey );
916 if ( $message !== null ) {
920 // Finally try the DB for the site language's fallbacks.
922 foreach ( $siteFallbackChain as $code ) {
923 $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
924 if ( $message === false && $code === $wgLanguageCode ) {
925 // Messages created in the content language will not have the /lang extension
926 $message = $this->getMsgFromNamespace( $uckey, $code );
929 if ( $message !== false ) {
930 // Found the message.
940 * Get a message from the MediaWiki namespace, with caching. The key must
941 * first be converted to two-part lang/msg form if necessary.
943 * Unlike self::get(), this function doesn't resolve fallback chains, and
944 * some callers require this behavior. LanguageConverter::parseCachedTable()
945 * and self::get() are some examples in core.
947 * @param string $title Message cache key with initial uppercase letter.
948 * @param string $code Code denoting the language to try.
949 * @return string|bool The message, or false if it does not exist or on error
951 function getMsgFromNamespace( $title, $code ) {
952 $this->load( $code );
953 if ( isset( $this->mCache
[$code][$title] ) ) {
954 $entry = $this->mCache
[$code][$title];
955 if ( substr( $entry, 0, 1 ) === ' ' ) {
956 // The message exists, so make sure a string
958 return (string)substr( $entry, 1 );
959 } elseif ( $entry === '!NONEXISTENT' ) {
961 } elseif ( $entry === '!TOO BIG' ) {
962 // Fall through and try invididual message cache below
965 // XXX: This is not cached in process cache, should it?
967 Hooks
::run( 'MessagesPreLoad', array( $title, &$message ) );
968 if ( $message !== false ) {
975 # Try the individual message cache
976 $titleKey = wfMemcKey( 'messages', 'individual', $title );
977 $entry = $this->mMemc
->get( $titleKey );
979 if ( substr( $entry, 0, 1 ) === ' ' ) {
980 $this->mCache
[$code][$title] = $entry;
982 // The message exists, so make sure a string
984 return (string)substr( $entry, 1 );
985 } elseif ( $entry === '!NONEXISTENT' ) {
986 $this->mCache
[$code][$title] = '!NONEXISTENT';
990 # Corrupt/obsolete entry, delete it
991 $this->mMemc
->delete( $titleKey );
995 # Try loading it from the database
996 $revision = Revision
::newFromTitle(
997 Title
::makeTitle( NS_MEDIAWIKI
, $title ), false, Revision
::READ_LATEST
1000 $content = $revision->getContent();
1002 // A possibly temporary loading failure.
1005 __METHOD__
. ": failed to load message page text for {$title} ($code)"
1007 $message = null; // no negative caching
1009 // XXX: Is this the right way to turn a Content object into a message?
1010 // NOTE: $content is typically either WikitextContent, JavaScriptContent or
1011 // CssContent. MessageContent is *not* used for storing messages, it's
1012 // only used for wrapping them when needed.
1013 $message = $content->getWikitextForTransclusion();
1015 if ( $message === false ||
$message === null ) {
1018 __METHOD__
. ": message content doesn't provide wikitext "
1019 . "(content model: " . $content->getContentHandler() . ")"
1022 $message = false; // negative caching
1024 $this->mCache
[$code][$title] = ' ' . $message;
1025 $this->mMemc
->set( $titleKey, ' ' . $message, $this->mExpiry
);
1029 $message = false; // negative caching
1032 if ( $message === false ) { // negative caching
1033 $this->mCache
[$code][$title] = '!NONEXISTENT';
1034 $this->mMemc
->set( $titleKey, '!NONEXISTENT', $this->mExpiry
);
1041 * @param string $message
1042 * @param bool $interface
1043 * @param string $language Language code
1044 * @param Title $title
1047 function transform( $message, $interface = false, $language = null, $title = null ) {
1048 // Avoid creating parser if nothing to transform
1049 if ( strpos( $message, '{{' ) === false ) {
1053 if ( $this->mInParser
) {
1057 $parser = $this->getParser();
1059 $popts = $this->getParserOptions();
1060 $popts->setInterfaceMessage( $interface );
1061 $popts->setTargetLanguage( $language );
1063 $userlang = $popts->setUserLang( $language );
1064 $this->mInParser
= true;
1065 $message = $parser->transformMsg( $message, $popts, $title );
1066 $this->mInParser
= false;
1067 $popts->setUserLang( $userlang );
1076 function getParser() {
1077 global $wgParser, $wgParserConf;
1078 if ( !$this->mParser
&& isset( $wgParser ) ) {
1079 # Do some initialisation so that we don't have to do it twice
1080 $wgParser->firstCallInit();
1081 # Clone it and store it
1082 $class = $wgParserConf['class'];
1083 if ( $class == 'ParserDiffTest' ) {
1085 $this->mParser
= new $class( $wgParserConf );
1087 $this->mParser
= clone $wgParser;
1091 return $this->mParser
;
1095 * @param string $text
1096 * @param Title $title
1097 * @param bool $linestart Whether or not this is at the start of a line
1098 * @param bool $interface Whether this is an interface message
1099 * @param string $language Language code
1100 * @return ParserOutput|string
1102 public function parse( $text, $title = null, $linestart = true,
1103 $interface = false, $language = null
1105 if ( $this->mInParser
) {
1106 return htmlspecialchars( $text );
1109 $parser = $this->getParser();
1110 $popts = $this->getParserOptions();
1111 $popts->setInterfaceMessage( $interface );
1112 $popts->setTargetLanguage( $language );
1114 if ( !$title ||
!$title instanceof Title
) {
1116 wfDebugLog( 'GlobalTitleFail', __METHOD__
. ' called by ' .
1117 wfGetAllCallers( 5 ) . ' with no title set.' );
1120 // Sometimes $wgTitle isn't set either...
1122 # It's not uncommon having a null $wgTitle in scripts. See r80898
1123 # Create a ghost title in such case
1124 $title = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/title not set in ' . __METHOD__
);
1127 $this->mInParser
= true;
1128 $res = $parser->parse( $text, $title, $popts, $linestart );
1129 $this->mInParser
= false;
1134 function disable() {
1135 $this->mDisable
= true;
1139 $this->mDisable
= false;
1143 * Clear all stored messages. Mainly used after a mass rebuild.
1146 $langs = Language
::fetchLanguageNames( null, 'mw' );
1147 foreach ( array_keys( $langs ) as $code ) {
1148 # Global and local caches
1149 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
1152 $this->mLoadedLanguages
= array();
1156 * @param string $key
1159 public function figureMessage( $key ) {
1160 global $wgLanguageCode;
1162 $pieces = explode( '/', $key );
1163 if ( count( $pieces ) < 2 ) {
1164 return array( $key, $wgLanguageCode );
1167 $lang = array_pop( $pieces );
1168 if ( !Language
::fetchLanguageName( $lang, null, 'mw' ) ) {
1169 return array( $key, $wgLanguageCode );
1172 $message = implode( '/', $pieces );
1174 return array( $message, $lang );
1178 * Get all message keys stored in the message cache for a given language.
1179 * If $code is the content language code, this will return all message keys
1180 * for which MediaWiki:msgkey exists. If $code is another language code, this
1181 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
1182 * @param string $code Language code
1183 * @return array Array of message keys (strings)
1185 public function getAllMessageKeys( $code ) {
1187 $this->load( $code );
1188 if ( !isset( $this->mCache
[$code] ) ) {
1189 // Apparently load() failed
1192 // Remove administrative keys
1193 $cache = $this->mCache
[$code];
1194 unset( $cache['VERSION'] );
1195 unset( $cache['EXPIRY'] );
1196 // Remove any !NONEXISTENT keys
1197 $cache = array_diff( $cache, array( '!NONEXISTENT' ) );
1199 // Keys may appear with a capital first letter. lcfirst them.
1200 return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) );