9 require_once( 'Revision.php' );
14 define( 'MSG_LOAD_TIMEOUT', 60);
15 define( 'MSG_LOCK_TIMEOUT', 10);
16 define( 'MSG_WAIT_TIMEOUT', 10);
20 * Performs various useful MediaWiki namespace-related functions
25 var $mCache, $mUseCache, $mDisable, $mExpiry;
26 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
27 var $mExtensionMessages = array();
28 var $mInitialised = false;
29 var $mDeferred = true;
31 function initialise( &$memCached, $useDB, $expiry, $memcPrefix) {
32 $fname = 'MessageCache::initialise';
33 wfProfileIn( $fname );
35 $this->mUseCache
= !is_null( $memCached );
36 $this->mMemc
= &$memCached;
37 $this->mDisable
= !$useDB;
38 $this->mExpiry
= $expiry;
39 $this->mDisableTransform
= false;
40 $this->mMemcKey
= $memcPrefix.':messages';
41 $this->mKeys
= false; # initialised on demand
42 $this->mInitialised
= true;
44 wfProfileIn( $fname.'-parseropt' );
45 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
46 wfProfileOut( $fname.'-parseropt' );
47 wfProfileIn( $fname.'-parser' );
48 $this->mParser
= new Parser
;
49 wfProfileOut( $fname.'-parser' );
51 # When we first get asked for a message,
52 # then we'll fill up the cache. If we
53 # can return a cache hit, this saves
54 # some extra milliseconds
55 $this->mDeferred
= true;
57 wfProfileOut( $fname );
61 * Try to load the cache from a local file
63 function loadFromLocal( $hash ) {
64 global $wgLocalMessageCache, $wgDBname;
66 $this->mCache
= false;
67 if ( $wgLocalMessageCache === false ) {
71 $filename = "$wgLocalMessageCache/messages-$wgDBname";
74 $file = fopen( $filename, 'r' );
80 // Check to see if the file has the hash specified
81 $localHash = fread( $file, 32 );
82 if ( $hash == $localHash ) {
83 // All good, get the rest of it
84 $serialized = fread( $file, 1000000 );
85 $this->mCache
= unserialize( $serialized );
91 * Save the cache to a local file
93 function saveToLocal( $serialized, $hash ) {
94 global $wgLocalMessageCache, $wgDBname;
96 if ( $wgLocalMessageCache === false ) {
100 $filename = "$wgLocalMessageCache/messages-$wgDBname";
101 $oldUmask = umask( 0 );
102 wfMkdirParents( $wgLocalMessageCache, 0777 );
105 $file = fopen( $filename, 'w' );
107 wfDebug( "Unable to open local cache file for writing\n" );
111 fwrite( $file, $hash . $serialized );
113 @chmod
( $filename, 0666 );
116 function loadFromScript( $hash ) {
117 global $wgLocalMessageCache, $wgDBname;
118 if ( $wgLocalMessageCache === false ) {
122 $filename = "$wgLocalMessageCache/messages-$wgDBname";
124 wfSuppressWarnings();
125 $file = fopen( $filename, 'r' );
130 $localHash=substr(fread($file,40),8);
132 if ($hash!=$localHash) {
135 require("$wgLocalMessageCache/messages-$wgDBname");
138 function saveToScript($array, $hash) {
139 global $wgLocalMessageCache, $wgDBname;
140 if ( $wgLocalMessageCache === false ) {
144 $filename = "$wgLocalMessageCache/messages-$wgDBname";
145 $oldUmask = umask( 0 );
146 wfMkdirParents( $wgLocalMessageCache, 0777 );
148 $file = fopen( $filename.'.tmp', 'w');
149 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
151 foreach ($array as $key => $message) {
152 fwrite($file, "'". $this->escapeForScript($key).
153 "' => '" . $this->escapeForScript($message).
156 fwrite($file,");\n?>");
158 rename($filename.'.tmp',$filename);
161 function escapeForScript($string) {
162 $string = str_replace( '\\', '\\\\', $string );
163 $string = str_replace( '\'', '\\\'', $string );
168 * Loads messages either from memcached or the database, if not disabled
169 * On error, quietly switches to a fallback mode
170 * Returns false for a reportable error, true otherwise
173 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
175 if ( $this->mDisable
) {
176 static $shownDisabled = false;
177 if ( !$shownDisabled ) {
178 wfDebug( "MessageCache::load(): disabled\n" );
179 $shownDisabled = true;
183 $fname = 'MessageCache::load';
184 wfProfileIn( $fname );
187 if ( $this->mUseCache
) {
188 $this->mCache
= false;
191 wfProfileIn( $fname.'-fromlocal' );
192 $hash = $this->mMemc
->get( "{$this->mMemcKey}-hash" );
194 if ($wgLocalMessageCacheSerialized) {
195 $this->loadFromLocal( $hash );
197 $this->loadFromScript( $hash );
200 wfProfileOut( $fname.'-fromlocal' );
203 if ( !$this->mCache
) {
204 wfProfileIn( $fname.'-fromcache' );
205 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
207 # Save to local cache
208 if ( $wgLocalMessageCache !== false ) {
209 $serialized = serialize( $this->mCache
);
211 $hash = md5( $serialized );
212 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
214 if ($wgLocalMessageCacheSerialized) {
215 $this->saveToLocal( $serialized,$hash );
217 $this->saveToScript( $this->mCache
, $hash );
220 wfProfileOut( $fname.'-fromcache' );
224 # If there's nothing in memcached, load all the messages from the database
225 if ( !$this->mCache
) {
226 wfDebug( "MessageCache::load(): loading all messages\n" );
228 # Other threads don't need to load the messages if another thread is doing it.
229 $success = $this->mMemc
->add( $this->mMemcKey
.'-status', "loading", MSG_LOAD_TIMEOUT
);
231 wfProfileIn( $fname.'-load' );
233 wfProfileOut( $fname.'-load' );
236 # Keep trying if it fails, this is kind of important
237 wfProfileIn( $fname.'-save' );
239 !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
241 usleep(mt_rand(500000,1500000));
244 # Save to local cache
245 if ( $wgLocalMessageCache !== false ) {
246 $serialized = serialize( $this->mCache
);
247 $hash = md5( $serialized );
248 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
249 if ($wgLocalMessageCacheSerialized) {
250 $this->saveToLocal( $serialized,$hash );
252 $this->saveToScript( $this->mCache
, $hash );
256 wfProfileOut( $fname.'-save' );
258 $this->mMemc
->set( $this->mMemcKey
.'-status', 'error', 60*5 );
259 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
265 if ( !is_array( $this->mCache
) ) {
266 wfDebug( "MessageCache::load(): individual message mode\n" );
267 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
268 # Causing too much DB load, disabling -- TS
269 $this->mDisable
= true;
271 if ( $this->mCache == "loading" ) {
272 $this->mUseCache = false;
273 } elseif ( $this->mCache == "error" ) {
274 $this->mUseCache = false;
277 $this->mDisable = true;
280 $this->mCache
= false;
283 wfProfileOut( $fname );
284 $this->mDeferred
= false;
289 * Loads all or main part of cacheable messages from the database
291 function loadFromDB() {
292 global $wgAllMessagesEn, $wgLang;
294 $fname = 'MessageCache::loadFromDB';
295 $dbr =& wfGetDB( DB_SLAVE
);
297 wfDebugDieBacktrace( 'Invalid database object' );
299 $conditions = array( 'page_is_redirect' => 0,
300 'page_namespace' => NS_MEDIAWIKI
);
301 $res = $dbr->select( array( 'page', 'revision', 'text' ),
302 array( 'page_title', 'old_text', 'old_flags' ),
303 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI
.' AND page_latest=rev_id AND rev_text_id=old_id',
307 $this->mCache
= array();
308 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
309 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
313 # Go through the language array and the extension array and make a note of
314 # any keys missing from the cache
315 foreach ( $wgAllMessagesEn as $key => $value ) {
316 $uckey = $wgLang->ucfirst( $key );
317 if ( !array_key_exists( $uckey, $this->mCache
) ) {
318 $this->mCache
[$uckey] = false;
321 foreach ( $this->mExtensionMessages
as $key => $value ) {
322 $uckey = $wgLang->ucfirst( $key );
323 if ( !array_key_exists( $uckey, $this->mCache
) ) {
324 $this->mCache
[$uckey] = false;
328 $dbr->freeResult( $res );
332 * Not really needed anymore
335 global $wgAllMessagesEn, $wgContLang;
336 if ( !$this->mKeys
) {
337 $this->mKeys
= array();
338 foreach ( $wgAllMessagesEn as $key => $value ) {
339 $title = $wgContLang->ucfirst( $key );
340 array_push( $this->mKeys
, $title );
349 function isCacheable( $key ) {
353 function replace( $title, $text ) {
354 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc, $wgDBname;
358 $parserMemc->delete("$wgDBname:sidebar");
359 if ( is_array( $this->mCache
) ) {
360 $this->mCache
[$title] = $text;
361 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
363 # Save to local cache
364 if ( $wgLocalMessageCache !== false ) {
365 $serialized = serialize( $this->mCache
);
366 $hash = md5( $serialized );
367 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
368 if ($wgLocalMessageCacheSerialized) {
369 $this->saveToLocal( $serialized,$hash );
371 $this->saveToScript( $this->mCache
, $hash );
382 * Represents a write lock on the messages key
385 if ( !$this->mUseCache
) {
389 $lockKey = $this->mMemcKey
. 'lock';
390 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
394 return $i >= MSG_WAIT_TIMEOUT
;
398 if ( !$this->mUseCache
) {
402 $lockKey = $this->mMemcKey
. 'lock';
403 $this->mMemc
->delete( $lockKey );
406 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
407 global $wgContLanguageCode;
410 $lang =& $wgContLang;
411 $langcode = $wgContLanguageCode;
413 global $wgLang, $wgLanguageCode;
415 $langcode = $wgLanguageCode;
417 # If uninitialised, someone is trying to call this halfway through Setup.php
418 if( !$this->mInitialised
) {
419 return '<' . htmlspecialchars($key) . '>';
421 # If cache initialization was deferred, start it now.
422 if( $this->mDeferred
&& !$this->mDisable
&& $useDB ) {
427 if( !$this->mDisable
&& $useDB ) {
428 $title = $lang->ucfirst( $key );
429 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
430 $title .= '/' . $langcode;
432 $message = $this->getFromCache( $title );
434 # Try the extension array
435 if( $message === false && array_key_exists( $key, $this->mExtensionMessages
) ) {
436 $message = $this->mExtensionMessages
[$key];
439 # Try the array in the language object
440 if( $message === false ) {
441 wfSuppressWarnings();
442 $message = $lang->getMessage( $key );
444 if ( is_null( $message ) ) {
449 # Try the English array
450 if( $message === false && $langcode != 'en' ) {
451 wfSuppressWarnings();
452 $message = Language
::getMessage( $key );
454 if ( is_null( $message ) ) {
459 # Is this a custom message? Try the default language in the db...
460 if( ($message === false ||
$message === '-' ) &&
461 !$this->mDisable
&& $useDB &&
462 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
463 $message = $this->getFromCache( $lang->ucfirst( $key ) );
467 if( $message === false ) {
468 return '<' . htmlspecialchars($key) . '>';
472 $message = $this->transform( $message );
476 function getFromCache( $title ) {
480 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
481 return $this->mCache
[$title];
484 # Try individual message cache
485 if ( $this->mUseCache
) {
486 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
487 if ( $message == '###NONEXISTENT###' ) {
489 } elseif( !is_null( $message ) ) {
490 $this->mCache
[$title] = $message;
497 # Call message Hooks, in case they are defined
498 wfRunHooks('MessagesPreLoad',array($title,&$message));
500 # If it wasn't in the cache, load each message from the DB individually
501 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
503 $message = $revision->getText();
504 if ($this->mUseCache
) {
505 $this->mCache
[$title]=$message;
506 /* individual messages may be often
507 recached until proper purge code exists
509 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
513 # Use some special text instead of false, because false gets converted to '' somewhere
514 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, '###NONEXISTENT###', $this->mExpiry
);
520 function transform( $message ) {
521 if( !$this->mDisableTransform
) {
522 if( strpos( $message, '{{' ) !== false ) {
523 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
529 function disable() { $this->mDisable
= true; }
530 function enable() { $this->mDisable
= false; }
531 function disableTransform() { $this->mDisableTransform
= true; }
532 function enableTransform() { $this->mDisableTransform
= false; }
533 function setTransform( $x ) { $this->mDisableTransform
= $x; }
534 function getTransform() { return $this->mDisableTransform
; }
537 * Add a message to the cache
540 * @param mixed $value
542 function addMessage( $key, $value ) {
543 $this->mExtensionMessages
[$key] = $value;
547 * Add an associative array of message to the cache
549 * @param array $messages An associative array of key => values to be added
551 function addMessages( $messages ) {
552 foreach ( $messages as $key => $value ) {
553 $this->addMessage( $key, $value );
558 * Clear all stored messages. Mainly used after a mass rebuild.
561 if( $this->mUseCache
) {
562 $this->mMemc
->delete( $this->mMemcKey
);