MediaWiki  1.28.0
MessageCache.php
Go to the documentation of this file.
1 <?php
25 
30 define( 'MSG_CACHE_VERSION', 2 );
31 
37 class MessageCache {
38  const FOR_UPDATE = 1; // force message reload
39 
41  const WAIT_SEC = 15;
43  const LOCK_TTL = 30;
44 
53  protected $mCache;
54 
59  protected $mDisable;
60 
65  protected $mExpiry;
66 
72 
77  protected $mLoadedLanguages = [];
78 
82  protected $mInParser = false;
83 
85  protected $mMemc;
87  protected $wanCache;
88 
94  private static $instance;
95 
102  public static function singleton() {
103  if ( self::$instance === null ) {
105  self::$instance = new self(
109  );
110  }
111 
112  return self::$instance;
113  }
114 
120  public static function destroyInstance() {
121  self::$instance = null;
122  }
123 
130  public static function normalizeKey( $key ) {
132  $lckey = strtr( $key, ' ', '_' );
133  if ( ord( $lckey ) < 128 ) {
134  $lckey[0] = strtolower( $lckey[0] );
135  } else {
136  $lckey = $wgContLang->lcfirst( $lckey );
137  }
138 
139  return $lckey;
140  }
141 
147  function __construct( $memCached, $useDB, $expiry ) {
149 
150  if ( !$memCached ) {
151  $memCached = wfGetCache( CACHE_NONE );
152  }
153 
154  $this->mMemc = $memCached;
155  $this->mDisable = !$useDB;
156  $this->mExpiry = $expiry;
157 
158  if ( $wgUseLocalMessageCache ) {
159  $this->localCache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
160  } else {
161  $this->localCache = new EmptyBagOStuff();
162  }
163 
164  $this->wanCache = ObjectCache::getMainWANInstance();
165  }
166 
172  function getParserOptions() {
173  global $wgUser;
174 
175  if ( !$this->mParserOptions ) {
176  if ( !$wgUser->isSafeToLoad() ) {
177  // $wgUser isn't unstubbable yet, so don't try to get a
178  // ParserOptions for it. And don't cache this ParserOptions
179  // either.
181  $po->setEditSection( false );
182  return $po;
183  }
184 
185  $this->mParserOptions = new ParserOptions;
186  $this->mParserOptions->setEditSection( false );
187  }
188 
189  return $this->mParserOptions;
190  }
191 
198  protected function getLocalCache( $code ) {
199  $cacheKey = wfMemcKey( __CLASS__, $code );
200 
201  return $this->localCache->get( $cacheKey );
202  }
203 
210  protected function saveToLocalCache( $code, $cache ) {
211  $cacheKey = wfMemcKey( __CLASS__, $code );
212  $this->localCache->set( $cacheKey, $cache );
213  }
214 
236  protected function load( $code, $mode = null ) {
237  if ( !is_string( $code ) ) {
238  throw new InvalidArgumentException( "Missing language code" );
239  }
240 
241  # Don't do double loading...
242  if ( isset( $this->mLoadedLanguages[$code] ) && $mode != self::FOR_UPDATE ) {
243  return true;
244  }
245 
246  # 8 lines of code just to say (once) that message cache is disabled
247  if ( $this->mDisable ) {
248  static $shownDisabled = false;
249  if ( !$shownDisabled ) {
250  wfDebug( __METHOD__ . ": disabled\n" );
251  $shownDisabled = true;
252  }
253 
254  return true;
255  }
256 
257  # Loading code starts
258  $success = false; # Keep track of success
259  $staleCache = false; # a cache array with expired data, or false if none has been loaded
260  $where = []; # Debug info, delayed to avoid spamming debug log too much
261 
262  # Hash of the contents is stored in memcache, to detect if data-center cache
263  # or local cache goes out of date (e.g. due to replace() on some other server)
264  list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
265 
266  # Try the local cache and check against the cluster hash key...
267  $cache = $this->getLocalCache( $code );
268  if ( !$cache ) {
269  $where[] = 'local cache is empty';
270  } elseif ( !isset( $cache['HASH'] ) || $cache['HASH'] !== $hash ) {
271  $where[] = 'local cache has the wrong hash';
272  $staleCache = $cache;
273  } elseif ( $this->isCacheExpired( $cache ) ) {
274  $where[] = 'local cache is expired';
275  $staleCache = $cache;
276  } elseif ( $hashVolatile ) {
277  $where[] = 'local cache validation key is expired/volatile';
278  $staleCache = $cache;
279  } else {
280  $where[] = 'got from local cache';
281  $success = true;
282  $this->mCache[$code] = $cache;
283  }
284 
285  if ( !$success ) {
286  $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
287  # Try the global cache. If it is empty, try to acquire a lock. If
288  # the lock can't be acquired, wait for the other thread to finish
289  # and then try the global cache a second time.
290  for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) {
291  if ( $hashVolatile && $staleCache ) {
292  # Do not bother fetching the whole cache blob to avoid I/O.
293  # Instead, just try to get the non-blocking $statusKey lock
294  # below, and use the local stale value if it was not acquired.
295  $where[] = 'global cache is presumed expired';
296  } else {
297  $cache = $this->mMemc->get( $cacheKey );
298  if ( !$cache ) {
299  $where[] = 'global cache is empty';
300  } elseif ( $this->isCacheExpired( $cache ) ) {
301  $where[] = 'global cache is expired';
302  $staleCache = $cache;
303  } elseif ( $hashVolatile ) {
304  # DB results are replica DB lag prone until the holdoff TTL passes.
305  # By then, updates should be reflected in loadFromDBWithLock().
306  # One thread renerates the cache while others use old values.
307  $where[] = 'global cache is expired/volatile';
308  $staleCache = $cache;
309  } else {
310  $where[] = 'got from global cache';
311  $this->mCache[$code] = $cache;
312  $this->saveToCaches( $cache, 'local-only', $code );
313  $success = true;
314  }
315  }
316 
317  if ( $success ) {
318  # Done, no need to retry
319  break;
320  }
321 
322  # We need to call loadFromDB. Limit the concurrency to one process.
323  # This prevents the site from going down when the cache expires.
324  # Note that the DB slam protection lock here is non-blocking.
325  $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
326  if ( $loadStatus === true ) {
327  $success = true;
328  break;
329  } elseif ( $staleCache ) {
330  # Use the stale cache while some other thread constructs the new one
331  $where[] = 'using stale cache';
332  $this->mCache[$code] = $staleCache;
333  $success = true;
334  break;
335  } elseif ( $failedAttempts > 0 ) {
336  # Already blocked once, so avoid another lock/unlock cycle.
337  # This case will typically be hit if memcached is down, or if
338  # loadFromDB() takes longer than LOCK_WAIT.
339  $where[] = "could not acquire status key.";
340  break;
341  } elseif ( $loadStatus === 'cantacquire' ) {
342  # Wait for the other thread to finish, then retry. Normally,
343  # the memcached get() will then yeild the other thread's result.
344  $where[] = 'waited for other thread to complete';
345  $this->getReentrantScopedLock( $cacheKey );
346  } else {
347  # Disable cache; $loadStatus is 'disabled'
348  break;
349  }
350  }
351  }
352 
353  if ( !$success ) {
354  $where[] = 'loading FAILED - cache is disabled';
355  $this->mDisable = true;
356  $this->mCache = false;
357  wfDebugLog( 'MessageCacheError', __METHOD__ . ": Failed to load $code\n" );
358  # This used to throw an exception, but that led to nasty side effects like
359  # the whole wiki being instantly down if the memcached server died
360  } else {
361  # All good, just record the success
362  $this->mLoadedLanguages[$code] = true;
363  }
364 
365  $info = implode( ', ', $where );
366  wfDebugLog( 'MessageCache', __METHOD__ . ": Loading $code... $info\n" );
367 
368  return $success;
369  }
370 
377  protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
379 
380  # If cache updates on all levels fail, give up on message overrides.
381  # This is to avoid easy site outages; see $saveSuccess comments below.
382  $statusKey = wfMemcKey( 'messages', $code, 'status' );
383  $status = $this->mMemc->get( $statusKey );
384  if ( $status === 'error' ) {
385  $where[] = "could not load; method is still globally disabled";
386  return 'disabled';
387  }
388 
389  # Now let's regenerate
390  $where[] = 'loading from database';
391 
392  # Lock the cache to prevent conflicting writes.
393  # This lock is non-blocking so stale cache can quickly be used.
394  # Note that load() will call a blocking getReentrantScopedLock()
395  # after this if it really need to wait for any current thread.
396  $cacheKey = wfMemcKey( 'messages', $code );
397  $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
398  if ( !$scopedLock ) {
399  $where[] = 'could not acquire main lock';
400  return 'cantacquire';
401  }
402 
403  $cache = $this->loadFromDB( $code, $mode );
404  $this->mCache[$code] = $cache;
405  $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
406 
407  if ( !$saveSuccess ) {
421  if ( !$wgUseLocalMessageCache ) {
422  $this->mMemc->set( $statusKey, 'error', 60 * 5 );
423  $where[] = 'could not save cache, disabled globally for 5 minutes';
424  } else {
425  $where[] = "could not save global cache";
426  }
427  }
428 
429  return true;
430  }
431 
441  function loadFromDB( $code, $mode = null ) {
443 
444  $dbr = wfGetDB( ( $mode == self::FOR_UPDATE ) ? DB_MASTER : DB_REPLICA );
445 
446  $cache = [];
447 
448  # Common conditions
449  $conds = [
450  'page_is_redirect' => 0,
451  'page_namespace' => NS_MEDIAWIKI,
452  ];
453 
454  $mostused = [];
455  if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
456  if ( !isset( $this->mCache[$wgLanguageCode] ) ) {
457  $this->load( $wgLanguageCode );
458  }
459  $mostused = array_keys( $this->mCache[$wgLanguageCode] );
460  foreach ( $mostused as $key => $value ) {
461  $mostused[$key] = "$value/$code";
462  }
463  }
464 
465  if ( count( $mostused ) ) {
466  $conds['page_title'] = $mostused;
467  } elseif ( $code !== $wgLanguageCode ) {
468  $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
469  } else {
470  # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
471  # other than language code.
472  $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
473  }
474 
475  # Conditions to fetch oversized pages to ignore them
476  $bigConds = $conds;
477  $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
478 
479  # Load titles for all oversized pages in the MediaWiki namespace
480  $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__ . "($code)-big" );
481  foreach ( $res as $row ) {
482  $cache[$row->page_title] = '!TOO BIG';
483  }
484 
485  # Conditions to load the remaining pages with their contents
486  $smallConds = $conds;
487  $smallConds[] = 'page_latest=rev_id';
488  $smallConds[] = 'rev_text_id=old_id';
489  $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
490 
491  $res = $dbr->select(
492  [ 'page', 'revision', 'text' ],
493  [ 'page_title', 'old_text', 'old_flags' ],
494  $smallConds,
495  __METHOD__ . "($code)-small"
496  );
497 
498  foreach ( $res as $row ) {
499  $text = Revision::getRevisionText( $row );
500  if ( $text === false ) {
501  // Failed to fetch data; possible ES errors?
502  // Store a marker to fetch on-demand as a workaround...
503  $entry = '!TOO BIG';
504  wfDebugLog(
505  'MessageCache',
506  __METHOD__
507  . ": failed to load message page text for {$row->page_title} ($code)"
508  );
509  } else {
510  $entry = ' ' . $text;
511  }
512  $cache[$row->page_title] = $entry;
513  }
514 
515  $cache['VERSION'] = MSG_CACHE_VERSION;
516  ksort( $cache );
517  $cache['HASH'] = md5( serialize( $cache ) );
518  $cache['EXPIRY'] = wfTimestamp( TS_MW, time() + $this->mExpiry );
519 
520  return $cache;
521  }
522 
529  public function replace( $title, $text ) {
531 
532  if ( $this->mDisable ) {
533  return;
534  }
535 
536  list( $msg, $code ) = $this->figureMessage( $title );
537  if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
538  // Content language overrides do not use the /<code> suffix
539  return;
540  }
541 
542  // Note that if the cache is volatile, load() may trigger a DB fetch.
543  // In that case we reenter/reuse the existing cache key lock to avoid
544  // a self-deadlock. This is safe as no reads happen *directly* in this
545  // method between getReentrantScopedLock() and load() below. There is
546  // no risk of data "changing under our feet" for replace().
547  $cacheKey = wfMemcKey( 'messages', $code );
548  $scopedLock = $this->getReentrantScopedLock( $cacheKey );
549  $this->load( $code, self::FOR_UPDATE );
550 
551  $titleKey = wfMemcKey( 'messages', 'individual', $title );
552  if ( $text === false ) {
553  // Article was deleted
554  $this->mCache[$code][$title] = '!NONEXISTENT';
555  $this->wanCache->delete( $titleKey );
556  } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
557  // Check for size
558  $this->mCache[$code][$title] = '!TOO BIG';
559  $this->wanCache->set( $titleKey, ' ' . $text, $this->mExpiry );
560  } else {
561  $this->mCache[$code][$title] = ' ' . $text;
562  $this->wanCache->delete( $titleKey );
563  }
564 
565  // Mark this cache as definitely "latest" (non-volatile) so
566  // load() calls do try to refresh the cache with replica DB data
567  $this->mCache[$code]['LATEST'] = time();
568 
569  // Update caches if the lock was acquired
570  if ( $scopedLock ) {
571  $this->saveToCaches( $this->mCache[$code], 'all', $code );
572  }
573 
574  ScopedCallback::consume( $scopedLock );
575  // Relay the purge to APC and other DCs
576  $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
577 
578  // Also delete cached sidebar... just in case it is affected
579  $codes = [ $code ];
580  if ( $code === 'en' ) {
581  // Delete all sidebars, like for example on action=purge on the
582  // sidebar messages
583  $codes = array_keys( Language::fetchLanguageNames() );
584  }
585 
586  foreach ( $codes as $code ) {
587  $sidebarKey = wfMemcKey( 'sidebar', $code );
588  $this->wanCache->delete( $sidebarKey );
589  }
590 
591  // Update the message in the message blob store
592  $resourceloader = RequestContext::getMain()->getOutput()->getResourceLoader();
593  $blobStore = $resourceloader->getMessageBlobStore();
594  $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
595 
596  Hooks::run( 'MessageCacheReplace', [ $title, $text ] );
597  }
598 
605  protected function isCacheExpired( $cache ) {
606  if ( !isset( $cache['VERSION'] ) || !isset( $cache['EXPIRY'] ) ) {
607  return true;
608  }
609  if ( $cache['VERSION'] != MSG_CACHE_VERSION ) {
610  return true;
611  }
612  if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
613  return true;
614  }
615 
616  return false;
617  }
618 
628  protected function saveToCaches( array $cache, $dest, $code = false ) {
629  if ( $dest === 'all' ) {
630  $cacheKey = wfMemcKey( 'messages', $code );
631  $success = $this->mMemc->set( $cacheKey, $cache );
632  $this->setValidationHash( $code, $cache );
633  } else {
634  $success = true;
635  }
636 
637  $this->saveToLocalCache( $code, $cache );
638 
639  return $success;
640  }
641 
648  protected function getValidationHash( $code ) {
649  $curTTL = null;
650  $value = $this->wanCache->get(
651  wfMemcKey( 'messages', $code, 'hash', 'v1' ),
652  $curTTL,
653  [ wfMemcKey( 'messages', $code ) ]
654  );
655 
656  if ( !$value ) {
657  // No hash found at all; cache must regenerate to be safe
658  $hash = false;
659  $expired = true;
660  } else {
661  $hash = $value['hash'];
662  if ( ( time() - $value['latest'] ) < WANObjectCache::HOLDOFF_TTL ) {
663  // Cache was recently updated via replace() and should be up-to-date
664  $expired = false;
665  } else {
666  // See if the "check" key was bumped after the hash was generated
667  $expired = ( $curTTL < 0 );
668  }
669  }
670 
671  return [ $hash, $expired ];
672  }
673 
683  protected function setValidationHash( $code, array $cache ) {
684  $this->wanCache->set(
685  wfMemcKey( 'messages', $code, 'hash', 'v1' ),
686  [
687  'hash' => $cache['HASH'],
688  'latest' => isset( $cache['LATEST'] ) ? $cache['LATEST'] : 0
689  ],
691  );
692  }
693 
699  protected function getReentrantScopedLock( $key, $timeout = self::WAIT_SEC ) {
700  return $this->mMemc->getScopedLock( $key, $timeout, self::LOCK_TTL, __METHOD__ );
701  }
702 
737  function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
738  if ( is_int( $key ) ) {
739  // Fix numerical strings that somehow become ints
740  // on their way here
741  $key = (string)$key;
742  } elseif ( !is_string( $key ) ) {
743  throw new MWException( 'Non-string key given' );
744  } elseif ( $key === '' ) {
745  // Shortcut: the empty key is always missing
746  return false;
747  }
748 
749  // For full keys, get the language code from the key
750  $pos = strrpos( $key, '/' );
751  if ( $isFullKey && $pos !== false ) {
752  $langcode = substr( $key, $pos + 1 );
753  $key = substr( $key, 0, $pos );
754  }
755 
756  // Normalise title-case input (with some inlining)
757  $lckey = MessageCache::normalizeKey( $key );
758 
759  Hooks::run( 'MessageCache::get', [ &$lckey ] );
760 
761  // Loop through each language in the fallback list until we find something useful
762  $lang = wfGetLangObj( $langcode );
763  $message = $this->getMessageFromFallbackChain(
764  $lang,
765  $lckey,
766  !$this->mDisable && $useDB
767  );
768 
769  // If we still have no message, maybe the key was in fact a full key so try that
770  if ( $message === false ) {
771  $parts = explode( '/', $lckey );
772  // We may get calls for things that are http-urls from sidebar
773  // Let's not load nonexistent languages for those
774  // They usually have more than one slash.
775  if ( count( $parts ) == 2 && $parts[1] !== '' ) {
776  $message = Language::getMessageFor( $parts[0], $parts[1] );
777  if ( $message === null ) {
778  $message = false;
779  }
780  }
781  }
782 
783  // Post-processing if the message exists
784  if ( $message !== false ) {
785  // Fix whitespace
786  $message = str_replace(
787  [
788  # Fix for trailing whitespace, removed by textarea
789  '&#32;',
790  # Fix for NBSP, converted to space by firefox
791  '&nbsp;',
792  '&#160;',
793  '&shy;'
794  ],
795  [
796  ' ',
797  "\xc2\xa0",
798  "\xc2\xa0",
799  "\xc2\xad"
800  ],
801  $message
802  );
803  }
804 
805  return $message;
806  }
807 
820  protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
822 
823  $alreadyTried = [];
824 
825  // First try the requested language.
826  $message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried );
827  if ( $message !== false ) {
828  return $message;
829  }
830 
831  // Now try checking the site language.
832  $message = $this->getMessageForLang( $wgContLang, $lckey, $useDB, $alreadyTried );
833  return $message;
834  }
835 
846  private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
848  $langcode = $lang->getCode();
849 
850  // Try checking the database for the requested language
851  if ( $useDB ) {
852  $uckey = $wgContLang->ucfirst( $lckey );
853 
854  if ( !isset( $alreadyTried[ $langcode ] ) ) {
855  $message = $this->getMsgFromNamespace(
856  $this->getMessagePageName( $langcode, $uckey ),
857  $langcode
858  );
859 
860  if ( $message !== false ) {
861  return $message;
862  }
863  $alreadyTried[ $langcode ] = true;
864  }
865  } else {
866  $uckey = null;
867  }
868 
869  // Check the CDB cache
870  $message = $lang->getMessage( $lckey );
871  if ( $message !== null ) {
872  return $message;
873  }
874 
875  // Try checking the database for all of the fallback languages
876  if ( $useDB ) {
877  $fallbackChain = Language::getFallbacksFor( $langcode );
878 
879  foreach ( $fallbackChain as $code ) {
880  if ( isset( $alreadyTried[ $code ] ) ) {
881  continue;
882  }
883 
884  $message = $this->getMsgFromNamespace(
885  $this->getMessagePageName( $code, $uckey ), $code );
886 
887  if ( $message !== false ) {
888  return $message;
889  }
890  $alreadyTried[ $code ] = true;
891  }
892  }
893 
894  return false;
895  }
896 
904  private function getMessagePageName( $langcode, $uckey ) {
906  if ( $langcode === $wgLanguageCode ) {
907  // Messages created in the content language will not have the /lang extension
908  return $uckey;
909  } else {
910  return "$uckey/$langcode";
911  }
912  }
913 
926  public function getMsgFromNamespace( $title, $code ) {
927  $this->load( $code );
928  if ( isset( $this->mCache[$code][$title] ) ) {
929  $entry = $this->mCache[$code][$title];
930  if ( substr( $entry, 0, 1 ) === ' ' ) {
931  // The message exists, so make sure a string
932  // is returned.
933  return (string)substr( $entry, 1 );
934  } elseif ( $entry === '!NONEXISTENT' ) {
935  return false;
936  } elseif ( $entry === '!TOO BIG' ) {
937  // Fall through and try invididual message cache below
938  }
939  } else {
940  // XXX: This is not cached in process cache, should it?
941  $message = false;
942  Hooks::run( 'MessagesPreLoad', [ $title, &$message ] );
943  if ( $message !== false ) {
944  return $message;
945  }
946 
947  return false;
948  }
949 
950  // Try the individual message cache
951  $titleKey = wfMemcKey( 'messages', 'individual', $title );
952 
953  $curTTL = null;
954  $entry = $this->wanCache->get(
955  $titleKey,
956  $curTTL,
957  [ wfMemcKey( 'messages', $code ) ]
958  );
959  $entry = ( $curTTL >= 0 ) ? $entry : false;
960 
961  if ( $entry ) {
962  if ( substr( $entry, 0, 1 ) === ' ' ) {
963  $this->mCache[$code][$title] = $entry;
964  // The message exists, so make sure a string is returned
965  return (string)substr( $entry, 1 );
966  } elseif ( $entry === '!NONEXISTENT' ) {
967  $this->mCache[$code][$title] = '!NONEXISTENT';
968 
969  return false;
970  } else {
971  // Corrupt/obsolete entry, delete it
972  $this->wanCache->delete( $titleKey );
973  }
974  }
975 
976  // Try loading it from the database
977  $dbr = wfGetDB( DB_REPLICA );
978  $cacheOpts = Database::getCacheSetOptions( $dbr );
979  // Use newKnownCurrent() to avoid querying revision/user tables
980  $titleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
981  if ( $titleObj->getLatestRevID() ) {
982  $revision = Revision::newKnownCurrent(
983  $dbr,
984  $titleObj->getArticleID(),
985  $titleObj->getLatestRevID()
986  );
987  } else {
988  $revision = false;
989  }
990 
991  if ( $revision ) {
992  $content = $revision->getContent();
993  if ( !$content ) {
994  // A possibly temporary loading failure.
995  wfDebugLog(
996  'MessageCache',
997  __METHOD__ . ": failed to load message page text for {$title} ($code)"
998  );
999  $message = null; // no negative caching
1000  } else {
1001  // XXX: Is this the right way to turn a Content object into a message?
1002  // NOTE: $content is typically either WikitextContent, JavaScriptContent or
1003  // CssContent. MessageContent is *not* used for storing messages, it's
1004  // only used for wrapping them when needed.
1005  $message = $content->getWikitextForTransclusion();
1006 
1007  if ( $message === false || $message === null ) {
1008  wfDebugLog(
1009  'MessageCache',
1010  __METHOD__ . ": message content doesn't provide wikitext "
1011  . "(content model: " . $content->getModel() . ")"
1012  );
1013 
1014  $message = false; // negative caching
1015  } else {
1016  $this->mCache[$code][$title] = ' ' . $message;
1017  $this->wanCache->set( $titleKey, ' ' . $message, $this->mExpiry, $cacheOpts );
1018  }
1019  }
1020  } else {
1021  $message = false; // negative caching
1022  }
1023 
1024  if ( $message === false ) { // negative caching
1025  $this->mCache[$code][$title] = '!NONEXISTENT';
1026  $this->wanCache->set( $titleKey, '!NONEXISTENT', $this->mExpiry, $cacheOpts );
1027  }
1028 
1029  return $message;
1030  }
1031 
1039  function transform( $message, $interface = false, $language = null, $title = null ) {
1040  // Avoid creating parser if nothing to transform
1041  if ( strpos( $message, '{{' ) === false ) {
1042  return $message;
1043  }
1044 
1045  if ( $this->mInParser ) {
1046  return $message;
1047  }
1048 
1049  $parser = $this->getParser();
1050  if ( $parser ) {
1051  $popts = $this->getParserOptions();
1052  $popts->setInterfaceMessage( $interface );
1053  $popts->setTargetLanguage( $language );
1054 
1055  $userlang = $popts->setUserLang( $language );
1056  $this->mInParser = true;
1057  $message = $parser->transformMsg( $message, $popts, $title );
1058  $this->mInParser = false;
1059  $popts->setUserLang( $userlang );
1060  }
1061 
1062  return $message;
1063  }
1064 
1068  function getParser() {
1069  global $wgParser, $wgParserConf;
1070  if ( !$this->mParser && isset( $wgParser ) ) {
1071  # Do some initialisation so that we don't have to do it twice
1072  $wgParser->firstCallInit();
1073  # Clone it and store it
1074  $class = $wgParserConf['class'];
1075  if ( $class == 'ParserDiffTest' ) {
1076  # Uncloneable
1077  $this->mParser = new $class( $wgParserConf );
1078  } else {
1079  $this->mParser = clone $wgParser;
1080  }
1081  }
1082 
1083  return $this->mParser;
1084  }
1085 
1094  public function parse( $text, $title = null, $linestart = true,
1095  $interface = false, $language = null
1096  ) {
1097  if ( $this->mInParser ) {
1098  return htmlspecialchars( $text );
1099  }
1100 
1101  $parser = $this->getParser();
1102  $popts = $this->getParserOptions();
1103  $popts->setInterfaceMessage( $interface );
1104 
1105  if ( is_string( $language ) ) {
1106  $language = Language::factory( $language );
1107  }
1108  $popts->setTargetLanguage( $language );
1109 
1110  if ( !$title || !$title instanceof Title ) {
1111  global $wgTitle;
1112  wfDebugLog( 'GlobalTitleFail', __METHOD__ . ' called by ' .
1113  wfGetAllCallers( 6 ) . ' with no title set.' );
1114  $title = $wgTitle;
1115  }
1116  // Sometimes $wgTitle isn't set either...
1117  if ( !$title ) {
1118  # It's not uncommon having a null $wgTitle in scripts. See r80898
1119  # Create a ghost title in such case
1120  $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/title not set in ' . __METHOD__ );
1121  }
1122 
1123  $this->mInParser = true;
1124  $res = $parser->parse( $text, $title, $popts, $linestart );
1125  $this->mInParser = false;
1126 
1127  return $res;
1128  }
1129 
1130  function disable() {
1131  $this->mDisable = true;
1132  }
1133 
1134  function enable() {
1135  $this->mDisable = false;
1136  }
1137 
1150  public function isDisabled() {
1151  return $this->mDisable;
1152  }
1153 
1157  function clear() {
1158  $langs = Language::fetchLanguageNames( null, 'mw' );
1159  foreach ( array_keys( $langs ) as $code ) {
1160  # Global and local caches
1161  $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
1162  }
1163 
1164  $this->mLoadedLanguages = [];
1165  }
1166 
1171  public function figureMessage( $key ) {
1173 
1174  $pieces = explode( '/', $key );
1175  if ( count( $pieces ) < 2 ) {
1176  return [ $key, $wgLanguageCode ];
1177  }
1178 
1179  $lang = array_pop( $pieces );
1180  if ( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
1181  return [ $key, $wgLanguageCode ];
1182  }
1183 
1184  $message = implode( '/', $pieces );
1185 
1186  return [ $message, $lang ];
1187  }
1188 
1197  public function getAllMessageKeys( $code ) {
1199  $this->load( $code );
1200  if ( !isset( $this->mCache[$code] ) ) {
1201  // Apparently load() failed
1202  return null;
1203  }
1204  // Remove administrative keys
1205  $cache = $this->mCache[$code];
1206  unset( $cache['VERSION'] );
1207  unset( $cache['EXPIRY'] );
1208  // Remove any !NONEXISTENT keys
1209  $cache = array_diff( $cache, [ '!NONEXISTENT' ] );
1210 
1211  // Keys may appear with a capital first letter. lcfirst them.
1212  return array_map( [ $wgContLang, 'lcfirst' ], array_keys( $cache ) );
1213  }
1214 }
saveToCaches(array $cache, $dest, $code=false)
Shortcut to update caches.
and how to run hooks for an and one after Each event has a preferably in CamelCase For ArticleDelete hook A clump of code and data that should be run when an event happens This can be either a function and a chunk of data
Definition: hooks.txt:6
static getMainWANInstance()
Get the main WAN cache object.
getMessageFromFallbackChain($lang, $lckey, $useDB)
Given a language, try and fetch messages from that language.
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
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
const MSG_CACHE_VERSION
MediaWiki message cache structure version.
the array() calling protocol came about after MediaWiki 1.4rc1.
static getRevisionText($row, $prefix= 'old_', $wiki=false)
Get revision text associated with an old or archive row $row is usually an object from wfFetchRow()...
Definition: Revision.php:1273
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:79
$success
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
isCacheExpired($cache)
Is the given cache array expired due to time passing or a version change?
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static getMessageFor($key, $code)
Get a message for a given language.
Definition: Language.php:4438
static getCacheSetOptions(IDatabase $db1)
Merge the result of getSessionLagStatus() for several DBs using the most pessimistic values to estima...
Definition: Database.php:3025
Set options of the Parser.
$wgParser
Definition: Setup.php:821
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
if(!isset($args[0])) $lang
static destroyInstance()
Destroy the singleton instance.
null for the local wiki Added in
Definition: hooks.txt:1555
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:177
$value
const NS_SPECIAL
Definition: Defines.php:45
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 MediaWikiServices
Definition: injection.txt:23
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static getFallbacksFor($code)
Get the ordered list of fallback languages.
Definition: Language.php:4378
magic word & $parser
Definition: hooks.txt:2487
wfGetMessageCacheStorage()
Get the cache object used by the message cache.
const DB_MASTER
Definition: defines.php:23
Message cache Performs various MediaWiki namespace-related functions.
transform($message, $interface=false, $language=null, $title=null)
$wgUseLocalMessageCache
Set this to true to maintain a copy of the message cache on the local server.
getMsgFromNamespace($title, $code)
Get a message from the MediaWiki namespace, with caching.
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
static fetchLanguageNames($inLanguage=null, $include= 'mw')
Get an array of language names, indexed by code.
Definition: Language.php:800
getParserOptions()
ParserOptions is lazy initialised.
getLocalCache($code)
Try to load the cache from APC.
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:1230
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
static normalizeKey($key)
Normalize message key input.
$wgLanguageCode
Site language code.
wfDebugLog($logGroup, $text, $dest= 'all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
saveToLocalCache($code, $cache)
Save the cache to APC.
$wgMaxMsgCacheEntrySize
Maximum entry size in the message cache, in bytes.
static getMain()
Static methods.
wfGetCache($cacheType)
Get a specific cache object.
$wgAdaptiveMessageCache
Instead of caching everything, only cache those messages which have been customised in the site conte...
$res
Definition: database.txt:21
$wgUseDatabaseMessages
Translation using MediaWiki: namespace.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
$cache
Definition: mcc.php:33
$mParserOptions
Message cache has its own parser which it uses to transform messages.
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: defines.php:11
$mLoadedLanguages
Variable for tracking which variables are already loaded.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
static newKnownCurrent(IDatabase $db, $pageId, $revId)
Load a revision based on a known page ID and current revision ID from the DB.
Definition: Revision.php:1905
static $instance
Singleton instance.
A BagOStuff object with no objects in it.
getAllMessageKeys($code)
Get all message keys stored in the message cache for a given language.
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
getValidationHash($code)
Get the md5 used to validate the local APC cache.
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
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
Definition: distributors.txt:9
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:802
const NS_MEDIAWIKI
Definition: Defines.php:64
static fetchLanguageName($code, $inLanguage=null, $include= 'all')
Definition: Language.php:888
static newFromAnon()
Get a ParserOptions object for an anonymous user.
__construct($memCached, $useDB, $expiry)
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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 as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive false for true for descending in case the handler function wants to provide a converted Content object Note that $result getContentModel() must return $toModel. 'CustomEditor'$rcid is used in generating this variable which contains information about the new such as the revision s whether the revision was marked as a minor edit or etc which include things like revision author info
Definition: hooks.txt:1156
const WAIT_SEC
How long to wait for memcached locks.
load($code, $mode=null)
Loads messages from caches or from database in this order: (1) local message cache (if $wgUseLocalMes...
const HOLDOFF_TTL
Seconds to tombstone keys on delete()
wfGetAllCallers($limit=3)
Return a string consisting of callers in the stack.
const FOR_UPDATE
isDisabled()
Whether DB/cache usage is disabled for determining messages.
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:35
$mExpiry
Lifetime for cache, used by object caching.
parse($text, $title=null, $linestart=true, $interface=false, $language=null)
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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:1046
$wgMsgCacheExpiry
Expiry time for the message cache key.
$mCache
Process local cache of loaded messages that are defined in MediaWiki namespace.
loadFromDBWithLock($code, array &$where, $mode=null)
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:56
getReentrantScopedLock($key, $timeout=self::WAIT_SEC)
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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:1046
loadFromDB($code, $mode=null)
Loads cacheable messages from the database.
clear()
Clear all stored messages.
setValidationHash($code, array $cache)
Set the md5 used to validate the local disk cache.
wfMemcKey()
Make a cache key for the local wiki.
const DB_REPLICA
Definition: defines.php:22
serialize()
Definition: ApiMessage.php:94
if(!$wgRequest->checkUrlExtension()) if(!$wgEnableAPI) $wgTitle
Definition: api.php:57
$mDisable
Should mean that database cannot be used, but check.
const CACHE_NONE
Definition: Defines.php:94
static factory($code)
Get a cached or new language object for a given language code.
Definition: Language.php:181
getMessageForLang($lang, $lckey, $useDB, &$alreadyTried)
Given a language, try and fetch messages from that language and its fallbacks.
WANObjectCache $wanCache
static makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:511
BagOStuff $mMemc
const LOCK_TTL
How long memcached locks last.
static singleton()
Get the signleton instance of this class.
replace($title, $text)
Updates cache as necessary when message page is changed.
getMessagePageName($langcode, $uckey)
Get the message page name for a given language.
wfGetLangObj($langcode=false)
Return a Language object from $langcode.
$wgUser
Definition: Setup.php:806