MediaWiki  1.28.3
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  $po->setAllowUnsafeRawHtml( false );
183  return $po;
184  }
185 
186  $this->mParserOptions = new ParserOptions;
187  $this->mParserOptions->setEditSection( false );
188  // Messages may take parameters that could come
189  // from malicious sources. As a precaution, disable
190  // the <html> parser tag when parsing messages.
191  $this->mParserOptions->setAllowUnsafeRawHtml( false );
192  }
193 
194  return $this->mParserOptions;
195  }
196 
203  protected function getLocalCache( $code ) {
204  $cacheKey = wfMemcKey( __CLASS__, $code );
205 
206  return $this->localCache->get( $cacheKey );
207  }
208 
215  protected function saveToLocalCache( $code, $cache ) {
216  $cacheKey = wfMemcKey( __CLASS__, $code );
217  $this->localCache->set( $cacheKey, $cache );
218  }
219 
241  protected function load( $code, $mode = null ) {
242  if ( !is_string( $code ) ) {
243  throw new InvalidArgumentException( "Missing language code" );
244  }
245 
246  # Don't do double loading...
247  if ( isset( $this->mLoadedLanguages[$code] ) && $mode != self::FOR_UPDATE ) {
248  return true;
249  }
250 
251  # 8 lines of code just to say (once) that message cache is disabled
252  if ( $this->mDisable ) {
253  static $shownDisabled = false;
254  if ( !$shownDisabled ) {
255  wfDebug( __METHOD__ . ": disabled\n" );
256  $shownDisabled = true;
257  }
258 
259  return true;
260  }
261 
262  # Loading code starts
263  $success = false; # Keep track of success
264  $staleCache = false; # a cache array with expired data, or false if none has been loaded
265  $where = []; # Debug info, delayed to avoid spamming debug log too much
266 
267  # Hash of the contents is stored in memcache, to detect if data-center cache
268  # or local cache goes out of date (e.g. due to replace() on some other server)
269  list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
270 
271  # Try the local cache and check against the cluster hash key...
272  $cache = $this->getLocalCache( $code );
273  if ( !$cache ) {
274  $where[] = 'local cache is empty';
275  } elseif ( !isset( $cache['HASH'] ) || $cache['HASH'] !== $hash ) {
276  $where[] = 'local cache has the wrong hash';
277  $staleCache = $cache;
278  } elseif ( $this->isCacheExpired( $cache ) ) {
279  $where[] = 'local cache is expired';
280  $staleCache = $cache;
281  } elseif ( $hashVolatile ) {
282  $where[] = 'local cache validation key is expired/volatile';
283  $staleCache = $cache;
284  } else {
285  $where[] = 'got from local cache';
286  $success = true;
287  $this->mCache[$code] = $cache;
288  }
289 
290  if ( !$success ) {
291  $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
292  # Try the global cache. If it is empty, try to acquire a lock. If
293  # the lock can't be acquired, wait for the other thread to finish
294  # and then try the global cache a second time.
295  for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) {
296  if ( $hashVolatile && $staleCache ) {
297  # Do not bother fetching the whole cache blob to avoid I/O.
298  # Instead, just try to get the non-blocking $statusKey lock
299  # below, and use the local stale value if it was not acquired.
300  $where[] = 'global cache is presumed expired';
301  } else {
302  $cache = $this->mMemc->get( $cacheKey );
303  if ( !$cache ) {
304  $where[] = 'global cache is empty';
305  } elseif ( $this->isCacheExpired( $cache ) ) {
306  $where[] = 'global cache is expired';
307  $staleCache = $cache;
308  } elseif ( $hashVolatile ) {
309  # DB results are replica DB lag prone until the holdoff TTL passes.
310  # By then, updates should be reflected in loadFromDBWithLock().
311  # One thread renerates the cache while others use old values.
312  $where[] = 'global cache is expired/volatile';
313  $staleCache = $cache;
314  } else {
315  $where[] = 'got from global cache';
316  $this->mCache[$code] = $cache;
317  $this->saveToCaches( $cache, 'local-only', $code );
318  $success = true;
319  }
320  }
321 
322  if ( $success ) {
323  # Done, no need to retry
324  break;
325  }
326 
327  # We need to call loadFromDB. Limit the concurrency to one process.
328  # This prevents the site from going down when the cache expires.
329  # Note that the DB slam protection lock here is non-blocking.
330  $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
331  if ( $loadStatus === true ) {
332  $success = true;
333  break;
334  } elseif ( $staleCache ) {
335  # Use the stale cache while some other thread constructs the new one
336  $where[] = 'using stale cache';
337  $this->mCache[$code] = $staleCache;
338  $success = true;
339  break;
340  } elseif ( $failedAttempts > 0 ) {
341  # Already blocked once, so avoid another lock/unlock cycle.
342  # This case will typically be hit if memcached is down, or if
343  # loadFromDB() takes longer than LOCK_WAIT.
344  $where[] = "could not acquire status key.";
345  break;
346  } elseif ( $loadStatus === 'cantacquire' ) {
347  # Wait for the other thread to finish, then retry. Normally,
348  # the memcached get() will then yeild the other thread's result.
349  $where[] = 'waited for other thread to complete';
350  $this->getReentrantScopedLock( $cacheKey );
351  } else {
352  # Disable cache; $loadStatus is 'disabled'
353  break;
354  }
355  }
356  }
357 
358  if ( !$success ) {
359  $where[] = 'loading FAILED - cache is disabled';
360  $this->mDisable = true;
361  $this->mCache = false;
362  wfDebugLog( 'MessageCacheError', __METHOD__ . ": Failed to load $code\n" );
363  # This used to throw an exception, but that led to nasty side effects like
364  # the whole wiki being instantly down if the memcached server died
365  } else {
366  # All good, just record the success
367  $this->mLoadedLanguages[$code] = true;
368  }
369 
370  $info = implode( ', ', $where );
371  wfDebugLog( 'MessageCache', __METHOD__ . ": Loading $code... $info\n" );
372 
373  return $success;
374  }
375 
382  protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
384 
385  # If cache updates on all levels fail, give up on message overrides.
386  # This is to avoid easy site outages; see $saveSuccess comments below.
387  $statusKey = wfMemcKey( 'messages', $code, 'status' );
388  $status = $this->mMemc->get( $statusKey );
389  if ( $status === 'error' ) {
390  $where[] = "could not load; method is still globally disabled";
391  return 'disabled';
392  }
393 
394  # Now let's regenerate
395  $where[] = 'loading from database';
396 
397  # Lock the cache to prevent conflicting writes.
398  # This lock is non-blocking so stale cache can quickly be used.
399  # Note that load() will call a blocking getReentrantScopedLock()
400  # after this if it really need to wait for any current thread.
401  $cacheKey = wfMemcKey( 'messages', $code );
402  $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
403  if ( !$scopedLock ) {
404  $where[] = 'could not acquire main lock';
405  return 'cantacquire';
406  }
407 
408  $cache = $this->loadFromDB( $code, $mode );
409  $this->mCache[$code] = $cache;
410  $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
411 
412  if ( !$saveSuccess ) {
426  if ( !$wgUseLocalMessageCache ) {
427  $this->mMemc->set( $statusKey, 'error', 60 * 5 );
428  $where[] = 'could not save cache, disabled globally for 5 minutes';
429  } else {
430  $where[] = "could not save global cache";
431  }
432  }
433 
434  return true;
435  }
436 
446  function loadFromDB( $code, $mode = null ) {
448 
449  $dbr = wfGetDB( ( $mode == self::FOR_UPDATE ) ? DB_MASTER : DB_REPLICA );
450 
451  $cache = [];
452 
453  # Common conditions
454  $conds = [
455  'page_is_redirect' => 0,
456  'page_namespace' => NS_MEDIAWIKI,
457  ];
458 
459  $mostused = [];
460  if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
461  if ( !isset( $this->mCache[$wgLanguageCode] ) ) {
462  $this->load( $wgLanguageCode );
463  }
464  $mostused = array_keys( $this->mCache[$wgLanguageCode] );
465  foreach ( $mostused as $key => $value ) {
466  $mostused[$key] = "$value/$code";
467  }
468  }
469 
470  if ( count( $mostused ) ) {
471  $conds['page_title'] = $mostused;
472  } elseif ( $code !== $wgLanguageCode ) {
473  $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
474  } else {
475  # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
476  # other than language code.
477  $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
478  }
479 
480  # Conditions to fetch oversized pages to ignore them
481  $bigConds = $conds;
482  $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
483 
484  # Load titles for all oversized pages in the MediaWiki namespace
485  $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__ . "($code)-big" );
486  foreach ( $res as $row ) {
487  $cache[$row->page_title] = '!TOO BIG';
488  }
489 
490  # Conditions to load the remaining pages with their contents
491  $smallConds = $conds;
492  $smallConds[] = 'page_latest=rev_id';
493  $smallConds[] = 'rev_text_id=old_id';
494  $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
495 
496  $res = $dbr->select(
497  [ 'page', 'revision', 'text' ],
498  [ 'page_title', 'old_text', 'old_flags' ],
499  $smallConds,
500  __METHOD__ . "($code)-small"
501  );
502 
503  foreach ( $res as $row ) {
504  $text = Revision::getRevisionText( $row );
505  if ( $text === false ) {
506  // Failed to fetch data; possible ES errors?
507  // Store a marker to fetch on-demand as a workaround...
508  $entry = '!TOO BIG';
509  wfDebugLog(
510  'MessageCache',
511  __METHOD__
512  . ": failed to load message page text for {$row->page_title} ($code)"
513  );
514  } else {
515  $entry = ' ' . $text;
516  }
517  $cache[$row->page_title] = $entry;
518  }
519 
520  $cache['VERSION'] = MSG_CACHE_VERSION;
521  ksort( $cache );
522  $cache['HASH'] = md5( serialize( $cache ) );
523  $cache['EXPIRY'] = wfTimestamp( TS_MW, time() + $this->mExpiry );
524 
525  return $cache;
526  }
527 
534  public function replace( $title, $text ) {
536 
537  if ( $this->mDisable ) {
538  return;
539  }
540 
541  list( $msg, $code ) = $this->figureMessage( $title );
542  if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
543  // Content language overrides do not use the /<code> suffix
544  return;
545  }
546 
547  // Note that if the cache is volatile, load() may trigger a DB fetch.
548  // In that case we reenter/reuse the existing cache key lock to avoid
549  // a self-deadlock. This is safe as no reads happen *directly* in this
550  // method between getReentrantScopedLock() and load() below. There is
551  // no risk of data "changing under our feet" for replace().
552  $cacheKey = wfMemcKey( 'messages', $code );
553  $scopedLock = $this->getReentrantScopedLock( $cacheKey );
554  $this->load( $code, self::FOR_UPDATE );
555 
556  $titleKey = wfMemcKey( 'messages', 'individual', $title );
557  if ( $text === false ) {
558  // Article was deleted
559  $this->mCache[$code][$title] = '!NONEXISTENT';
560  $this->wanCache->delete( $titleKey );
561  } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
562  // Check for size
563  $this->mCache[$code][$title] = '!TOO BIG';
564  $this->wanCache->set( $titleKey, ' ' . $text, $this->mExpiry );
565  } else {
566  $this->mCache[$code][$title] = ' ' . $text;
567  $this->wanCache->delete( $titleKey );
568  }
569 
570  // Mark this cache as definitely "latest" (non-volatile) so
571  // load() calls do try to refresh the cache with replica DB data
572  $this->mCache[$code]['LATEST'] = time();
573 
574  // Update caches if the lock was acquired
575  if ( $scopedLock ) {
576  $this->saveToCaches( $this->mCache[$code], 'all', $code );
577  }
578 
579  ScopedCallback::consume( $scopedLock );
580  // Relay the purge to APC and other DCs
581  $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
582 
583  // Also delete cached sidebar... just in case it is affected
584  $codes = [ $code ];
585  if ( $code === 'en' ) {
586  // Delete all sidebars, like for example on action=purge on the
587  // sidebar messages
588  $codes = array_keys( Language::fetchLanguageNames() );
589  }
590 
591  foreach ( $codes as $code ) {
592  $sidebarKey = wfMemcKey( 'sidebar', $code );
593  $this->wanCache->delete( $sidebarKey );
594  }
595 
596  // Update the message in the message blob store
597  $resourceloader = RequestContext::getMain()->getOutput()->getResourceLoader();
598  $blobStore = $resourceloader->getMessageBlobStore();
599  $blobStore->updateMessage( $wgContLang->lcfirst( $msg ) );
600 
601  Hooks::run( 'MessageCacheReplace', [ $title, $text ] );
602  }
603 
610  protected function isCacheExpired( $cache ) {
611  if ( !isset( $cache['VERSION'] ) || !isset( $cache['EXPIRY'] ) ) {
612  return true;
613  }
614  if ( $cache['VERSION'] != MSG_CACHE_VERSION ) {
615  return true;
616  }
617  if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
618  return true;
619  }
620 
621  return false;
622  }
623 
633  protected function saveToCaches( array $cache, $dest, $code = false ) {
634  if ( $dest === 'all' ) {
635  $cacheKey = wfMemcKey( 'messages', $code );
636  $success = $this->mMemc->set( $cacheKey, $cache );
637  $this->setValidationHash( $code, $cache );
638  } else {
639  $success = true;
640  }
641 
642  $this->saveToLocalCache( $code, $cache );
643 
644  return $success;
645  }
646 
653  protected function getValidationHash( $code ) {
654  $curTTL = null;
655  $value = $this->wanCache->get(
656  wfMemcKey( 'messages', $code, 'hash', 'v1' ),
657  $curTTL,
658  [ wfMemcKey( 'messages', $code ) ]
659  );
660 
661  if ( !$value ) {
662  // No hash found at all; cache must regenerate to be safe
663  $hash = false;
664  $expired = true;
665  } else {
666  $hash = $value['hash'];
667  if ( ( time() - $value['latest'] ) < WANObjectCache::HOLDOFF_TTL ) {
668  // Cache was recently updated via replace() and should be up-to-date
669  $expired = false;
670  } else {
671  // See if the "check" key was bumped after the hash was generated
672  $expired = ( $curTTL < 0 );
673  }
674  }
675 
676  return [ $hash, $expired ];
677  }
678 
688  protected function setValidationHash( $code, array $cache ) {
689  $this->wanCache->set(
690  wfMemcKey( 'messages', $code, 'hash', 'v1' ),
691  [
692  'hash' => $cache['HASH'],
693  'latest' => isset( $cache['LATEST'] ) ? $cache['LATEST'] : 0
694  ],
696  );
697  }
698 
704  protected function getReentrantScopedLock( $key, $timeout = self::WAIT_SEC ) {
705  return $this->mMemc->getScopedLock( $key, $timeout, self::LOCK_TTL, __METHOD__ );
706  }
707 
742  function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
743  if ( is_int( $key ) ) {
744  // Fix numerical strings that somehow become ints
745  // on their way here
746  $key = (string)$key;
747  } elseif ( !is_string( $key ) ) {
748  throw new MWException( 'Non-string key given' );
749  } elseif ( $key === '' ) {
750  // Shortcut: the empty key is always missing
751  return false;
752  }
753 
754  // For full keys, get the language code from the key
755  $pos = strrpos( $key, '/' );
756  if ( $isFullKey && $pos !== false ) {
757  $langcode = substr( $key, $pos + 1 );
758  $key = substr( $key, 0, $pos );
759  }
760 
761  // Normalise title-case input (with some inlining)
762  $lckey = MessageCache::normalizeKey( $key );
763 
764  Hooks::run( 'MessageCache::get', [ &$lckey ] );
765 
766  // Loop through each language in the fallback list until we find something useful
767  $lang = wfGetLangObj( $langcode );
768  $message = $this->getMessageFromFallbackChain(
769  $lang,
770  $lckey,
771  !$this->mDisable && $useDB
772  );
773 
774  // If we still have no message, maybe the key was in fact a full key so try that
775  if ( $message === false ) {
776  $parts = explode( '/', $lckey );
777  // We may get calls for things that are http-urls from sidebar
778  // Let's not load nonexistent languages for those
779  // They usually have more than one slash.
780  if ( count( $parts ) == 2 && $parts[1] !== '' ) {
781  $message = Language::getMessageFor( $parts[0], $parts[1] );
782  if ( $message === null ) {
783  $message = false;
784  }
785  }
786  }
787 
788  // Post-processing if the message exists
789  if ( $message !== false ) {
790  // Fix whitespace
791  $message = str_replace(
792  [
793  # Fix for trailing whitespace, removed by textarea
794  '&#32;',
795  # Fix for NBSP, converted to space by firefox
796  '&nbsp;',
797  '&#160;',
798  '&shy;'
799  ],
800  [
801  ' ',
802  "\xc2\xa0",
803  "\xc2\xa0",
804  "\xc2\xad"
805  ],
806  $message
807  );
808  }
809 
810  return $message;
811  }
812 
825  protected function getMessageFromFallbackChain( $lang, $lckey, $useDB ) {
827 
828  $alreadyTried = [];
829 
830  // First try the requested language.
831  $message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried );
832  if ( $message !== false ) {
833  return $message;
834  }
835 
836  // Now try checking the site language.
837  $message = $this->getMessageForLang( $wgContLang, $lckey, $useDB, $alreadyTried );
838  return $message;
839  }
840 
851  private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) {
853  $langcode = $lang->getCode();
854 
855  // Try checking the database for the requested language
856  if ( $useDB ) {
857  $uckey = $wgContLang->ucfirst( $lckey );
858 
859  if ( !isset( $alreadyTried[ $langcode ] ) ) {
860  $message = $this->getMsgFromNamespace(
861  $this->getMessagePageName( $langcode, $uckey ),
862  $langcode
863  );
864 
865  if ( $message !== false ) {
866  return $message;
867  }
868  $alreadyTried[ $langcode ] = true;
869  }
870  } else {
871  $uckey = null;
872  }
873 
874  // Check the CDB cache
875  $message = $lang->getMessage( $lckey );
876  if ( $message !== null ) {
877  return $message;
878  }
879 
880  // Try checking the database for all of the fallback languages
881  if ( $useDB ) {
882  $fallbackChain = Language::getFallbacksFor( $langcode );
883 
884  foreach ( $fallbackChain as $code ) {
885  if ( isset( $alreadyTried[ $code ] ) ) {
886  continue;
887  }
888 
889  $message = $this->getMsgFromNamespace(
890  $this->getMessagePageName( $code, $uckey ), $code );
891 
892  if ( $message !== false ) {
893  return $message;
894  }
895  $alreadyTried[ $code ] = true;
896  }
897  }
898 
899  return false;
900  }
901 
909  private function getMessagePageName( $langcode, $uckey ) {
911  if ( $langcode === $wgLanguageCode ) {
912  // Messages created in the content language will not have the /lang extension
913  return $uckey;
914  } else {
915  return "$uckey/$langcode";
916  }
917  }
918 
931  public function getMsgFromNamespace( $title, $code ) {
932  $this->load( $code );
933  if ( isset( $this->mCache[$code][$title] ) ) {
934  $entry = $this->mCache[$code][$title];
935  if ( substr( $entry, 0, 1 ) === ' ' ) {
936  // The message exists, so make sure a string
937  // is returned.
938  return (string)substr( $entry, 1 );
939  } elseif ( $entry === '!NONEXISTENT' ) {
940  return false;
941  } elseif ( $entry === '!TOO BIG' ) {
942  // Fall through and try invididual message cache below
943  }
944  } else {
945  // XXX: This is not cached in process cache, should it?
946  $message = false;
947  Hooks::run( 'MessagesPreLoad', [ $title, &$message ] );
948  if ( $message !== false ) {
949  return $message;
950  }
951 
952  return false;
953  }
954 
955  // Try the individual message cache
956  $titleKey = wfMemcKey( 'messages', 'individual', $title );
957 
958  $curTTL = null;
959  $entry = $this->wanCache->get(
960  $titleKey,
961  $curTTL,
962  [ wfMemcKey( 'messages', $code ) ]
963  );
964  $entry = ( $curTTL >= 0 ) ? $entry : false;
965 
966  if ( $entry ) {
967  if ( substr( $entry, 0, 1 ) === ' ' ) {
968  $this->mCache[$code][$title] = $entry;
969  // The message exists, so make sure a string is returned
970  return (string)substr( $entry, 1 );
971  } elseif ( $entry === '!NONEXISTENT' ) {
972  $this->mCache[$code][$title] = '!NONEXISTENT';
973 
974  return false;
975  } else {
976  // Corrupt/obsolete entry, delete it
977  $this->wanCache->delete( $titleKey );
978  }
979  }
980 
981  // Try loading it from the database
982  $dbr = wfGetDB( DB_REPLICA );
983  $cacheOpts = Database::getCacheSetOptions( $dbr );
984  // Use newKnownCurrent() to avoid querying revision/user tables
985  $titleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
986  if ( $titleObj->getLatestRevID() ) {
987  $revision = Revision::newKnownCurrent(
988  $dbr,
989  $titleObj->getArticleID(),
990  $titleObj->getLatestRevID()
991  );
992  } else {
993  $revision = false;
994  }
995 
996  if ( $revision ) {
997  $content = $revision->getContent();
998  if ( !$content ) {
999  // A possibly temporary loading failure.
1000  wfDebugLog(
1001  'MessageCache',
1002  __METHOD__ . ": failed to load message page text for {$title} ($code)"
1003  );
1004  $message = null; // no negative caching
1005  } else {
1006  // XXX: Is this the right way to turn a Content object into a message?
1007  // NOTE: $content is typically either WikitextContent, JavaScriptContent or
1008  // CssContent. MessageContent is *not* used for storing messages, it's
1009  // only used for wrapping them when needed.
1010  $message = $content->getWikitextForTransclusion();
1011 
1012  if ( $message === false || $message === null ) {
1013  wfDebugLog(
1014  'MessageCache',
1015  __METHOD__ . ": message content doesn't provide wikitext "
1016  . "(content model: " . $content->getModel() . ")"
1017  );
1018 
1019  $message = false; // negative caching
1020  } else {
1021  $this->mCache[$code][$title] = ' ' . $message;
1022  $this->wanCache->set( $titleKey, ' ' . $message, $this->mExpiry, $cacheOpts );
1023  }
1024  }
1025  } else {
1026  $message = false; // negative caching
1027  }
1028 
1029  if ( $message === false ) { // negative caching
1030  $this->mCache[$code][$title] = '!NONEXISTENT';
1031  $this->wanCache->set( $titleKey, '!NONEXISTENT', $this->mExpiry, $cacheOpts );
1032  }
1033 
1034  return $message;
1035  }
1036 
1044  function transform( $message, $interface = false, $language = null, $title = null ) {
1045  // Avoid creating parser if nothing to transform
1046  if ( strpos( $message, '{{' ) === false ) {
1047  return $message;
1048  }
1049 
1050  if ( $this->mInParser ) {
1051  return $message;
1052  }
1053 
1054  $parser = $this->getParser();
1055  if ( $parser ) {
1056  $popts = $this->getParserOptions();
1057  $popts->setInterfaceMessage( $interface );
1058  $popts->setTargetLanguage( $language );
1059 
1060  $userlang = $popts->setUserLang( $language );
1061  $this->mInParser = true;
1062  $message = $parser->transformMsg( $message, $popts, $title );
1063  $this->mInParser = false;
1064  $popts->setUserLang( $userlang );
1065  }
1066 
1067  return $message;
1068  }
1069 
1073  function getParser() {
1074  global $wgParser, $wgParserConf;
1075  if ( !$this->mParser && isset( $wgParser ) ) {
1076  # Do some initialisation so that we don't have to do it twice
1077  $wgParser->firstCallInit();
1078  # Clone it and store it
1079  $class = $wgParserConf['class'];
1080  if ( $class == 'ParserDiffTest' ) {
1081  # Uncloneable
1082  $this->mParser = new $class( $wgParserConf );
1083  } else {
1084  $this->mParser = clone $wgParser;
1085  }
1086  }
1087 
1088  return $this->mParser;
1089  }
1090 
1099  public function parse( $text, $title = null, $linestart = true,
1100  $interface = false, $language = null
1101  ) {
1102  if ( $this->mInParser ) {
1103  return htmlspecialchars( $text );
1104  }
1105 
1106  $parser = $this->getParser();
1107  $popts = $this->getParserOptions();
1108  $popts->setInterfaceMessage( $interface );
1109 
1110  if ( is_string( $language ) ) {
1111  $language = Language::factory( $language );
1112  }
1113  $popts->setTargetLanguage( $language );
1114 
1115  if ( !$title || !$title instanceof Title ) {
1116  global $wgTitle;
1117  wfDebugLog( 'GlobalTitleFail', __METHOD__ . ' called by ' .
1118  wfGetAllCallers( 6 ) . ' with no title set.' );
1119  $title = $wgTitle;
1120  }
1121  // Sometimes $wgTitle isn't set either...
1122  if ( !$title ) {
1123  # It's not uncommon having a null $wgTitle in scripts. See r80898
1124  # Create a ghost title in such case
1125  $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/title not set in ' . __METHOD__ );
1126  }
1127 
1128  $this->mInParser = true;
1129  $res = $parser->parse( $text, $title, $popts, $linestart );
1130  $this->mInParser = false;
1131 
1132  return $res;
1133  }
1134 
1135  function disable() {
1136  $this->mDisable = true;
1137  }
1138 
1139  function enable() {
1140  $this->mDisable = false;
1141  }
1142 
1155  public function isDisabled() {
1156  return $this->mDisable;
1157  }
1158 
1162  function clear() {
1163  $langs = Language::fetchLanguageNames( null, 'mw' );
1164  foreach ( array_keys( $langs ) as $code ) {
1165  # Global and local caches
1166  $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
1167  }
1168 
1169  $this->mLoadedLanguages = [];
1170  }
1171 
1176  public function figureMessage( $key ) {
1178 
1179  $pieces = explode( '/', $key );
1180  if ( count( $pieces ) < 2 ) {
1181  return [ $key, $wgLanguageCode ];
1182  }
1183 
1184  $lang = array_pop( $pieces );
1185  if ( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
1186  return [ $key, $wgLanguageCode ];
1187  }
1188 
1189  $message = implode( '/', $pieces );
1190 
1191  return [ $message, $lang ];
1192  }
1193 
1202  public function getAllMessageKeys( $code ) {
1204  $this->load( $code );
1205  if ( !isset( $this->mCache[$code] ) ) {
1206  // Apparently load() failed
1207  return null;
1208  }
1209  // Remove administrative keys
1210  $cache = $this->mCache[$code];
1211  unset( $cache['VERSION'] );
1212  unset( $cache['EXPIRY'] );
1213  // Remove any !NONEXISTENT keys
1214  $cache = array_diff( $cache, [ '!NONEXISTENT' ] );
1215 
1216  // Keys may appear with a capital first letter. lcfirst them.
1217  return array_map( [ $wgContLang, 'lcfirst' ], array_keys( $cache ) );
1218  }
1219 }
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:3039
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:1559
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:2491
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:1234
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:957
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:806
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:1160
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:1050
$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:1050
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(isset($_SERVER['PATH_INFO'])&&$_SERVER['PATH_INFO']!= '') if(!$wgEnableAPI) $wgTitle
Definition: api.php:68
$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