convert "::1" and other pseudo-IPv6 addresses that Apache may throw at us to their...
[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 $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',
304 $fname
305 );
306
307 $this->mCache = array();
308 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
309 $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
310 }
311
312 # Negative caching
313 # Go through the language array and the extension array and make a note of
314 # any keys missing from the cache
315 $allMessages = Language::getMessagesFor( 'en' );
316 foreach ( $allMessages as $key => $unused ) {
317 $uckey = $wgLang->ucfirst( $key );
318 if ( !array_key_exists( $uckey, $this->mCache ) ) {
319 $this->mCache[$uckey] = false;
320 }
321 }
322
323 # Make sure all extension messages are available
324 MessageCache::loadAllMessages();
325
326 # Add them to the cache
327 foreach ( $this->mExtensionMessages as $key => $unused ) {
328 $uckey = $wgLang->ucfirst( $key );
329 if ( !array_key_exists( $uckey, $this->mCache ) &&
330 ( isset( $this->mExtensionMessages[$key][$wgLang->getCode()] ) || isset( $this->mExtensionMessages[$key]['en'] ) ) ) {
331 $this->mCache[$uckey] = false;
332 }
333 }
334
335 $dbr->freeResult( $res );
336 }
337
338 /**
339 * Not really needed anymore
340 */
341 function getKeys() {
342 global $wgContLang;
343 if ( !$this->mKeys ) {
344 $this->mKeys = array();
345 $allMessages = Language::getMessagesFor( 'en' );
346 foreach ( $allMessages as $key => $unused ) {
347 $title = $wgContLang->ucfirst( $key );
348 array_push( $this->mKeys, $title );
349 }
350 }
351 return $this->mKeys;
352 }
353
354 /**
355 * @deprecated
356 */
357 function isCacheable( $key ) {
358 return true;
359 }
360
361 function replace( $title, $text ) {
362 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc;
363
364 $this->lock();
365 $this->load();
366 $parserMemc->delete(wfMemcKey('sidebar'));
367 if ( is_array( $this->mCache ) ) {
368 $this->mCache[$title] = $text;
369 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
370
371 # Save to local cache
372 if ( $wgLocalMessageCache !== false ) {
373 $serialized = serialize( $this->mCache );
374 $hash = md5( $serialized );
375 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
376 if ($wgLocalMessageCacheSerialized) {
377 $this->saveToLocal( $serialized,$hash );
378 } else {
379 $this->saveToScript( $this->mCache, $hash );
380 }
381 }
382
383
384 }
385 $this->unlock();
386 }
387
388 /**
389 * Returns success
390 * Represents a write lock on the messages key
391 */
392 function lock() {
393 if ( !$this->mUseCache ) {
394 return true;
395 }
396
397 $lockKey = $this->mMemcKey . 'lock';
398 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
399 sleep(1);
400 }
401
402 return $i >= MSG_WAIT_TIMEOUT;
403 }
404
405 function unlock() {
406 if ( !$this->mUseCache ) {
407 return;
408 }
409
410 $lockKey = $this->mMemcKey . 'lock';
411 $this->mMemc->delete( $lockKey );
412 }
413
414 function get( $key, $useDB = true, $forcontent = true, $isfullkey = false ) {
415 global $wgContLanguageCode, $wgContLang, $wgLang;
416 if( $forcontent ) {
417 $lang =& $wgContLang;
418 } else {
419 $lang =& $wgLang;
420 }
421 $langcode = $lang->getCode();
422 # If uninitialised, someone is trying to call this halfway through Setup.php
423 if( !$this->mInitialised ) {
424 return '&lt;' . htmlspecialchars($key) . '&gt;';
425 }
426 # If cache initialization was deferred, start it now.
427 if( $this->mDeferred && !$this->mDisable && $useDB ) {
428 $this->load();
429 }
430
431 $message = false;
432 if( !$this->mDisable && $useDB ) {
433 $title = $wgContLang->ucfirst( $key );
434 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
435 $title .= '/' . $langcode;
436 }
437 $message = $this->getFromCache( $title );
438 }
439 # Try the extension array
440 if( $message === false && array_key_exists( $key, $this->mExtensionMessages ) ) {
441 if ( isset( $this->mExtensionMessages[$key][$langcode] ) ) {
442 $message = $this->mExtensionMessages[$key][$langcode];
443 } elseif ( isset( $this->mExtensionMessages[$key]['en'] ) ) {
444 $message = $this->mExtensionMessages[$key]['en'];
445 }
446 }
447
448 # Try the array in the language object
449 if( $message === false ) {
450 #wfDebug( "Trying language object for message $key\n" );
451 wfSuppressWarnings();
452 $message = $lang->getMessage( $key );
453 wfRestoreWarnings();
454 if ( is_null( $message ) ) {
455 $message = false;
456 }
457 }
458
459 # Try the array of another language
460 if( $message === false && strpos( $key, '/' ) ) {
461 $message = explode( '/', $key );
462 if ( $message[1] ) {
463 wfSuppressWarnings();
464 $message = Language::getMessageFor( $message[0], $message[1] );
465 wfRestoreWarnings();
466 if ( is_null( $message ) ) {
467 $message = false;
468 }
469 } else {
470 $message = false;
471 }
472 }
473
474 # Is this a custom message? Try the default language in the db...
475 if( ($message === false || $message === '-' ) &&
476 !$this->mDisable && $useDB &&
477 !$isfullkey && ($langcode != $wgContLanguageCode) ) {
478 $message = $this->getFromCache( $wgContLang->ucfirst( $key ) );
479 }
480
481 # Final fallback
482 if( $message === false ) {
483 return '&lt;' . htmlspecialchars($key) . '&gt;';
484 }
485
486 # Replace brace tags
487 $message = $this->transform( $message );
488 return $message;
489 }
490
491 function getFromCache( $title ) {
492 $message = false;
493
494 # Try the cache
495 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
496 return $this->mCache[$title];
497 }
498
499 # Try individual message cache
500 if ( $this->mUseCache ) {
501 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
502 if ( $message == '###NONEXISTENT###' ) {
503 $this->mCache[$title] = false;
504 return false;
505 } elseif( !is_null( $message ) ) {
506 $this->mCache[$title] = $message;
507 return $message;
508 } else {
509 $message = false;
510 }
511 }
512
513 # Call message Hooks, in case they are defined
514 wfRunHooks('MessagesPreLoad',array($title,&$message));
515
516 # If it wasn't in the cache, load each message from the DB individually
517 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
518 if( $revision ) {
519 $message = $revision->getText();
520 if ($this->mUseCache) {
521 $this->mCache[$title]=$message;
522 /* individual messages may be often
523 recached until proper purge code exists
524 */
525 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
526 }
527 } else {
528 # Negative caching
529 # Use some special text instead of false, because false gets converted to '' somewhere
530 $this->mMemc->set( $this->mMemcKey . ':' . $title, '###NONEXISTENT###', $this->mExpiry );
531 $this->mCache[$title] = false;
532 }
533
534 return $message;
535 }
536
537 function transform( $message ) {
538 global $wgParser;
539 if ( !$this->mParser && isset( $wgParser ) ) {
540 # Do some initialisation so that we don't have to do it twice
541 $wgParser->firstCallInit();
542 # Clone it and store it
543 $this->mParser = clone $wgParser;
544 }
545 if ( !$this->mDisableTransform && $this->mParser ) {
546 if( strpos( $message, '{{' ) !== false ) {
547 $message = $this->mParser->transformMsg( $message, $this->getParserOptions() );
548 }
549 }
550 return $message;
551 }
552
553 function disable() { $this->mDisable = true; }
554 function enable() { $this->mDisable = false; }
555 function disableTransform() { $this->mDisableTransform = true; }
556 function enableTransform() { $this->mDisableTransform = false; }
557 function setTransform( $x ) { $this->mDisableTransform = $x; }
558 function getTransform() { return $this->mDisableTransform; }
559
560 /**
561 * Add a message to the cache
562 *
563 * @param mixed $key
564 * @param mixed $value
565 * @param string $lang The messages language, English by default
566 */
567 function addMessage( $key, $value, $lang = 'en' ) {
568 $this->mExtensionMessages[$key][$lang] = $value;
569 }
570
571 /**
572 * Add an associative array of message to the cache
573 *
574 * @param array $messages An associative array of key => values to be added
575 * @param string $lang The messages language, English by default
576 */
577 function addMessages( $messages, $lang = 'en' ) {
578 wfProfileIn( __METHOD__ );
579 foreach ( $messages as $key => $value ) {
580 $this->addMessage( $key, $value, $lang );
581 }
582 wfProfileOut( __METHOD__ );
583 }
584
585 /**
586 * Get the extension messages for a specific language
587 *
588 * @param string $lang The messages language, English by default
589 */
590 function getExtensionMessagesFor( $lang = 'en' ) {
591 wfProfileIn( __METHOD__ );
592 $messages = array();
593 foreach( $this->mExtensionMessages as $key => $message ) {
594 if ( isset( $message[$lang] ) ) {
595 $messages[$key] = $message[$lang];
596 } elseif ( isset( $message['en'] ) ) {
597 $messages[$key] = $message['en'];
598 }
599 }
600 wfProfileOut( __METHOD__ );
601 return $messages;
602 }
603
604 /**
605 * Clear all stored messages. Mainly used after a mass rebuild.
606 */
607 function clear() {
608 global $wgLocalMessageCache;
609 if( $this->mUseCache ) {
610 # Global cache
611 $this->mMemc->delete( $this->mMemcKey );
612 # Invalidate all local caches
613 $this->mMemc->delete( "{$this->mMemcKey}-hash" );
614 }
615 }
616
617 static function loadAllMessages() {
618 # Some extensions will load their messages when you load their class file
619 wfLoadAllExtensions();
620 # Others will respond to this hook
621 wfRunHooks( 'LoadAllMessages' );
622 # Still others will respond to neither, they are EVIL. We sometimes need to know!
623 }
624 }
625 ?>