66ebf82d0d25aab74d69c115940d3324dac3dfec
[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 $mGeneralMessages; # General messages in English, divided to groups
18 protected $mIgnoredMessages; # All the messages which should be exist only in the English file
19 protected $mOptionalMessages; # All the messages which may be translated or not, depending on the language
20
21 protected $mNamespaceNames; # Namespace names
22 protected $mNamespaceAliases; # Namespace aliases
23 protected $mSkinNames; # Skin names
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->mNamespaceNames[$code] ) &&
81 isset( $this->mNamespaceAliases[$code] ) &&
82 isset( $this->mSkinNames[$code] ) &&
83 isset( $this->mMagicWords[$code] ) &&
84 isset( $this->mSpecialPageAliases[$code] ) ) {
85 return;
86 }
87 $this->mRawMessages[$code] = array();
88 $this->mNamespaceNames[$code] = array();
89 $this->mNamespaceAliases[$code] = array();
90 $this->mSkinNames[$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( $namespaceNames ) ) {
100 $this->mNamespaceNames[$code] = $namespaceNames;
101 }
102 if ( isset( $namespaceAliases ) ) {
103 $this->mNamespaceAliases[$code] = $namespaceAliases;
104 }
105 if ( isset( $skinNames ) ) {
106 $this->mSkinNames[$code] = $skinNames;
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 namespace names for a specific language.
217 *
218 * @param $code The language code.
219 *
220 * @return Namespace names.
221 */
222 public function getNamespaceNames( $code ) {
223 $this->loadFile( $code );
224 return $this->mNamespaceNames[$code];
225 }
226
227 /**
228 * Get namespace aliases for a specific language.
229 *
230 * @param $code The language code.
231 *
232 * @return Namespace aliases.
233 */
234 public function getNamespaceAliases( $code ) {
235 $this->loadFile( $code );
236 return $this->mNamespaceAliases[$code];
237 }
238
239 /**
240 * Get skin names for a specific language.
241 *
242 * @param $code The language code.
243 *
244 * @return Skin names.
245 */
246 public function getSkinNames( $code ) {
247 $this->loadFile( $code );
248 return $this->mSkinNames[$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 which do not use some variables.
322 *
323 * @param $code The language code.
324 *
325 * @return The messages which do not use some variables in this language.
326 */
327 public function getMessagesWithoutVariables( $code ) {
328 $this->loadGeneralMessages();
329 $this->loadMessages( $code );
330 $variables = array( '\$1', '\$2', '\$3', '\$4', '\$5', '\$6', '\$7', '\$8', '\$9' );
331 $messagesWithoutVariables = 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 }
340 if ( $missing ) {
341 $messagesWithoutVariables[$key] = $value;
342 }
343 }
344 return $messagesWithoutVariables;
345 }
346
347 /**
348 * Get the messages which do not use plural.
349 *
350 * @param $code The language code.
351 *
352 * @return The messages which do not use plural in this language.
353 */
354 public function getMessagesWithoutPlural( $code ) {
355 $this->loadGeneralMessages();
356 $this->loadMessages( $code );
357 $messagesWithoutPlural = array();
358 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
359 if ( stripos( $this->mGeneralMessages['translatable'][$key], '{{plural:' ) !== false && stripos( $value, '{{plural:' ) === false ) {
360 $messagesWithoutPlural[$key] = $value;
361 }
362 }
363 return $messagesWithoutPlural;
364 }
365
366 /**
367 * Get the empty messages.
368 *
369 * @param $code The language code.
370 *
371 * @return The empty messages for this language.
372 */
373 public function getEmptyMessages( $code ) {
374 $this->loadGeneralMessages();
375 $this->loadMessages( $code );
376 $emptyMessages = array();
377 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
378 if ( $value === '' || $value === '-' ) {
379 $emptyMessages[$key] = $value;
380 }
381 }
382 return $emptyMessages;
383 }
384
385 /**
386 * Get the messages with trailing whitespace.
387 *
388 * @param $code The language code.
389 *
390 * @return The messages with trailing whitespace in this language.
391 */
392 public function getMessagesWithWhitespace( $code ) {
393 $this->loadGeneralMessages();
394 $this->loadMessages( $code );
395 $messagesWithWhitespace = array();
396 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
397 if ( $this->mGeneralMessages['translatable'][$key] !== '' && $value !== rtrim( $value ) ) {
398 $messagesWithWhitespace[$key] = $value;
399 }
400 }
401 return $messagesWithWhitespace;
402 }
403
404 /**
405 * Get the non-XHTML messages.
406 *
407 * @param $code The language code.
408 *
409 * @return The non-XHTML messages for this language.
410 */
411 public function getNonXHTMLMessages( $code ) {
412 $this->loadGeneralMessages();
413 $this->loadMessages( $code );
414 $wrongPhrases = array(
415 '<hr *\\?>',
416 '<br *\\?>',
417 '<hr/>',
418 '<br/>',
419 '<hr>',
420 '<br>',
421 );
422 $wrongPhrases = '~(' . implode( '|', $wrongPhrases ) . ')~sDu';
423 $nonXHTMLMessages = array();
424 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
425 if ( preg_match( $wrongPhrases, $value ) ) {
426 $nonXHTMLMessages[$key] = $value;
427 }
428 }
429 return $nonXHTMLMessages;
430 }
431
432 /**
433 * Get the messages which include wrong characters.
434 *
435 * @param $code The language code.
436 *
437 * @return The messages which include wrong characters in this language.
438 */
439 public function getMessagesWithWrongChars( $code ) {
440 $this->loadGeneralMessages();
441 $this->loadMessages( $code );
442 $wrongChars = array(
443 '[LRM]' => "\xE2\x80\x8E",
444 '[RLM]' => "\xE2\x80\x8F",
445 '[LRE]' => "\xE2\x80\xAA",
446 '[RLE]' => "\xE2\x80\xAB",
447 '[POP]' => "\xE2\x80\xAC",
448 '[LRO]' => "\xE2\x80\xAD",
449 '[RLO]' => "\xE2\x80\xAB",
450 '[ZWSP]'=> "\xE2\x80\x8B",
451 '[NBSP]'=> "\xC2\xA0",
452 '[WJ]' => "\xE2\x81\xA0",
453 '[BOM]' => "\xEF\xBB\xBF",
454 '[FFFD]'=> "\xEF\xBF\xBD",
455 );
456 $wrongRegExp = '/(' . implode( '|', array_values( $wrongChars ) ) . ')/sDu';
457 $wrongCharsMessages = array();
458 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
459 if ( preg_match( $wrongRegExp, $value ) ) {
460 foreach ( $wrongChars as $viewableChar => $hiddenChar ) {
461 $value = str_replace( $hiddenChar, $viewableChar, $value );
462 }
463 $wrongCharsMessages[$key] = $value;
464 }
465 }
466 return $wrongCharsMessages;
467 }
468
469 /**
470 * Get the messages which include dubious links.
471 *
472 * @param $code The language code.
473 *
474 * @return The messages which include dubious links in this language.
475 */
476 public function getMessagesWithDubiousLinks( $code ) {
477 $this->loadGeneralMessages();
478 $this->loadMessages( $code );
479 $tc = Title::legalChars() . '#%{}';
480 $messages = array();
481 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
482 $matches = array();
483 preg_match_all( "/\[\[([{$tc}]+)(?:\\|(.+?))?]]/sDu", $value, $matches );
484 for ($i = 0; $i < count($matches[0]); $i++ ) {
485 if ( preg_match( "/.*project.*/isDu", $matches[1][$i] ) ) {
486 $messages[$key][] = $matches[0][$i];
487 }
488 }
489
490
491 if ( isset( $messages[$key] ) ) {
492 $messages[$key] = implode( $messages[$key],", " );
493 }
494 }
495 return $messages;
496 }
497
498 /**
499 * Get the messages which include unbalanced brackets.
500 *
501 * @param $code The language code.
502 *
503 * @return The messages which include unbalanced brackets in this language.
504 */
505 public function getMessagesWithUnbalanced( $code ) {
506 $this->loadGeneralMessages();
507 $this->loadMessages( $code );
508 $messages = array();
509 foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
510 $a = $b = $c = $d = 0;
511 foreach ( preg_split( '//', $value ) as $char ) {
512 switch ( $char ) {
513 case '[':
514 $a++;
515 break;
516 case ']':
517 $b++;
518 break;
519 case '{':
520 $c++;
521 break;
522 case '}':
523 $d++;
524 break;
525 }
526 }
527
528 if ( $a !== $b || $c !== $d ) {
529 $messages[$key] = "$a, $b, $c, $d";
530 }
531
532 }
533 return $messages;
534 }
535
536 /**
537 * Get the untranslated namespace names.
538 *
539 * @param $code The language code.
540 *
541 * @return The untranslated namespace names in this language.
542 */
543 public function getUntranslatedNamespaces( $code ) {
544 $this->loadFile( 'en' );
545 $this->loadFile( $code );
546 return array_flip( array_diff_key( $this->mNamespaceNames['en'], $this->mNamespaceNames[$code] ) );
547 }
548
549 /**
550 * Get the project talk namespace names with no $1.
551 *
552 * @param $code The language code.
553 *
554 * @return The problematic project talk namespaces in this language.
555 */
556 public function getProblematicProjectTalks( $code ) {
557 $this->loadFile( $code );
558 $namespaces = array();
559
560 # Check default namespace name
561 if( isset( $this->mNamespaceNames[$code][NS_PROJECT_TALK] ) ) {
562 $default = $this->mNamespaceNames[$code][NS_PROJECT_TALK];
563 if ( strpos( $default, '$1' ) === FALSE ) {
564 $namespaces[$default] = 'default';
565 }
566 }
567
568 # Check namespace aliases
569 foreach( $this->mNamespaceAliases[$code] as $key => $value ) {
570 if ( $value == NS_PROJECT_TALK && strpos( $key, '$1' ) === FALSE ) {
571 $namespaces[$key] = '';
572 }
573 }
574
575 return $namespaces;
576 }
577
578 /**
579 * Get the untranslated skin names.
580 *
581 * @param $code The language code.
582 *
583 * @return The untranslated skin names in this language.
584 */
585 public function getUntranslatedSkins( $code ) {
586 $this->loadFile( 'en' );
587 $this->loadFile( $code );
588 return array_diff_key( $this->mSkinNames['en'], $this->mSkinNames[$code] );
589 }
590
591 /**
592 * Get the untranslated magic words.
593 *
594 * @param $code The language code.
595 *
596 * @return The untranslated magic words in this language.
597 */
598 public function getUntranslatedMagicWords( $code ) {
599 $this->loadFile( 'en' );
600 $this->loadFile( $code );
601 $magicWords = array();
602 foreach ( $this->mMagicWords['en'] as $key => $value ) {
603 if ( !isset( $this->mMagicWords[$code][$key] ) ) {
604 $magicWords[$key] = $value[1];
605 }
606 }
607 return $magicWords;
608 }
609
610 /**
611 * Get the magic words that override the original English magic word.
612 *
613 * @param $code The language code.
614 *
615 * @return The overriding magic words in this language.
616 */
617 public function getOverridingMagicWords( $code ) {
618 $this->loadFile( 'en' );
619 $this->loadFile( $code );
620 $magicWords = array();
621 foreach ( $this->mMagicWords[$code] as $key => $local ) {
622 $en = $this->mMagicWords['en'][$key];
623 array_shift( $local );
624 array_shift( $en );
625 foreach ( $en as $word ) {
626 if ( !in_array( $word, $local ) ) {
627 $magicWords[$key] = $word;
628 break;
629 }
630 }
631 }
632 return $magicWords;
633 }
634
635 /**
636 * Get the magic words which do not match the case-sensitivity of the original words.
637 *
638 * @param $code The language code.
639 *
640 * @return The magic words whose case does not match in this language.
641 */
642 public function getCaseMismatchMagicWords( $code ) {
643 $this->loadFile( 'en' );
644 $this->loadFile( $code );
645 $magicWords = array();
646 foreach ( $this->mMagicWords[$code] as $key => $local ) {
647 if ( $local[0] != $this->mMagicWords['en'][$key][0] ) {
648 $magicWords[$key] = $local[0];
649 }
650 }
651 return $magicWords;
652 }
653
654 /**
655 * Get the untranslated special page names.
656 *
657 * @param $code The language code.
658 *
659 * @return The untranslated special page names in this language.
660 */
661 public function getUntraslatedSpecialPages( $code ) {
662 $this->loadFile( 'en' );
663 $this->loadFile( $code );
664 $specialPageAliases = array();
665 foreach ( $this->mSpecialPageAliases['en'] as $key => $value ) {
666 if ( !isset( $this->mSpecialPageAliases[$code][$key] ) ) {
667 $specialPageAliases[$key] = $value[0];
668 }
669 }
670 return $specialPageAliases;
671 }
672 }
673
674 class extensionLanguages extends languages {
675 private $mMessageGroup; # The message group
676
677 /**
678 * Load the messages group.
679 * @param $group The messages group.
680 */
681 function __construct( MessageGroup $group ) {
682 $this->mMessageGroup = $group;
683
684 $bools = $this->mMessageGroup->getBools();
685 $this->mIgnoredMessages = $bools['ignored'];
686 $this->mOptionalMessages = $bools['optional'];
687 }
688
689 /**
690 * Get the extension name.
691 *
692 * @return The extension name.
693 */
694 public function name() {
695 return $this->mMessageGroup->getLabel();
696 }
697
698 /**
699 * Load the language file.
700 *
701 * @param $code The language code.
702 */
703 protected function loadFile( $code ) {
704 if( !isset( $this->mRawMessages[$code] ) ) {
705 $this->mRawMessages[$code] = $this->mMessageGroup->load( $code );
706 if( empty( $this->mRawMessages[$code] ) ) {
707 $this->mRawMessages[$code] = array();
708 }
709 }
710 }
711 }