Further cleanup by removing unused member variables, adding protected access and...
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2 /**
3 * @file
4 * @ingroup Cache
5 */
6
7 /**
8 *
9 */
10 define( 'MSG_LOAD_TIMEOUT', 60);
11 define( 'MSG_LOCK_TIMEOUT', 10);
12 define( 'MSG_WAIT_TIMEOUT', 10);
13 define( 'MSG_CACHE_VERSION', 1 );
14
15 /**
16 * Message cache
17 * Performs various MediaWiki namespace-related functions
18 * @ingroup Cache
19 */
20 class MessageCache {
21 /**
22 * Process local cache of loaded messages that are defined in
23 * MediaWiki namespace. First array level is a language code,
24 * second level is message key and the values are either message
25 * content prefixed with space, or !NONEXISTENT for negative
26 * caching.
27 */
28 protected $mCache;
29
30 // Should mean that database cannot be used, but check
31 protected $mDisable;
32
33 /// Lifetime for cache, used by object caching
34 protected $mExpiry;
35
36 /**
37 * Message cache has it's own parser which it uses to transform
38 * messages.
39 */
40 protected $mParserOptions, $mParser;
41
42 /// Variable for tracking which variables are already loaded
43 protected $mLoadedLanguages = array();
44
45 function __construct( $memCached, $useDB, $expiry ) {
46 if ( !$memCached ) {
47 $memCached = wfGetCache( CACHE_NONE );
48 }
49
50 $this->mMemc = $memCached;
51 $this->mDisable = !$useDB;
52 $this->mExpiry = $expiry;
53 }
54
55
56 /**
57 * ParserOptions is lazy initialised.
58 */
59 function getParserOptions() {
60 if ( !$this->mParserOptions ) {
61 $this->mParserOptions = new ParserOptions;
62 }
63 return $this->mParserOptions;
64 }
65
66 /**
67 * Try to load the cache from a local file.
68 * Actual format of the file depends on the $wgLocalMessageCacheSerialized
69 * setting.
70 *
71 * @param $hash String: the hash of contents, to check validity.
72 * @param $code Mixed: Optional language code, see documenation of load().
73 * @return false on failure.
74 */
75 function loadFromLocal( $hash, $code ) {
76 global $wgCacheDirectory, $wgLocalMessageCacheSerialized;
77
78 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
79
80 # Check file existence
81 wfSuppressWarnings();
82 $file = fopen( $filename, 'r' );
83 wfRestoreWarnings();
84 if ( !$file ) {
85 return false; // No cache file
86 }
87
88 if ( $wgLocalMessageCacheSerialized ) {
89 // Check to see if the file has the hash specified
90 $localHash = fread( $file, 32 );
91 if ( $hash === $localHash ) {
92 // All good, get the rest of it
93 $serialized = '';
94 while ( !feof( $file ) ) {
95 $serialized .= fread( $file, 100000 );
96 }
97 fclose( $file );
98 return $this->setCache( unserialize( $serialized ), $code );
99 } else {
100 fclose( $file );
101 return false; // Wrong hash
102 }
103 } else {
104 $localHash=substr(fread($file,40),8);
105 fclose($file);
106 if ($hash!=$localHash) {
107 return false; // Wrong hash
108 }
109
110 # Require overwrites the member variable or just shadows it?
111 require( $filename );
112 return $this->setCache( $this->mCache, $code );
113 }
114 }
115
116 /**
117 * Save the cache to a local file.
118 */
119 function saveToLocal( $serialized, $hash, $code ) {
120 global $wgCacheDirectory;
121
122 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
123 wfMkdirParents( $wgCacheDirectory ); // might fail
124
125 wfSuppressWarnings();
126 $file = fopen( $filename, 'w' );
127 wfRestoreWarnings();
128
129 if ( !$file ) {
130 wfDebug( "Unable to open local cache file for writing\n" );
131 return;
132 }
133
134 fwrite( $file, $hash . $serialized );
135 fclose( $file );
136 @chmod( $filename, 0666 );
137 }
138
139 function saveToScript( $array, $hash, $code ) {
140 global $wgCacheDirectory;
141
142 $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
143 $tempFilename = $filename . '.tmp';
144 wfMkdirParents( $wgCacheDirectory ); // might fail
145
146 wfSuppressWarnings();
147 $file = fopen( $tempFilename, 'w');
148 wfRestoreWarnings();
149
150 if ( !$file ) {
151 wfDebug( "Unable to open local cache file for writing\n" );
152 return;
153 }
154
155 fwrite($file,"<?php\n//$hash\n\n \$this->mCache = array(");
156
157 foreach ($array as $key => $message) {
158 $key = $this->escapeForScript($key);
159 $messages = $this->escapeForScript($message);
160 fwrite($file, "'$key' => '$message',\n");
161 }
162
163 fwrite($file,");\n?>");
164 fclose($file);
165 rename($tempFilename, $filename);
166 }
167
168 function escapeForScript($string) {
169 $string = str_replace( '\\', '\\\\', $string );
170 $string = str_replace( '\'', '\\\'', $string );
171 return $string;
172 }
173
174 /**
175 * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
176 */
177 function setCache( $cache, $code ) {
178 if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) {
179 $this->mCache[$code] = $cache;
180 return true;
181 } else {
182 return false;
183 }
184 }
185
186 /**
187 * Loads messages from caches or from database in this order:
188 * (1) local message cache (if $wgUseLocalMessageCache is enabled)
189 * (2) memcached
190 * (3) from the database.
191 *
192 * When succesfully loading from (2) or (3), all higher level caches are
193 * updated for the newest version.
194 *
195 * Nothing is loaded if member variable mDisabled is true, either manually
196 * set by calling code or if message loading fails (is this possible?).
197 *
198 * Returns true if cache is already populated or it was succesfully populated,
199 * or false if populating empty cache fails. Also returns true if MessageCache
200 * is disabled.
201 *
202 * @param $code String: language to which load messages
203 */
204 function load( $code = false ) {
205 global $wgUseLocalMessageCache;
206
207 if( !is_string( $code ) ) {
208 # This isn't really nice, so at least make a note about it and try to
209 # fall back
210 wfDebug( __METHOD__ . " called without providing a language code\n" );
211 $code = 'en';
212 }
213
214 # Don't do double loading...
215 if ( isset($this->mLoadedLanguages[$code]) ) return true;
216
217 # 8 lines of code just to say (once) that message cache is disabled
218 if ( $this->mDisable ) {
219 static $shownDisabled = false;
220 if ( !$shownDisabled ) {
221 wfDebug( __METHOD__ . ": disabled\n" );
222 $shownDisabled = true;
223 }
224 return true;
225 }
226
227 # Loading code starts
228 wfProfileIn( __METHOD__ );
229 $success = false; # Keep track of success
230 $where = array(); # Debug info, delayed to avoid spamming debug log too much
231 $cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
232
233
234 # (1) local cache
235 # Hash of the contents is stored in memcache, to detect if local cache goes
236 # out of date (due to update in other thread?)
237 if ( $wgUseLocalMessageCache ) {
238 wfProfileIn( __METHOD__ . '-fromlocal' );
239
240 $hash = $this->mMemc->get( wfMemcKey( 'messages', $code, 'hash' ) );
241 if ( $hash ) {
242 $success = $this->loadFromLocal( $hash, $code );
243 if ( $success ) $where[] = 'got from local cache';
244 }
245 wfProfileOut( __METHOD__ . '-fromlocal' );
246 }
247
248 # (2) memcache
249 # Fails if nothing in cache, or in the wrong version.
250 if ( !$success ) {
251 wfProfileIn( __METHOD__ . '-fromcache' );
252 $cache = $this->mMemc->get( $cacheKey );
253 $success = $this->setCache( $cache, $code );
254 if ( $success ) {
255 $where[] = 'got from global cache';
256 $this->saveToCaches( $cache, false, $code );
257 }
258 wfProfileOut( __METHOD__ . '-fromcache' );
259 }
260
261
262 # (3)
263 # Nothing in caches... so we need create one and store it in caches
264 if ( !$success ) {
265 $where[] = 'cache is empty';
266 $where[] = 'loading from database';
267
268 $this->lock($cacheKey);
269
270 # Limit the concurrency of loadFromDB to a single process
271 # This prevents the site from going down when the cache expires
272 $statusKey = wfMemcKey( 'messages', $code, 'status' );
273 $success = $this->mMemc->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT );
274 if ( $success ) {
275 $cache = $this->loadFromDB( $code );
276 $success = $this->setCache( $cache, $code );
277 }
278 if ( $success ) {
279 $success = $this->saveToCaches( $cache, true, $code );
280 if ( $success ) {
281 $this->mMemc->delete( $statusKey );
282 } else {
283 $this->mMemc->set( $statusKey, 'error', 60*5 );
284 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
285 }
286 }
287 $this->unlock($cacheKey);
288 }
289
290 if ( !$success ) {
291 # Bad luck... this should not happen
292 $where[] = 'loading FAILED - cache is disabled';
293 $info = implode( ', ', $where );
294 wfDebug( __METHOD__ . ": Loading $code... $info\n" );
295 $this->mDisable = true;
296 $this->mCache = false;
297 } else {
298 # All good, just record the success
299 $info = implode( ', ', $where );
300 wfDebug( __METHOD__ . ": Loading $code... $info\n" );
301 $this->mLoadedLanguages[$code] = true;
302 }
303 wfProfileOut( __METHOD__ );
304 return $success;
305 }
306
307 /**
308 * Loads cacheable messages from the database. Messages bigger than
309 * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
310 * on-demand from the database later.
311 *
312 * @param $code Optional language code, see documenation of load().
313 * @return Array: Loaded messages for storing in caches.
314 */
315 function loadFromDB( $code = false ) {
316 wfProfileIn( __METHOD__ );
317 global $wgMaxMsgCacheEntrySize, $wgContLanguageCode;
318 $dbr = wfGetDB( DB_SLAVE );
319 $cache = array();
320
321 # Common conditions
322 $conds = array(
323 'page_is_redirect' => 0,
324 'page_namespace' => NS_MEDIAWIKI,
325 );
326
327 if ( $code ) {
328 # Is this fast enough. Should not matter if the filtering is done in the
329 # database or in code.
330 if ( $code !== $wgContLanguageCode ) {
331 # Messages for particular language
332 $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), "/$code" );
333 } else {
334 # Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
335 # other than language code.
336 $conds[] = 'page_title NOT' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() );
337 }
338 }
339
340 # Conditions to fetch oversized pages to ignore them
341 $bigConds = $conds;
342 $bigConds[] = 'page_len > ' . intval( $wgMaxMsgCacheEntrySize );
343
344 # Load titles for all oversized pages in the MediaWiki namespace
345 $res = $dbr->select( 'page', 'page_title', $bigConds, __METHOD__ . "($code)-big" );
346 while ( $row = $dbr->fetchObject( $res ) ) {
347 $cache[$row->page_title] = '!TOO BIG';
348 }
349 $dbr->freeResult( $res );
350
351 # Conditions to load the remaining pages with their contents
352 $smallConds = $conds;
353 $smallConds[] = 'page_latest=rev_id';
354 $smallConds[] = 'rev_text_id=old_id';
355 $smallConds[] = 'page_len <= ' . intval( $wgMaxMsgCacheEntrySize );
356
357 $res = $dbr->select( array( 'page', 'revision', 'text' ),
358 array( 'page_title', 'old_text', 'old_flags' ),
359 $smallConds, __METHOD__ . "($code)-small" );
360
361 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
362 $cache[$row->page_title] = ' ' . Revision::getRevisionText( $row );
363 }
364 $dbr->freeResult( $res );
365
366 $cache['VERSION'] = MSG_CACHE_VERSION;
367 wfProfileOut( __METHOD__ );
368 return $cache;
369 }
370
371 /**
372 * Updates cache as necessary when message page is changed
373 *
374 * @param $title String: name of the page changed.
375 * @param $text Mixed: new contents of the page.
376 */
377 public function replace( $title, $text ) {
378 global $wgMaxMsgCacheEntrySize;
379 wfProfileIn( __METHOD__ );
380
381
382 list( , $code ) = $this->figureMessage( $title );
383
384 $cacheKey = wfMemcKey( 'messages', $code );
385 $this->load($code);
386 $this->lock($cacheKey);
387
388 if ( is_array($this->mCache[$code]) ) {
389 $titleKey = wfMemcKey( 'messages', 'individual', $title );
390
391 if ( $text === false ) {
392 # Article was deleted
393 unset( $this->mCache[$code][$title] );
394 $this->mMemc->delete( $titleKey );
395
396 } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
397 # Check for size
398 $this->mCache[$code][$title] = '!TOO BIG';
399 $this->mMemc->set( $titleKey, ' ' . $text, $this->mExpiry );
400
401 } else {
402 $this->mCache[$code][$title] = ' ' . $text;
403 $this->mMemc->delete( $titleKey );
404 }
405
406 # Update caches
407 $this->saveToCaches( $this->mCache[$code], true, $code );
408 }
409 $this->unlock($cacheKey);
410
411 // Also delete cached sidebar... just in case it is affected
412 global $parserMemc;
413 $codes = array( $code );
414 if ( $code === 'en' ) {
415 // Delete all sidebars, like for example on action=purge on the
416 // sidebar messages
417 $codes = array_keys( Language::getLanguageNames() );
418 }
419
420 foreach ( $codes as $code ) {
421 $sidebarKey = wfMemcKey( 'sidebar', $code );
422 $parserMemc->delete( $sidebarKey );
423 }
424
425 wfRunHooks( "MessageCacheReplace", array( $title, $text ) );
426
427 wfProfileOut( __METHOD__ );
428 }
429
430 /**
431 * Shortcut to update caches.
432 *
433 * @param $cache Array: cached messages with a version.
434 * @param $memc Bool: Wether to update or not memcache.
435 * @param $code String: Language code.
436 * @return False on somekind of error.
437 */
438 protected function saveToCaches( $cache, $memc = true, $code = false ) {
439 wfProfileIn( __METHOD__ );
440 global $wgUseLocalMessageCache, $wgLocalMessageCacheSerialized;
441
442 $cacheKey = wfMemcKey( 'messages', $code );
443
444 if ( $memc ) {
445 $success = $this->mMemc->set( $cacheKey, $cache, $this->mExpiry );
446 } else {
447 $success = true;
448 }
449
450 # Save to local cache
451 if ( $wgUseLocalMessageCache ) {
452 $serialized = serialize( $cache );
453 $hash = md5( $serialized );
454 $this->mMemc->set( wfMemcKey( 'messages', $code, 'hash' ), $hash, $this->mExpiry );
455 if ($wgLocalMessageCacheSerialized) {
456 $this->saveToLocal( $serialized, $hash, $code );
457 } else {
458 $this->saveToScript( $cache, $hash, $code );
459 }
460 }
461
462 wfProfileOut( __METHOD__ );
463 return $success;
464 }
465
466 /**
467 * Represents a write lock on the messages key
468 *
469 * @return Boolean: success
470 */
471 function lock($key) {
472 $lockKey = $key . ':lock';
473 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
474 sleep(1);
475 }
476
477 return $i >= MSG_WAIT_TIMEOUT;
478 }
479
480 function unlock($key) {
481 $lockKey = $key . ':lock';
482 $this->mMemc->delete( $lockKey );
483 }
484
485 /**
486 * Get a message from either the content language or the user language.
487 *
488 * @param $key String: the message cache key
489 * @param $useDB Boolean: get the message from the DB, false to use only
490 * the localisation
491 * @param $langcode String: code of the language to get the message for, if
492 * it is a valid code create a language for that language,
493 * if it is a string but not a valid code then make a basic
494 * language object, if it is a false boolean then use the
495 * current users language (as a fallback for the old
496 * parameter functionality), or if it is a true boolean
497 * then use the wikis content language (also as a
498 * fallback).
499 * @param $isFullKey Boolean: specifies whether $key is a two part key
500 * "msg/lang".
501 */
502 function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
503 global $wgContLanguageCode, $wgContLang;
504
505 if ( !is_string( $key ) ) {
506 throw new MWException( "Non-string key given" );
507 }
508
509 if ( strval( $key ) === '' ) {
510 # Shortcut: the empty key is always missing
511 return false;
512 }
513
514 $lang = wfGetLangObj( $langcode );
515 $langcode = $lang->getCode();
516
517 $message = false;
518
519 # Normalise title-case input (with some inlining)
520 $lckey = str_replace( ' ', '_', $key );
521 if ( ord( $key ) < 128 ) {
522 $lckey[0] = strtolower( $lckey[0] );
523 $uckey = ucfirst( $lckey );
524 } else {
525 $lckey = $wgContLang->lcfirst( $lckey );
526 $uckey = $wgContLang->ucfirst( $lckey );
527 }
528
529 # Try the MediaWiki namespace
530 if( !$this->mDisable && $useDB ) {
531 $title = $uckey;
532 if(!$isFullKey && ( $langcode != $wgContLanguageCode ) ) {
533 $title .= '/' . $langcode;
534 }
535 $message = $this->getMsgFromNamespace( $title, $langcode );
536 }
537
538 # Try the array in the language object
539 if ( $message === false ) {
540 $message = $lang->getMessage( $lckey );
541 if ( is_null( $message ) ) {
542 $message = false;
543 }
544 }
545
546 # Try the array of another language
547 if( $message === false ) {
548 $parts = explode( '/', $lckey );
549 # We may get calls for things that are http-urls from sidebar
550 # Let's not load nonexistent languages for those
551 # They usually have more than one slash.
552 if ( count( $parts ) == 2 && $parts[1] !== '' ) {
553 $message = Language::getMessageFor( $parts[0], $parts[1] );
554 if ( is_null( $message ) ) {
555 $message = false;
556 }
557 }
558 }
559
560 # Is this a custom message? Try the default language in the db...
561 if( ($message === false || $message === '-' ) &&
562 !$this->mDisable && $useDB &&
563 !$isFullKey && ($langcode != $wgContLanguageCode) ) {
564 $message = $this->getMsgFromNamespace( $uckey, $wgContLanguageCode );
565 }
566
567 # Final fallback
568 if( $message === false ) {
569 return false;
570 }
571
572 # Fix whitespace
573 $message = strtr( $message,
574 array(
575 # Fix for trailing whitespace, removed by textarea
576 '&#32;' => ' ',
577 # Fix for NBSP, converted to space by firefox
578 '&nbsp;' => "\xc2\xa0",
579 '&#160;' => "\xc2\xa0",
580 ) );
581
582 return $message;
583 }
584
585 /**
586 * Get a message from the MediaWiki namespace, with caching. The key must
587 * first be converted to two-part lang/msg form if necessary.
588 *
589 * @param $title String: Message cache key with initial uppercase letter.
590 * @param $code String: code denoting the language to try.
591 */
592 function getMsgFromNamespace( $title, $code ) {
593 $type = false;
594 $message = false;
595
596 $this->load( $code );
597 if (isset( $this->mCache[$code][$title] ) ) {
598 $entry = $this->mCache[$code][$title];
599 $type = substr( $entry, 0, 1 );
600 if ( $type == ' ' ) {
601 return substr( $entry, 1 );
602 }
603 }
604
605 # Call message hooks, in case they are defined
606 wfRunHooks('MessagesPreLoad', array( $title, &$message ) );
607 if ( $message !== false ) {
608 return $message;
609 }
610
611 # If there is no cache entry and no placeholder, it doesn't exist
612 if ( $type !== '!' ) {
613 return false;
614 }
615
616 $titleKey = wfMemcKey( 'messages', 'individual', $title );
617
618 # Try the individual message cache
619 $entry = $this->mMemc->get( $titleKey );
620 if ( $entry ) {
621 $type = substr( $entry, 0, 1 );
622
623 if ( $type === ' ' ) {
624 # Ok!
625 $message = substr( $entry, 1 );
626 $this->mCache[$code][$title] = $entry;
627 return $message;
628 } elseif ( $entry === '!NONEXISTENT' ) {
629 return false;
630 } else {
631 # Corrupt/obsolete entry, delete it
632 $this->mMemc->delete( $titleKey );
633 }
634 }
635
636 # Try loading it from the DB
637 $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
638 if( $revision ) {
639 $message = $revision->getText();
640 $this->mCache[$code][$title] = ' ' . $message;
641 $this->mMemc->set( $titleKey, ' ' . $message, $this->mExpiry );
642 } else {
643 # Negative caching
644 # Use some special text instead of false, because false gets converted to '' somewhere
645 $this->mMemc->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
646 $this->mCache[$code][$title] = false;
647 }
648 return $message;
649 }
650
651 function transform( $message, $interface = false, $language = null ) {
652 // Avoid creating parser if nothing to transform
653 if( strpos( $message, '{{' ) === false ) {
654 return $message;
655 }
656
657 global $wgParser, $wgParserConf;
658 if ( !$this->mParser && isset( $wgParser ) ) {
659 # Do some initialisation so that we don't have to do it twice
660 $wgParser->firstCallInit();
661 # Clone it and store it
662 $class = $wgParserConf['class'];
663 if ( $class == 'Parser_DiffTest' ) {
664 # Uncloneable
665 $this->mParser = new $class( $wgParserConf );
666 } else {
667 $this->mParser = clone $wgParser;
668 }
669 #wfDebug( __METHOD__ . ": following contents triggered transform: $message\n" );
670 }
671 if ( $this->mParser ) {
672 $popts = $this->getParserOptions();
673 $popts->setInterfaceMessage( $interface );
674 $popts->setTargetLanguage( $language );
675 $message = $this->mParser->transformMsg( $message, $popts );
676 }
677 return $message;
678 }
679
680 function disable() { $this->mDisable = true; }
681 function enable() { $this->mDisable = false; }
682
683 /** @deprecated */
684 function disableTransform(){
685 wfDeprecated( __METHOD__ );
686 }
687 function enableTransform() {
688 wfDeprecated( __METHOD__ );
689 }
690 function setTransform( $x ) {
691 wfDeprecated( __METHOD__ );
692 }
693 function getTransform() {
694 wfDeprecated( __METHOD__ );
695 return false;
696 }
697
698 /**
699 * Clear all stored messages. Mainly used after a mass rebuild.
700 */
701 function clear() {
702 $langs = Language::getLanguageNames( false );
703 foreach ( array_keys($langs) as $code ) {
704 # Global cache
705 $this->mMemc->delete( wfMemcKey( 'messages', $code ) );
706 # Invalidate all local caches
707 $this->mMemc->delete( wfMemcKey( 'messages', $code, 'hash' ) );
708 }
709 }
710
711 /**
712 * Add a message to the cache
713 * @deprecated Use $wgExtensionMessagesFiles
714 *
715 * @param $key Mixed
716 * @param $value Mixed
717 * @param $lang String: the messages language, English by default
718 */
719 function addMessage( $key, $value, $lang = 'en' ) {
720 wfDeprecated( __METHOD__ );
721 $lc = Language::getLocalisationCache();
722 $lc->addLegacyMessages( array( $lang => array( $key => $value ) ) );
723 }
724
725 /**
726 * Add an associative array of message to the cache
727 * @deprecated Use $wgExtensionMessagesFiles
728 *
729 * @param $messages Array: an associative array of key => values to be added
730 * @param $lang String: the messages language, English by default
731 */
732 function addMessages( $messages, $lang = 'en' ) {
733 wfDeprecated( __METHOD__ );
734 $lc = Language::getLocalisationCache();
735 $lc->addLegacyMessages( array( $lang => $messages ) );
736 }
737
738 /**
739 * Add a 2-D array of messages by lang. Useful for extensions.
740 * @deprecated Use $wgExtensionMessagesFiles
741 *
742 * @param $messages Array: the array to be added
743 */
744 function addMessagesByLang( $messages ) {
745 wfDeprecated( __METHOD__ );
746 $lc = Language::getLocalisationCache();
747 $lc->addLegacyMessages( $messages );
748 }
749
750 /**
751 * Set a hook for addMessagesByLang()
752 */
753 function setExtensionMessagesHook( $callback ) {
754 $this->mAddMessagesHook = $callback;
755 }
756
757 /**
758 * @deprecated
759 */
760 function loadAllMessages( $lang = false ) {
761 }
762
763 /**
764 * @deprecated
765 */
766 function loadMessagesFile( $filename, $langcode = false ) {
767 }
768
769 public function figureMessage( $key ) {
770 global $wgContLanguageCode;
771 $pieces = explode( '/', $key );
772 if( count( $pieces ) < 2 )
773 return array( $key, $wgContLanguageCode );
774
775 $lang = array_pop( $pieces );
776 $validCodes = Language::getLanguageNames();
777 if( !array_key_exists( $lang, $validCodes ) )
778 return array( $key, $wgContLanguageCode );
779
780 $message = implode( '/', $pieces );
781 return array( $message, $lang );
782 }
783
784 }