304c4f219229d17918152d003e9fcff6a7bbeaf7
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', 2 );
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
66 * Should mean that database cannot be used, but check
72 * Lifetime for cache, used by object caching.
73 * Set on construction, see __construct().
78 * Message cache has its own parser which it uses to transform
81 protected $mParserOptions, $mParser;
84 * Variable for tracking which variables are already loaded
85 * @var array $mLoadedLanguages
87 protected $mLoadedLanguages = array();
90 * @var bool $mInParser
92 protected $mInParser = false;
96 /** @var WANObjectCache */
102 * @var MessageCache $instance
104 private static $instance;
107 * Get the signleton instance of this class
110 * @return MessageCache
112 public static function singleton() {
113 if ( is_null( self
::$instance ) ) {
114 global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
115 self
::$instance = new self(
116 wfGetMessageCacheStorage(),
117 $wgUseDatabaseMessages,
122 return self
::$instance;
126 * Destroy the singleton instance
130 public static function destroyInstance() {
131 self
::$instance = null;
135 * Normalize message key input
137 * @param string $key Input message key to be normalized
138 * @return string Normalized message key
140 public static function normalizeKey( $key ) {
142 $lckey = strtr( $key, ' ', '_' );
143 if ( ord( $lckey ) < 128 ) {
144 $lckey[0] = strtolower( $lckey[0] );
146 $lckey = $wgContLang->lcfirst( $lckey );
153 * @param BagOStuff $memCached A cache instance. If none, fall back to CACHE_NONE.
155 * @param int $expiry Lifetime for cache. @see $mExpiry.
157 function __construct( $memCached, $useDB, $expiry ) {
158 global $wgUseLocalMessageCache;
161 $memCached = wfGetCache( CACHE_NONE
);
164 $this->mMemc
= $memCached;
165 $this->mDisable
= !$useDB;
166 $this->mExpiry
= $expiry;
168 if ( $wgUseLocalMessageCache ) {
169 $this->localCache
= ObjectCache
::newAccelerator( array(), CACHE_NONE
);
171 $this->localCache
= wfGetCache( CACHE_NONE
);
174 $this->wanCache
= ObjectCache
::getMainWANInstance();
178 * ParserOptions is lazy initialised.
180 * @return ParserOptions
182 function getParserOptions() {
183 if ( !$this->mParserOptions
) {
184 $this->mParserOptions
= new ParserOptions
;
185 $this->mParserOptions
->setEditSection( false );
188 return $this->mParserOptions
;
192 * Try to load the cache from APC.
194 * @param string $code Optional language code, see documenation of load().
195 * @return array|bool The cache array, or false if not in cache.
197 protected function getLocalCache( $code ) {
198 $cacheKey = wfMemcKey( __CLASS__
, $code );
199 return $this->localCache
->get( $cacheKey );
203 * Save the cache to APC.
205 * @param string $code
206 * @param array $cache The cache array
208 protected function saveToLocalCache( $code, $cache ) {
209 $cacheKey = wfMemcKey( __CLASS__
, $code );
210 $this->localCache
->set( $cacheKey, $cache );
214 * Loads messages from caches or from database in this order:
215 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
217 * (3) from the database.
219 * When succesfully loading from (2) or (3), all higher level caches are
220 * updated for the newest version.
222 * Nothing is loaded if member variable mDisable is true, either manually
223 * set by calling code or if message loading fails (is this possible?).
225 * Returns true if cache is already populated or it was succesfully populated,
226 * or false if populating empty cache fails. Also returns true if MessageCache
229 * @param bool|string $code Language to which load messages
230 * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache
231 * @throws MWException
234 function load( $code = false, $mode = null ) {
235 global $wgUseLocalMessageCache;
237 if ( !is_string( $code ) ) {
238 # This isn't really nice, so at least make a note about it and try to
240 wfDebug( __METHOD__
. " called without providing a language code\n" );
244 # Don't do double loading...
245 if ( isset( $this->mLoadedLanguages
[$code] ) && $mode != self
::FOR_UPDATE
) {
249 # 8 lines of code just to say (once) that message cache is disabled
250 if ( $this->mDisable
) {
251 static $shownDisabled = false;
252 if ( !$shownDisabled ) {
253 wfDebug( __METHOD__
. ": disabled\n" );
254 $shownDisabled = true;
260 # Loading code starts
261 $success = false; # Keep track of success
262 $staleCache = false; # a cache array with expired data, or false if none has been loaded
263 $where = array(); # Debug info, delayed to avoid spamming debug log too much
265 # Hash of the contents is stored in memcache, to detect if data-center cache
266 # or local cache goes out of date (e.g. due to replace() on some other server)
267 list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
269 if ( $wgUseLocalMessageCache && $hash ) {
270 # Try the local cache and check against the cluster hash key...
271 $cache = $this->getLocalCache( $code );
273 $where[] = 'local cache is empty';
274 } elseif ( !isset( $cache['HASH'] ) ||
$cache['HASH'] !== $hash ) {
275 $where[] = 'local cache has the wrong hash';
276 $staleCache = $cache;
277 } elseif ( $this->isCacheExpired( $cache ) ) {
278 $where[] = 'local cache is expired';
279 $staleCache = $cache;
280 } elseif ( $hashVolatile ) {
281 $where[] = 'local cache validation key is expired/volatile';
282 $staleCache = $cache;
284 $where[] = 'got from local cache';
286 $this->mCache
[$code] = $cache;
291 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
292 # Try the global cache. If it is empty, try to acquire a lock. If
293 # the lock can't be acquired, wait for the other thread to finish
294 # and then try the global cache a second time.
295 for ( $failedAttempts = 0; $failedAttempts < 2; $failedAttempts++
) {
296 if ( $hashVolatile && $staleCache ) {
297 # Do not bother fetching the whole cache blob to avoid I/O.
298 # Instead, just try to get the non-blocking $statusKey lock
299 # below, and use the local stale value if it was not acquired.
300 $where[] = 'global cache is presumed expired';
302 $cache = $this->mMemc
->get( $cacheKey );
304 $where[] = 'global cache is empty';
305 } elseif ( $this->isCacheExpired( $cache ) ) {
306 $where[] = 'global cache is expired';
307 $staleCache = $cache;
308 } elseif ( $hashVolatile ) {
309 # DB results are slave lag prone until the holdoff TTL passes.
310 # By then, updates should be reflected in loadFromDBWithLock().
311 # One thread renerates the cache while others use old values.
312 $where[] = 'global cache is expired/volatile';
313 $staleCache = $cache;
315 $where[] = 'got from global cache';
316 $this->mCache
[$code] = $cache;
317 $this->saveToCaches( $cache, 'local-only', $code );
323 # Done, no need to retry
327 # We need to call loadFromDB. Limit the concurrency to one process.
328 # This prevents the site from going down when the cache expires.
329 # Note that the slam-protection lock here is non-blocking.
330 if ( $this->loadFromDBWithLock( $code, $where ) ) {
333 } elseif ( $staleCache ) {
334 # Use the stale cache while some other thread constructs the new one
335 $where[] = 'using stale cache';
336 $this->mCache
[$code] = $staleCache;
339 } elseif ( $failedAttempts > 0 ) {
340 # Already retried once, still failed, so don't do another lock/unlock cycle
341 # This case will typically be hit if memcached is down, or if
342 # loadFromDB() takes longer than MSG_WAIT_TIMEOUT
343 $where[] = "could not acquire status key.";
346 $statusKey = wfMemcKey( 'messages', $code, 'status' );
347 $status = $this->mMemc
->get( $statusKey );
348 if ( $status === 'error' ) {
352 # Wait for the other thread to finish, then retry
353 $where[] = 'waited for other thread to complete';
354 $this->lock( $cacheKey );
355 $this->unlock( $cacheKey );
362 $where[] = 'loading FAILED - cache is disabled';
363 $this->mDisable
= true;
364 $this->mCache
= false;
365 # This used to throw an exception, but that led to nasty side effects like
366 # the whole wiki being instantly down if the memcached server died
368 # All good, just record the success
369 $this->mLoadedLanguages
[$code] = true;
372 $info = implode( ', ', $where );
373 wfDebugLog( 'MessageCache', __METHOD__
. ": Loading $code... $info\n" );
379 * @param string $code
380 * @param array $where List of wfDebug() comments
381 * @return bool Lock acquired and loadFromDB() called
383 protected function loadFromDBWithLock( $code, array &$where ) {
384 global $wgUseLocalMessageCache;
386 $memCache = $this->mMemc
;
388 $statusKey = wfMemcKey( 'messages', $code, 'status' );
389 if ( !$memCache->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT
) ) {
390 return false; // could not acquire lock
393 # Unlock the status key if there is an exception
394 $statusUnlocker = new ScopedCallback( function () use ( $memCache, $statusKey ) {
395 $memCache->delete( $statusKey );
398 # Now let's regenerate
399 $where[] = 'loading from database';
401 $cacheKey = wfMemcKey( 'messages', $code );
402 # Lock the cache to prevent conflicting writes
403 # If this lock fails, it doesn't really matter, it just means the
404 # write is potentially non-atomic, e.g. the results of a replace()
406 if ( $this->lock( $cacheKey ) ) {
408 $mainUnlocker = new ScopedCallback( function () use ( $that, $cacheKey ) {
409 $that->unlock( $cacheKey );
412 $mainUnlocker = null;
413 $where[] = 'could not acquire main lock';
416 $cache = $this->loadFromDB( $code );
417 $this->mCache
[$code] = $cache;
418 $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
421 ScopedCallback
::consume( $mainUnlocker );
422 ScopedCallback
::consume( $statusUnlocker );
424 if ( !$saveSuccess ) {
425 # Cache save has failed.
426 # There are two main scenarios where this could be a problem:
428 # - The cache is more than the maximum size (typically
431 # - Memcached has no space remaining in the relevant slab
432 # class. This is unlikely with recent versions of
435 # Either way, if there is a local cache, nothing bad will
436 # happen. If there is no local cache, disabling the message
437 # cache for all requests avoids incurring a loadFromDB()
438 # overhead on every request, and thus saves the wiki from
439 # complete downtime under moderate traffic conditions.
440 if ( !$wgUseLocalMessageCache ) {
441 $memCache->set( $statusKey, 'error', 60 * 5 );
442 $where[] = 'could not save cache, disabled globally for 5 minutes';
444 $where[] = "could not save global cache";
452 * Loads cacheable messages from the database. Messages bigger than
453 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
454 * on-demand from the database later.
456 * @param string $code Language code.
457 * @return array Loaded messages for storing in caches.
459 function loadFromDB( $code ) {
460 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
462 $dbr = wfGetDB( DB_SLAVE
);
467 'page_is_redirect' => 0,
468 'page_namespace' => NS_MEDIAWIKI
,
472 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
473 if ( !isset( $this->mCache
[$wgLanguageCode] ) ) {
474 $this->load( $wgLanguageCode );
476 $mostused = array_keys( $this->mCache
[$wgLanguageCode] );
477 foreach ( $mostused as $key => $value ) {
478 $mostused[$key] = "$value/$code";
482 if ( count( $mostused ) ) {
483 $conds['page_title'] = $mostused;
484 } elseif ( $code !== $wgLanguageCode ) {
485 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
487 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
488 # other than language code.
489 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
492 # Conditions to fetch oversized pages to ignore them
494 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
496 # Load titles for all oversized pages in the MediaWiki namespace
497 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__
. "($code)-big" );
498 foreach ( $res as $row ) {
499 $cache[$row->page_title
] = '!TOO BIG';
502 # Conditions to load the remaining pages with their contents
503 $smallConds = $conds;
504 $smallConds[] = 'page_latest=rev_id';
505 $smallConds[] = 'rev_text_id=old_id';
506 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
509 array( 'page', 'revision', 'text' ),
510 array( 'page_title', 'old_text', 'old_flags' ),
512 __METHOD__
. "($code)-small"
515 foreach ( $res as $row ) {
516 $text = Revision
::getRevisionText( $row );
517 if ( $text === false ) {
518 // Failed to fetch data; possible ES errors?
519 // Store a marker to fetch on-demand as a workaround...
524 . ": failed to load message page text for {$row->page_title} ($code)"
527 $entry = ' ' . $text;
529 $cache[$row->page_title
] = $entry;
532 $cache['VERSION'] = MSG_CACHE_VERSION
;
533 $cache['HASH'] = wfRandomString( 8 );
534 $cache['EXPIRY'] = wfTimestamp( TS_MW
, time() +
$this->mExpiry
);
540 * Updates cache as necessary when message page is changed
542 * @param string $title Name of the page changed.
543 * @param mixed $text New contents of the page.
545 public function replace( $title, $text ) {
546 global $wgMaxMsgCacheEntrySize, $wgContLang, $wgLanguageCode;
548 if ( $this->mDisable
) {
552 list( $msg, $code ) = $this->figureMessage( $title );
553 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
554 # Content language overrides do not use the /<code> suffix
558 $cacheKey = wfMemcKey( 'messages', $code );
559 $this->lock( $cacheKey );
560 $this->load( $code, self
::FOR_UPDATE
);
562 $titleKey = wfMemcKey( 'messages', 'individual', $title );
564 if ( $text === false ) {
565 # Article was deleted
566 $this->mCache
[$code][$title] = '!NONEXISTENT';
567 $this->mMemc
->delete( $titleKey );
568 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
570 $this->mCache
[$code][$title] = '!TOO BIG';
571 $this->mMemc
->set( $titleKey, ' ' . $text, $this->mExpiry
);
573 $this->mCache
[$code][$title] = ' ' . $text;
574 $this->mMemc
->delete( $titleKey );
578 $this->saveToCaches( $this->mCache
[$code], 'all', $code );
579 $this->unlock( $cacheKey );
580 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
582 // Also delete cached sidebar... just in case it is affected
583 $codes = array( $code );
584 if ( $code === 'en' ) {
585 // Delete all sidebars, like for example on action=purge on the
587 $codes = array_keys( Language
::fetchLanguageNames() );
590 foreach ( $codes as $code ) {
591 $sidebarKey = wfMemcKey( 'sidebar', $code );
592 $this->wanCache
->delete( $sidebarKey, 5 );
595 // Update the message in the message blob store
596 $blobStore = new MessageBlobStore();
597 $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
599 Hooks
::run( 'MessageCacheReplace', array( $title, $text ) );
603 * Is the given cache array expired due to time passing or a version change?
605 * @param array $cache
608 protected function isCacheExpired( $cache ) {
609 if ( !isset( $cache['VERSION'] ) ||
!isset( $cache['EXPIRY'] ) ) {
612 if ( $cache['VERSION'] != MSG_CACHE_VERSION
) {
615 if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
623 * Shortcut to update caches.
625 * @param array $cache Cached messages with a version.
626 * @param string $dest Either "local-only" to save to local caches only
627 * or "all" to save to all caches.
628 * @param string|bool $code Language code (default: false)
631 protected function saveToCaches( $cache, $dest, $code = false ) {
632 global $wgUseLocalMessageCache;
634 if ( $dest === 'all' ) {
635 $cacheKey = wfMemcKey( 'messages', $code );
636 $success = $this->mMemc
->set( $cacheKey, $cache );
641 # Save to local cache
642 if ( $wgUseLocalMessageCache ) {
643 $this->setValidationHash( $code, $cache['HASH'] );
644 $this->saveToLocalCache( $code, $cache );
651 * Get the md5 used to validate the local disk cache
653 * @param string $code
654 * @return array (hash or false, bool expiry/volatility status)
656 protected function getValidationHash( $code ) {
658 $value = $this->wanCache
->get(
659 wfMemcKey( 'messages', $code, 'hash' ),
661 array( wfMemcKey( 'messages', $code ) )
663 $expired = ( $curTTL === null ||
$curTTL < 0 );
665 return array( $value, $expired );
669 * Set the md5 used to validate the local disk cache
671 * @param string $code
672 * @param string $hash
674 protected function setValidationHash( $code, $hash ) {
675 $this->wanCache
->set(
676 wfMemcKey( 'messages', $code, 'hash' ),
678 WANObjectCache
::TTL_NONE
683 * Represents a write lock on the messages key.
685 * Will retry MessageCache::MSG_WAIT_TIMEOUT times, each operations having
686 * a timeout of MessageCache::MSG_LOCK_TIMEOUT.
689 * @return bool Success
691 function lock( $key ) {
692 $lockKey = $key . ':lock';
695 for ( $i = 0; $i < MSG_WAIT_TIMEOUT
&& !$acquired; $i++
) {
696 $acquired = $this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
);
701 # Fail fast if memcached is totally down
704 if ( !$this->mMemc
->set( wfMemcKey( 'test' ), 'test', 1 ) ) {
714 function unlock( $key ) {
715 $lockKey = $key . ':lock';
716 $this->mMemc
->delete( $lockKey );
720 * Get a message from either the content language or the user language.
722 * First, assemble a list of languages to attempt getting the message from. This
723 * chain begins with the requested language and its fallbacks and then continues with
724 * the content language and its fallbacks. For each language in the chain, the following
725 * process will occur (in this order):
726 * 1. If a language-specific override, i.e., [[MW:msg/lang]], is available, use that.
727 * Note: for the content language, there is no /lang subpage.
728 * 2. Fetch from the static CDB cache.
729 * 3. If available, check the database for fallback language overrides.
731 * This process provides a number of guarantees. When changing this code, make sure all
732 * of these guarantees are preserved.
733 * * If the requested language is *not* the content language, then the CDB cache for that
734 * specific language will take precedence over the root database page ([[MW:msg]]).
735 * * Fallbacks will be just that: fallbacks. A fallback language will never be reached if
736 * the message is available *anywhere* in the language for which it is a fallback.
738 * @param string $key The message key
739 * @param bool $useDB If true, look for the message in the DB, false
740 * to use only the compiled l10n cache.
741 * @param bool|string|object $langcode Code of the language to get the message for.
742 * - If string and a valid code, will create a standard language object
743 * - If string but not a valid code, will create a basic language object
744 * - If boolean and false, create object from the current users language
745 * - If boolean and true, create object from the wikis content language
746 * - If language object, use it as given
747 * @param bool $isFullKey Specifies whether $key is a two part key "msg/lang".
749 * @throws MWException When given an invalid key
750 * @return string|bool False if the message doesn't exist, otherwise the
751 * message (which can be empty)
753 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
756 if ( is_int( $key ) ) {
757 // Fix numerical strings that somehow become ints
760 } elseif ( !is_string( $key ) ) {
761 throw new MWException( 'Non-string key given' );
762 } elseif ( $key === '' ) {
763 // Shortcut: the empty key is always missing
767 // For full keys, get the language code from the key
768 $pos = strrpos( $key, '/' );
769 if ( $isFullKey && $pos !== false ) {
770 $langcode = substr( $key, $pos +
1 );
771 $key = substr( $key, 0, $pos );
774 // Normalise title-case input (with some inlining)
775 $lckey = MessageCache
::normalizeKey( $key );
777 Hooks
::run( 'MessageCache::get', array( &$lckey ) );
779 if ( ord( $lckey ) < 128 ) {
780 $uckey = ucfirst( $lckey );
782 $uckey = $wgContLang->ucfirst( $lckey );
785 // Loop through each language in the fallback list until we find something useful
786 $lang = wfGetLangObj( $langcode );
787 $message = $this->getMessageFromFallbackChain(
791 !$this->mDisable
&& $useDB
794 // If we still have no message, maybe the key was in fact a full key so try that
795 if ( $message === false ) {
796 $parts = explode( '/', $lckey );
797 // We may get calls for things that are http-urls from sidebar
798 // Let's not load nonexistent languages for those
799 // They usually have more than one slash.
800 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
801 $message = Language
::getMessageFor( $parts[0], $parts[1] );
802 if ( $message === null ) {
808 // Post-processing if the message exists
809 if ( $message !== false ) {
811 $message = str_replace(
813 # Fix for trailing whitespace, removed by textarea
815 # Fix for NBSP, converted to space by firefox
832 * Given a language, try and fetch a message from that language, then the
833 * fallbacks of that language, then the site language, then the fallbacks for the
836 * @param Language $lang Requested language
837 * @param string $lckey Lowercase key for the message
838 * @param string $uckey Uppercase key for the message
839 * @param bool $useDB Whether to use the database
841 * @see MessageCache::get
842 * @return string|bool The message, or false if not found
844 protected function getMessageFromFallbackChain( $lang, $lckey, $uckey, $useDB ) {
845 global $wgLanguageCode, $wgContLang;
847 $langcode = $lang->getCode();
850 // First try the requested language.
852 if ( $langcode === $wgLanguageCode ) {
853 // Messages created in the content language will not have the /lang extension
854 $message = $this->getMsgFromNamespace( $uckey, $langcode );
856 $message = $this->getMsgFromNamespace( "$uckey/$langcode", $langcode );
860 if ( $message !== false ) {
864 // Check the CDB cache
865 $message = $lang->getMessage( $lckey );
866 if ( $message !== null ) {
870 list( $fallbackChain, $siteFallbackChain ) =
871 Language
::getFallbacksIncludingSiteLanguage( $langcode );
873 // Next try checking the database for all of the fallback languages of the requested language.
875 foreach ( $fallbackChain as $code ) {
876 if ( $code === $wgLanguageCode ) {
877 // Messages created in the content language will not have the /lang extension
878 $message = $this->getMsgFromNamespace( $uckey, $code );
880 $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
883 if ( $message !== false ) {
884 // Found the message.
890 // Now try checking the site language.
892 $message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode );
893 if ( $message !== false ) {
898 $message = $wgContLang->getMessage( $lckey );
899 if ( $message !== null ) {
903 // Finally try the DB for the site language's fallbacks.
905 foreach ( $siteFallbackChain as $code ) {
906 $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
907 if ( $message === false && $code === $wgLanguageCode ) {
908 // Messages created in the content language will not have the /lang extension
909 $message = $this->getMsgFromNamespace( $uckey, $code );
912 if ( $message !== false ) {
913 // Found the message.
923 * Get a message from the MediaWiki namespace, with caching. The key must
924 * first be converted to two-part lang/msg form if necessary.
926 * Unlike self::get(), this function doesn't resolve fallback chains, and
927 * some callers require this behavior. LanguageConverter::parseCachedTable()
928 * and self::get() are some examples in core.
930 * @param string $title Message cache key with initial uppercase letter.
931 * @param string $code Code denoting the language to try.
932 * @return string|bool The message, or false if it does not exist or on error
934 function getMsgFromNamespace( $title, $code ) {
935 $this->load( $code );
936 if ( isset( $this->mCache
[$code][$title] ) ) {
937 $entry = $this->mCache
[$code][$title];
938 if ( substr( $entry, 0, 1 ) === ' ' ) {
939 // The message exists, so make sure a string
941 return (string)substr( $entry, 1 );
942 } elseif ( $entry === '!NONEXISTENT' ) {
944 } elseif ( $entry === '!TOO BIG' ) {
945 // Fall through and try invididual message cache below
948 // XXX: This is not cached in process cache, should it?
950 Hooks
::run( 'MessagesPreLoad', array( $title, &$message ) );
951 if ( $message !== false ) {
958 # Try the individual message cache
959 $titleKey = wfMemcKey( 'messages', 'individual', $title );
960 $entry = $this->mMemc
->get( $titleKey );
962 if ( substr( $entry, 0, 1 ) === ' ' ) {
963 $this->mCache
[$code][$title] = $entry;
965 // The message exists, so make sure a string
967 return (string)substr( $entry, 1 );
968 } elseif ( $entry === '!NONEXISTENT' ) {
969 $this->mCache
[$code][$title] = '!NONEXISTENT';
973 # Corrupt/obsolete entry, delete it
974 $this->mMemc
->delete( $titleKey );
978 # Try loading it from the database
979 $revision = Revision
::newFromTitle(
980 Title
::makeTitle( NS_MEDIAWIKI
, $title ), false, Revision
::READ_LATEST
983 $content = $revision->getContent();
985 // A possibly temporary loading failure.
988 __METHOD__
. ": failed to load message page text for {$title} ($code)"
990 $message = null; // no negative caching
992 // XXX: Is this the right way to turn a Content object into a message?
993 // NOTE: $content is typically either WikitextContent, JavaScriptContent or
994 // CssContent. MessageContent is *not* used for storing messages, it's
995 // only used for wrapping them when needed.
996 $message = $content->getWikitextForTransclusion();
998 if ( $message === false ||
$message === null ) {
1001 __METHOD__
. ": message content doesn't provide wikitext "
1002 . "(content model: " . $content->getContentHandler() . ")"
1005 $message = false; // negative caching
1007 $this->mCache
[$code][$title] = ' ' . $message;
1008 $this->mMemc
->set( $titleKey, ' ' . $message, $this->mExpiry
);
1012 $message = false; // negative caching
1015 if ( $message === false ) { // negative caching
1016 $this->mCache
[$code][$title] = '!NONEXISTENT';
1017 $this->mMemc
->set( $titleKey, '!NONEXISTENT', $this->mExpiry
);
1024 * @param string $message
1025 * @param bool $interface
1026 * @param string $language Language code
1027 * @param Title $title
1030 function transform( $message, $interface = false, $language = null, $title = null ) {
1031 // Avoid creating parser if nothing to transform
1032 if ( strpos( $message, '{{' ) === false ) {
1036 if ( $this->mInParser
) {
1040 $parser = $this->getParser();
1042 $popts = $this->getParserOptions();
1043 $popts->setInterfaceMessage( $interface );
1044 $popts->setTargetLanguage( $language );
1046 $userlang = $popts->setUserLang( $language );
1047 $this->mInParser
= true;
1048 $message = $parser->transformMsg( $message, $popts, $title );
1049 $this->mInParser
= false;
1050 $popts->setUserLang( $userlang );
1059 function getParser() {
1060 global $wgParser, $wgParserConf;
1061 if ( !$this->mParser
&& isset( $wgParser ) ) {
1062 # Do some initialisation so that we don't have to do it twice
1063 $wgParser->firstCallInit();
1064 # Clone it and store it
1065 $class = $wgParserConf['class'];
1066 if ( $class == 'ParserDiffTest' ) {
1068 $this->mParser
= new $class( $wgParserConf );
1070 $this->mParser
= clone $wgParser;
1074 return $this->mParser
;
1078 * @param string $text
1079 * @param Title $title
1080 * @param bool $linestart Whether or not this is at the start of a line
1081 * @param bool $interface Whether this is an interface message
1082 * @param string $language Language code
1083 * @return ParserOutput|string
1085 public function parse( $text, $title = null, $linestart = true,
1086 $interface = false, $language = null
1088 if ( $this->mInParser
) {
1089 return htmlspecialchars( $text );
1092 $parser = $this->getParser();
1093 $popts = $this->getParserOptions();
1094 $popts->setInterfaceMessage( $interface );
1095 $popts->setTargetLanguage( $language );
1097 if ( !$title ||
!$title instanceof Title
) {
1099 wfDebugLog( 'GlobalTitleFail', __METHOD__
. ' called by ' .
1100 wfGetAllCallers( 5 ) . ' with no title set.' );
1103 // Sometimes $wgTitle isn't set either...
1105 # It's not uncommon having a null $wgTitle in scripts. See r80898
1106 # Create a ghost title in such case
1107 $title = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/title not set in ' . __METHOD__
);
1110 $this->mInParser
= true;
1111 $res = $parser->parse( $text, $title, $popts, $linestart );
1112 $this->mInParser
= false;
1117 function disable() {
1118 $this->mDisable
= true;
1122 $this->mDisable
= false;
1126 * Clear all stored messages. Mainly used after a mass rebuild.
1129 $langs = Language
::fetchLanguageNames( null, 'mw' );
1130 foreach ( array_keys( $langs ) as $code ) {
1131 # Global and local caches
1132 $this->wanCache
->touchCheckKey( wfMemcKey( 'messages', $code ) );
1135 $this->mLoadedLanguages
= array();
1139 * @param string $key
1142 public function figureMessage( $key ) {
1143 global $wgLanguageCode;
1145 $pieces = explode( '/', $key );
1146 if ( count( $pieces ) < 2 ) {
1147 return array( $key, $wgLanguageCode );
1150 $lang = array_pop( $pieces );
1151 if ( !Language
::fetchLanguageName( $lang, null, 'mw' ) ) {
1152 return array( $key, $wgLanguageCode );
1155 $message = implode( '/', $pieces );
1157 return array( $message, $lang );
1161 * Get all message keys stored in the message cache for a given language.
1162 * If $code is the content language code, this will return all message keys
1163 * for which MediaWiki:msgkey exists. If $code is another language code, this
1164 * will ONLY return message keys for which MediaWiki:msgkey/$code exists.
1165 * @param string $code Language code
1166 * @return array Array of message keys (strings)
1168 public function getAllMessageKeys( $code ) {
1170 $this->load( $code );
1171 if ( !isset( $this->mCache
[$code] ) ) {
1172 // Apparently load() failed
1175 // Remove administrative keys
1176 $cache = $this->mCache
[$code];
1177 unset( $cache['VERSION'] );
1178 unset( $cache['EXPIRY'] );
1179 // Remove any !NONEXISTENT keys
1180 $cache = array_diff( $cache, array( '!NONEXISTENT' ) );
1182 // Keys may appear with a capital first letter. lcfirst them.
1183 return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) );