Code style tweaks per brion's suggestions on Wikitech-l
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage Cache
6 */
7
8 /**
9 *
10 */
11 define( 'MSG_LOAD_TIMEOUT', 60);
12 define( 'MSG_LOCK_TIMEOUT', 10);
13 define( 'MSG_WAIT_TIMEOUT', 10);
14
15 /**
16 * Message cache
17 * Performs various useful MediaWiki namespace-related functions
18 *
19 * @package MediaWiki
20 */
21 class MessageCache {
22 var $mCache, $mUseCache, $mDisable, $mExpiry;
23 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
24 var $mExtensionMessages = array();
25 var $mInitialised = false;
26 var $mDeferred = true;
27
28 function __construct( &$memCached, $useDB, $expiry, $memcPrefix) {
29 wfProfileIn( __METHOD__ );
30
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;
40
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;
46
47 wfProfileOut( __METHOD__ );
48 }
49
50 function getParserOptions() {
51 if ( !$this->mParserOptions ) {
52 $this->mParserOptions = new ParserOptions;
53 }
54 return $this->mParserOptions;
55 }
56
57 /**
58 * Try to load the cache from a local file
59 */
60 function loadFromLocal( $hash ) {
61 global $wgLocalMessageCache;
62
63 $this->mCache = false;
64 if ( $wgLocalMessageCache === false ) {
65 return;
66 }
67
68 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
69
70 wfSuppressWarnings();
71 $file = fopen( $filename, 'r' );
72 wfRestoreWarnings();
73 if ( !$file ) {
74 return;
75 }
76
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, 10000000 );
82 $this->mCache = unserialize( $serialized );
83 }
84 fclose( $file );
85 }
86
87 /**
88 * Save the cache to a local file
89 */
90 function saveToLocal( $serialized, $hash ) {
91 global $wgLocalMessageCache;
92
93 if ( $wgLocalMessageCache === false ) {
94 return;
95 }
96
97 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
98 $oldUmask = umask( 0 );
99 wfMkdirParents( $wgLocalMessageCache, 0777 );
100 umask( $oldUmask );
101
102 $file = fopen( $filename, 'w' );
103 if ( !$file ) {
104 wfDebug( "Unable to open local cache file for writing\n" );
105 return;
106 }
107
108 fwrite( $file, $hash . $serialized );
109 fclose( $file );
110 @chmod( $filename, 0666 );
111 }
112
113 function loadFromScript( $hash ) {
114 global $wgLocalMessageCache;
115 if ( $wgLocalMessageCache === false ) {
116 return;
117 }
118
119 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
120
121 wfSuppressWarnings();
122 $file = fopen( $filename, 'r' );
123 wfRestoreWarnings();
124 if ( !$file ) {
125 return;
126 }
127 $localHash=substr(fread($file,40),8);
128 fclose($file);
129 if ($hash!=$localHash) {
130 return;
131 }
132 require("$wgLocalMessageCache/messages-" . wfWikiID());
133 }
134
135 function saveToScript($array, $hash) {
136 global $wgLocalMessageCache;
137 if ( $wgLocalMessageCache === false ) {
138 return;
139 }
140
141 $filename = "$wgLocalMessageCache/messages-" . wfWikiID();
142 $oldUmask = umask( 0 );
143 wfMkdirParents( $wgLocalMessageCache, 0777 );
144 umask( $oldUmask );
145 $file = fopen( $filename.'.tmp', 'w');
146 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
147
148 foreach ($array as $key => $message) {
149 fwrite($file, "'". $this->escapeForScript($key).
150 "' => '" . $this->escapeForScript($message).
151 "',\n");
152 }
153 fwrite($file,");\n?>");
154 fclose($file);
155 rename($filename.'.tmp',$filename);
156 }
157
158 function escapeForScript($string) {
159 $string = str_replace( '\\', '\\\\', $string );
160 $string = str_replace( '\'', '\\\'', $string );
161 return $string;
162 }
163
164 /**
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
168 */
169 function load() {
170 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
171
172 if ( $this->mDisable ) {
173 static $shownDisabled = false;
174 if ( !$shownDisabled ) {
175 wfDebug( "MessageCache::load(): disabled\n" );
176 $shownDisabled = true;
177 }
178 return true;
179 }
180 $fname = 'MessageCache::load';
181 wfProfileIn( $fname );
182 $success = true;
183
184 if ( $this->mUseCache ) {
185 $this->mCache = false;
186
187 # Try local cache
188 wfProfileIn( $fname.'-fromlocal' );
189 $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
190 if ( $hash ) {
191 if ($wgLocalMessageCacheSerialized) {
192 $this->loadFromLocal( $hash );
193 } else {
194 $this->loadFromScript( $hash );
195 }
196 if ( $this->mCache ) {
197 wfDebug( "MessageCache::load(): got from local cache\n" );
198 }
199 }
200 wfProfileOut( $fname.'-fromlocal' );
201
202 # Try memcached
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 );
211 if ( !$hash ) {
212 $hash = md5( $serialized );
213 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
214 }
215 if ($wgLocalMessageCacheSerialized) {
216 $this->saveToLocal( $serialized,$hash );
217 } else {
218 $this->saveToScript( $this->mCache, $hash );
219 }
220 }
221 }
222 wfProfileOut( $fname.'-fromcache' );
223 }
224
225
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" );
229 $this->lock();
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 );
232 if ( $success ) {
233 wfProfileIn( $fname.'-load' );
234 $this->loadFromDB();
235 wfProfileOut( $fname.'-load' );
236
237 # Save in memcached
238 # Keep trying if it fails, this is kind of important
239 wfProfileIn( $fname.'-save' );
240 for ($i=0; $i<20 &&
241 !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
242 $i++ ) {
243 usleep(mt_rand(500000,1500000));
244 }
245
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 );
253 } else {
254 $this->saveToScript( $this->mCache, $hash );
255 }
256 }
257
258 wfProfileOut( $fname.'-save' );
259 if ( $i == 20 ) {
260 $this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 );
261 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
262 }
263 }
264 $this->unlock();
265 }
266
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;
272 /*
273 if ( $this->mCache == "loading" ) {
274 $this->mUseCache = false;
275 } elseif ( $this->mCache == "error" ) {
276 $this->mUseCache = false;
277 $success = false;
278 } else {
279 $this->mDisable = true;
280 $success = false;
281 }*/
282 $this->mCache = false;
283 }
284 }
285 wfProfileOut( $fname );
286 $this->mDeferred = false;
287 return $success;
288 }
289
290 /**
291 * Loads all or main part of cacheable messages from the database
292 */
293 function loadFromDB() {
294 global $wgLang;
295
296 $fname = 'MessageCache::loadFromDB';
297 $dbr =& wfGetDB( DB_SLAVE );
298 if ( !$dbr ) {
299 throw new MWException( 'Invalid database object' );
300 }
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',
306 $fname
307 );
308
309 $this->mCache = array();
310 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
311 $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
312 }
313
314 # Negative caching
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;
322 }
323 }
324
325 # Make sure all extension messages are available
326 MessageCache::loadAllMessages();
327
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;
334 }
335 }
336
337 $dbr->freeResult( $res );
338 }
339
340 /**
341 * Not really needed anymore
342 */
343 function getKeys() {
344 global $wgContLang;
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 );
351 }
352 }
353 return $this->mKeys;
354 }
355
356 /**
357 * @deprecated
358 */
359 function isCacheable( $key ) {
360 return true;
361 }
362
363 function replace( $title, $text ) {
364 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc;
365
366 $this->lock();
367 $this->load();
368 $parserMemc->delete(wfMemcKey('sidebar'));
369 if ( is_array( $this->mCache ) ) {
370 $this->mCache[$title] = $text;
371 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
372
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 );
380 } else {
381 $this->saveToScript( $this->mCache, $hash );
382 }
383 }
384
385
386 }
387 $this->unlock();
388 }
389
390 /**
391 * Returns success
392 * Represents a write lock on the messages key
393 */
394 function lock() {
395 if ( !$this->mUseCache ) {
396 return true;
397 }
398
399 $lockKey = $this->mMemcKey . 'lock';
400 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
401 sleep(1);
402 }
403
404 return $i >= MSG_WAIT_TIMEOUT;
405 }
406
407 function unlock() {
408 if ( !$this->mUseCache ) {
409 return;
410 }
411
412 $lockKey = $this->mMemcKey . 'lock';
413 $this->mMemc->delete( $lockKey );
414 }
415
416 function get( $key, $useDB = true, $forcontent = true, $isfullkey = false ) {
417 global $wgContLanguageCode, $wgContLang, $wgLang;
418 if( $forcontent ) {
419 $lang =& $wgContLang;
420 } else {
421 $lang =& $wgLang;
422 }
423 $langcode = $lang->getCode();
424 # If uninitialised, someone is trying to call this halfway through Setup.php
425 if( !$this->mInitialised ) {
426 return '&lt;' . htmlspecialchars($key) . '&gt;';
427 }
428 # If cache initialization was deferred, start it now.
429 if( $this->mDeferred && !$this->mDisable && $useDB ) {
430 $this->load();
431 }
432
433 $message = false;
434 if( !$this->mDisable && $useDB ) {
435 $title = $wgContLang->ucfirst( $key );
436 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
437 $title .= '/' . $langcode;
438 }
439 $message = $this->getFromCache( $title );
440 }
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'];
447 }
448 }
449
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 );
455 wfRestoreWarnings();
456 if ( is_null( $message ) ) {
457 $message = false;
458 }
459 }
460
461 # Try the array of another language
462 if( $message === false && strpos( $key, '/' ) ) {
463 $message = explode( '/', $key );
464 if ( $message[1] ) {
465 wfSuppressWarnings();
466 $message = Language::getMessageFor( $message[0], $message[1] );
467 wfRestoreWarnings();
468 if ( is_null( $message ) ) {
469 $message = false;
470 }
471 } else {
472 $message = false;
473 }
474 }
475
476 # Is this a custom message? Try the default language in the db...
477 if( ($message === false || $message === '-' ) &&
478 !$this->mDisable && $useDB &&
479 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
480 $message = $this->getFromCache( $wgContLang->ucfirst( $key ) );
481 }
482
483 # Final fallback
484 if( $message === false ) {
485 return '&lt;' . htmlspecialchars($key) . '&gt;';
486 }
487
488 # Replace brace tags
489 $message = $this->transform( $message );
490 return $message;
491 }
492
493 function getFromCache( $title ) {
494 $message = false;
495
496 # Try the cache
497 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
498 return $this->mCache[$title];
499 }
500
501 # Try individual message cache
502 if ( $this->mUseCache ) {
503 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
504 if ( $message == '###NONEXISTENT###' ) {
505 $this->mCache[$title] = false;
506 return false;
507 } elseif( !is_null( $message ) ) {
508 $this->mCache[$title] = $message;
509 return $message;
510 } else {
511 $message = false;
512 }
513 }
514
515 # Call message Hooks, in case they are defined
516 wfRunHooks('MessagesPreLoad',array($title,&$message));
517
518 # If it wasn't in the cache, load each message from the DB individually
519 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
520 if( $revision ) {
521 $message = $revision->getText();
522 if ($this->mUseCache) {
523 $this->mCache[$title]=$message;
524 /* individual messages may be often
525 recached until proper purge code exists
526 */
527 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
528 }
529 } else {
530 # Negative caching
531 # Use some special text instead of false, because false gets converted to '' somewhere
532 $this->mMemc->set( $this->mMemcKey . ':' . $title, '###NONEXISTENT###', $this->mExpiry );
533 $this->mCache[$title] = false;
534 }
535
536 return $message;
537 }
538
539 function transform( $message ) {
540 global $wgParser;
541 if ( !$this->mParser && isset( $wgParser ) ) {
542 # Do some initialisation so that we don't have to do it twice
543 $wgParser->firstCallInit();
544 # Clone it and store it
545 $this->mParser = clone $wgParser;
546 }
547 if ( !$this->mDisableTransform && $this->mParser ) {
548 if( strpos( $message, '{{' ) !== false ) {
549 $message = $this->mParser->transformMsg( $message, $this->getParserOptions() );
550 }
551 }
552 return $message;
553 }
554
555 function disable() { $this->mDisable = true; }
556 function enable() { $this->mDisable = false; }
557 function disableTransform() { $this->mDisableTransform = true; }
558 function enableTransform() { $this->mDisableTransform = false; }
559 function setTransform( $x ) { $this->mDisableTransform = $x; }
560 function getTransform() { return $this->mDisableTransform; }
561
562 /**
563 * Add a message to the cache
564 *
565 * @param mixed $key
566 * @param mixed $value
567 * @param string $lang The messages language, English by default
568 */
569 function addMessage( $key, $value, $lang = 'en' ) {
570 $this->mExtensionMessages[$key][$lang] = $value;
571 }
572
573 /**
574 * Add an associative array of message to the cache
575 *
576 * @param array $messages An associative array of key => values to be added
577 * @param string $lang The messages language, English by default
578 */
579 function addMessages( $messages, $lang = 'en' ) {
580 wfProfileIn( __METHOD__ );
581 foreach ( $messages as $key => $value ) {
582 $this->addMessage( $key, $value, $lang );
583 }
584 wfProfileOut( __METHOD__ );
585 }
586
587 /**
588 * Get the extension messages for a specific language
589 *
590 * @param string $lang The messages language, English by default
591 */
592 function getExtensionMessagesFor( $lang = 'en' ) {
593 wfProfileIn( __METHOD__ );
594 $messages = array();
595 foreach( $this->mExtensionMessages as $key => $message ) {
596 if ( isset( $message[$lang] ) ) {
597 $messages[$key] = $message[$lang];
598 } elseif ( isset( $message['en'] ) ) {
599 $messages[$key] = $message['en'];
600 }
601 }
602 wfProfileOut( __METHOD__ );
603 return $messages;
604 }
605
606 /**
607 * Clear all stored messages. Mainly used after a mass rebuild.
608 */
609 function clear() {
610 global $wgLocalMessageCache;
611 if( $this->mUseCache ) {
612 # Global cache
613 $this->mMemc->delete( $this->mMemcKey );
614 # Invalidate all local caches
615 $this->mMemc->delete( "{$this->mMemcKey}-hash" );
616 }
617 }
618
619 static function loadAllMessages() {
620 # Some extensions will load their messages when you load their class file
621 wfLoadAllExtensions();
622 # Others will respond to this hook
623 wfRunHooks( 'LoadAllMessages' );
624 # Still others will respond to neither, they are EVIL. We sometimes need to know!
625 }
626 }
627 ?>