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(");
152 foreach ($array as $key => $message) {
153 fwrite($file, "'". preg_replace($re, "\'", $key).
154 "' => '" . preg_replace( $re, "\'", $message) . "',\n");
156 fwrite($file,");\n?>");
158 rename($filename.'.tmp',$filename);
162 * Loads messages either from memcached or the database, if not disabled
163 * On error, quietly switches to a fallback mode
164 * Returns false for a reportable error, true otherwise
167 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
169 if ( $this->mDisable
) {
170 static $shownDisabled = false;
171 if ( !$shownDisabled ) {
172 wfDebug( "MessageCache::load(): disabled\n" );
173 $shownDisabled = true;
177 $fname = 'MessageCache::load';
178 wfProfileIn( $fname );
181 if ( $this->mUseCache
) {
182 $this->mCache
= false;
185 wfProfileIn( $fname.'-fromlocal' );
186 $hash = $this->mMemc
->get( "{$this->mMemcKey}-hash" );
188 if ($wgLocalMessageCacheSerialized) {
189 $this->loadFromLocal( $hash );
191 $this->loadFromScript( $hash );
194 wfProfileOut( $fname.'-fromlocal' );
197 if ( !$this->mCache
) {
198 wfProfileIn( $fname.'-fromcache' );
199 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
201 # Save to local cache
202 if ( $wgLocalMessageCache !== false ) {
203 $serialized = serialize( $this->mCache
);
205 $hash = md5( $serialized );
206 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
208 if ($wgLocalMessageCacheSerialized) {
209 $this->saveToLocal( $serialized,$hash );
211 $this->saveToScript( $this->mCache
, $hash );
214 wfProfileOut( $fname.'-fromcache' );
218 # If there's nothing in memcached, load all the messages from the database
219 if ( !$this->mCache
) {
220 wfDebug( "MessageCache::load(): loading all messages\n" );
222 # Other threads don't need to load the messages if another thread is doing it.
223 $success = $this->mMemc
->add( $this->mMemcKey
.'-status', "loading", MSG_LOAD_TIMEOUT
);
225 wfProfileIn( $fname.'-load' );
227 wfProfileOut( $fname.'-load' );
230 # Keep trying if it fails, this is kind of important
231 wfProfileIn( $fname.'-save' );
233 !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
235 usleep(mt_rand(500000,1500000));
238 # Save to local cache
239 if ( $wgLocalMessageCache !== false ) {
240 $serialized = serialize( $this->mCache
);
241 $hash = md5( $serialized );
242 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
243 if ($wgLocalMessageCacheSerialized) {
244 $this->saveToLocal( $serialized,$hash );
246 $this->saveToScript( $this->mCache
, $hash );
250 wfProfileOut( $fname.'-save' );
252 $this->mMemc
->set( $this->mMemcKey
.'-status', 'error', 60*5 );
253 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
259 if ( !is_array( $this->mCache
) ) {
260 wfDebug( "MessageCache::load(): individual message mode\n" );
261 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
262 # Causing too much DB load, disabling -- TS
263 $this->mDisable
= true;
265 if ( $this->mCache == "loading" ) {
266 $this->mUseCache = false;
267 } elseif ( $this->mCache == "error" ) {
268 $this->mUseCache = false;
271 $this->mDisable = true;
274 $this->mCache
= false;
277 wfProfileOut( $fname );
278 $this->mDeferred
= false;
283 * Loads all or main part of cacheable messages from the database
285 function loadFromDB() {
286 global $wgAllMessagesEn, $wgLang;
288 $fname = 'MessageCache::loadFromDB';
289 $dbr =& wfGetDB( DB_SLAVE
);
291 wfDebugDieBacktrace( 'Invalid database object' );
293 $conditions = array( 'page_is_redirect' => 0,
294 'page_namespace' => NS_MEDIAWIKI
);
295 $res = $dbr->select( array( 'page', 'revision', 'text' ),
296 array( 'page_title', 'old_text', 'old_flags' ),
297 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI
.' AND page_latest=rev_id AND rev_text_id=old_id',
301 $this->mCache
= array();
302 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
303 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
307 # Go through the language array and the extension array and make a note of
308 # any keys missing from the cache
309 foreach ( $wgAllMessagesEn as $key => $value ) {
310 $uckey = $wgLang->ucfirst( $key );
311 if ( !array_key_exists( $uckey, $this->mCache
) ) {
312 $this->mCache
[$uckey] = false;
315 foreach ( $this->mExtensionMessages
as $key => $value ) {
316 $uckey = $wgLang->ucfirst( $key );
317 if ( !array_key_exists( $uckey, $this->mCache
) ) {
318 $this->mCache
[$uckey] = false;
322 $dbr->freeResult( $res );
326 * Not really needed anymore
329 global $wgAllMessagesEn, $wgContLang;
330 if ( !$this->mKeys
) {
331 $this->mKeys
= array();
332 foreach ( $wgAllMessagesEn as $key => $value ) {
333 $title = $wgContLang->ucfirst( $key );
334 array_push( $this->mKeys
, $title );
343 function isCacheable( $key ) {
347 function replace( $title, $text ) {
348 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc, $wgDBname;
352 $parserMemc->delete("$wgDBname:sidebar");
353 if ( is_array( $this->mCache
) ) {
354 $this->mCache
[$title] = $text;
355 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
357 # Save to local cache
358 if ( $wgLocalMessageCache !== false ) {
359 $serialized = serialize( $this->mCache
);
360 $hash = md5( $serialized );
361 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
362 if ($wgLocalMessageCacheSerialized) {
363 $this->saveToLocal( $serialized,$hash );
365 $this->saveToScript( $this->mCache
, $hash );
376 * Represents a write lock on the messages key
379 if ( !$this->mUseCache
) {
383 $lockKey = $this->mMemcKey
. 'lock';
384 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
388 return $i >= MSG_WAIT_TIMEOUT
;
392 if ( !$this->mUseCache
) {
396 $lockKey = $this->mMemcKey
. 'lock';
397 $this->mMemc
->delete( $lockKey );
400 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
401 global $wgContLanguageCode;
404 $lang =& $wgContLang;
405 $langcode = $wgContLanguageCode;
407 global $wgLang, $wgLanguageCode;
409 $langcode = $wgLanguageCode;
411 # If uninitialised, someone is trying to call this halfway through Setup.php
412 if( !$this->mInitialised
) {
413 return '<' . htmlspecialchars($key) . '>';
415 # If cache initialization was deferred, start it now.
416 if( $this->mDeferred
&& !$this->mDisable
&& $useDB ) {
421 if( !$this->mDisable
&& $useDB ) {
422 $title = $lang->ucfirst( $key );
423 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
424 $title .= '/' . $langcode;
426 $message = $this->getFromCache( $title );
428 # Try the extension array
429 if( $message === false && array_key_exists( $key, $this->mExtensionMessages
) ) {
430 $message = $this->mExtensionMessages
[$key];
433 # Try the array in the language object
434 if( $message === false ) {
435 wfSuppressWarnings();
436 $message = $lang->getMessage( $key );
438 if ( is_null( $message ) ) {
443 # Try the English array
444 if( $message === false && $langcode != 'en' ) {
445 wfSuppressWarnings();
446 $message = Language
::getMessage( $key );
448 if ( is_null( $message ) ) {
453 # Is this a custom message? Try the default language in the db...
454 if( $message === false &&
455 !$this->mDisable
&& $useDB &&
456 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
457 $message = $this->getFromCache( $lang->ucfirst( $key ) );
461 if( $message === false ) {
462 return '<' . htmlspecialchars($key) . '>';
466 $message = $this->transform( $message );
470 function getFromCache( $title ) {
474 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
475 return $this->mCache
[$title];
478 # Try individual message cache
479 if ( $this->mUseCache
) {
480 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
481 if ( $message == '###NONEXISTENT###' ) {
483 } elseif( !is_null( $message ) ) {
484 $this->mCache
[$title] = $message;
491 # Call message Hooks, in case they are defined
492 wfRunHooks('MessagesPreLoad',array($title,&$message));
494 # If it wasn't in the cache, load each message from the DB individually
495 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
497 $message = $revision->getText();
498 if ($this->mUseCache
) {
499 $this->mCache
[$title]=$message;
500 /* individual messages may be often
501 recached until proper purge code exists
503 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
507 # Use some special text instead of false, because false gets converted to '' somewhere
508 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, '###NONEXISTENT###', $this->mExpiry
);
514 function transform( $message ) {
515 if( !$this->mDisableTransform
) {
516 if( strpos( $message, '{{' ) !== false ) {
517 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
523 function disable() { $this->mDisable
= true; }
524 function enable() { $this->mDisable
= false; }
525 function disableTransform() { $this->mDisableTransform
= true; }
526 function enableTransform() { $this->mDisableTransform
= false; }
527 function setTransform( $x ) { $this->mDisableTransform
= $x; }
528 function getTransform() { return $this->mDisableTransform
; }
531 * Add a message to the cache
534 * @param mixed $value
536 function addMessage( $key, $value ) {
537 $this->mExtensionMessages
[$key] = $value;
541 * Add an associative array of message to the cache
543 * @param array $messages An associative array of key => values to be added
545 function addMessages( $messages ) {
546 foreach ( $messages as $key => $value ) {
547 $this->addMessage( $key, $value );
552 * Clear all stored messages. Mainly used after a mass rebuild.
555 if( $this->mUseCache
) {
556 $this->mMemc
->delete( $this->mMemcKey
);