00001 <?php
00010 define( 'MSG_LOAD_TIMEOUT', 60);
00011 define( 'MSG_LOCK_TIMEOUT', 10);
00012 define( 'MSG_WAIT_TIMEOUT', 10);
00013 define( 'MSG_CACHE_VERSION', 1 );
00014
00020 class MessageCache {
00021
00022 var $mCache;
00023
00024 var $mUseCache, $mDisable, $mExpiry;
00025 var $mKeys, $mParserOptions, $mParser;
00026
00027
00028 var $mLoadedLanguages = array();
00029
00030 function __construct( &$memCached, $useDB, $expiry, $memcPrefix ) {
00031 $this->mUseCache = !is_null( $memCached );
00032 $this->mMemc = &$memCached;
00033 $this->mDisable = !$useDB;
00034 $this->mExpiry = $expiry;
00035 $this->mDisableTransform = false;
00036 $this->mKeys = false; # initialised on demand
00037 $this->mParser = null;
00038 }
00039
00040
00044 function getParserOptions() {
00045 if ( !$this->mParserOptions ) {
00046 $this->mParserOptions = new ParserOptions;
00047 }
00048 return $this->mParserOptions;
00049 }
00050
00060 function loadFromLocal( $hash, $code ) {
00061 global $wgCacheDirectory, $wgLocalMessageCacheSerialized;
00062
00063 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
00064
00065 # Check file existence
00066 wfSuppressWarnings();
00067 $file = fopen( $filename, 'r' );
00068 wfRestoreWarnings();
00069 if ( !$file ) {
00070 return false;
00071 }
00072
00073 if ( $wgLocalMessageCacheSerialized ) {
00074
00075 $localHash = fread( $file, 32 );
00076 if ( $hash === $localHash ) {
00077
00078 $serialized = '';
00079 while ( !feof( $file ) ) {
00080 $serialized .= fread( $file, 100000 );
00081 }
00082 fclose( $file );
00083 return $this->setCache( unserialize( $serialized ), $code );
00084 } else {
00085 fclose( $file );
00086 return false;
00087 }
00088 } else {
00089 $localHash=substr(fread($file,40),8);
00090 fclose($file);
00091 if ($hash!=$localHash) {
00092 return false;
00093 }
00094
00095 # Require overwrites the member variable or just shadows it?
00096 require( $filename );
00097 return $this->setCache( $this->mCache, $code );
00098 }
00099 }
00100
00104 function saveToLocal( $serialized, $hash, $code ) {
00105 global $wgCacheDirectory;
00106
00107 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
00108 wfMkdirParents( $wgCacheDirectory );
00109
00110 wfSuppressWarnings();
00111 $file = fopen( $filename, 'w' );
00112 wfRestoreWarnings();
00113
00114 if ( !$file ) {
00115 wfDebug( "Unable to open local cache file for writing\n" );
00116 return;
00117 }
00118
00119 fwrite( $file, $hash . $serialized );
00120 fclose( $file );
00121 @chmod( $filename, 0666 );
00122 }
00123
00124 function saveToScript( $array, $hash, $code ) {
00125 global $wgCacheDirectory;
00126
00127 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
00128 $tempFilename = $filename . '.tmp';
00129 wfMkdirParents( $wgCacheDirectory );
00130
00131 wfSuppressWarnings();
00132 $file = fopen( $tempFilename, 'w');
00133 wfRestoreWarnings();
00134
00135 if ( !$file ) {
00136 wfDebug( "Unable to open local cache file for writing\n" );
00137 return;
00138 }
00139
00140 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
00141
00142 foreach ($array as $key => $message) {
00143 $key = $this->escapeForScript($key);
00144 $messages = $this->escapeForScript($message);
00145 fwrite($file, "'$key' => '$message',\n");
00146 }
00147
00148 fwrite($file,");\n?>");
00149 fclose($file);
00150 rename($tempFilename, $filename);
00151 }
00152
00153 function escapeForScript($string) {
00154 $string = str_replace( '\\', '\\\\', $string );
00155 $string = str_replace( '\'', '\\\'', $string );
00156 return $string;
00157 }
00158
00162 function setCache( $cache, $code ) {
00163 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) {
00164 $this->mCache[$code] = $cache;
00165 return true;
00166 } else {
00167 return false;
00168 }
00169 }
00170
00189 function load( $code = false ) {
00190 global $wgUseLocalMessageCache;
00191
00192 if ( !$this->mUseCache ) {
00193 return true;
00194 }
00195
00196 if( !is_string( $code ) ) {
00197 # This isn't really nice, so at least make a note about it and try to
00198 # fall back
00199 wfDebug( __METHOD__ . " called without providing a language code\n" );
00200 $code = 'en';
00201 }
00202
00203 # Don't do double loading...
00204 if ( isset($this->mLoadedLanguages[$code]) ) return true;
00205
00206 # 8 lines of code just to say (once) that message cache is disabled
00207 if ( $this->mDisable ) {
00208 static $shownDisabled = false;
00209 if ( !$shownDisabled ) {
00210 wfDebug( __METHOD__ . ": disabled\n" );
00211 $shownDisabled = true;
00212 }
00213 return true;
00214 }
00215
00216 # Loading code starts
00217 wfProfileIn( __METHOD__ );
00218 $success = false; # Keep track of success
00219 $where = array(); # Debug info, delayed to avoid spamming debug log too much
00220 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
00221
00222
00223 # (1) local cache
00224 # Hash of the contents is stored in memcache, to detect if local cache goes
00225 # out of date (due to update in other thread?)
00226 if ( $wgUseLocalMessageCache ) {
00227 wfProfileIn( __METHOD__ . '-fromlocal' );
00228
00229 $hash = $this->mMemc->get( wfMemcKey( 'messages', $code, 'hash' ) );
00230 if ( $hash ) {
00231 $success = $this->loadFromLocal( $hash, $code );
00232 if ( $success ) $where[] = 'got from local cache';
00233 }
00234 wfProfileOut( __METHOD__ . '-fromlocal' );
00235 }
00236
00237 # (2) memcache
00238 # Fails if nothing in cache, or in the wrong version.
00239 if ( !$success ) {
00240 wfProfileIn( __METHOD__ . '-fromcache' );
00241 $cache = $this->mMemc->get( $cacheKey );
00242 $success = $this->setCache( $cache, $code );
00243 if ( $success ) {
00244 $where[] = 'got from global cache';
00245 $this->saveToCaches( $cache, false, $code );
00246 }
00247 wfProfileOut( __METHOD__ . '-fromcache' );
00248 }
00249
00250
00251 # (3)
00252 # Nothing in caches... so we need create one and store it in caches
00253 if ( !$success ) {
00254 $where[] = 'cache is empty';
00255 $where[] = 'loading from database';
00256
00257 $this->lock($cacheKey);
00258
00259 # Limit the concurrency of loadFromDB to a single process
00260 # This prevents the site from going down when the cache expires
00261 $statusKey = wfMemcKey( 'messages', $code, 'status' );
00262 $success = $this->mMemc->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT );
00263 if ( $success ) {
00264 $cache = $this->loadFromDB( $code );
00265 $success = $this->setCache( $cache, $code );
00266 }
00267 if ( $success ) {
00268 $success = $this->saveToCaches( $cache, true, $code );
00269 if ( $success ) {
00270 $this->mMemc->delete( $statusKey );
00271 } else {
00272 $this->mMemc->set( $statusKey, 'error', 60*5 );
00273 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
00274 }
00275 }
00276 $this->unlock($cacheKey);
00277 }
00278
00279 if ( !$success ) {
00280 # Bad luck... this should not happen
00281 $where[] = 'loading FAILED - cache is disabled';
00282 $info = implode( ', ', $where );
00283 wfDebug( __METHOD__ . ": Loading $code... $info\n" );
00284 $this->mDisable = true;
00285 $this->mCache = false;
00286 } else {
00287 # All good, just record the success
00288 $info = implode( ', ', $where );
00289 wfDebug( __METHOD__ . ": Loading $code... $info\n" );
00290 $this->mLoadedLanguages[$code] = true;
00291 }
00292 wfProfileOut( __METHOD__ );
00293 return $success;
00294 }
00295
00304 function loadFromDB( $code = false ) {
00305 wfProfileIn( __METHOD__ );
00306 global $wgMaxMsgCacheEntrySize, $wgContLanguageCode;
00307 $dbr = wfGetDB( DB_SLAVE );
00308 $cache = array();
00309
00310 # Common conditions
00311 $conds = array(
00312 'page_is_redirect' => 0,
00313 'page_namespace' => NS_MEDIAWIKI,
00314 );
00315
00316 if ( $code ) {
00317 # Is this fast enough. Should not matter if the filtering is done in the
00318 # database or in code.
00319 if ( $code !== $wgContLanguageCode ) {
00320 # Messages for particular language
00321 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), "/$code" );
00322 } else {
00323 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
00324 # other than language code.
00325 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
00326 }
00327 }
00328
00329 # Conditions to fetch oversized pages to ignore them
00330 $bigConds = $conds;
00331 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
00332
00333 # Load titles for all oversized pages in the MediaWiki namespace
00334 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__ );
00335 while ( $row = $dbr->fetchObject( $res ) ) {
00336 $cache[$row->page_title] = '!TOO BIG';
00337 }
00338 $dbr->freeResult( $res );
00339
00340 # Conditions to load the remaining pages with their contents
00341 $smallConds = $conds;
00342 $smallConds[] = 'page_latest=rev_id';
00343 $smallConds[] = 'rev_text_id=old_id';
00344 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
00345
00346 $res = $dbr->select( array( 'page', 'revision', 'text' ),
00347 array( 'page_title', 'old_text', 'old_flags' ),
00348 $smallConds, __METHOD__. "($code)" );
00349
00350 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
00351 $cache[$row->page_title] = ' ' . Revision::getRevisionText( $row );
00352 }
00353 $dbr->freeResult( $res );
00354
00355 $cache['VERSION'] = MSG_CACHE_VERSION;
00356 wfProfileOut( __METHOD__ );
00357 return $cache;
00358 }
00359
00366 public function replace( $title, $text ) {
00367 global $wgMaxMsgCacheEntrySize;
00368 wfProfileIn( __METHOD__ );
00369
00370
00371 list( , $code ) = $this->figureMessage( $title );
00372
00373 $cacheKey = wfMemcKey( 'messages', $code );
00374 $this->load($code);
00375 $this->lock($cacheKey);
00376
00377 if ( is_array($this->mCache[$code]) ) {
00378 $titleKey = wfMemcKey( 'messages', 'individual', $title );
00379
00380 if ( $text === false ) {
00381 # Article was deleted
00382 unset( $this->mCache[$code][$title] );
00383 $this->mMemc->delete( $titleKey );
00384
00385 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
00386 # Check for size
00387 $this->mCache[$code][$title] = '!TOO BIG';
00388 $this->mMemc->set( $titleKey, ' ' . $text, $this->mExpiry );
00389
00390 } else {
00391 $this->mCache[$code][$title] = ' ' . $text;
00392 $this->mMemc->delete( $titleKey );
00393 }
00394
00395 # Update caches
00396 $this->saveToCaches( $this->mCache[$code], true, $code );
00397 }
00398 $this->unlock($cacheKey);
00399
00400
00401 global $parserMemc;
00402 $codes = array( $code );
00403 if ( $code === 'en' ) {
00404
00405
00406 $codes = array_keys( Language::getLanguageNames() );
00407 }
00408
00409 foreach ( $codes as $code ) {
00410 $sidebarKey = wfMemcKey( 'sidebar', $code );
00411 $parserMemc->delete( $sidebarKey );
00412 }
00413
00414 wfRunHooks( "MessageCacheReplace", array( $title, $text ) );
00415
00416 wfProfileOut( __METHOD__ );
00417 }
00418
00428 protected function saveToCaches( $cache, $memc = true, $code = false ) {
00429 wfProfileIn( __METHOD__ );
00430 global $wgUseLocalMessageCache, $wgLocalMessageCacheSerialized;
00431
00432 $cacheKey = wfMemcKey( 'messages', $code );
00433
00434 if ( $memc ) {
00435 $success = $this->mMemc->set( $cacheKey, $cache, $this->mExpiry );
00436 } else {
00437 $success = true;
00438 }
00439
00440 # Save to local cache
00441 if ( $wgUseLocalMessageCache ) {
00442 $serialized = serialize( $cache );
00443 $hash = md5( $serialized );
00444 $this->mMemc->set( wfMemcKey( 'messages', $code, 'hash' ), $hash, $this->mExpiry );
00445 if ($wgLocalMessageCacheSerialized) {
00446 $this->saveToLocal( $serialized, $hash, $code );
00447 } else {
00448 $this->saveToScript( $cache, $hash, $code );
00449 }
00450 }
00451
00452 wfProfileOut( __METHOD__ );
00453 return $success;
00454 }
00455
00460 function lock($key) {
00461 if ( !$this->mUseCache ) {
00462 return true;
00463 }
00464
00465 $lockKey = $key . ':lock';
00466 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
00467 sleep(1);
00468 }
00469
00470 return $i >= MSG_WAIT_TIMEOUT;
00471 }
00472
00473 function unlock($key) {
00474 if ( !$this->mUseCache ) {
00475 return;
00476 }
00477
00478 $lockKey = $key . ':lock';
00479 $this->mMemc->delete( $lockKey );
00480 }
00481
00498 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
00499 global $wgContLanguageCode, $wgContLang;
00500
00501 if ( strval( $key ) === '' ) {
00502 # Shortcut: the empty key is always missing
00503 return '<>';
00504 }
00505
00506 $lang = wfGetLangObj( $langcode );
00507 $langcode = $lang->getCode();
00508
00509 $message = false;
00510
00511 # Normalise title-case input (with some inlining)
00512 $lckey = str_replace( ' ', '_', $key );
00513 if ( ord( $key ) < 128 ) {
00514 $lckey[0] = strtolower( $lckey[0] );
00515 $uckey = ucfirst( $lckey );
00516 } else {
00517 $lckey = $wgContLang->lcfirst( $lckey );
00518 $uckey = $wgContLang->ucfirst( $lckey );
00519 }
00520
00521 # Try the MediaWiki namespace
00522 if( !$this->mDisable && $useDB ) {
00523 $title = $uckey;
00524 if(!$isFullKey && ( $langcode != $wgContLanguageCode ) ) {
00525 $title .= '/' . $langcode;
00526 }
00527 $message = $this->getMsgFromNamespace( $title, $langcode );
00528 }
00529
00530 # Try the array in the language object
00531 if ( $message === false ) {
00532 $message = $lang->getMessage( $lckey );
00533 if ( is_null( $message ) ) {
00534 $message = false;
00535 }
00536 }
00537
00538 # Try the array of another language
00539 if( $message === false ) {
00540 $parts = explode( '/', $lckey );
00541 # We may get calls for things that are http-urls from sidebar
00542 # Let's not load nonexistent languages for those
00543 # They usually have more than one slash.
00544 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
00545 $message = Language::getMessageFor( $parts[0], $parts[1] );
00546 if ( is_null( $message ) ) {
00547 $message = false;
00548 }
00549 }
00550 }
00551
00552 # Is this a custom message? Try the default language in the db...
00553 if( ($message === false || $message === '-' ) &&
00554 !$this->mDisable && $useDB &&
00555 !$isFullKey && ($langcode != $wgContLanguageCode) ) {
00556 $message = $this->getMsgFromNamespace( $uckey, $wgContLanguageCode );
00557 }
00558
00559 # Final fallback
00560 if( $message === false ) {
00561 return '<' . htmlspecialchars($key) . '>';
00562 }
00563
00564 # Fix whitespace
00565 $message = strtr( $message,
00566 array(
00567 # Fix for trailing whitespace, removed by textarea
00568 ' ' => ' ',
00569 # Fix for NBSP, converted to space by firefox
00570 ' ' => "\xc2\xa0",
00571 ) );
00572
00573 return $message;
00574 }
00575
00583 function getMsgFromNamespace( $title, $code ) {
00584 $type = false;
00585 $message = false;
00586
00587 if ( $this->mUseCache ) {
00588 $this->load( $code );
00589 if (isset( $this->mCache[$code][$title] ) ) {
00590 $entry = $this->mCache[$code][$title];
00591 $type = substr( $entry, 0, 1 );
00592 if ( $type == ' ' ) {
00593 return substr( $entry, 1 );
00594 }
00595 }
00596 }
00597
00598 # Call message hooks, in case they are defined
00599 wfRunHooks('MessagesPreLoad', array( $title, &$message ) );
00600 if ( $message !== false ) {
00601 return $message;
00602 }
00603
00604 # If there is no cache entry and no placeholder, it doesn't exist
00605 if ( $type !== '!' ) {
00606 return false;
00607 }
00608
00609 $titleKey = wfMemcKey( 'messages', 'individual', $title );
00610
00611 # Try the individual message cache
00612 if ( $this->mUseCache ) {
00613 $entry = $this->mMemc->get( $titleKey );
00614 if ( $entry ) {
00615 $type = substr( $entry, 0, 1 );
00616
00617 if ( $type === ' ' ) {
00618 # Ok!
00619 $message = substr( $entry, 1 );
00620 $this->mCache[$code][$title] = $entry;
00621 return $message;
00622 } elseif ( $entry === '!NONEXISTENT' ) {
00623 return false;
00624 } else {
00625 # Corrupt/obsolete entry, delete it
00626 $this->mMemc->delete( $titleKey );
00627 }
00628
00629 }
00630 }
00631
00632 # Try loading it from the DB
00633 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
00634 if( $revision ) {
00635 $message = $revision->getText();
00636 if ($this->mUseCache) {
00637 $this->mCache[$code][$title] = ' ' . $message;
00638 $this->mMemc->set( $titleKey, ' ' . $message, $this->mExpiry );
00639 }
00640 } else {
00641 # Negative caching
00642 # Use some special text instead of false, because false gets converted to '' somewhere
00643 $this->mMemc->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
00644 $this->mCache[$code][$title] = false;
00645 }
00646 return $message;
00647 }
00648
00649 function transform( $message, $interface = false, $language = null ) {
00650
00651 if( strpos( $message, '{{' ) === false ) {
00652 return $message;
00653 }
00654
00655 global $wgParser, $wgParserConf;
00656 if ( !$this->mParser && isset( $wgParser ) ) {
00657 # Do some initialisation so that we don't have to do it twice
00658 $wgParser->firstCallInit();
00659 # Clone it and store it
00660 $class = $wgParserConf['class'];
00661 if ( $class == 'Parser_DiffTest' ) {
00662 # Uncloneable
00663 $this->mParser = new $class( $wgParserConf );
00664 } else {
00665 $this->mParser = clone $wgParser;
00666 }
00667 #wfDebug( __METHOD__ . ": following contents triggered transform: $message\n" );
00668 }
00669 if ( $this->mParser ) {
00670 $popts = $this->getParserOptions();
00671 $popts->setInterfaceMessage( $interface );
00672 $popts->setTargetLanguage( $language );
00673 $message = $this->mParser->transformMsg( $message, $popts );
00674 }
00675 return $message;
00676 }
00677
00678 function disable() { $this->mDisable = true; }
00679 function enable() { $this->mDisable = false; }
00680
00682 function disableTransform(){
00683 wfDeprecated( __METHOD__ );
00684 }
00685 function enableTransform() {
00686 wfDeprecated( __METHOD__ );
00687 }
00688 function setTransform( $x ) {
00689 wfDeprecated( __METHOD__ );
00690 }
00691 function getTransform() {
00692 wfDeprecated( __METHOD__ );
00693 return false;
00694 }
00695
00699 function clear() {
00700 if( $this->mUseCache ) {
00701 $langs = Language::getLanguageNames( false );
00702 foreach ( array_keys($langs) as $code ) {
00703 # Global cache
00704 $this->mMemc->delete( wfMemcKey( 'messages', $code ) );
00705 # Invalidate all local caches
00706 $this->mMemc->delete( wfMemcKey( 'messages', $code, 'hash' ) );
00707 }
00708 }
00709 }
00710
00719 function addMessage( $key, $value, $lang = 'en' ) {
00720 wfDeprecated( __METHOD__ );
00721 $lc = Language::getLocalisationCache();
00722 $lc->addLegacyMessages( array( $lang => array( $key => $value ) ) );
00723 }
00724
00732 function addMessages( $messages, $lang = 'en' ) {
00733 wfDeprecated( __METHOD__ );
00734 $lc = Language::getLocalisationCache();
00735 $lc->addLegacyMessages( array( $lang => $messages ) );
00736 }
00737
00744 function addMessagesByLang( $messages ) {
00745 wfDeprecated( __METHOD__ );
00746 $lc = Language::getLocalisationCache();
00747 $lc->addLegacyMessages( $messages );
00748 }
00749
00753 function setExtensionMessagesHook( $callback ) {
00754 $this->mAddMessagesHook = $callback;
00755 }
00756
00760 function loadAllMessages( $lang = false ) {
00761 }
00762
00766 function loadMessagesFile( $filename, $langcode = false ) {
00767 }
00768
00769 public function figureMessage( $key ) {
00770 global $wgContLanguageCode;
00771 $pieces = explode( '/', $key );
00772 if( count( $pieces ) < 2 )
00773 return array( $key, $wgContLanguageCode );
00774
00775 $lang = array_pop( $pieces );
00776 $validCodes = Language::getLanguageNames();
00777 if( !array_key_exists( $lang, $validCodes ) )
00778 return array( $key, $wgContLanguageCode );
00779
00780 $message = implode( '/', $pieces );
00781 return array( $message, $lang );
00782 }
00783
00784 }