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 initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
29 $fname = 'MessageCache::initialise';
30 wfProfileIn( $fname );
32 $this->mUseCache
= !is_null( $memCached );
33 $this->mMemc
= &$memCached;
34 $this->mDisable
= !$useDB;
35 $this->mExpiry
= $expiry;
36 $this->mDisableTransform
= false;
37 $this->mMemcKey
= $memcPrefix.':messages';
38 $this->mKeys
= false; # initialised on demand
39 $this->mInitialised
= true;
41 wfProfileIn( $fname.'-parseropt' );
42 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
43 wfProfileOut( $fname.'-parseropt' );
44 wfProfileIn( $fname.'-parser' );
45 $this->mParser
= new Parser
;
46 wfProfileOut( $fname.'-parser' );
48 # When we first get asked for a message,
49 # then we'll fill up the cache. If we
50 # can return a cache hit, this saves
51 # some extra milliseconds
52 $this->mDeferred
= true;
54 wfProfileOut( $fname );
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 );
197 wfProfileOut( $fname.'-fromlocal' );
200 if ( !$this->mCache
) {
201 wfProfileIn( $fname.'-fromcache' );
202 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
204 # Save to local cache
205 if ( $wgLocalMessageCache !== false ) {
206 $serialized = serialize( $this->mCache
);
208 $hash = md5( $serialized );
209 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
211 if ($wgLocalMessageCacheSerialized) {
212 $this->saveToLocal( $serialized,$hash );
214 $this->saveToScript( $this->mCache
, $hash );
217 wfProfileOut( $fname.'-fromcache' );
221 # If there's nothing in memcached, load all the messages from the database
222 if ( !$this->mCache
) {
223 wfDebug( "MessageCache::load(): loading all messages\n" );
225 # Other threads don't need to load the messages if another thread is doing it.
226 $success = $this->mMemc
->add( $this->mMemcKey
.'-status', "loading", MSG_LOAD_TIMEOUT
);
228 wfProfileIn( $fname.'-load' );
230 wfProfileOut( $fname.'-load' );
233 # Keep trying if it fails, this is kind of important
234 wfProfileIn( $fname.'-save' );
236 !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
238 usleep(mt_rand(500000,1500000));
241 # Save to local cache
242 if ( $wgLocalMessageCache !== false ) {
243 $serialized = serialize( $this->mCache
);
244 $hash = md5( $serialized );
245 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
246 if ($wgLocalMessageCacheSerialized) {
247 $this->saveToLocal( $serialized,$hash );
249 $this->saveToScript( $this->mCache
, $hash );
253 wfProfileOut( $fname.'-save' );
255 $this->mMemc
->set( $this->mMemcKey
.'-status', 'error', 60*5 );
256 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
262 if ( !is_array( $this->mCache
) ) {
263 wfDebug( "MessageCache::load(): individual message mode\n" );
264 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
265 # Causing too much DB load, disabling -- TS
266 $this->mDisable
= true;
268 if ( $this->mCache == "loading" ) {
269 $this->mUseCache = false;
270 } elseif ( $this->mCache == "error" ) {
271 $this->mUseCache = false;
274 $this->mDisable = true;
277 $this->mCache
= false;
280 wfProfileOut( $fname );
281 $this->mDeferred
= false;
286 * Loads all or main part of cacheable messages from the database
288 function loadFromDB() {
289 global $wgAllMessagesEn, $wgLang;
291 $fname = 'MessageCache::loadFromDB';
292 $dbr =& wfGetDB( DB_SLAVE
);
294 throw new MWException( 'Invalid database object' );
296 $conditions = array( 'page_is_redirect' => 0,
297 'page_namespace' => NS_MEDIAWIKI
);
298 $res = $dbr->select( array( 'page', 'revision', 'text' ),
299 array( 'page_title', 'old_text', 'old_flags' ),
300 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI
.' AND page_latest=rev_id AND rev_text_id=old_id',
304 $this->mCache
= array();
305 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
306 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
310 # Go through the language array and the extension array and make a note of
311 # any keys missing from the cache
312 foreach ( $wgAllMessagesEn as $key => $value ) {
313 $uckey = $wgLang->ucfirst( $key );
314 if ( !array_key_exists( $uckey, $this->mCache
) ) {
315 $this->mCache
[$uckey] = false;
319 # Make sure all extension messages are available
320 wfLoadAllExtensions();
322 # Add them to the cache
323 foreach ( $this->mExtensionMessages
as $key => $value ) {
324 $uckey = $wgLang->ucfirst( $key );
325 if ( !array_key_exists( $uckey, $this->mCache
) ) {
326 $this->mCache
[$uckey] = false;
330 $dbr->freeResult( $res );
334 * Not really needed anymore
337 global $wgAllMessagesEn, $wgContLang;
338 if ( !$this->mKeys
) {
339 $this->mKeys
= array();
340 foreach ( $wgAllMessagesEn as $key => $value ) {
341 $title = $wgContLang->ucfirst( $key );
342 array_push( $this->mKeys
, $title );
351 function isCacheable( $key ) {
355 function replace( $title, $text ) {
356 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc, $wgDBname;
360 $parserMemc->delete("$wgDBname:sidebar");
361 if ( is_array( $this->mCache
) ) {
362 $this->mCache
[$title] = $text;
363 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
365 # Save to local cache
366 if ( $wgLocalMessageCache !== false ) {
367 $serialized = serialize( $this->mCache
);
368 $hash = md5( $serialized );
369 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
370 if ($wgLocalMessageCacheSerialized) {
371 $this->saveToLocal( $serialized,$hash );
373 $this->saveToScript( $this->mCache
, $hash );
384 * Represents a write lock on the messages key
387 if ( !$this->mUseCache
) {
391 $lockKey = $this->mMemcKey
. 'lock';
392 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
396 return $i >= MSG_WAIT_TIMEOUT
;
400 if ( !$this->mUseCache
) {
404 $lockKey = $this->mMemcKey
. 'lock';
405 $this->mMemc
->delete( $lockKey );
408 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
409 global $wgContLanguageCode;
412 $lang =& $wgContLang;
413 $langcode = $wgContLanguageCode;
415 global $wgLang, $wgLanguageCode;
417 $langcode = $wgLanguageCode;
419 # If uninitialised, someone is trying to call this halfway through Setup.php
420 if( !$this->mInitialised
) {
421 return '<' . htmlspecialchars($key) . '>';
423 # If cache initialization was deferred, start it now.
424 if( $this->mDeferred
&& !$this->mDisable
&& $useDB ) {
429 if( !$this->mDisable
&& $useDB ) {
430 $title = $lang->ucfirst( $key );
431 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
432 $title .= '/' . $langcode;
434 $message = $this->getFromCache( $title );
436 # Try the extension array
437 if( $message === false && array_key_exists( $key, $this->mExtensionMessages
) ) {
438 $message = $this->mExtensionMessages
[$key];
441 # Try the array in the language object
442 if( $message === false ) {
443 wfSuppressWarnings();
444 $message = $lang->getMessage( $key );
446 if ( is_null( $message ) ) {
451 # Try the English array
452 if( $message === false && $langcode != 'en' ) {
453 wfSuppressWarnings();
454 $message = Language
::getMessage( $key );
456 if ( is_null( $message ) ) {
461 # Is this a custom message? Try the default language in the db...
462 if( ($message === false ||
$message === '-' ) &&
463 !$this->mDisable
&& $useDB &&
464 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
465 $message = $this->getFromCache( $lang->ucfirst( $key ) );
469 if( $message === false ) {
470 return '<' . htmlspecialchars($key) . '>';
474 $message = $this->transform( $message );
478 function getFromCache( $title ) {
482 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
483 return $this->mCache
[$title];
486 # Try individual message cache
487 if ( $this->mUseCache
) {
488 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
489 if ( $message == '###NONEXISTENT###' ) {
491 } elseif( !is_null( $message ) ) {
492 $this->mCache
[$title] = $message;
499 # Call message Hooks, in case they are defined
500 wfRunHooks('MessagesPreLoad',array($title,&$message));
502 # If it wasn't in the cache, load each message from the DB individually
503 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
505 $message = $revision->getText();
506 if ($this->mUseCache
) {
507 $this->mCache
[$title]=$message;
508 /* individual messages may be often
509 recached until proper purge code exists
511 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
515 # Use some special text instead of false, because false gets converted to '' somewhere
516 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, '###NONEXISTENT###', $this->mExpiry
);
522 function transform( $message ) {
523 if( !$this->mDisableTransform
) {
524 if( strpos( $message, '{{' ) !== false ) {
525 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
531 function disable() { $this->mDisable
= true; }
532 function enable() { $this->mDisable
= false; }
533 function disableTransform() { $this->mDisableTransform
= true; }
534 function enableTransform() { $this->mDisableTransform
= false; }
535 function setTransform( $x ) { $this->mDisableTransform
= $x; }
536 function getTransform() { return $this->mDisableTransform
; }
539 * Add a message to the cache
542 * @param mixed $value
544 function addMessage( $key, $value ) {
545 $this->mExtensionMessages
[$key] = $value;
549 * Add an associative array of message to the cache
551 * @param array $messages An associative array of key => values to be added
553 function addMessages( $messages ) {
554 foreach ( $messages as $key => $value ) {
555 $this->addMessage( $key, $value );
560 * Clear all stored messages. Mainly used after a mass rebuild.
563 if( $this->mUseCache
) {
564 $this->mMemc
->delete( $this->mMemcKey
);