Follow-up r53286: Fix some issues:
[lhc/web/wiklou.git] / maintenance / language / languages.inc
1 <?php
2 /**
3 * Handle messages in the language files.
4 *
5 * @file
6 * @ingroup MaintenanceLanguage
7 */
8
9 /**
10 * @ingroup MaintenanceLanguage
11 */
12 class languages {
13 protected $mLanguages; # List of languages
14
15 protected $mRawMessages; # Raw list of the messages in each language
16 protected $mMessages; # Messages in each language (except for English), divided to groups
17 protected $mFallback; # Fallback language in each language
18 protected $mGeneralMessages; # General messages in English, divided to groups
19 protected $mIgnoredMessages; # All the messages which should be exist only in the English file
20 protected $mOptionalMessages; # All the messages which may be translated or not, depending on the language
21
22 protected $mNamespaceNames; # Namespace names
23 protected $mNamespaceAliases; # Namespace aliases
24 protected $mMagicWords; # Magic words
25 protected $mSpecialPageAliases; # Special page aliases
26
27 /**
28 * Load the list of languages: all the Messages*.php
29 * files in the languages directory.
30 *
31 * @param $exif Treat the EXIF messages?
32 */
33 function __construct( $exif = true ) {
34 require( dirname(__FILE__) . '/messageTypes.inc' );
35 $this->mIgnoredMessages = $wgIgnoredMessages;
36 if ( $exif ) {
37 $this->mOptionalMessages = array_merge( $wgOptionalMessages );
38 } else {
39 $this->mOptionalMessages = array_merge( $wgOptionalMessages, $wgEXIFMessages );
40 }
41
42 $this->mLanguages = array_keys( Language::getLanguageNames( true ) );
43 sort( $this->mLanguages );
44 }
45
46 /**
47 * Get the language list.
48 *
49 * @return The language list.
50 */
51 public function getLanguages() {
52 return $this->mLanguages;
53 }
54
55 /**
56 * Get the ignored messages list.
57 *
58 * @return The ignored messages list.
59 */
60 public function getIgnoredMessages() {
61 return $this->mIgnoredMessages;
62 }
63
64 /**
65 * Get the optional messages list.
66 *
67 * @return The optional messages list.
68 */
69 public function getOptionalMessages() {
70 return $this->mOptionalMessages;
71 }
72
73 /**
74 * Load the language file.
75 *
76 * @param $code The language code.
77 */
78 protected function loadFile( $code ) {
79 if ( isset( $this->mRawMessages[$code] ) &&
80 isset( $this->mFallback[$code] ) &&
81 isset( $this->mNamespaceNames[$code] ) &&
82 isset( $this->mNamespaceAliases[$code] ) &&
83 isset( $this->mMagicWords[$code] ) &&
84 isset( $this->mSpecialPageAliases[$code] ) ) {
85 return;
86 }
87 $this->mRawMessages[$code] = array();
88 $this->mFallback[$code] = '';
89 $this->mNamespaceNames[$code] = array();
90 $this->mNamespaceAliases[$code] = array();
91 $this->mMagicWords[$code] = array();
92 $this->mSpecialPageAliases[$code] = array();
93 $filename = Language::getMessagesFileName( $code );
94 if ( file_exists( $filename ) ) {
95 require( $filename );
96 if ( isset( $messages ) ) {
97 $this->mRawMessages[$code] = $messages;
98 }
99 if ( isset( $fallback ) ) {
100 $this->mFallback[$code] = $fallback;
101 }
102 if ( isset( $namespaceNames ) ) {
103 $this->mNamespaceNames[$code] = $namespaceNames;
104 }
105 if ( isset( $namespaceAliases ) ) {
106 $this->mNamespaceAliases[$code] = $namespaceAliases;
107 }
108 if ( isset( $magicWords ) ) {
109 $this->mMagicWords[$code] = $magicWords;
110 }
111 if ( isset( $specialPageAliases ) ) {
112 $this->mSpecialPageAliases[$code] = $specialPageAliases;
113 }
114 }
115 }
116
117 /**
118 * Load the messages for a specific language (which is not English) and divide them to groups:
119 * all - all the messages.
120 * required - messages which should be translated in order to get a complete translation.
121 * optional - messages which can be translated, the fallback translation is used if not translated.
122 * obsolete - messages which should not be translated, either because they do not exist, or they are ignored messages.
123 * translated - messages which are either required or optional, but translated from English and needed.
124 *
125 * @param $code The language code.
126 */
127 private function loadMessages( $code ) {
128 if ( isset( $this->mMessages[$code] ) ) {
129 return;
130 }
131 $this->loadFile( $code );
132 $this->loadGeneralMessages();
133 $this->mMessages[$code]['all'] = $this->mRawMessages[$code];
134 $this->mMessages[$code]['required'] = array();
135 $this->mMessages[$code]['optional'] = array();
136 $this->mMessages[$code]['obsolete'] = array();
137 $this->mMessages[$code]['translated'] = array();
138 foreach ( $this->mMessages[$code]['all'] as $key => $value ) {
139 if ( isset( $this->mGeneralMessages['required'][$key] ) ) {
140 $this->mMessages[$code]['required'][$key] = $value;
141 $this->mMessages[$code]['translated'][$key] = $value;
142 } else if ( isset( $this->mGeneralMessages['optional'][$key] ) ) {
143 $this->mMessages[$code]['optional'][$key] = $value;
144 $this->mMessages[$code]['translated'][$key] = $value;
145 } else {
146 $this->mMessages[$code]['obsolete'][$key] = $value;
147 }
148 }
149 }
150
151 /**
152 * Load the messages for English and divide them to groups:
153 * all - all the messages.
154 * required - messages which should be translated to other languages in order to get a complete translation.
155 * optional - messages which can be translated to other languages, but it's not required for a complete translation.
156 * ignored - messages which should not be translated to other languages.
157 * translatable - messages which are either required or optional, but can be translated from English.
158 */
159 private function loadGeneralMessages() {
160 if ( isset( $this->mGeneralMessages ) ) {
161 return;
162 }
163 $this->loadFile( 'en' );
164 $this->mGeneralMessages['all'] = $this->mRawMessages['en'];
165 $this->mGeneralMessages['required'] = array();
166 $this->mGeneralMessages['optional'] = array();
167 $this->mGeneralMessages['ignored'] = array();
168 $this->mGeneralMessages['translatable'] = array();
169 foreach ( $this->mGeneralMessages['all'] as $key => $value ) {
170 if ( in_array( $key, $this->mIgnoredMessages ) ) {
171 $this->mGeneralMessages['ignored'][$key] = $value;
172 } else if ( in_array( $key, $this->mOptionalMessages ) ) {
173 $this->mGeneralMessages['optional'][$key] = $value;
174 $this->mGeneralMessages['translatable'][$key] = $value;
175 } else {
176 $this->mGeneralMessages['required'][$key] = $value;
177 $this->mGeneralMessages['translatable'][$key] = $value;
178 }
179 }
180 }
181
182 /**
183 * Get all the messages for a specific language (not English), without the
184 * fallback language messages, divided to groups:
185 * all - all the messages.
186 * required - messages which should be translated in order to get a complete translation.
187 * optional - messages which can be translated, the fallback translation is used if not translated.
188 * obsolete - messages which should not be translated, either because they do not exist, or they are ignored messages.
189 * translated - messages which are either required or optional, but translated from English and needed.
190 *
191 * @param $code The language code.
192 *
193 * @return The messages in this language.
194 */
195 public function getMessages( $code ) {
196 $this->loadMessages( $code );
197 return $this->mMessages[$code];
198 }
199
200 /**
201 * Get all the general English messages, divided to groups:
202 * all - all the messages.
203 * required - messages which should be translated to other languages in order to get a complete translation.
204 * optional - messages which can be translated to other languages, but it's not required for a complete translation.
205 * ignored - messages which should not be translated to other languages.
206 * translatable - messages which are either required or optional, but can be translated from English.
207 *
208 * @return The general English messages.
209 */
210 public function getGeneralMessages() {
211 $this->loadGeneralMessages();
212 return $this->mGeneralMessages;
213 }
214
215 /**
216 * Get fallback language code for a specific language.
217 *
218 * @param $code The language code.
219 *
220 * @return Fallback code.
221 */
222 public function getFallback( $code ) {
223 $this->loadFile( $code );
224 return $this->mFallback[$code];
225 }
226
227 /**
228 * Get namespace names for a specific language.
229 *
230 * @param $code The language code.
231 *
232 * @return Namespace names.
233 */
234 public function getNamespaceNames( $code ) {
235 $this->loadFile( $code );
236 return $this->mNamespaceNames[$code];
237 }
238
239 /**
240 * Get namespace aliases for a specific language.
241 *
242 * @param $code The language code.
243 *
244 * @return Namespace aliases.
245 */
246 public function getNamespaceAliases( $code ) {
247 $this->loadFile( $code );
248 return $this->mNamespaceAliases[$code];
249 }
250
251 /**
252 * Get magic words for a specific language.
253 *
254 * @param $code The language code.
255 *
256 * @return Magic words.
257 */
258 public function getMagicWords( $code ) {
259 $this->loadFile( $code );
260 return $this->mMagicWords[$code];
261 }
262
263 /**
264 * Get special page aliases for a specific language.
265 *
266 * @param $code The language code.
267 *
268 * @return Special page aliases.
269 */
270 public function getSpecialPageAliases( $code ) {
271 $this->loadFile( $code );
272 return $this->mSpecialPageAliases[$code];
273 }
274
275 /**
276 * Get the untranslated messages for a specific language.
277 *
278 * @param $code The language code.
279 *
280 * @return The untranslated messages for this language.
281 */
282 public function getUntranslatedMessages( $code ) {
283 $this->loadGeneralMessages();
284 $this->loadMessages( $code );
285 return array_diff_key( $this->mGeneralMessages['required'], $this->mMessages[$code]['required'] );
286 }
287
288 /**
289 * Get the duplicate messages for a specific language.
290 *
291 * @param $code The language code.
292 *
293 * @return The duplicate messages for this language.
294 */
295 public function getDuplicateMessages( $code ) {
296 $this->loadGeneralMessages();
297 $this->loadMessages( $code );
298 $duplicateMessages = array();
299 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
300 if ( $this->mGeneralMessages['translatable'][$key] == $value ) {
301 $duplicateMessages[$key] = $value;
302 }
303 }
304 return $duplicateMessages;
305 }
306
307 /**
308 * Get the obsolete messages for a specific language.
309 *
310 * @param $code The language code.
311 *
312 * @return The obsolete messages for this language.
313 */
314 public function getObsoleteMessages( $code ) {
315 $this->loadGeneralMessages();
316 $this->loadMessages( $code );
317 return $this->mMessages[$code]['obsolete'];
318 }
319
320 /**
321 * Get the messages whose variables do not match the original ones.
322 *
323 * @param $code The language code.
324 *
325 * @return The messages whose variables do not match the original ones.
326 */
327 public function getMessagesWithMismatchVariables( $code ) {
328 $this->loadGeneralMessages();
329 $this->loadMessages( $code );
330 $variables = array( '\$1', '\$2', '\$3', '\$4', '\$5', '\$6', '\$7', '\$8', '\$9' );
331 $mismatchMessages = array();
332 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
333 $missing = false;
334 foreach ( $variables as $var ) {
335 if ( preg_match( "/$var/sU", $this->mGeneralMessages['translatable'][$key] ) &&
336 !preg_match( "/$var/sU", $value ) ) {
337 $missing = true;
338 }
339 if ( !preg_match( "/$var/sU", $this->mGeneralMessages['translatable'][$key] ) &&
340 preg_match( "/$var/sU", $value ) ) {
341 $missing = true;
342 }
343 }
344 if ( $missing ) {
345 $mismatchMessages[$key] = $value;
346 }
347 }
348 return $mismatchMessages;
349 }
350
351 /**
352 * Get the messages which do not use plural.
353 *
354 * @param $code The language code.
355 *
356 * @return The messages which do not use plural in this language.
357 */
358 public function getMessagesWithoutPlural( $code ) {
359 $this->loadGeneralMessages();
360 $this->loadMessages( $code );
361 $messagesWithoutPlural = array();
362 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
363 if ( stripos( $this->mGeneralMessages['translatable'][$key], '{{plural:' ) !== false && stripos( $value, '{{plural:' ) === false ) {
364 $messagesWithoutPlural[$key] = $value;
365 }
366 }
367 return $messagesWithoutPlural;
368 }
369
370 /**
371 * Get the empty messages.
372 *
373 * @param $code The language code.
374 *
375 * @return The empty messages for this language.
376 */
377 public function getEmptyMessages( $code ) {
378 $this->loadGeneralMessages();
379 $this->loadMessages( $code );
380 $emptyMessages = array();
381 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
382 if ( $value === '' || $value === '-' ) {
383 $emptyMessages[$key] = $value;
384 }
385 }
386 return $emptyMessages;
387 }
388
389 /**
390 * Get the messages with trailing whitespace.
391 *
392 * @param $code The language code.
393 *
394 * @return The messages with trailing whitespace in this language.
395 */
396 public function getMessagesWithWhitespace( $code ) {
397 $this->loadGeneralMessages();
398 $this->loadMessages( $code );
399 $messagesWithWhitespace = array();
400 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
401 if ( $this->mGeneralMessages['translatable'][$key] !== '' && $value !== rtrim( $value ) ) {
402 $messagesWithWhitespace[$key] = $value;
403 }
404 }
405 return $messagesWithWhitespace;
406 }
407
408 /**
409 * Get the non-XHTML messages.
410 *
411 * @param $code The language code.
412 *
413 * @return The non-XHTML messages for this language.
414 */
415 public function getNonXHTMLMessages( $code ) {
416 $this->loadGeneralMessages();
417 $this->loadMessages( $code );
418 $wrongPhrases = array(
419 '<hr *\\?>',
420 '<br *\\?>',
421 '<hr/>',
422 '<br/>',
423 '<hr>',
424 '<br>',
425 );
426 $wrongPhrases = '~(' . implode( '|', $wrongPhrases ) . ')~sDu';
427 $nonXHTMLMessages = array();
428 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
429 if ( preg_match( $wrongPhrases, $value ) ) {
430 $nonXHTMLMessages[$key] = $value;
431 }
432 }
433 return $nonXHTMLMessages;
434 }
435
436 /**
437 * Get the messages which include wrong characters.
438 *
439 * @param $code The language code.
440 *
441 * @return The messages which include wrong characters in this language.
442 */
443 public function getMessagesWithWrongChars( $code ) {
444 $this->loadGeneralMessages();
445 $this->loadMessages( $code );
446 $wrongChars = array(
447 '[LRM]' => "\xE2\x80\x8E",
448 '[RLM]' => "\xE2\x80\x8F",
449 '[LRE]' => "\xE2\x80\xAA",
450 '[RLE]' => "\xE2\x80\xAB",
451 '[POP]' => "\xE2\x80\xAC",
452 '[LRO]' => "\xE2\x80\xAD",
453 '[RLO]' => "\xE2\x80\xAB",
454 '[ZWSP]'=> "\xE2\x80\x8B",
455 '[NBSP]'=> "\xC2\xA0",
456 '[WJ]' => "\xE2\x81\xA0",
457 '[BOM]' => "\xEF\xBB\xBF",
458 '[FFFD]'=> "\xEF\xBF\xBD",
459 );
460 $wrongRegExp = '/(' . implode( '|', array_values( $wrongChars ) ) . ')/sDu';
461 $wrongCharsMessages = array();
462 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
463 if ( preg_match( $wrongRegExp, $value ) ) {
464 foreach ( $wrongChars as $viewableChar => $hiddenChar ) {
465 $value = str_replace( $hiddenChar, $viewableChar, $value );
466 }
467 $wrongCharsMessages[$key] = $value;
468 }
469 }
470 return $wrongCharsMessages;
471 }
472
473 /**
474 * Get the messages which include dubious links.
475 *
476 * @param $code The language code.
477 *
478 * @return The messages which include dubious links in this language.
479 */
480 public function getMessagesWithDubiousLinks( $code ) {
481 $this->loadGeneralMessages();
482 $this->loadMessages( $code );
483 $tc = Title::legalChars() . '#%{}';
484 $messages = array();
485 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
486 $matches = array();
487 preg_match_all( "/\[\[([{$tc}]+)(?:\\|(.+?))?]]/sDu", $value, $matches );
488 for ($i = 0; $i < count($matches[0]); $i++ ) {
489 if ( preg_match( "/.*project.*/isDu", $matches[1][$i] ) ) {
490 $messages[$key][] = $matches[0][$i];
491 }
492 }
493
494
495 if ( isset( $messages[$key] ) ) {
496 $messages[$key] = implode( $messages[$key],", " );
497 }
498 }
499 return $messages;
500 }
501
502 /**
503 * Get the messages which include unbalanced brackets.
504 *
505 * @param $code The language code.
506 *
507 * @return The messages which include unbalanced brackets in this language.
508 */
509 public function getMessagesWithUnbalanced( $code ) {
510 $this->loadGeneralMessages();
511 $this->loadMessages( $code );
512 $messages = array();
513 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
514 $a = $b = $c = $d = 0;
515 foreach ( preg_split( '//', $value ) as $char ) {
516 switch ( $char ) {
517 case '[':
518 $a++;
519 break;
520 case ']':
521 $b++;
522 break;
523 case '{':
524 $c++;
525 break;
526 case '}':
527 $d++;
528 break;
529 }
530 }
531
532 if ( $a !== $b || $c !== $d ) {
533 $messages[$key] = "$a, $b, $c, $d";
534 }
535
536 }
537 return $messages;
538 }
539
540 /**
541 * Get the untranslated namespace names.
542 *
543 * @param $code The language code.
544 *
545 * @return The untranslated namespace names in this language.
546 */
547 public function getUntranslatedNamespaces( $code ) {
548 $this->loadFile( 'en' );
549 $this->loadFile( $code );
550 return array_flip( array_diff_key( $this->mNamespaceNames['en'], $this->mNamespaceNames[$code] ) );
551 }
552
553 /**
554 * Get the project talk namespace names with no $1.
555 *
556 * @param $code The language code.
557 *
558 * @return The problematic project talk namespaces in this language.
559 */
560 public function getProblematicProjectTalks( $code ) {
561 $this->loadFile( $code );
562 $namespaces = array();
563
564 # Check default namespace name
565 if( isset( $this->mNamespaceNames[$code][NS_PROJECT_TALK] ) ) {
566 $default = $this->mNamespaceNames[$code][NS_PROJECT_TALK];
567 if ( strpos( $default, '$1' ) === FALSE ) {
568 $namespaces[$default] = 'default';
569 }
570 }
571
572 # Check namespace aliases
573 foreach( $this->mNamespaceAliases[$code] as $key => $value ) {
574 if ( $value == NS_PROJECT_TALK && strpos( $key, '$1' ) === FALSE ) {
575 $namespaces[$key] = '';
576 }
577 }
578
579 return $namespaces;
580 }
581
582 /**
583 * Get the untranslated magic words.
584 *
585 * @param $code The language code.
586 *
587 * @return The untranslated magic words in this language.
588 */
589 public function getUntranslatedMagicWords( $code ) {
590 $this->loadFile( 'en' );
591 $this->loadFile( $code );
592 $magicWords = array();
593 foreach ( $this->mMagicWords['en'] as $key => $value ) {
594 if ( !isset( $this->mMagicWords[$code][$key] ) ) {
595 $magicWords[$key] = $value[1];
596 }
597 }
598 return $magicWords;
599 }
600
601 /**
602 * Get the obsolete magic words.
603 *
604 * @param $code The language code.
605 *
606 * @return The obsolete magic words in this language.
607 */
608 public function getObsoleteMagicWords( $code ) {
609 $this->loadFile( 'en' );
610 $this->loadFile( $code );
611 $magicWords = array();
612 foreach ( $this->mMagicWords[$code] as $key => $value ) {
613 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
614 $magicWords[$key] = $value[1];
615 }
616 }
617 return $magicWords;
618 }
619
620 /**
621 * Get the magic words that override the original English magic word.
622 *
623 * @param $code The language code.
624 *
625 * @return The overriding magic words in this language.
626 */
627 public function getOverridingMagicWords( $code ) {
628 $this->loadFile( 'en' );
629 $this->loadFile( $code );
630 $magicWords = array();
631 foreach ( $this->mMagicWords[$code] as $key => $local ) {
632 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
633 # Unrecognized magic word
634 continue;
635 }
636 $en = $this->mMagicWords['en'][$key];
637 array_shift( $local );
638 array_shift( $en );
639 foreach ( $en as $word ) {
640 if ( !in_array( $word, $local ) ) {
641 $magicWords[$key] = $word;
642 break;
643 }
644 }
645 }
646 return $magicWords;
647 }
648
649 /**
650 * Get the magic words which do not match the case-sensitivity of the original words.
651 *
652 * @param $code The language code.
653 *
654 * @return The magic words whose case does not match in this language.
655 */
656 public function getCaseMismatchMagicWords( $code ) {
657 $this->loadFile( 'en' );
658 $this->loadFile( $code );
659 $magicWords = array();
660 foreach ( $this->mMagicWords[$code] as $key => $local ) {
661 if ( !isset( $this->mMagicWords['en'][$key] ) ) {
662 # Unrecognized magic word
663 continue;
664 }
665 if ( $local[0] != $this->mMagicWords['en'][$key][0] ) {
666 $magicWords[$key] = $local[0];
667 }
668 }
669 return $magicWords;
670 }
671
672 /**
673 * Get the untranslated special page names.
674 *
675 * @param $code The language code.
676 *
677 * @return The untranslated special page names in this language.
678 */
679 public function getUntraslatedSpecialPages( $code ) {
680 $this->loadFile( 'en' );
681 $this->loadFile( $code );
682 $specialPageAliases = array();
683 foreach ( $this->mSpecialPageAliases['en'] as $key => $value ) {
684 if ( !isset( $this->mSpecialPageAliases[$code][$key] ) ) {
685 $specialPageAliases[$key] = $value[0];
686 }
687 }
688 return $specialPageAliases;
689 }
690
691 /**
692 * Get the obsolete special page names.
693 *
694 * @param $code The language code.
695 *
696 * @return The obsolete special page names in this language.
697 */
698 public function getObsoleteSpecialPages( $code ) {
699 $this->loadFile( 'en' );
700 $this->loadFile( $code );
701 $specialPageAliases = array();
702 foreach ( $this->mSpecialPageAliases[$code] as $key => $value ) {
703 if ( !isset( $this->mSpecialPageAliases['en'][$key] ) ) {
704 $specialPageAliases[$key] = $value[0];
705 }
706 }
707 return $specialPageAliases;
708 }
709 }
710
711 class extensionLanguages extends languages {
712 private $mMessageGroup; # The message group
713
714 /**
715 * Load the messages group.
716 * @param $group The messages group.
717 */
718 function __construct( MessageGroup $group ) {
719 $this->mMessageGroup = $group;
720
721 $bools = $this->mMessageGroup->getBools();
722 $this->mIgnoredMessages = $bools['ignored'];
723 $this->mOptionalMessages = $bools['optional'];
724 }
725
726 /**
727 * Get the extension name.
728 *
729 * @return The extension name.
730 */
731 public function name() {
732 return $this->mMessageGroup->getLabel();
733 }
734
735 /**
736 * Load the language file.
737 *
738 * @param $code The language code.
739 */
740 protected function loadFile( $code ) {
741 if( !isset( $this->mRawMessages[$code] ) ) {
742 $this->mRawMessages[$code] = $this->mMessageGroup->load( $code );
743 if( empty( $this->mRawMessages[$code] ) ) {
744 $this->mRawMessages[$code] = array();
745 }
746 }
747 }
748 }