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