Use $wgParser instead of constructing a new parser object. Constructing a new one...
[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
40 wfProfileIn( __METHOD__.'-parseropt' );
41 $this->mParserOptions = new ParserOptions( $u=NULL );
42 wfProfileOut( __METHOD__.'-parseropt' );
43
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;
49
50 wfProfileOut( __METHOD__ );
51 }
52
53 /**
54 * Try to load the cache from a local file
55 */
56 function loadFromLocal( $hash ) {
57 global $wgLocalMessageCache, $wgDBname;
58
59 $this->mCache = false;
60 if ( $wgLocalMessageCache === false ) {
61 return;
62 }
63
64 $filename = "$wgLocalMessageCache/messages-$wgDBname";
65
66 wfSuppressWarnings();
67 $file = fopen( $filename, 'r' );
68 wfRestoreWarnings();
69 if ( !$file ) {
70 return;
71 }
72
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 );
79 }
80 fclose( $file );
81 }
82
83 /**
84 * Save the cache to a local file
85 */
86 function saveToLocal( $serialized, $hash ) {
87 global $wgLocalMessageCache, $wgDBname;
88
89 if ( $wgLocalMessageCache === false ) {
90 return;
91 }
92
93 $filename = "$wgLocalMessageCache/messages-$wgDBname";
94 $oldUmask = umask( 0 );
95 wfMkdirParents( $wgLocalMessageCache, 0777 );
96 umask( $oldUmask );
97
98 $file = fopen( $filename, 'w' );
99 if ( !$file ) {
100 wfDebug( "Unable to open local cache file for writing\n" );
101 return;
102 }
103
104 fwrite( $file, $hash . $serialized );
105 fclose( $file );
106 @chmod( $filename, 0666 );
107 }
108
109 function loadFromScript( $hash ) {
110 global $wgLocalMessageCache, $wgDBname;
111 if ( $wgLocalMessageCache === false ) {
112 return;
113 }
114
115 $filename = "$wgLocalMessageCache/messages-$wgDBname";
116
117 wfSuppressWarnings();
118 $file = fopen( $filename, 'r' );
119 wfRestoreWarnings();
120 if ( !$file ) {
121 return;
122 }
123 $localHash=substr(fread($file,40),8);
124 fclose($file);
125 if ($hash!=$localHash) {
126 return;
127 }
128 require("$wgLocalMessageCache/messages-$wgDBname");
129 }
130
131 function saveToScript($array, $hash) {
132 global $wgLocalMessageCache, $wgDBname;
133 if ( $wgLocalMessageCache === false ) {
134 return;
135 }
136
137 $filename = "$wgLocalMessageCache/messages-$wgDBname";
138 $oldUmask = umask( 0 );
139 wfMkdirParents( $wgLocalMessageCache, 0777 );
140 umask( $oldUmask );
141 $file = fopen( $filename.'.tmp', 'w');
142 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
143
144 foreach ($array as $key => $message) {
145 fwrite($file, "'". $this->escapeForScript($key).
146 "' => '" . $this->escapeForScript($message).
147 "',\n");
148 }
149 fwrite($file,");\n?>");
150 fclose($file);
151 rename($filename.'.tmp',$filename);
152 }
153
154 function escapeForScript($string) {
155 $string = str_replace( '\\', '\\\\', $string );
156 $string = str_replace( '\'', '\\\'', $string );
157 return $string;
158 }
159
160 /**
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
164 */
165 function load() {
166 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
167
168 if ( $this->mDisable ) {
169 static $shownDisabled = false;
170 if ( !$shownDisabled ) {
171 wfDebug( "MessageCache::load(): disabled\n" );
172 $shownDisabled = true;
173 }
174 return true;
175 }
176 $fname = 'MessageCache::load';
177 wfProfileIn( $fname );
178 $success = true;
179
180 if ( $this->mUseCache ) {
181 $this->mCache = false;
182
183 # Try local cache
184 wfProfileIn( $fname.'-fromlocal' );
185 $hash = $this->mMemc->get( "{$this->mMemcKey}-hash" );
186 if ( $hash ) {
187 if ($wgLocalMessageCacheSerialized) {
188 $this->loadFromLocal( $hash );
189 } else {
190 $this->loadFromScript( $hash );
191 }
192 }
193 wfProfileOut( $fname.'-fromlocal' );
194
195 # Try memcached
196 if ( !$this->mCache ) {
197 wfProfileIn( $fname.'-fromcache' );
198 $this->mCache = $this->mMemc->get( $this->mMemcKey );
199
200 # Save to local cache
201 if ( $wgLocalMessageCache !== false ) {
202 $serialized = serialize( $this->mCache );
203 if ( !$hash ) {
204 $hash = md5( $serialized );
205 $this->mMemc->set( "{$this->mMemcKey}-hash", $hash, $this->mExpiry );
206 }
207 if ($wgLocalMessageCacheSerialized) {
208 $this->saveToLocal( $serialized,$hash );
209 } else {
210 $this->saveToScript( $this->mCache, $hash );
211 }
212 }
213 wfProfileOut( $fname.'-fromcache' );
214 }
215
216
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" );
220 $this->lock();
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 );
223 if ( $success ) {
224 wfProfileIn( $fname.'-load' );
225 $this->loadFromDB();
226 wfProfileOut( $fname.'-load' );
227
228 # Save in memcached
229 # Keep trying if it fails, this is kind of important
230 wfProfileIn( $fname.'-save' );
231 for ($i=0; $i<20 &&
232 !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
233 $i++ ) {
234 usleep(mt_rand(500000,1500000));
235 }
236
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 );
244 } else {
245 $this->saveToScript( $this->mCache, $hash );
246 }
247 }
248
249 wfProfileOut( $fname.'-save' );
250 if ( $i == 20 ) {
251 $this->mMemc->set( $this->mMemcKey.'-status', 'error', 60*5 );
252 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
253 }
254 }
255 $this->unlock();
256 }
257
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;
263 /*
264 if ( $this->mCache == "loading" ) {
265 $this->mUseCache = false;
266 } elseif ( $this->mCache == "error" ) {
267 $this->mUseCache = false;
268 $success = false;
269 } else {
270 $this->mDisable = true;
271 $success = false;
272 }*/
273 $this->mCache = false;
274 }
275 }
276 wfProfileOut( $fname );
277 $this->mDeferred = false;
278 return $success;
279 }
280
281 /**
282 * Loads all or main part of cacheable messages from the database
283 */
284 function loadFromDB() {
285 global $wgAllMessagesEn, $wgLang;
286
287 $fname = 'MessageCache::loadFromDB';
288 $dbr =& wfGetDB( DB_SLAVE );
289 if ( !$dbr ) {
290 throw new MWException( 'Invalid database object' );
291 }
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',
297 $fname
298 );
299
300 $this->mCache = array();
301 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
302 $this->mCache[$row->page_title] = Revision::getRevisionText( $row );
303 }
304
305 # Negative caching
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;
312 }
313 }
314
315 # Make sure all extension messages are available
316 wfLoadAllExtensions();
317
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;
324 }
325 }
326
327 $dbr->freeResult( $res );
328 }
329
330 /**
331 * Not really needed anymore
332 */
333 function getKeys() {
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 );
340 }
341 }
342 return $this->mKeys;
343 }
344
345 /**
346 * @deprecated
347 */
348 function isCacheable( $key ) {
349 return true;
350 }
351
352 function replace( $title, $text ) {
353 global $wgLocalMessageCache, $wgLocalMessageCacheSerialized, $parserMemc, $wgDBname;
354
355 $this->lock();
356 $this->load();
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 );
361
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 );
369 } else {
370 $this->saveToScript( $this->mCache, $hash );
371 }
372 }
373
374
375 }
376 $this->unlock();
377 }
378
379 /**
380 * Returns success
381 * Represents a write lock on the messages key
382 */
383 function lock() {
384 if ( !$this->mUseCache ) {
385 return true;
386 }
387
388 $lockKey = $this->mMemcKey . 'lock';
389 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
390 sleep(1);
391 }
392
393 return $i >= MSG_WAIT_TIMEOUT;
394 }
395
396 function unlock() {
397 if ( !$this->mUseCache ) {
398 return;
399 }
400
401 $lockKey = $this->mMemcKey . 'lock';
402 $this->mMemc->delete( $lockKey );
403 }
404
405 function get( $key, $useDB, $forcontent=true, $isfullkey = false ) {
406 global $wgContLanguageCode;
407 if( $forcontent ) {
408 global $wgContLang;
409 $lang =& $wgContLang;
410 $langcode = $wgContLanguageCode;
411 } else {
412 global $wgLang, $wgLanguageCode;
413 $lang =& $wgLang;
414 $langcode = $wgLanguageCode;
415 }
416 # If uninitialised, someone is trying to call this halfway through Setup.php
417 if( !$this->mInitialised ) {
418 return '&lt;' . htmlspecialchars($key) . '&gt;';
419 }
420 # If cache initialization was deferred, start it now.
421 if( $this->mDeferred && !$this->mDisable && $useDB ) {
422 $this->load();
423 }
424
425 $message = false;
426 if( !$this->mDisable && $useDB ) {
427 $title = $lang->ucfirst( $key );
428 if(!$isfullkey && ($langcode != $wgContLanguageCode) ) {
429 $title .= '/' . $langcode;
430 }
431 $message = $this->getFromCache( $title );
432 }
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'];
439 }
440 }
441
442 # Try the array in the language object
443 if( $message === false ) {
444 wfSuppressWarnings();
445 $message = $lang->getMessage( $key );
446 wfRestoreWarnings();
447 if ( is_null( $message ) ) {
448 $message = false;
449 }
450 }
451
452 # Try the English array
453 if( $message === false && $langcode != 'en' ) {
454 wfSuppressWarnings();
455 $message = Language::getMessage( $key );
456 wfRestoreWarnings();
457 if ( is_null( $message ) ) {
458 $message = false;
459 }
460 }
461
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 ) );
467 }
468
469 # Final fallback
470 if( $message === false ) {
471 return '&lt;' . htmlspecialchars($key) . '&gt;';
472 }
473
474 # Replace brace tags
475 $message = $this->transform( $message );
476 return $message;
477 }
478
479 function getFromCache( $title ) {
480 $message = false;
481
482 # Try the cache
483 if( $this->mUseCache && is_array( $this->mCache ) && array_key_exists( $title, $this->mCache ) ) {
484 return $this->mCache[$title];
485 }
486
487 # Try individual message cache
488 if ( $this->mUseCache ) {
489 $message = $this->mMemc->get( $this->mMemcKey . ':' . $title );
490 if ( $message == '###NONEXISTENT###' ) {
491 return false;
492 } elseif( !is_null( $message ) ) {
493 $this->mCache[$title] = $message;
494 return $message;
495 } else {
496 $message = false;
497 }
498 }
499
500 # Call message Hooks, in case they are defined
501 wfRunHooks('MessagesPreLoad',array($title,&$message));
502
503 # If it wasn't in the cache, load each message from the DB individually
504 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
505 if( $revision ) {
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
511 */
512 $this->mMemc->set( $this->mMemcKey . ':' . $title, $message, 300 );
513 }
514 } else {
515 # Negative caching
516 # Use some special text instead of false, because false gets converted to '' somewhere
517 $this->mMemc->set( $this->mMemcKey . ':' . $title, '###NONEXISTENT###', $this->mExpiry );
518 }
519
520 return $message;
521 }
522
523 function transform( $message ) {
524 global $wgParser;
525 if( !$this->mDisableTransform && isset( $wgParser ) ) {
526 if( strpos( $message, '{{' ) !== false ) {
527 $message = $wgParser->transformMsg( $message, $this->mParserOptions );
528 }
529 }
530 return $message;
531 }
532
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; }
539
540 /**
541 * Add a message to the cache
542 *
543 * @param mixed $key
544 * @param mixed $value
545 * @param string $lang The messages language, English by default
546 */
547 function addMessage( $key, $value, $lang = 'en' ) {
548 $this->mExtensionMessages[$key][$lang] = $value;
549 }
550
551 /**
552 * Add an associative array of message to the cache
553 *
554 * @param array $messages An associative array of key => values to be added
555 * @param string $lang The messages language, English by default
556 */
557 function addMessages( $messages, $lang = 'en' ) {
558 wfProfileIn( __METHOD__ );
559 foreach ( $messages as $key => $value ) {
560 $this->addMessage( $key, $value, $lang );
561 }
562 wfProfileOut( __METHOD__ );
563 }
564
565 /**
566 * Clear all stored messages. Mainly used after a mass rebuild.
567 */
568 function clear() {
569 if( $this->mUseCache ) {
570 $this->mMemc->delete( $this->mMemcKey );
571 }
572 }
573 }
574 ?>