PADLEFT/PADRIGHT:
[mediawiki.git] / maintenance / checkLanguage.php
blob2f8d23ad0a81bf5e05c8c8cf16db3f4ec8de850c
1 <?php
2 /**
3 * Check a language file.
5 * @package MediaWiki
6 * @subpackage Maintenance
7 */
9 require_once( 'commandLine.inc' );
10 require_once( 'languages.inc' );
12 /**
13 * Check a language.
15 * @param $code The language code.
17 function checkLanguage( $code ) {
18 global $wgLanguages, $wgDisplayLevel, $wgLinks, $wgChecks;
20 # Get messages number
21 $translatableMessagesNumber = count( $wgLanguages->getTranslatableMessages() );
22 $localMessagesNumber = count( $wgLanguages->getMessagesFor( $code ) );
24 # Skip the checks if specified
25 if ( $wgDisplayLevel == 0 ) {
26 return;
29 # Untranslated messages
30 if ( in_array( 'untranslated', $wgChecks ) ) {
31 $untranslatedMessages = $wgLanguages->getUntranslatedMessages( $code );
32 $untranslatedMessagesNumber = count( $untranslatedMessages );
33 $wgLanguages->outputMessagesList( $untranslatedMessages, $code, "\n$untranslatedMessagesNumber messages of $translatableMessagesNumber are not translated to $code, but exist in en:", $wgDisplayLevel, $wgLinks );
36 # Duplicate messages
37 if ( in_array( 'duplicate', $wgChecks ) ) {
38 $duplicateMessages = $wgLanguages->getDuplicateMessages( $code );
39 $duplicateMessagesNumber = count( $duplicateMessages );
40 $wgLanguages->outputMessagesList( $duplicateMessages, $code, "\n$duplicateMessagesNumber messages of $localMessagesNumber are translated the same in en and $code:", $wgDisplayLevel, $wgLinks );
43 # Obsolete messages
44 if ( in_array( 'obsolete', $wgChecks ) ) {
45 $obsoleteMessages = $wgLanguages->getObsoleteMessages( $code );
46 $obsoleteMessagesNumber = count( $obsoleteMessages );
47 $wgLanguages->outputMessagesList( $obsoleteMessages, $code, "\n$obsoleteMessagesNumber messages of $localMessagesNumber are not exist in en (or are in the ignored list), but still exist in $code:", $wgDisplayLevel, $wgLinks );
50 # Messages without variables
51 if ( in_array( 'variables', $wgChecks ) ) {
52 $messagesWithoutVariables = $wgLanguages->getMessagesWithoutVariables( $code );
53 $messagesWithoutVariablesNumber = count( $messagesWithoutVariables );
54 $wgLanguages->outputMessagesList( $messagesWithoutVariables, $code, "\n$messagesWithoutVariablesNumber messages of $localMessagesNumber in $code don't use some variables while en uses them:", $wgDisplayLevel, $wgLinks );
57 # Empty messages
58 if ( in_array( 'empty', $wgChecks ) ) {
59 $emptyMessages = $wgLanguages->getEmptyMessages( $code );
60 $emptyMessagesNumber = count( $emptyMessages );
61 $wgLanguages->outputMessagesList( $emptyMessages, $code, "\n$emptyMessagesNumber messages of $localMessagesNumber in $code are empty or -:", $wgDisplayLevel, $wgLinks );
64 # Messages with whitespace
65 if ( in_array( 'whitespace', $wgChecks ) ) {
66 $messagesWithWhitespace = $wgLanguages->getMessagesWithWhitespace( $code );
67 $messagesWithWhitespaceNumber = count( $messagesWithWhitespace );
68 $wgLanguages->outputMessagesList( $messagesWithWhitespace, $code, "\n$messagesWithWhitespaceNumber messages of $localMessagesNumber in $code have a trailing whitespace:", $wgDisplayLevel, $wgLinks );
71 # Non-XHTML messages
72 if ( in_array( 'xhtml', $wgChecks ) ) {
73 $nonXHTMLMessages = $wgLanguages->getNonXHTMLMessages( $code );
74 $nonXHTMLMessagesNumber = count( $nonXHTMLMessages );
75 $wgLanguages->outputMessagesList( $nonXHTMLMessages, $code, "\n$nonXHTMLMessagesNumber messages of $localMessagesNumber in $code are not well-formed XHTML:", $wgDisplayLevel, $wgLinks );
78 # Messages with wrong characters
79 if ( in_array( 'chars', $wgChecks ) ) {
80 $messagesWithWrongChars = $wgLanguages->getMessagesWithWrongChars( $code );
81 $messagesWithWrongCharsNumber = count( $messagesWithWrongChars );
82 $wgLanguages->outputMessagesList( $messagesWithWrongChars, $code, "\n$messagesWithWrongCharsNumber messages of $localMessagesNumber in $code include hidden chars which should not be used in the messages:", $wgDisplayLevel, $wgLinks );
86 # Show help
87 if ( isset( $options['help'] ) ) {
88 echo "Run this script to check a specific language file.\n";
89 echo "Parameters:\n";
90 echo "\t* lang: Language code (default: the installation default language). You can also specify \"all\" to check all the languages.\n";
91 echo "\t* help: Show help.\n";
92 echo "\t* level: Show the following level (default: 2).\n";
93 echo "\t* links: Link the message values (default off).\n";
94 echo "\t* whitelist: Make only the following checks (form: code,code).\n";
95 echo "\t* blacklist: Don't make the following checks (form: code,code).\n";
96 echo "\t* duplicate: Additionally check for messages which are translated the same to English (default off).\n";
97 echo "Check codes (ideally, should be zero; all the checks are executed by default):\n";
98 echo "\t* untranslated: Messages which are translatable, but not translated.\n";
99 echo "\t* obsolete: Messages which are untranslatable, but translated.\n";
100 echo "\t* variables: Messages without variables which should be used.\n";
101 echo "\t* empty: Empty messages.\n";
102 echo "\t* whitespace: Messages which have trailing whitespace.\n";
103 echo "\t* xhtml: Messages which are not well-formed XHTML.\n";
104 echo "\t* chars: Messages with hidden characters.\n";
105 echo "Display levels (default: 2):\n";
106 echo "\t* 0: Skip the checks (useful for checking syntax).\n";
107 echo "\t* 1: Show only the stub headers and number of wrong messages, without list of messages.\n";
108 echo "\t* 2: Show only the headers and the message keys, without the message values.\n";
109 echo "\t* 3: Show both the headers and the complete messages, with both keys and values.\n";
110 exit();
113 # Get the language code
114 if ( isset( $options['lang'] ) ) {
115 $wgCode = $options['lang'];
116 } else {
117 $wgCode = $wgContLang->getCode();
120 # Get the display level
121 if ( isset( $options['level'] ) ) {
122 $wgDisplayLevel = $options['level'];
123 } else {
124 $wgDisplayLevel = 2;
127 # Get the links option
128 $wgLinks = isset( $options['links'] );
130 # Get the checks to do
131 $wgChecks = array( 'untranslated', 'obsolete', 'variables', 'empty', 'whitespace', 'xhtml', 'chars' );
132 if ( isset( $options['whitelist'] ) ) {
133 $wgChecks = explode( ',', $options['whitelist'] );
134 } elseif ( isset( $options['blacklist'] ) ) {
135 $wgChecks = array_diff( $wgChecks, explode( ',', $options['blacklist'] ) );
138 # Add duplicate option if specified
139 if ( isset( $options['duplicate'] ) ) {
140 $wgChecks[] = 'duplicate';
143 # Get language objects
144 $wgLanguages = new languages();
146 # Check the language
147 if ( $wgCode == 'all' ) {
148 foreach ( $wgLanguages->getList() as $language ) {
149 if ( $language != 'en' && $language != 'enRTL' ) {
150 checkLanguage( $language );
153 } else {
154 # Can't check English
155 if ( $wgCode == 'en' ) {
156 echo "Current selected language is English, which cannot be checked.\n";
157 } else if ( $wgCode == 'enRTL' ) {
158 echo "Current selected language is RTL English, which cannot be checked.\n";
159 } else {
160 checkLanguage( $wgCode );