MediaWiki REL1_29
MessageCache.php
Go to the documentation of this file.
1<?php
24use Wikimedia\ScopedCallback;
27
32define( 'MSG_CACHE_VERSION', 2 );
33
40 const FOR_UPDATE = 1; // force message reload
41
43 const WAIT_SEC = 15;
45 const LOCK_TTL = 30;
46
55 protected $mCache;
56
60 protected $mCacheVolatile = [];
61
66 protected $mDisable;
67
72 protected $mExpiry;
73
78 protected $mParserOptions;
80 protected $mParser;
81
86 protected $mLoadedLanguages = [];
87
91 protected $mInParser = false;
92
94 protected $wanCache;
96 protected $clusterCache;
98 protected $srvCache;
99
105 private static $instance;
106
113 public static function singleton() {
114 if ( self::$instance === null ) {
116 self::$instance = new self(
117 MediaWikiServices::getInstance()->getMainWANObjectCache(),
120 ? MediaWikiServices::getInstance()->getLocalServerObjectCache()
121 : new EmptyBagOStuff(),
124 );
125 }
126
127 return self::$instance;
128 }
129
135 public static function destroyInstance() {
136 self::$instance = null;
137 }
138
145 public static function normalizeKey( $key ) {
147
148 $lckey = strtr( $key, ' ', '_' );
149 if ( ord( $lckey ) < 128 ) {
150 $lckey[0] = strtolower( $lckey[0] );
151 } else {
152 $lckey = $wgContLang->lcfirst( $lckey );
153 }
154
155 return $lckey;
156 }
157
165 public function __construct(
169 $useDB,
170 $expiry
171 ) {
172 $this->wanCache = $wanCache;
173 $this->clusterCache = $clusterCache;
174 $this->srvCache = $srvCache;
175
176 $this->mDisable = !$useDB;
177 $this->mExpiry = $expiry;
178 }
179
185 function getParserOptions() {
187
188 if ( !$this->mParserOptions ) {
189 if ( !$wgUser->isSafeToLoad() ) {
190 // $wgUser isn't unstubbable yet, so don't try to get a
191 // ParserOptions for it. And don't cache this ParserOptions
192 // either.
194 $po->setEditSection( false );
195 $po->setAllowUnsafeRawHtml( false );
196 return $po;
197 }
198
199 $this->mParserOptions = new ParserOptions;
200 $this->mParserOptions->setEditSection( false );
201 // Messages may take parameters that could come
202 // from malicious sources. As a precaution, disable
203 // the <html> parser tag when parsing messages.
204 $this->mParserOptions->setAllowUnsafeRawHtml( false );
205 }
206
208 }
209
216 protected function getLocalCache( $code ) {
217 $cacheKey = wfMemcKey( __CLASS__, $code );
218
219 return $this->srvCache->get( $cacheKey );
220 }
221
228 protected function saveToLocalCache( $code, $cache ) {
229 $cacheKey = wfMemcKey( __CLASS__, $code );
230 $this->srvCache->set( $cacheKey, $cache );
231 }
232
254 protected function load( $code, $mode = null ) {
255 if ( !is_string( $code ) ) {
256 throw new InvalidArgumentException( "Missing language code" );
257 }
258
259 # Don't do double loading...
260 if ( isset( $this->mLoadedLanguages[$code] ) && $mode != self::FOR_UPDATE ) {
261 return true;
262 }
263
264 # 8 lines of code just to say (once) that message cache is disabled
265 if ( $this->mDisable ) {
266 static $shownDisabled = false;
267 if ( !$shownDisabled ) {
268 wfDebug( __METHOD__ . ": disabled\n" );
269 $shownDisabled = true;
270 }
271
272 return true;
273 }
274
275 # Loading code starts
276 $success = false; # Keep track of success
277 $staleCache = false; # a cache array with expired data, or false if none has been loaded
278 $where = []; # Debug info, delayed to avoid spamming debug log too much
279
280 # Hash of the contents is stored in memcache, to detect if data-center cache
281 # or local cache goes out of date (e.g. due to replace() on some other server)
282 list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
283 $this->mCacheVolatile[$code] = $hashVolatile;
284
285 # Try the local cache and check against the cluster hash key...
286 $cache = $this->getLocalCache( $code );
287 if ( !$cache ) {
288 $where[] = 'local cache is empty';
289 } elseif ( !isset( $cache['HASH'] ) || $cache['HASH'] !== $hash ) {
290 $where[] = 'local cache has the wrong hash';
291 $staleCache = $cache;
292 } elseif ( $this->isCacheExpired( $cache ) ) {
293 $where[] = 'local cache is expired';
294 $staleCache = $cache;
295 } elseif ( $hashVolatile ) {
296 $where[] = 'local cache validation key is expired/volatile';
297 $staleCache = $cache;
298 } else {
299 $where[] = 'got from local cache';
300 $success = true;
301 $this->mCache[$code] = $cache;
302 }
303
304 if ( !$success ) {
305 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
306 # Try the global cache. If it is empty, try to acquire a lock. If
307 # the lock can't be acquired, wait for the other thread to finish
308 # and then try the global cache a second time.
309 for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) {
310 if ( $hashVolatile && $staleCache ) {
311 # Do not bother fetching the whole cache blob to avoid I/O.
312 # Instead, just try to get the non-blocking $statusKey lock
313 # below, and use the local stale value if it was not acquired.
314 $where[] = 'global cache is presumed expired';
315 } else {
316 $cache = $this->clusterCache->get( $cacheKey );
317 if ( !$cache ) {
318 $where[] = 'global cache is empty';
319 } elseif ( $this->isCacheExpired( $cache ) ) {
320 $where[] = 'global cache is expired';
321 $staleCache = $cache;
322 } elseif ( $hashVolatile ) {
323 # DB results are replica DB lag prone until the holdoff TTL passes.
324 # By then, updates should be reflected in loadFromDBWithLock().
325 # One thread renerates the cache while others use old values.
326 $where[] = 'global cache is expired/volatile';
327 $staleCache = $cache;
328 } else {
329 $where[] = 'got from global cache';
330 $this->mCache[$code] = $cache;
331 $this->saveToCaches( $cache, 'local-only', $code );
332 $success = true;
333 }
334 }
335
336 if ( $success ) {
337 # Done, no need to retry
338 break;
339 }
340
341 # We need to call loadFromDB. Limit the concurrency to one process.
342 # This prevents the site from going down when the cache expires.
343 # Note that the DB slam protection lock here is non-blocking.
344 $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
345 if ( $loadStatus === true ) {
346 $success = true;
347 break;
348 } elseif ( $staleCache ) {
349 # Use the stale cache while some other thread constructs the new one
350 $where[] = 'using stale cache';
351 $this->mCache[$code] = $staleCache;
352 $success = true;
353 break;
354 } elseif ( $failedAttempts > 0 ) {
355 # Already blocked once, so avoid another lock/unlock cycle.
356 # This case will typically be hit if memcached is down, or if
357 # loadFromDB() takes longer than LOCK_WAIT.
358 $where[] = "could not acquire status key.";
359 break;
360 } elseif ( $loadStatus === 'cantacquire' ) {
361 # Wait for the other thread to finish, then retry. Normally,
362 # the memcached get() will then yeild the other thread's result.
363 $where[] = 'waited for other thread to complete';
364 $this->getReentrantScopedLock( $cacheKey );
365 } else {
366 # Disable cache; $loadStatus is 'disabled'
367 break;
368 }
369 }
370 }
371
372 if ( !$success ) {
373 $where[] = 'loading FAILED - cache is disabled';
374 $this->mDisable = true;
375 $this->mCache = false;
376 wfDebugLog( 'MessageCacheError', __METHOD__ . ": Failed to load $code\n" );
377 # This used to throw an exception, but that led to nasty side effects like
378 # the whole wiki being instantly down if the memcached server died
379 } else {
380 # All good, just record the success
381 $this->mLoadedLanguages[$code] = true;
382 }
383
384 $info = implode( ', ', $where );
385 wfDebugLog( 'MessageCache', __METHOD__ . ": Loading $code... $info\n" );
386
387 return $success;
388 }
389
396 protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
397 # If cache updates on all levels fail, give up on message overrides.
398 # This is to avoid easy site outages; see $saveSuccess comments below.
399 $statusKey = wfMemcKey( 'messages', $code, 'status' );
400 $status = $this->clusterCache->get( $statusKey );
401 if ( $status === 'error' ) {
402 $where[] = "could not load; method is still globally disabled";
403 return 'disabled';
404 }
405
406 # Now let's regenerate
407 $where[] = 'loading from database';
408
409 # Lock the cache to prevent conflicting writes.
410 # This lock is non-blocking so stale cache can quickly be used.
411 # Note that load() will call a blocking getReentrantScopedLock()
412 # after this if it really need to wait for any current thread.
413 $cacheKey = wfMemcKey( 'messages', $code );
414 $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
415 if ( !$scopedLock ) {
416 $where[] = 'could not acquire main lock';
417 return 'cantacquire';
418 }
419
420 $cache = $this->loadFromDB( $code, $mode );
421 $this->mCache[$code] = $cache;
422 $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
423
424 if ( !$saveSuccess ) {
438 if ( $this->srvCache instanceof EmptyBagOStuff ) {
439 $this->clusterCache->set( $statusKey, 'error', 60 * 5 );
440 $where[] = 'could not save cache, disabled globally for 5 minutes';
441 } else {
442 $where[] = "could not save global cache";
443 }
444 }
445
446 return true;
447 }
448
458 protected function loadFromDB( $code, $mode = null ) {
460
461 $dbr = wfGetDB( ( $mode == self::FOR_UPDATE ) ? DB_MASTER : DB_REPLICA );
462
463 $cache = [];
464
465 # Common conditions
466 $conds = [
467 'page_is_redirect' => 0,
468 'page_namespace' => NS_MEDIAWIKI,
469 ];
470
471 $mostused = [];
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' .
490 $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
491 }
492
493 # Conditions to fetch oversized pages to ignore them
494 $bigConds = $conds;
495 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
496
497 # Load titles for all oversized pages in the MediaWiki namespace
498 $res = $dbr->select(
499 'page',
500 [ 'page_title', 'page_latest' ],
501 $bigConds,
502 __METHOD__ . "($code)-big"
503 );
504 foreach ( $res as $row ) {
505 $cache[$row->page_title] = '!TOO BIG';
506 // At least include revision ID so page changes are reflected in the hash
507 $cache['EXCESSIVE'][$row->page_title] = $row->page_latest;
508 }
509
510 # Conditions to load the remaining pages with their contents
511 $smallConds = $conds;
512 $smallConds[] = 'page_latest=rev_id';
513 $smallConds[] = 'rev_text_id=old_id';
514 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
515
516 $res = $dbr->select(
517 [ 'page', 'revision', 'text' ],
518 [ 'page_title', 'old_id', 'old_text', 'old_flags' ],
519 $smallConds,
520 __METHOD__ . "($code)-small"
521 );
522
523 foreach ( $res as $row ) {
524 $text = Revision::getRevisionText( $row );
525 if ( $text === false ) {
526 // Failed to fetch data; possible ES errors?
527 // Store a marker to fetch on-demand as a workaround...
528 // TODO Use a differnt marker
529 $entry = '!TOO BIG';
531 'MessageCache',
532 __METHOD__
533 . ": failed to load message page text for {$row->page_title} ($code)"
534 );
535 } else {
536 $entry = ' ' . $text;
537 }
538 $cache[$row->page_title] = $entry;
539 }
540
541 $cache['VERSION'] = MSG_CACHE_VERSION;
542 ksort( $cache );
543
544 # Hash for validating local cache (APC). No need to take into account
545 # messages larger than $wgMaxMsgCacheEntrySize, since those are only
546 # stored and fetched from memcache.
547 $cache['HASH'] = md5( serialize( $cache ) );
548 $cache['EXPIRY'] = wfTimestamp( TS_MW, time() + $this->mExpiry );
549
550 return $cache;
551 }
552
559 public function replace( $title, $text ) {
561
562 if ( $this->mDisable ) {
563 return;
564 }
565
566 list( $msg, $code ) = $this->figureMessage( $title );
567 if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
568 // Content language overrides do not use the /<code> suffix
569 return;
570 }
571
572 // (a) Update the process cache with the new message text
573 if ( $text === false ) {
574 // Page deleted
575 $this->mCache[$code][$title] = '!NONEXISTENT';
576 } else {
577 // Ignore $wgMaxMsgCacheEntrySize so the process cache is up to date
578 $this->mCache[$code][$title] = ' ' . $text;
579 }
580
581 // (b) Update the shared caches in a deferred update with a fresh DB snapshot
582 DeferredUpdates::addCallableUpdate(
583 function () use ( $title, $msg, $code ) {
585 // Allow one caller at a time to avoid race conditions
586 $scopedLock = $this->getReentrantScopedLock( wfMemcKey( 'messages', $code ) );
587 if ( !$scopedLock ) {
588 LoggerFactory::getInstance( 'MessageCache' )->error(
589 __METHOD__ . ': could not acquire lock to update {title} ({code})',
590 [ 'title' => $title, 'code' => $code ] );
591 return;
592 }
593 // Load the messages from the master DB to avoid race conditions
594 $cache = $this->loadFromDB( $code, self::FOR_UPDATE );
595 $this->mCache[$code] = $cache;
596 // Load the process cache values and set the per-title cache keys
597 $page = WikiPage::factory( Title::makeTitle( NS_MEDIAWIKI, $title ) );
598 $page->loadPageData( $page::READ_LATEST );
599 $text = $this->getMessageTextFromContent( $page->getContent() );
600 // Check if an individual cache key should exist and update cache accordingly
601 if ( is_string( $text ) && strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
602 $titleKey = $this->bigMessageCacheKey( $this->mCache[$code]['HASH'], $title );
603 $this->wanCache->set( $titleKey, ' ' . $text, $this->mExpiry );
604 }
605 // Mark this cache as definitely being "latest" (non-volatile) so
606 // load() calls do try to refresh the cache with replica DB data
607 $this->mCache[$code]['LATEST'] = time();
608 // Pre-emptively update the local datacenter cache so things like edit filter and
609 // blacklist changes are reflect immediately, as these often use MediaWiki: pages.
610 // The datacenter handling replace() calls should be the same one handling edits
611 // as they require HTTP POST.
612 $this->saveToCaches( $this->mCache[$code], 'all', $code );
613 // Release the lock now that the cache is saved
614 ScopedCallback::consume( $scopedLock );
615
616 // Relay the purge. Touching this check key expires cache contents
617 // and local cache (APC) validation hash across all datacenters.
618 $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
619 // Also delete cached sidebar... just in case it is affected
620 // @TODO: shouldn't this be $code === $wgLanguageCode?
621 if ( $code === 'en' ) {
622 // Purge all language sidebars, e.g. on ?action=purge to the sidebar messages
623 $codes = array_keys( Language::fetchLanguageNames() );
624 } else {
625 // Purge only the sidebar for this language
626 $codes = [ $code ];
627 }
628 foreach ( $codes as $code ) {
629 $this->wanCache->delete( wfMemcKey( 'sidebar', $code ) );
630 }
631
632 // Purge the message in the message blob store
633 $resourceloader = RequestContext::getMain()->getOutput()->getResourceLoader();
634 $blobStore = $resourceloader->getMessageBlobStore();
635 $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
636
637 Hooks::run( 'MessageCacheReplace', [ $title, $text ] );
638 },
639 DeferredUpdates::PRESEND
640 );
641 }
642
649 protected function isCacheExpired( $cache ) {
650 if ( !isset( $cache['VERSION'] ) || !isset( $cache['EXPIRY'] ) ) {
651 return true;
652 }
653 if ( $cache['VERSION'] != MSG_CACHE_VERSION ) {
654 return true;
655 }
656 if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
657 return true;
658 }
659
660 return false;
661 }
662
672 protected function saveToCaches( array $cache, $dest, $code = false ) {
673 if ( $dest === 'all' ) {
674 $cacheKey = wfMemcKey( 'messages', $code );
675 $success = $this->clusterCache->set( $cacheKey, $cache );
676 $this->setValidationHash( $code, $cache );
677 } else {
678 $success = true;
679 }
680
681 $this->saveToLocalCache( $code, $cache );
682
683 return $success;
684 }
685
692 protected function getValidationHash( $code ) {
693 $curTTL = null;
694 $value = $this->wanCache->get(
695 $this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ),
696 $curTTL,
697 [ wfMemcKey( 'messages', $code ) ]
698 );
699
700 if ( $value ) {
701 $hash = $value['hash'];
702 if ( ( time() - $value['latest'] ) < WANObjectCache::TTL_MINUTE ) {
703 // Cache was recently updated via replace() and should be up-to-date.
704 // That method is only called in the primary datacenter and uses FOR_UPDATE.
705 // Also, it is unlikely that the current datacenter is *now* secondary one.
706 $expired = false;
707 } else {
708 // See if the "check" key was bumped after the hash was generated
709 $expired = ( $curTTL < 0 );
710 }
711 } else {
712 // No hash found at all; cache must regenerate to be safe
713 $hash = false;
714 $expired = true;
715 }
716
717 return [ $hash, $expired ];
718 }
719
730 protected function setValidationHash( $code, array $cache ) {
731 $this->wanCache->set(
732 $this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ),
733 [
734 'hash' => $cache['HASH'],
735 'latest' => isset( $cache['LATEST'] ) ? $cache['LATEST'] : 0
736 ],
737 WANObjectCache::TTL_INDEFINITE
738 );
739 }
740
746 protected function getReentrantScopedLock( $key, $timeout = self::WAIT_SEC ) {
747 return $this->clusterCache->getScopedLock( $key, $timeout, self::LOCK_TTL, __METHOD__ );
748 }
749
784 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
785 if ( is_int( $key ) ) {
786 // Fix numerical strings that somehow become ints
787 // on their way here
788 $key = (string)$key;
789 } elseif ( !is_string( $key ) ) {
790 throw new MWException( 'Non-string key given' );
791 } elseif ( $key === '' ) {
792 // Shortcut: the empty key is always missing
793 return false;
794 }
795
796 // For full keys, get the language code from the key
797 $pos = strrpos( $key, '/' );
798 if ( $isFullKey && $pos !== false ) {
799 $langcode = substr( $key, $pos + 1 );
800 $key = substr( $key, 0, $pos );
801 }
802
803 // Normalise title-case input (with some inlining)
804 $lckey = MessageCache::normalizeKey( $key );
805
806 Hooks::run( 'MessageCache::get', [ &$lckey ] );
807
808 // Loop through each language in the fallback list until we find something useful
809 $lang = wfGetLangObj( $langcode );
810 $message = $this->getMessageFromFallbackChain(
811 $lang,
812 $lckey,
813 !$this->mDisable && $useDB
814 );
815
816 // If we still have no message, maybe the key was in fact a full key so try that
817 if ( $message === false ) {
818 $parts = explode( '/', $lckey );
819 // We may get calls for things that are http-urls from sidebar
820 // Let's not load nonexistent languages for those
821 // They usually have more than one slash.
822 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
823 $message = Language::getMessageFor( $parts[0], $parts[1] );
824 if ( $message === null ) {
825 $message = false;
826 }
827 }
828 }
829
830 // Post-processing if the message exists
831 if ( $message !== false ) {
832 // Fix whitespace
833 $message = str_replace(
834 [
835 # Fix for trailing whitespace, removed by textarea
836 '&#32;',
837 # Fix for NBSP, converted to space by firefox
838 '&nbsp;',
839 '&#160;',
840 '&shy;'
841 ],
842 [
843 ' ',
844 "\xc2\xa0",
845 "\xc2\xa0",
846 "\xc2\xad"
847 ],
848 $message
849 );
850 }
851
852 return $message;
853 }
854
867 protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
869
870 $alreadyTried = [];
871
872 // First try the requested language.
873 $message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried );
874 if ( $message !== false ) {
875 return $message;
876 }
877
878 // Now try checking the site language.
879 $message = $this->getMessageForLang( $wgContLang, $lckey, $useDB, $alreadyTried );
880 return $message;
881 }
882
893 private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
895
896 $langcode = $lang->getCode();
897
898 // Try checking the database for the requested language
899 if ( $useDB ) {
900 $uckey = $wgContLang->ucfirst( $lckey );
901
902 if ( !isset( $alreadyTried[ $langcode ] ) ) {
903 $message = $this->getMsgFromNamespace(
904 $this->getMessagePageName( $langcode, $uckey ),
905 $langcode
906 );
907
908 if ( $message !== false ) {
909 return $message;
910 }
911 $alreadyTried[ $langcode ] = true;
912 }
913 } else {
914 $uckey = null;
915 }
916
917 // Check the CDB cache
918 $message = $lang->getMessage( $lckey );
919 if ( $message !== null ) {
920 return $message;
921 }
922
923 // Try checking the database for all of the fallback languages
924 if ( $useDB ) {
925 $fallbackChain = Language::getFallbacksFor( $langcode );
926
927 foreach ( $fallbackChain as $code ) {
928 if ( isset( $alreadyTried[ $code ] ) ) {
929 continue;
930 }
931
932 $message = $this->getMsgFromNamespace(
933 $this->getMessagePageName( $code, $uckey ), $code );
934
935 if ( $message !== false ) {
936 return $message;
937 }
938 $alreadyTried[ $code ] = true;
939 }
940 }
941
942 return false;
943 }
944
952 private function getMessagePageName( $langcode, $uckey ) {
954
955 if ( $langcode === $wgLanguageCode ) {
956 // Messages created in the content language will not have the /lang extension
957 return $uckey;
958 } else {
959 return "$uckey/$langcode";
960 }
961 }
962
975 public function getMsgFromNamespace( $title, $code ) {
976 $this->load( $code );
977
978 if ( isset( $this->mCache[$code][$title] ) ) {
979 $entry = $this->mCache[$code][$title];
980 if ( substr( $entry, 0, 1 ) === ' ' ) {
981 // The message exists, so make sure a string is returned.
982 return (string)substr( $entry, 1 );
983 } elseif ( $entry === '!NONEXISTENT' ) {
984 return false;
985 } elseif ( $entry === '!TOO BIG' ) {
986 // Fall through and try invididual message cache below
987 }
988 } else {
989 // XXX: This is not cached in process cache, should it?
990 $message = false;
991 Hooks::run( 'MessagesPreLoad', [ $title, &$message, $code ] );
992 if ( $message !== false ) {
993 return $message;
994 }
995
996 return false;
997 }
998
999 // Individual message cache key
1000 $titleKey = $this->bigMessageCacheKey( $this->mCache[$code]['HASH'], $title );
1001
1002 if ( $this->mCacheVolatile[$code] ) {
1003 $entry = false;
1004 // Make sure that individual keys respect the WAN cache holdoff period too
1005 LoggerFactory::getInstance( 'MessageCache' )->debug(
1006 __METHOD__ . ': loading volatile key \'{titleKey}\'',
1007 [ 'titleKey' => $titleKey, 'code' => $code ] );
1008 } else {
1009 // Try the individual message cache
1010 $entry = $this->wanCache->get( $titleKey );
1011 }
1012
1013 if ( $entry !== false ) {
1014 if ( substr( $entry, 0, 1 ) === ' ' ) {
1015 $this->mCache[$code][$title] = $entry;
1016 // The message exists, so make sure a string is returned
1017 return (string)substr( $entry, 1 );
1018 } elseif ( $entry === '!NONEXISTENT' ) {
1019 $this->mCache[$code][$title] = '!NONEXISTENT';
1020
1021 return false;
1022 } else {
1023 // Corrupt/obsolete entry, delete it
1024 $this->wanCache->delete( $titleKey );
1025 }
1026 }
1027
1028 // Try loading the message from the database
1029 $dbr = wfGetDB( DB_REPLICA );
1030 $cacheOpts = Database::getCacheSetOptions( $dbr );
1031 // Use newKnownCurrent() to avoid querying revision/user tables
1032 $titleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
1033 if ( $titleObj->getLatestRevID() ) {
1034 $revision = Revision::newKnownCurrent(
1035 $dbr,
1036 $titleObj->getArticleID(),
1037 $titleObj->getLatestRevID()
1038 );
1039 } else {
1040 $revision = false;
1041 }
1042
1043 if ( $revision ) {
1044 $content = $revision->getContent();
1045 if ( $content ) {
1046 $message = $this->getMessageTextFromContent( $content );
1047 if ( is_string( $message ) ) {
1048 $this->mCache[$code][$title] = ' ' . $message;
1049 $this->wanCache->set( $titleKey, ' ' . $message, $this->mExpiry, $cacheOpts );
1050 }
1051 } else {
1052 // A possibly temporary loading failure
1053 LoggerFactory::getInstance( 'MessageCache' )->warning(
1054 __METHOD__ . ': failed to load message page text for \'{titleKey}\'',
1055 [ 'titleKey' => $titleKey, 'code' => $code ] );
1056 $message = null; // no negative caching
1057 }
1058 } else {
1059 $message = false; // negative caching
1060 }
1061
1062 if ( $message === false ) {
1063 // Negative caching in case a "too big" message is no longer available (deleted)
1064 $this->mCache[$code][$title] = '!NONEXISTENT';
1065 $this->wanCache->set( $titleKey, '!NONEXISTENT', $this->mExpiry, $cacheOpts );
1066 }
1067
1068 return $message;
1069 }
1070
1078 function transform( $message, $interface = false, $language = null, $title = null ) {
1079 // Avoid creating parser if nothing to transform
1080 if ( strpos( $message, '{{' ) === false ) {
1081 return $message;
1082 }
1083
1084 if ( $this->mInParser ) {
1085 return $message;
1086 }
1087
1088 $parser = $this->getParser();
1089 if ( $parser ) {
1090 $popts = $this->getParserOptions();
1091 $popts->setInterfaceMessage( $interface );
1092 $popts->setTargetLanguage( $language );
1093
1094 $userlang = $popts->setUserLang( $language );
1095 $this->mInParser = true;
1096 $message = $parser->transformMsg( $message, $popts, $title );
1097 $this->mInParser = false;
1098 $popts->setUserLang( $userlang );
1099 }
1100
1101 return $message;
1102 }
1103
1107 function getParser() {
1109
1110 if ( !$this->mParser && isset( $wgParser ) ) {
1111 # Do some initialisation so that we don't have to do it twice
1112 $wgParser->firstCallInit();
1113 # Clone it and store it
1114 $class = $wgParserConf['class'];
1115 if ( $class == 'ParserDiffTest' ) {
1116 # Uncloneable
1117 $this->mParser = new $class( $wgParserConf );
1118 } else {
1119 $this->mParser = clone $wgParser;
1120 }
1121 }
1122
1123 return $this->mParser;
1124 }
1125
1134 public function parse( $text, $title = null, $linestart = true,
1135 $interface = false, $language = null
1136 ) {
1138
1139 if ( $this->mInParser ) {
1140 return htmlspecialchars( $text );
1141 }
1142
1143 $parser = $this->getParser();
1144 $popts = $this->getParserOptions();
1145 $popts->setInterfaceMessage( $interface );
1146
1147 if ( is_string( $language ) ) {
1148 $language = Language::factory( $language );
1149 }
1150 $popts->setTargetLanguage( $language );
1151
1152 if ( !$title || !$title instanceof Title ) {
1153 wfDebugLog( 'GlobalTitleFail', __METHOD__ . ' called by ' .
1154 wfGetAllCallers( 6 ) . ' with no title set.' );
1155 $title = $wgTitle;
1156 }
1157 // Sometimes $wgTitle isn't set either...
1158 if ( !$title ) {
1159 # It's not uncommon having a null $wgTitle in scripts. See r80898
1160 # Create a ghost title in such case
1161 $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/title not set in ' . __METHOD__ );
1162 }
1163
1164 $this->mInParser = true;
1165 $res = $parser->parse( $text, $title, $popts, $linestart );
1166 $this->mInParser = false;
1167
1168 return $res;
1169 }
1170
1171 function disable() {
1172 $this->mDisable = true;
1173 }
1174
1175 function enable() {
1176 $this->mDisable = false;
1177 }
1178
1191 public function isDisabled() {
1192 return $this->mDisable;
1193 }
1194
1198 function clear() {
1199 $langs = Language::fetchLanguageNames( null, 'mw' );
1200 foreach ( array_keys( $langs ) as $code ) {
1201 # Global and local caches
1202 $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
1203 }
1204
1205 $this->mLoadedLanguages = [];
1206 }
1207
1212 public function figureMessage( $key ) {
1214
1215 $pieces = explode( '/', $key );
1216 if ( count( $pieces ) < 2 ) {
1217 return [ $key, $wgLanguageCode ];
1218 }
1219
1220 $lang = array_pop( $pieces );
1221 if ( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
1222 return [ $key, $wgLanguageCode ];
1223 }
1224
1225 $message = implode( '/', $pieces );
1226
1227 return [ $message, $lang ];
1228 }
1229
1238 public function getAllMessageKeys( $code ) {
1240
1241 $this->load( $code );
1242 if ( !isset( $this->mCache[$code] ) ) {
1243 // Apparently load() failed
1244 return null;
1245 }
1246 // Remove administrative keys
1247 $cache = $this->mCache[$code];
1248 unset( $cache['VERSION'] );
1249 unset( $cache['EXPIRY'] );
1250 unset( $cache['EXCESSIVE'] );
1251 // Remove any !NONEXISTENT keys
1252 $cache = array_diff( $cache, [ '!NONEXISTENT' ] );
1253
1254 // Keys may appear with a capital first letter. lcfirst them.
1255 return array_map( [ $wgContLang, 'lcfirst' ], array_keys( $cache ) );
1256 }
1257
1265 public function updateMessageOverride( Title $title, Content $content = null ) {
1267
1268 $msgText = $this->getMessageTextFromContent( $content );
1269 if ( $msgText === null ) {
1270 $msgText = false; // treat as not existing
1271 }
1272
1273 $this->replace( $title->getDBkey(), $msgText );
1274
1275 if ( $wgContLang->hasVariants() ) {
1276 $wgContLang->updateConversionTable( $title );
1277 }
1278 }
1279
1284 private function getMessageTextFromContent( Content $content = null ) {
1285 // @TODO: could skip pseudo-messages like js/css here, based on content model
1286 if ( $content ) {
1287 // Message page exists...
1288 // XXX: Is this the right way to turn a Content object into a message?
1289 // NOTE: $content is typically either WikitextContent, JavaScriptContent or
1290 // CssContent. MessageContent is *not* used for storing messages, it's
1291 // only used for wrapping them when needed.
1292 $msgText = $content->getWikitextForTransclusion();
1293 if ( $msgText === false || $msgText === null ) {
1294 // This might be due to some kind of misconfiguration...
1295 $msgText = null;
1296 LoggerFactory::getInstance( 'MessageCache' )->warning(
1297 __METHOD__ . ": message content doesn't provide wikitext "
1298 . "(content model: " . $content->getModel() . ")" );
1299 }
1300 } else {
1301 // Message page does not exist...
1302 $msgText = false;
1303 }
1304
1305 return $msgText;
1306 }
1307
1313 private function bigMessageCacheKey( $hash, $title ) {
1314 return $this->wanCache->makeKey( 'messages-big', $hash, $title );
1315 }
1316}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable or merely the Work and Derivative Works thereof Contribution shall mean any work of including the original version of the Work and any modifications or additions to that Work or Derivative Works that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner For the purposes of this submitted means any form of or written communication sent to the Licensor or its including but not limited to communication on electronic mailing source code control and issue tracking systems that are managed by
serialize()
$wgLanguageCode
Site language code.
$wgUseLocalMessageCache
Set this to true to maintain a copy of the message cache on the local server.
$wgAdaptiveMessageCache
Instead of caching everything, only cache those messages which have been customised in the site conte...
$wgUseDatabaseMessages
Translation using MediaWiki: namespace.
$wgMaxMsgCacheEntrySize
Maximum entry size in the message cache, in bytes.
$wgParserConf
Parser configuration.
$wgMsgCacheExpiry
Expiry time for the message cache key.
wfGetLangObj( $langcode=false)
Return a Language object from $langcode.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfGetMessageCacheStorage()
Get the cache object used by the message cache.
wfMemcKey()
Make a cache key for the local wiki.
wfGetAllCallers( $limit=3)
Return a string consisting of callers in the stack.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
const MSG_CACHE_VERSION
MediaWiki message cache structure version.
$wgUser
Definition Setup.php:781
$wgParser
Definition Setup.php:796
if(! $wgRequest->checkUrlExtension()) if(isset($_SERVER[ 'PATH_INFO']) &&$_SERVER[ 'PATH_INFO'] !='') if(! $wgEnableAPI) $wgTitle
Definition api.php:68
interface is intended to be more or less compatible with the PHP memcached client.
Definition BagOStuff.php:47
A BagOStuff object with no objects in it.
MediaWiki exception.
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Message cache Performs various MediaWiki namespace-related functions.
getValidationHash( $code)
Get the md5 used to validate the local APC cache.
loadFromDBWithLock( $code, array &$where, $mode=null)
const LOCK_TTL
How long memcached locks last.
loadFromDB( $code, $mode=null)
Loads cacheable messages from the database.
getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried)
Given a language, try and fetch messages from that language and its fallbacks.
$mExpiry
Lifetime for cache, used by object caching.
saveToLocalCache( $code, $cache)
Save the cache to APC.
getMessagePageName( $langcode, $uckey)
Get the message page name for a given language.
static $instance
Singleton instance.
saveToCaches(array $cache, $dest, $code=false)
Shortcut to update caches.
setValidationHash( $code, array $cache)
Set the md5 used to validate the local disk cache.
bool[] $mCacheVolatile
Map of (language code => boolean)
isCacheExpired( $cache)
Is the given cache array expired due to time passing or a version change?
getMsgFromNamespace( $title, $code)
Get a message from the MediaWiki namespace, with caching.
getReentrantScopedLock( $key, $timeout=self::WAIT_SEC)
const WAIT_SEC
How long to wait for memcached locks.
$mDisable
Should mean that database cannot be used, but check.
getLocalCache( $code)
Try to load the cache from APC.
static destroyInstance()
Destroy the singleton instance.
load( $code, $mode=null)
Loads messages from caches or from database in this order: (1) local message cache (if $wgUseLocalMes...
BagOStuff $srvCache
transform( $message, $interface=false, $language=null, $title=null)
updateMessageOverride(Title $title, Content $content=null)
Purge message caches when a MediaWiki: page is created, updated, or deleted.
getMessageFromFallbackChain( $lang, $lckey, $useDB)
Given a language, try and fetch messages from that language.
$mLoadedLanguages
Variable for tracking which variables are already loaded.
isDisabled()
Whether DB/cache usage is disabled for determining messages.
BagOStuff $clusterCache
getMessageTextFromContent(Content $content=null)
__construct(WANObjectCache $wanCache, BagOStuff $clusterCache, BagOStuff $srvCache, $useDB, $expiry)
clear()
Clear all stored messages.
static singleton()
Get the signleton instance of this class.
getAllMessageKeys( $code)
Get all message keys stored in the message cache for a given language.
$mCache
Process local cache of loaded messages that are defined in MediaWiki namespace.
WANObjectCache $wanCache
static normalizeKey( $key)
Normalize message key input.
parse( $text, $title=null, $linestart=true, $interface=false, $language=null)
ParserOptions $mParserOptions
Message cache has its own parser which it uses to transform messages.
replace( $title, $text)
Updates cache as necessary when message page is changed.
getParserOptions()
ParserOptions is lazy initialised.
bigMessageCacheKey( $hash, $title)
Set options of the Parser.
static newFromAnon()
Get a ParserOptions object for an anonymous user.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:70
static getMain()
Static methods.
static newKnownCurrent(IDatabase $db, $pageId, $revId)
Load a revision based on a known page ID and current revision ID from the DB.
static getRevisionText( $row, $prefix='old_', $wiki=false)
Get revision text associated with an old or archive row.
Represents a title within MediaWiki.
Definition Title.php:39
getDBkey()
Get the main part with underscores.
Definition Title.php:901
Multi-datacenter aware caching interface.
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition WikiPage.php:120
Relational database abstraction object.
Definition Database.php:45
$res
Definition database.txt:21
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition design.txt:57
I won t presume to tell you how to I m just describing the methods I chose to use for myself If you do choose to follow these it will probably be easier for you to collaborate with others on the but if you want to contribute without by all means do which work well I also use K &R brace matching style I know that s a religious issue for so if you want to use a style that puts opening braces on the next that s OK too
Definition design.txt:80
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
globals txt Globals are evil The original MediaWiki code relied on globals for processing context far too often MediaWiki development since then has been a story of slowly moving context out of global variables and into objects Storing processing context in object member variables allows those objects to be reused in a much more flexible way Consider the elegance of
database rows
Definition globals.txt:10
const NS_MEDIAWIKI
Definition Defines.php:70
const NS_SPECIAL
Definition Defines.php:51
the array() calling protocol came about after MediaWiki 1.4rc1.
do that in ParserLimitReportFormat instead $parser
Definition hooks.txt:2536
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition hooks.txt:2578
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition hooks.txt:183
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition hooks.txt:1100
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition hooks.txt:865
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:964
null for the local wiki Added in
Definition hooks.txt:1572
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition hooks.txt:1049
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place replace
Definition hooks.txt:2206
processing should stop and the error should be shown to the user * false
Definition hooks.txt:189
passed in as a query string parameter to the various URLs constructed here(i.e. $prevlink) $ldel you ll need to handle error messages
Definition hooks.txt:1252
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Base interface for content objects.
Definition Content.php:34
you have access to all of the normal MediaWiki so you can get a DB use the cache
$cache
Definition mcc.php:33
MediaWiki has optional support for a high distributed memory object caching system For general information on but for a larger site with heavy load
Definition memcached.txt:6
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres as and are nearing end of but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN boolean columns are always mapped to as the code does not always treat the column as a and VARBINARY columns should simply be TEXT The only exception is when VARBINARY is used to store true binary data
Definition postgres.txt:43
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26
if(!isset( $args[0])) $lang