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;
40 wfProfileIn( __METHOD__
.'-parseropt' );
41 $this->mParserOptions
= new ParserOptions( $u=NULL );
42 wfProfileOut( __METHOD__
.'-parseropt' );
44 # When we first get asked for a message,
45 # then we'll fill up the cache. If we
46 # can return a cache hit, this saves
47 # some extra milliseconds
48 $this->mDeferred
= true;
50 wfProfileOut( __METHOD__
);
54 * Try to load the cache from a local file
56 function loadFromLocal( $hash ) {
57 global $wgLocalMessageCache, $wgDBname;
59 $this->mCache
= false;
60 if ( $wgLocalMessageCache === false ) {
64 $filename = "$wgLocalMessageCache/messages-$wgDBname";
67 $file = fopen( $filename, 'r' );
73 // Check to see if the file has the hash specified
74 $localHash = fread( $file, 32 );
75 if ( $hash == $localHash ) {
76 // All good, get the rest of it
77 $serialized = fread( $file, 1000000 );
78 $this->mCache
= unserialize( $serialized );
84 * Save the cache to a local file
86 function saveToLocal( $serialized, $hash ) {
87 global $wgLocalMessageCache, $wgDBname;
89 if ( $wgLocalMessageCache === false ) {
93 $filename = "$wgLocalMessageCache/messages-$wgDBname";
94 $oldUmask = umask( 0 );
95 wfMkdirParents( $wgLocalMessageCache, 0777 );
98 $file = fopen( $filename, 'w' );
100 wfDebug( "Unable to open local cache file for writing\n" );
104 fwrite( $file, $hash . $serialized );
106 @chmod
( $filename, 0666 );
109 function loadFromScript( $hash ) {
110 global $wgLocalMessageCache, $wgDBname;
111 if ( $wgLocalMessageCache === false ) {
115 $filename = "$wgLocalMessageCache/messages-$wgDBname";
117 wfSuppressWarnings();
118 $file = fopen( $filename, 'r' );
123 $localHash=substr(fread($file,40),8);
125 if ($hash!=$localHash) {
128 require("$wgLocalMessageCache/messages-$wgDBname");
131 function saveToScript($array, $hash) {
132 global $wgLocalMessageCache, $wgDBname;
133 if ( $wgLocalMessageCache === false ) {
137 $filename = "$wgLocalMessageCache/messages-$wgDBname";
138 $oldUmask = umask( 0 );
139 wfMkdirParents( $wgLocalMessageCache, 0777 );
141 $file = fopen( $filename.'.tmp', 'w');
142 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
144 foreach ($array as $key => $message) {
145 fwrite($file, "'". $this->escapeForScript($key).
146 "' => '" . $this->escapeForScript($message).
149 fwrite($file,");\n?>");
151 rename($filename.'.tmp',$filename);
154 function escapeForScript($string) {
155 $string = str_replace( '\\', '\\\\', $string );
156 $string = str_replace( '\'', '\\\'', $string );
161 * Loads messages either from memcached or the database, if not disabled
162 * On error, quietly switches to a fallback mode
163 * Returns false for a reportable error, true otherwise
166 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
168 if ( $this->mDisable
) {
169 static $shownDisabled = false;
170 if ( !$shownDisabled ) {
171 wfDebug( "MessageCache::load(): disabled\n" );
172 $shownDisabled = true;
176 $fname = 'MessageCache::load';
177 wfProfileIn( $fname );
180 if ( $this->mUseCache
) {
181 $this->mCache
= false;
184 wfProfileIn( $fname.'-fromlocal' );
185 $hash = $this->mMemc
->get( "{$this->mMemcKey}-hash" );
187 if ($wgLocalMessageCacheSerialized) {
188 $this->loadFromLocal( $hash );
190 $this->loadFromScript( $hash );
193 wfProfileOut( $fname.'-fromlocal' );
196 if ( !$this->mCache
) {
197 wfProfileIn( $fname.'-fromcache' );
198 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
200 # Save to local cache
201 if ( $wgLocalMessageCache !== false ) {
202 $serialized = serialize( $this->mCache
);
204 $hash = md5( $serialized );
205 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
207 if ($wgLocalMessageCacheSerialized) {
208 $this->saveToLocal( $serialized,$hash );
210 $this->saveToScript( $this->mCache
, $hash );
213 wfProfileOut( $fname.'-fromcache' );
217 # If there's nothing in memcached, load all the messages from the database
218 if ( !$this->mCache
) {
219 wfDebug( "MessageCache::load(): loading all messages\n" );
221 # Other threads don't need to load the messages if another thread is doing it.
222 $success = $this->mMemc
->add( $this->mMemcKey
.'-status', "loading", MSG_LOAD_TIMEOUT
);
224 wfProfileIn( $fname.'-load' );
226 wfProfileOut( $fname.'-load' );
229 # Keep trying if it fails, this is kind of important
230 wfProfileIn( $fname.'-save' );
232 !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
234 usleep(mt_rand(500000,1500000));
237 # Save to local cache
238 if ( $wgLocalMessageCache !== false ) {
239 $serialized = serialize( $this->mCache
);
240 $hash = md5( $serialized );
241 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
242 if ($wgLocalMessageCacheSerialized) {
243 $this->saveToLocal( $serialized,$hash );
245 $this->saveToScript( $this->mCache
, $hash );
249 wfProfileOut( $fname.'-save' );
251 $this->mMemc
->set( $this->mMemcKey
.'-status', 'error', 60*5 );
252 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
258 if ( !is_array( $this->mCache
) ) {
259 wfDebug( "MessageCache::load(): individual message mode\n" );
260 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
261 # Causing too much DB load, disabling -- TS
262 $this->mDisable
= true;
264 if ( $this->mCache == "loading" ) {
265 $this->mUseCache = false;
266 } elseif ( $this->mCache == "error" ) {
267 $this->mUseCache = false;
270 $this->mDisable = true;
273 $this->mCache
= false;
276 wfProfileOut( $fname );
277 $this->mDeferred
= false;
282 * Loads all or main part of cacheable messages from the database
284 function loadFromDB() {
285 global $wgAllMessagesEn, $wgLang;
287 $fname = 'MessageCache::loadFromDB';
288 $dbr =& wfGetDB( DB_SLAVE
);
290 throw new MWException( 'Invalid database object' );
292 $conditions = array( 'page_is_redirect' => 0,
293 'page_namespace' => NS_MEDIAWIKI
);
294 $res = $dbr->select( array( 'page', 'revision', 'text' ),
295 array( 'page_title', 'old_text', 'old_flags' ),
296 'page_is_redirect=0 AND page_namespace='.NS_MEDIAWIKI
.' AND page_latest=rev_id AND rev_text_id=old_id',
300 $this->mCache
= array();
301 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
302 $this->mCache
[$row->page_title
] = Revision
::getRevisionText( $row );
306 # Go through the language array and the extension array and make a note of
307 # any keys missing from the cache
308 foreach ( $wgAllMessagesEn as $key => $value ) {
309 $uckey = $wgLang->ucfirst( $key );
310 if ( !array_key_exists( $uckey, $this->mCache
) ) {
311 $this->mCache
[$uckey] = false;
315 # Make sure all extension messages are available
316 wfLoadAllExtensions();
318 # Add them to the cache
319 foreach ( $this->mExtensionMessages
as $key => $value ) {
320 $uckey = $wgLang->ucfirst( $key );
321 if ( !array_key_exists( $uckey, $this->mCache
) &&
322 ( isset( $this->mExtensionMessages
[$key][$wgLang->getCode()] ) ||
isset( $this->mExtensionMessages
[$key]['en'] ) ) ) {
323 $this->mCache
[$uckey] = false;
327 $dbr->freeResult( $res );
331 * Not really needed anymore
334 global $wgAllMessagesEn, $wgContLang;
335 if ( !$this->mKeys
) {
336 $this->mKeys
= array();
337 foreach ( $wgAllMessagesEn as $key => $value ) {
338 $title = $wgContLang->ucfirst( $key );
339 array_push( $this->mKeys
, $title );
348 function isCacheable( $key ) {
352 function replace( $title, $text ) {
353 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc, $wgDBname;
357 $parserMemc->delete("$wgDBname:sidebar");
358 if ( is_array( $this->mCache
) ) {
359 $this->mCache
[$title] = $text;
360 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
362 # Save to local cache
363 if ( $wgLocalMessageCache !== false ) {
364 $serialized = serialize( $this->mCache
);
365 $hash = md5( $serialized );
366 $this->mMemc
->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry
);
367 if ($wgLocalMessageCacheSerialized) {
368 $this->saveToLocal( $serialized,$hash );
370 $this->saveToScript( $this->mCache
, $hash );
381 * Represents a write lock on the messages key
384 if ( !$this->mUseCache
) {
388 $lockKey = $this->mMemcKey
. 'lock';
389 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
393 return $i >= MSG_WAIT_TIMEOUT
;
397 if ( !$this->mUseCache
) {
401 $lockKey = $this->mMemcKey
. 'lock';
402 $this->mMemc
->delete( $lockKey );
405 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
406 global $wgContLanguageCode;
409 $lang =& $wgContLang;
410 $langcode = $wgContLanguageCode;
412 global $wgLang, $wgLanguageCode;
414 $langcode = $wgLanguageCode;
416 # If uninitialised, someone is trying to call this halfway through Setup.php
417 if( !$this->mInitialised
) {
418 return '<' . htmlspecialchars($key) . '>';
420 # If cache initialization was deferred, start it now.
421 if( $this->mDeferred
&& !$this->mDisable
&& $useDB ) {
426 if( !$this->mDisable
&& $useDB ) {
427 $title = $lang->ucfirst( $key );
428 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
429 $title .= '/' . $langcode;
431 $message = $this->getFromCache( $title );
433 # Try the extension array
434 if( $message === false && array_key_exists( $key, $this->mExtensionMessages
) ) {
435 if ( isset( $this->mExtensionMessages
[$key][$langcode] ) ) {
436 $message = $this->mExtensionMessages
[$key][$langcode];
437 } elseif ( isset( $this->mExtensionMessages
[$key]['en'] ) ) {
438 $message = $this->mExtensionMessages
[$key]['en'];
442 # Try the array in the language object
443 if( $message === false ) {
444 wfSuppressWarnings();
445 $message = $lang->getMessage( $key );
447 if ( is_null( $message ) ) {
452 # Try the English array
453 if( $message === false && $langcode != 'en' ) {
454 wfSuppressWarnings();
455 $message = Language
::getMessage( $key );
457 if ( is_null( $message ) ) {
462 # Is this a custom message? Try the default language in the db...
463 if( ($message === false ||
$message === '-' ) &&
464 !$this->mDisable
&& $useDB &&
465 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
466 $message = $this->getFromCache( $lang->ucfirst( $key ) );
470 if( $message === false ) {
471 return '<' . htmlspecialchars($key) . '>';
475 $message = $this->transform( $message );
479 function getFromCache( $title ) {
483 if( $this->mUseCache
&& is_array( $this->mCache
) && array_key_exists( $title, $this->mCache
) ) {
484 return $this->mCache
[$title];
487 # Try individual message cache
488 if ( $this->mUseCache
) {
489 $message = $this->mMemc
->get( $this->mMemcKey
. ':' . $title );
490 if ( $message == '###NONEXISTENT###' ) {
492 } elseif( !is_null( $message ) ) {
493 $this->mCache
[$title] = $message;
500 # Call message Hooks, in case they are defined
501 wfRunHooks('MessagesPreLoad',array($title,&$message));
503 # If it wasn't in the cache, load each message from the DB individually
504 $revision = Revision
::newFromTitle( Title
::makeTitle( NS_MEDIAWIKI
, $title ) );
506 $message = $revision->getText();
507 if ($this->mUseCache
) {
508 $this->mCache
[$title]=$message;
509 /* individual messages may be often
510 recached until proper purge code exists
512 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, $message, 300 );
516 # Use some special text instead of false, because false gets converted to '' somewhere
517 $this->mMemc
->set( $this->mMemcKey
. ':' . $title, '###NONEXISTENT###', $this->mExpiry
);
523 function transform( $message ) {
525 if( !$this->mDisableTransform
&& isset( $wgParser ) ) {
526 if( strpos( $message, '{{' ) !== false ) {
527 $message = $wgParser->transformMsg( $message, $this->mParserOptions
);
533 function disable() { $this->mDisable
= true; }
534 function enable() { $this->mDisable
= false; }
535 function disableTransform() { $this->mDisableTransform
= true; }
536 function enableTransform() { $this->mDisableTransform
= false; }
537 function setTransform( $x ) { $this->mDisableTransform
= $x; }
538 function getTransform() { return $this->mDisableTransform
; }
541 * Add a message to the cache
544 * @param mixed $value
545 * @param string $lang The messages language, English by default
547 function addMessage( $key, $value, $lang = 'en' ) {
548 $this->mExtensionMessages
[$key][$lang] = $value;
552 * Add an associative array of message to the cache
554 * @param array $messages An associative array of key => values to be added
555 * @param string $lang The messages language, English by default
557 function addMessages( $messages, $lang = 'en' ) {
558 wfProfileIn( __METHOD__
);
559 foreach ( $messages as $key => $value ) {
560 $this->addMessage( $key, $value, $lang );
562 wfProfileOut( __METHOD__
);
566 * Clear all stored messages. Mainly used after a mass rebuild.
569 if( $this->mUseCache
) {
570 $this->mMemc
->delete( $this->mMemcKey
);