11 define( 'MSG_LOAD_TIMEOUT', 60);
12 define( 'MSG_LOCK_TIMEOUT', 10);
13 define( 'MSG_WAIT_TIMEOUT', 10);
17 * Performs various useful MediaWiki namespace-related functions
22 var $mCache, $mUseCache, $mDisable, $mExpiry;
23 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
24 var $mExtensionMessages = array();
25 var $mInitialised = false;
26 var $mDeferred = true;
28 function __construct( &$memCached, $useDB, $expiry, $memcPrefix) {
29 wfProfileIn( __METHOD__
);
31 $this->mUseCache
= !is_null( $memCached );
32 $this->mMemc
= &$memCached;
33 $this->mDisable
= !$useDB;
34 $this->mExpiry
= $expiry;
35 $this->mDisableTransform
= false;
36 $this->mMemcKey
= $memcPrefix.':messages';
37 $this->mKeys
= false; # initialised on demand
38 $this->mInitialised
= true;
39 $this->mParser
= null;
41 # When we first get asked for a message,
42 # then we'll fill up the cache. If we
43 # can return a cache hit, this saves
44 # some extra milliseconds
45 $this->mDeferred
= true;
47 wfProfileOut( __METHOD__
);
50 function getParserOptions() {
51 if ( !$this->mParserOptions
) {
52 $this->mParserOptions
= new ParserOptions
;
54 return $this->mParserOptions
;
58 * Try to load the cache from a local file
60 function loadFromLocal( $hash ) {
61 global $wgLocalMessageCache, $wgDBname;
63 $this->mCache
= false;
64 if ( $wgLocalMessageCache === false ) {
68 $filename = "$wgLocalMessageCache/messages-$wgDBname";
71 $file = fopen( $filename, 'r' );
77 // Check to see if the file has the hash specified
78 $localHash = fread( $file, 32 );
79 if ( $hash == $localHash ) {
80 // All good, get the rest of it
81 $serialized = fread( $file, 1000000 );
82 $this->mCache
= unserialize( $serialized );
88 * Save the cache to a local file
90 function saveToLocal( $serialized, $hash ) {
91 global $wgLocalMessageCache, $wgDBname;
93 if ( $wgLocalMessageCache === false ) {
97 $filename = "$wgLocalMessageCache/messages-$wgDBname";
98 $oldUmask = umask( 0 );
99 wfMkdirParents( $wgLocalMessageCache, 0777 );
102 $file = fopen( $filename, 'w' );
104 wfDebug( "Unable to open local cache file for writing\n" );
108 fwrite( $file, $hash . $serialized );
110 @chmod
( $filename, 0666 );
113 function loadFromScript( $hash ) {
114 global $wgLocalMessageCache, $wgDBname;
115 if ( $wgLocalMessageCache === false ) {
119 $filename = "$wgLocalMessageCache/messages-$wgDBname";
121 wfSuppressWarnings();
122 $file = fopen( $filename, 'r' );
127 $localHash=substr(fread($file,40),8);
129 if ($hash!=$localHash) {
132 require("$wgLocalMessageCache/messages-$wgDBname");
135 function saveToScript($array, $hash) {
136 global $wgLocalMessageCache, $wgDBname;
137 if ( $wgLocalMessageCache === false ) {
141 $filename = "$wgLocalMessageCache/messages-$wgDBname";
142 $oldUmask = umask( 0 );
143 wfMkdirParents( $wgLocalMessageCache, 0777 );
145 $file = fopen( $filename.'.tmp', 'w');
146 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
148 foreach ($array as $key => $message) {
149 fwrite($file, "'". $this->escapeForScript($key).
150 "' => '" . $this->escapeForScript($message).
153 fwrite($file,");\n?>");
155 rename($filename.'.tmp',$filename);
158 function escapeForScript($string) {
159 $string = str_replace( '\\', '\\\\', $string );
160 $string = str_replace( '\'', '\\\'', $string );
165 * Loads messages either from memcached or the database, if not disabled
166 * On error, quietly switches to a fallback mode
167 * Returns false for a reportable error, true otherwise
170 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
172 if ( $this->mDisable
) {
173 static $shownDisabled = false;
174 if ( !$shownDisabled ) {
175 wfDebug( "MessageCache::load(): disabled\n" );
176 $shownDisabled = true;
180 $fname = 'MessageCache::load';
181 wfProfileIn( $fname );
184 if ( $this->mUseCache
) {
185 $this->mCache
= false;
188 wfProfileIn( $fname.'-fromlocal' );
189 $hash = $this->mMemc
->get( "{$this->mMemcKey}-hash" );
191 if ($wgLocalMessageCacheSerialized) {
192 $this->loadFromLocal( $hash );
194 $this->loadFromScript( $hash );
196 if ( $this->mCache
) {
197 wfDebug( "MessageCache::load(): got from local cache\n" );
200 wfProfileOut( $fname.'-fromlocal' );
203 if ( !$this->mCache
) {
204 wfProfileIn( $fname.'-fromcache' );
205 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
206 if ( $this->mCache
) {
207 wfDebug( "MessageCache::load(): got from global cache\n" );
208 # Save to local cache
209 if ( $wgLocalMessageCache !== false ) {
210 $serialized = serialize( $this->mCache
);
212 $hash = md5( $serialized );
213 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
215 if ($wgLocalMessageCacheSerialized) {
216 $this->saveToLocal( $serialized,$hash );
218 $this->saveToScript( $this->mCache
, $hash );
222 wfProfileOut( $fname.'-fromcache' );
226 # If there's nothing in memcached, load all the messages from the database
227 if ( !$this->mCache
) {
228 wfDebug( "MessageCache::load(): loading all messages\n" );
230 # Other threads don't need to load the messages if another thread is doing it.
231 $success = $this->mMemc
->add( $this->mMemcKey
.'-status', "loading", MSG_LOAD_TIMEOUT
);
233 wfProfileIn( $fname.'-load' );
235 wfProfileOut( $fname.'-load' );
238 # Keep trying if it fails, this is kind of important
239 wfProfileIn( $fname.'-save' );
241 !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
243 usleep(mt_rand(500000,1500000));
246 # Save to local cache
247 if ( $wgLocalMessageCache !== false ) {
248 $serialized = serialize( $this->mCache
);
249 $hash = md5( $serialized );
250 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
251 if ($wgLocalMessageCacheSerialized) {
252 $this->saveToLocal( $serialized,$hash );
254 $this->saveToScript( $this->mCache
, $hash );
258 wfProfileOut( $fname.'-save' );
260 $this->mMemc
->set( $this->mMemcKey
.'-status', 'error', 60*5 );
261 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
267 if ( !is_array( $this->mCache
) ) {
268 wfDebug( "MessageCache::load(): individual message mode\n" );
269 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
270 # Causing too much DB load, disabling -- TS
271 $this->mDisable
= true;
273 if ( $this->mCache == "loading" ) {
274 $this->mUseCache = false;
275 } elseif ( $this->mCache == "error" ) {
276 $this->mUseCache = false;
279 $this->mDisable = true;
282 $this->mCache
= false;
285 wfProfileOut( $fname );
286 $this->mDeferred
= false;
291 * Loads all or main part of cacheable messages from the database
293 function loadFromDB() {
296 $fname = 'MessageCache::loadFromDB';
297 $dbr =& wfGetDB( DB_SLAVE
);
299 throw new MWException( 'Invalid database object' );
301 $conditions = array( 'page_is_redirect' => 0,
302 'page_namespace' => NS_MEDIAWIKI
);
303 $res = $dbr->select( array( 'page', 'revision', 'text' ),
304 array( 'page_title', 'old_text', 'old_flags' ),
305 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI
.' AND page_latest=rev_id AND rev_text_id=old_id',
309 $this->mCache
= array();
310 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
311 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
315 # Go through the language array and the extension array and make a note of
316 # any keys missing from the cache
317 $allMessages = Language
::getMessagesFor( 'en' );
318 foreach ( $allMessages as $key => $value ) {
319 $uckey = $wgLang->ucfirst( $key );
320 if ( !array_key_exists( $uckey, $this->mCache
) ) {
321 $this->mCache
[$uckey] = false;
325 # Make sure all extension messages are available
326 wfLoadAllExtensions();
328 # Add them to the cache
329 foreach ( $this->mExtensionMessages
as $key => $value ) {
330 $uckey = $wgLang->ucfirst( $key );
331 if ( !array_key_exists( $uckey, $this->mCache
) &&
332 ( isset( $this->mExtensionMessages
[$key][$wgLang->getCode()] ) ||
isset( $this->mExtensionMessages
[$key]['en'] ) ) ) {
333 $this->mCache
[$uckey] = false;
337 $dbr->freeResult( $res );
341 * Not really needed anymore
345 if ( !$this->mKeys
) {
346 $this->mKeys
= array();
347 $allMessages = Language
::getMessagesFor( 'en' );
348 foreach ( $allMessages as $key => $value ) {
349 $title = $wgContLang->ucfirst( $key );
350 array_push( $this->mKeys
, $title );
359 function isCacheable( $key ) {
363 function replace( $title, $text ) {
364 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc, $wgDBname;
368 $parserMemc->delete("$wgDBname:sidebar");
369 if ( is_array( $this->mCache
) ) {
370 $this->mCache
[$title] = $text;
371 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
373 # Save to local cache
374 if ( $wgLocalMessageCache !== false ) {
375 $serialized = serialize( $this->mCache
);
376 $hash = md5( $serialized );
377 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
378 if ($wgLocalMessageCacheSerialized) {
379 $this->saveToLocal( $serialized,$hash );
381 $this->saveToScript( $this->mCache
, $hash );
392 * Represents a write lock on the messages key
395 if ( !$this->mUseCache
) {
399 $lockKey = $this->mMemcKey
. 'lock';
400 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
404 return $i >= MSG_WAIT_TIMEOUT
;
408 if ( !$this->mUseCache
) {
412 $lockKey = $this->mMemcKey
. 'lock';
413 $this->mMemc
->delete( $lockKey );
416 function get( $key, $useDB = true, $forcontent = true, $isfullkey = false ) {
417 global $wgContLanguageCode, $wgContLang, $wgLang;
419 $lang =& $wgContLang;
423 $langcode = $lang->getCode();
424 # If uninitialised, someone is trying to call this halfway through Setup.php
425 if( !$this->mInitialised
) {
426 return '<' . htmlspecialchars($key) . '>';
428 # If cache initialization was deferred, start it now.
429 if( $this->mDeferred
&& !$this->mDisable
&& $useDB ) {
434 if( !$this->mDisable
&& $useDB ) {
435 $title = $wgContLang->ucfirst( $key );
436 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
437 $title .= '/' . $langcode;
439 $message = $this->getFromCache( $title );
441 # Try the extension array
442 if( $message === false && array_key_exists( $key, $this->mExtensionMessages
) ) {
443 if ( isset( $this->mExtensionMessages
[$key][$langcode] ) ) {
444 $message = $this->mExtensionMessages
[$key][$langcode];
445 } elseif ( isset( $this->mExtensionMessages
[$key]['en'] ) ) {
446 $message = $this->mExtensionMessages
[$key]['en'];
450 # Try the array in the language object
451 if( $message === false ) {
452 #wfDebug( "Trying language object for message $key\n" );
453 wfSuppressWarnings();
454 $message = $lang->getMessage( $key );
456 if ( is_null( $message ) ) {
461 # Try the English array
462 if( $message === false && $langcode != 'en' ) {
463 wfSuppressWarnings();
464 $message = Language
::getMessage( $key );
466 if ( is_null( $message ) ) {
471 # Try the array of another language
472 if( $message === false && strpos( $key, '/' ) ) {
473 $message = explode( '/', $key );
474 wfSuppressWarnings();
475 $message = Language
::getMessageFor( $message[0], $message[1] );
477 if ( is_null( $message ) ) {
482 # Is this a custom message? Try the default language in the db...
483 if( ($message === false ||
$message === '-' ) &&
484 !$this->mDisable
&& $useDB &&
485 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
486 $message = $this->getFromCache( $wgContLang->ucfirst( $key ) );
490 if( $message === false ) {
491 return '<' . htmlspecialchars($key) . '>';
495 $message = $this->transform( $message );
499 function getFromCache( $title ) {
503 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
504 return $this->mCache
[$title];
507 # Try individual message cache
508 if ( $this->mUseCache
) {
509 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
510 if ( $message == '###NONEXISTENT###' ) {
511 $this->mCache
[$title] = false;
513 } elseif( !is_null( $message ) ) {
514 $this->mCache
[$title] = $message;
521 # Call message Hooks, in case they are defined
522 wfRunHooks('MessagesPreLoad',array($title,&$message));
524 # If it wasn't in the cache, load each message from the DB individually
525 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
527 $message = $revision->getText();
528 if ($this->mUseCache
) {
529 $this->mCache
[$title]=$message;
530 /* individual messages may be often
531 recached until proper purge code exists
533 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
537 # Use some special text instead of false, because false gets converted to '' somewhere
538 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, '###NONEXISTENT###', $this->mExpiry
);
539 $this->mCache
[$title] = false;
545 function transform( $message ) {
547 if ( !$this->mParser
&& isset( $wgParser ) ) {
548 # Do some initialisation so that we don't have to do it twice
549 $wgParser->firstCallInit();
550 # Clone it and store it
551 $this->mParser
= clone $wgParser;
553 if ( !$this->mDisableTransform
&& $this->mParser
) {
554 if( strpos( $message, '{{' ) !== false ) {
555 $message = $this->mParser
->transformMsg( $message, $this->getParserOptions() );
561 function disable() { $this->mDisable
= true; }
562 function enable() { $this->mDisable
= false; }
563 function disableTransform() { $this->mDisableTransform
= true; }
564 function enableTransform() { $this->mDisableTransform
= false; }
565 function setTransform( $x ) { $this->mDisableTransform
= $x; }
566 function getTransform() { return $this->mDisableTransform
; }
569 * Add a message to the cache
572 * @param mixed $value
573 * @param string $lang The messages language, English by default
575 function addMessage( $key, $value, $lang = 'en' ) {
576 $this->mExtensionMessages
[$key][$lang] = $value;
580 * Add an associative array of message to the cache
582 * @param array $messages An associative array of key => values to be added
583 * @param string $lang The messages language, English by default
585 function addMessages( $messages, $lang = 'en' ) {
586 wfProfileIn( __METHOD__
);
587 foreach ( $messages as $key => $value ) {
588 $this->addMessage( $key, $value, $lang );
590 wfProfileOut( __METHOD__
);
594 * Clear all stored messages. Mainly used after a mass rebuild.
597 global $wgLocalMessageCache, $wgDBname;
598 if( $this->mUseCache
) {
600 $this->mMemc
->delete( $this->mMemcKey
);
601 # Invalidate all local caches
602 $this->mMemc
->delete( "{$this->mMemcKey}-hash" );