More multi-DC tweaks to MessageCache::load()
[lhc/web/wiklou.git] / includes / cache / MessageCache.php
1 <?php
2 /**
3 * Localisation messages cache.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * MediaWiki message cache structure version.
26 * Bump this whenever the message cache format has changed.
27 */
28 define( 'MSG_CACHE_VERSION', 2 );
29
30 /**
31 * Memcached timeout when loading a key.
32 * See MessageCache::load()
33 */
34 define( 'MSG_LOAD_TIMEOUT', 60 );
35
36 /**
37 * Memcached timeout when locking a key for a writing operation.
38 * See MessageCache::lock()
39 */
40 define( 'MSG_LOCK_TIMEOUT', 30 );
41 /**
42 * Number of times we will try to acquire a lock from Memcached.
43 * This comes in addition to MSG_LOCK_TIMEOUT.
44 */
45 define( 'MSG_WAIT_TIMEOUT', 30 );
46
47 /**
48 * Message cache
49 * Performs various MediaWiki namespace-related functions
50 * @ingroup Cache
51 */
52 class MessageCache {
53 const FOR_UPDATE = 1; // force message reload
54
55 /**
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
60 * caching.
61 * @var array $mCache
62 */
63 protected $mCache;
64
65 /**
66 * Should mean that database cannot be used, but check
67 * @var bool $mDisable
68 */
69 protected $mDisable;
70
71 /**
72 * Lifetime for cache, used by object caching.
73 * Set on construction, see __construct().
74 */
75 protected $mExpiry;
76
77 /**
78 * Message cache has its own parser which it uses to transform
79 * messages.
80 */
81 protected $mParserOptions, $mParser;
82
83 /**
84 * Variable for tracking which variables are already loaded
85 * @var array $mLoadedLanguages
86 */
87 protected $mLoadedLanguages = array();
88
89 /**
90 * @var bool $mInParser
91 */
92 protected $mInParser = false;
93
94 /** @var BagOStuff */
95 protected $mMemc;
96 /** @var WANObjectCache */
97 protected $wanCache;
98
99 /**
100 * Singleton instance
101 *
102 * @var MessageCache $instance
103 */
104 private static $instance;
105
106 /**
107 * Get the signleton instance of this class
108 *
109 * @since 1.18
110 * @return MessageCache
111 */
112 public static function singleton() {
113 if ( is_null( self::$instance ) ) {
114 global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
115 self::$instance = new self(
116 wfGetMessageCacheStorage(),
117 $wgUseDatabaseMessages,
118 $wgMsgCacheExpiry
119 );
120 }
121
122 return self::$instance;
123 }
124
125 /**
126 * Destroy the singleton instance
127 *
128 * @since 1.18
129 */
130 public static function destroyInstance() {
131 self::$instance = null;
132 }
133
134 /**
135 * Normalize message key input
136 *
137 * @param string $key Input message key to be normalized
138 * @return string Normalized message key
139 */
140 public static function normalizeKey( $key ) {
141 global $wgContLang;
142 $lckey = strtr( $key, ' ', '_' );
143 if ( ord( $lckey ) < 128 ) {
144 $lckey[0] = strtolower( $lckey[0] );
145 } else {
146 $lckey = $wgContLang->lcfirst( $lckey );
147 }
148
149 return $lckey;
150 }
151
152 /**
153 * @param BagOStuff $memCached A cache instance. If none, fall back to CACHE_NONE.
154 * @param bool $useDB
155 * @param int $expiry Lifetime for cache. @see $mExpiry.
156 */
157 function __construct( $memCached, $useDB, $expiry ) {
158 global $wgUseLocalMessageCache;
159
160 if ( !$memCached ) {
161 $memCached = wfGetCache( CACHE_NONE );
162 }
163
164 $this->mMemc = $memCached;
165 $this->mDisable = !$useDB;
166 $this->mExpiry = $expiry;
167
168 if ( $wgUseLocalMessageCache ) {
169 $this->localCache = ObjectCache::newAccelerator( array(), CACHE_NONE );
170 } else {
171 $this->localCache = wfGetCache( CACHE_NONE );
172 }
173
174 $this->wanCache = ObjectCache::getMainWANInstance();
175 }
176
177 /**
178 * ParserOptions is lazy initialised.
179 *
180 * @return ParserOptions
181 */
182 function getParserOptions() {
183 if ( !$this->mParserOptions ) {
184 $this->mParserOptions = new ParserOptions;
185 $this->mParserOptions->setEditSection( false );
186 }
187
188 return $this->mParserOptions;
189 }
190
191 /**
192 * Try to load the cache from APC.
193 *
194 * @param string $code Optional language code, see documenation of load().
195 * @return array|bool The cache array, or false if not in cache.
196 */
197 protected function getLocalCache( $code ) {
198 $cacheKey = wfMemcKey( __CLASS__, $code );
199 return $this->localCache->get( $cacheKey );
200 }
201
202 /**
203 * Save the cache to APC.
204 *
205 * @param string $code
206 * @param array $cache The cache array
207 */
208 protected function saveToLocalCache( $code, $cache ) {
209 $cacheKey = wfMemcKey( __CLASS__, $code );
210 $this->localCache->set( $cacheKey, $cache );
211 }
212
213 /**
214 * Loads messages from caches or from database in this order:
215 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
216 * (2) memcached
217 * (3) from the database.
218 *
219 * When succesfully loading from (2) or (3), all higher level caches are
220 * updated for the newest version.
221 *
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?).
224 *
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
227 * is disabled.
228 *
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
232 * @return bool
233 */
234 function load( $code = false, $mode = null ) {
235 global $wgUseLocalMessageCache;
236
237 if ( !is_string( $code ) ) {
238 # This isn't really nice, so at least make a note about it and try to
239 # fall back
240 wfDebug( __METHOD__ . " called without providing a language code\n" );
241 $code = 'en';
242 }
243
244 # Don't do double loading...
245 if ( isset( $this->mLoadedLanguages[$code] ) && $mode != self::FOR_UPDATE ) {
246 return true;
247 }
248
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;
255 }
256
257 return true;
258 }
259
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
264
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 );
268
269 if ( $wgUseLocalMessageCache && $hash ) {
270 # Try the local cache and check against the cluster hash key...
271 $cache = $this->getLocalCache( $code );
272 if ( !$cache ) {
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;
283 } else {
284 $where[] = 'got from local cache';
285 $success = true;
286 $this->mCache[$code] = $cache;
287 }
288 }
289
290 if ( !$success ) {
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';
301 } else {
302 $cache = $this->mMemc->get( $cacheKey );
303 if ( !$cache ) {
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;
314 } else {
315 $where[] = 'got from global cache';
316 $this->mCache[$code] = $cache;
317 $this->saveToCaches( $cache, 'local-only', $code );
318 $success = true;
319 }
320 }
321
322 if ( $success ) {
323 # Done, no need to retry
324 break;
325 }
326
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 ) ) {
331 $success = true;
332 break;
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;
337 $success = true;
338 break;
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.";
344 break;
345 } else {
346 $statusKey = wfMemcKey( 'messages', $code, 'status' );
347 $status = $this->mMemc->get( $statusKey );
348 if ( $status === 'error' ) {
349 # Disable cache
350 break;
351 } else {
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 );
356 }
357 }
358 }
359 }
360
361 if ( !$success ) {
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
367 } else {
368 # All good, just record the success
369 $this->mLoadedLanguages[$code] = true;
370 }
371
372 $info = implode( ', ', $where );
373 wfDebugLog( 'MessageCache', __METHOD__ . ": Loading $code... $info\n" );
374
375 return $success;
376 }
377
378 /**
379 * @param string $code
380 * @param array $where List of wfDebug() comments
381 * @return bool Lock acquired and loadFromDB() called
382 */
383 protected function loadFromDBWithLock( $code, array &$where ) {
384 global $wgUseLocalMessageCache;
385
386 $memCache = $this->mMemc;
387
388 $statusKey = wfMemcKey( 'messages', $code, 'status' );
389 if ( !$memCache->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT ) ) {
390 return false; // could not acquire lock
391 }
392
393 # Unlock the status key if there is an exception
394 $statusUnlocker = new ScopedCallback( function () use ( $memCache, $statusKey ) {
395 $memCache->delete( $statusKey );
396 } );
397
398 # Now let's regenerate
399 $where[] = 'loading from database';
400
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()
405 # may be discarded.
406 if ( $this->lock( $cacheKey ) ) {
407 $that = $this;
408 $mainUnlocker = new ScopedCallback( function () use ( $that, $cacheKey ) {
409 $that->unlock( $cacheKey );
410 } );
411 } else {
412 $mainUnlocker = null;
413 $where[] = 'could not acquire main lock';
414 }
415
416 $cache = $this->loadFromDB( $code );
417 $this->mCache[$code] = $cache;
418 $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
419
420 # Unlock
421 ScopedCallback::consume( $mainUnlocker );
422 ScopedCallback::consume( $statusUnlocker );
423
424 if ( !$saveSuccess ) {
425 # Cache save has failed.
426 # There are two main scenarios where this could be a problem:
427 #
428 # - The cache is more than the maximum size (typically
429 # 1MB compressed).
430 #
431 # - Memcached has no space remaining in the relevant slab
432 # class. This is unlikely with recent versions of
433 # memcached.
434 #
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';
443 } else {
444 $where[] = "could not save global cache";
445 }
446 }
447
448 return true;
449 }
450
451 /**
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.
455 *
456 * @param string $code Language code.
457 * @return array Loaded messages for storing in caches.
458 */
459 function loadFromDB( $code ) {
460 global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
461
462 $dbr = wfGetDB( DB_SLAVE );
463 $cache = array();
464
465 # Common conditions
466 $conds = array(
467 'page_is_redirect' => 0,
468 'page_namespace' => NS_MEDIAWIKI,
469 );
470
471 $mostused = array();
472 if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
473 if ( !isset( $this->mCache[$wgLanguageCode] ) ) {
474 $this->load( $wgLanguageCode );
475 }
476 $mostused = array_keys( $this->mCache[$wgLanguageCode] );
477 foreach ( $mostused as $key => $value ) {
478 $mostused[$key] = "$value/$code";
479 }
480 }
481
482 if ( count( $mostused ) ) {
483 $conds['page_title'] = $mostused;
484 } elseif ( $code !== $wgLanguageCode ) {
485 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
486 } else {
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() );
490 }
491
492 # Conditions to fetch oversized pages to ignore them
493 $bigConds = $conds;
494 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
495
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';
500 }
501
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 );
507
508 $res = $dbr->select(
509 array( 'page', 'revision', 'text' ),
510 array( 'page_title', 'old_text', 'old_flags' ),
511 $smallConds,
512 __METHOD__ . "($code)-small"
513 );
514
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...
520 $entry = '!TOO BIG';
521 wfDebugLog(
522 'MessageCache',
523 __METHOD__
524 . ": failed to load message page text for {$row->page_title} ($code)"
525 );
526 } else {
527 $entry = ' ' . $text;
528 }
529 $cache[$row->page_title] = $entry;
530 }
531
532 $cache['VERSION'] = MSG_CACHE_VERSION;
533 $cache['HASH'] = wfRandomString( 8 );
534 $cache['EXPIRY'] = wfTimestamp( TS_MW, time() + $this->mExpiry );
535
536 return $cache;
537 }
538
539 /**
540 * Updates cache as necessary when message page is changed
541 *
542 * @param string $title Name of the page changed.
543 * @param mixed $text New contents of the page.
544 */
545 public function replace( $title, $text ) {
546 global $wgMaxMsgCacheEntrySize, $wgContLang, $wgLanguageCode;
547
548 if ( $this->mDisable ) {
549 return;
550 }
551
552 list( $msg, $code ) = $this->figureMessage( $title );
553 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
554 # Content language overrides do not use the /<code> suffix
555 return;
556 }
557
558 $cacheKey = wfMemcKey( 'messages', $code );
559 $this->lock( $cacheKey );
560 $this->load( $code, self::FOR_UPDATE );
561
562 $titleKey = wfMemcKey( 'messages', 'individual', $title );
563
564 if ( $text === false ) {
565 # Article was deleted
566 $this->mCache[$code][$title] = '!NONEXISTENT';
567 $this->mMemc->delete( $titleKey );
568 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
569 # Check for size
570 $this->mCache[$code][$title] = '!TOO BIG';
571 $this->mMemc->set( $titleKey, ' ' . $text, $this->mExpiry );
572 } else {
573 $this->mCache[$code][$title] = ' ' . $text;
574 $this->mMemc->delete( $titleKey );
575 }
576
577 # Update caches
578 $this->saveToCaches( $this->mCache[$code], 'all', $code );
579 $this->unlock( $cacheKey );
580 $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
581
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
586 // sidebar messages
587 $codes = array_keys( Language::fetchLanguageNames() );
588 }
589
590 foreach ( $codes as $code ) {
591 $sidebarKey = wfMemcKey( 'sidebar', $code );
592 $this->wanCache->delete( $sidebarKey, 5 );
593 }
594
595 // Update the message in the message blob store
596 $blobStore = new MessageBlobStore();
597 $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
598
599 Hooks::run( 'MessageCacheReplace', array( $title, $text ) );
600 }
601
602 /**
603 * Is the given cache array expired due to time passing or a version change?
604 *
605 * @param array $cache
606 * @return bool
607 */
608 protected function isCacheExpired( $cache ) {
609 if ( !isset( $cache['VERSION'] ) || !isset( $cache['EXPIRY'] ) ) {
610 return true;
611 }
612 if ( $cache['VERSION'] != MSG_CACHE_VERSION ) {
613 return true;
614 }
615 if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
616 return true;
617 }
618
619 return false;
620 }
621
622 /**
623 * Shortcut to update caches.
624 *
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)
629 * @return bool
630 */
631 protected function saveToCaches( $cache, $dest, $code = false ) {
632 global $wgUseLocalMessageCache;
633
634 if ( $dest === 'all' ) {
635 $cacheKey = wfMemcKey( 'messages', $code );
636 $success = $this->mMemc->set( $cacheKey, $cache );
637 } else {
638 $success = true;
639 }
640
641 # Save to local cache
642 if ( $wgUseLocalMessageCache ) {
643 $this->setValidationHash( $code, $cache['HASH'] );
644 $this->saveToLocalCache( $code, $cache );
645 }
646
647 return $success;
648 }
649
650 /**
651 * Get the md5 used to validate the local disk cache
652 *
653 * @param string $code
654 * @return array (hash or false, bool expiry/volatility status)
655 */
656 protected function getValidationHash( $code ) {
657 $curTTL = null;
658 $value = $this->wanCache->get(
659 wfMemcKey( 'messages', $code, 'hash' ),
660 $curTTL,
661 array( wfMemcKey( 'messages', $code ) )
662 );
663 $expired = ( $curTTL === null || $curTTL < 0 );
664
665 return array( $value, $expired );
666 }
667
668 /**
669 * Set the md5 used to validate the local disk cache
670 *
671 * @param string $code
672 * @param string $hash
673 */
674 protected function setValidationHash( $code, $hash ) {
675 $this->wanCache->set(
676 wfMemcKey( 'messages', $code, 'hash' ),
677 $hash,
678 WANObjectCache::TTL_NONE
679 );
680 }
681
682 /**
683 * Represents a write lock on the messages key.
684 *
685 * Will retry MessageCache::MSG_WAIT_TIMEOUT times, each operations having
686 * a timeout of MessageCache::MSG_LOCK_TIMEOUT.
687 *
688 * @param string $key
689 * @return bool Success
690 */
691 function lock( $key ) {
692 $lockKey = $key . ':lock';
693 $acquired = false;
694 $testDone = false;
695 for ( $i = 0; $i < MSG_WAIT_TIMEOUT && !$acquired; $i++ ) {
696 $acquired = $this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT );
697 if ( $acquired ) {
698 break;
699 }
700
701 # Fail fast if memcached is totally down
702 if ( !$testDone ) {
703 $testDone = true;
704 if ( !$this->mMemc->set( wfMemcKey( 'test' ), 'test', 1 ) ) {
705 break;
706 }
707 }
708 sleep( 1 );
709 }
710
711 return $acquired;
712 }
713
714 function unlock( $key ) {
715 $lockKey = $key . ':lock';
716 $this->mMemc->delete( $lockKey );
717 }
718
719 /**
720 * Get a message from either the content language or the user language.
721 *
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.
730 *
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.
737 *
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".
748 *
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)
752 */
753 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
754 global $wgContLang;
755
756 if ( is_int( $key ) ) {
757 // Fix numerical strings that somehow become ints
758 // on their way here
759 $key = (string)$key;
760 } elseif ( !is_string( $key ) ) {
761 throw new MWException( 'Non-string key given' );
762 } elseif ( $key === '' ) {
763 // Shortcut: the empty key is always missing
764 return false;
765 }
766
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 );
772 }
773
774 // Normalise title-case input (with some inlining)
775 $lckey = MessageCache::normalizeKey( $key );
776
777 Hooks::run( 'MessageCache::get', array( &$lckey ) );
778
779 if ( ord( $lckey ) < 128 ) {
780 $uckey = ucfirst( $lckey );
781 } else {
782 $uckey = $wgContLang->ucfirst( $lckey );
783 }
784
785 // Loop through each language in the fallback list until we find something useful
786 $lang = wfGetLangObj( $langcode );
787 $message = $this->getMessageFromFallbackChain(
788 $lang,
789 $lckey,
790 $uckey,
791 !$this->mDisable && $useDB
792 );
793
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 ) {
803 $message = false;
804 }
805 }
806 }
807
808 // Post-processing if the message exists
809 if ( $message !== false ) {
810 // Fix whitespace
811 $message = str_replace(
812 array(
813 # Fix for trailing whitespace, removed by textarea
814 '&#32;',
815 # Fix for NBSP, converted to space by firefox
816 '&nbsp;',
817 '&#160;',
818 ),
819 array(
820 ' ',
821 "\xc2\xa0",
822 "\xc2\xa0"
823 ),
824 $message
825 );
826 }
827
828 return $message;
829 }
830
831 /**
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
834 * site language.
835 *
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
840 *
841 * @see MessageCache::get
842 * @return string|bool The message, or false if not found
843 */
844 protected function getMessageFromFallbackChain( $lang, $lckey, $uckey, $useDB ) {
845 global $wgLanguageCode, $wgContLang;
846
847 $langcode = $lang->getCode();
848 $message = false;
849
850 // First try the requested language.
851 if ( $useDB ) {
852 if ( $langcode === $wgLanguageCode ) {
853 // Messages created in the content language will not have the /lang extension
854 $message = $this->getMsgFromNamespace( $uckey, $langcode );
855 } else {
856 $message = $this->getMsgFromNamespace( "$uckey/$langcode", $langcode );
857 }
858 }
859
860 if ( $message !== false ) {
861 return $message;
862 }
863
864 // Check the CDB cache
865 $message = $lang->getMessage( $lckey );
866 if ( $message !== null ) {
867 return $message;
868 }
869
870 list( $fallbackChain, $siteFallbackChain ) =
871 Language::getFallbacksIncludingSiteLanguage( $langcode );
872
873 // Next try checking the database for all of the fallback languages of the requested language.
874 if ( $useDB ) {
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 );
879 } else {
880 $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
881 }
882
883 if ( $message !== false ) {
884 // Found the message.
885 return $message;
886 }
887 }
888 }
889
890 // Now try checking the site language.
891 if ( $useDB ) {
892 $message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode );
893 if ( $message !== false ) {
894 return $message;
895 }
896 }
897
898 $message = $wgContLang->getMessage( $lckey );
899 if ( $message !== null ) {
900 return $message;
901 }
902
903 // Finally try the DB for the site language's fallbacks.
904 if ( $useDB ) {
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 );
910 }
911
912 if ( $message !== false ) {
913 // Found the message.
914 return $message;
915 }
916 }
917 }
918
919 return false;
920 }
921
922 /**
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.
925 *
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.
929 *
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
933 */
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
940 // is returned.
941 return (string)substr( $entry, 1 );
942 } elseif ( $entry === '!NONEXISTENT' ) {
943 return false;
944 } elseif ( $entry === '!TOO BIG' ) {
945 // Fall through and try invididual message cache below
946 }
947 } else {
948 // XXX: This is not cached in process cache, should it?
949 $message = false;
950 Hooks::run( 'MessagesPreLoad', array( $title, &$message ) );
951 if ( $message !== false ) {
952 return $message;
953 }
954
955 return false;
956 }
957
958 # Try the individual message cache
959 $titleKey = wfMemcKey( 'messages', 'individual', $title );
960 $entry = $this->mMemc->get( $titleKey );
961 if ( $entry ) {
962 if ( substr( $entry, 0, 1 ) === ' ' ) {
963 $this->mCache[$code][$title] = $entry;
964
965 // The message exists, so make sure a string
966 // is returned.
967 return (string)substr( $entry, 1 );
968 } elseif ( $entry === '!NONEXISTENT' ) {
969 $this->mCache[$code][$title] = '!NONEXISTENT';
970
971 return false;
972 } else {
973 # Corrupt/obsolete entry, delete it
974 $this->mMemc->delete( $titleKey );
975 }
976 }
977
978 # Try loading it from the database
979 $revision = Revision::newFromTitle(
980 Title::makeTitle( NS_MEDIAWIKI, $title ), false, Revision::READ_LATEST
981 );
982 if ( $revision ) {
983 $content = $revision->getContent();
984 if ( !$content ) {
985 // A possibly temporary loading failure.
986 wfDebugLog(
987 'MessageCache',
988 __METHOD__ . ": failed to load message page text for {$title} ($code)"
989 );
990 $message = null; // no negative caching
991 } else {
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();
997
998 if ( $message === false || $message === null ) {
999 wfDebugLog(
1000 'MessageCache',
1001 __METHOD__ . ": message content doesn't provide wikitext "
1002 . "(content model: " . $content->getContentHandler() . ")"
1003 );
1004
1005 $message = false; // negative caching
1006 } else {
1007 $this->mCache[$code][$title] = ' ' . $message;
1008 $this->mMemc->set( $titleKey, ' ' . $message, $this->mExpiry );
1009 }
1010 }
1011 } else {
1012 $message = false; // negative caching
1013 }
1014
1015 if ( $message === false ) { // negative caching
1016 $this->mCache[$code][$title] = '!NONEXISTENT';
1017 $this->mMemc->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
1018 }
1019
1020 return $message;
1021 }
1022
1023 /**
1024 * @param string $message
1025 * @param bool $interface
1026 * @param string $language Language code
1027 * @param Title $title
1028 * @return string
1029 */
1030 function transform( $message, $interface = false, $language = null, $title = null ) {
1031 // Avoid creating parser if nothing to transform
1032 if ( strpos( $message, '{{' ) === false ) {
1033 return $message;
1034 }
1035
1036 if ( $this->mInParser ) {
1037 return $message;
1038 }
1039
1040 $parser = $this->getParser();
1041 if ( $parser ) {
1042 $popts = $this->getParserOptions();
1043 $popts->setInterfaceMessage( $interface );
1044 $popts->setTargetLanguage( $language );
1045
1046 $userlang = $popts->setUserLang( $language );
1047 $this->mInParser = true;
1048 $message = $parser->transformMsg( $message, $popts, $title );
1049 $this->mInParser = false;
1050 $popts->setUserLang( $userlang );
1051 }
1052
1053 return $message;
1054 }
1055
1056 /**
1057 * @return Parser
1058 */
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' ) {
1067 # Uncloneable
1068 $this->mParser = new $class( $wgParserConf );
1069 } else {
1070 $this->mParser = clone $wgParser;
1071 }
1072 }
1073
1074 return $this->mParser;
1075 }
1076
1077 /**
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
1084 */
1085 public function parse( $text, $title = null, $linestart = true,
1086 $interface = false, $language = null
1087 ) {
1088 if ( $this->mInParser ) {
1089 return htmlspecialchars( $text );
1090 }
1091
1092 $parser = $this->getParser();
1093 $popts = $this->getParserOptions();
1094 $popts->setInterfaceMessage( $interface );
1095 $popts->setTargetLanguage( $language );
1096
1097 if ( !$title || !$title instanceof Title ) {
1098 global $wgTitle;
1099 wfDebugLog( 'GlobalTitleFail', __METHOD__ . ' called by ' .
1100 wfGetAllCallers( 5 ) . ' with no title set.' );
1101 $title = $wgTitle;
1102 }
1103 // Sometimes $wgTitle isn't set either...
1104 if ( !$title ) {
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__ );
1108 }
1109
1110 $this->mInParser = true;
1111 $res = $parser->parse( $text, $title, $popts, $linestart );
1112 $this->mInParser = false;
1113
1114 return $res;
1115 }
1116
1117 function disable() {
1118 $this->mDisable = true;
1119 }
1120
1121 function enable() {
1122 $this->mDisable = false;
1123 }
1124
1125 /**
1126 * Clear all stored messages. Mainly used after a mass rebuild.
1127 */
1128 function clear() {
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 ) );
1133 }
1134
1135 $this->mLoadedLanguages = array();
1136 }
1137
1138 /**
1139 * @param string $key
1140 * @return array
1141 */
1142 public function figureMessage( $key ) {
1143 global $wgLanguageCode;
1144
1145 $pieces = explode( '/', $key );
1146 if ( count( $pieces ) < 2 ) {
1147 return array( $key, $wgLanguageCode );
1148 }
1149
1150 $lang = array_pop( $pieces );
1151 if ( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
1152 return array( $key, $wgLanguageCode );
1153 }
1154
1155 $message = implode( '/', $pieces );
1156
1157 return array( $message, $lang );
1158 }
1159
1160 /**
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)
1167 */
1168 public function getAllMessageKeys( $code ) {
1169 global $wgContLang;
1170 $this->load( $code );
1171 if ( !isset( $this->mCache[$code] ) ) {
1172 // Apparently load() failed
1173 return null;
1174 }
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' ) );
1181
1182 // Keys may appear with a capital first letter. lcfirst them.
1183 return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) );
1184 }
1185 }