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