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