3 * @ingroup MaintenanceLanguage
6 class CheckLanguageCLI {
7 protected $code = null;
9 protected $doLinks = false;
10 protected $wikiCode = 'en';
11 protected $checkAll = false;
12 protected $output = 'plain';
13 protected $checks = array();
16 protected $defaultChecks = array(
17 'untranslated', 'obsolete', 'variables', 'empty', 'plural',
18 'whitespace', 'xhtml', 'chars', 'links', 'unbalanced'
21 protected $results = array();
23 private $includeExif = false;
26 * GLOBALS: $wgLanguageCode;
28 public function __construct( Array $options ) {
30 if ( isset( $options['help'] ) ) {
35 if ( isset($options['lang']) ) {
36 $this->code = $options['lang'];
38 global $wgLanguageCode;
39 $this->code = $wgLanguageCode;
42 if ( isset($options['level']) ) {
43 $this->level = $options['level'];
46 $this->doLinks = isset($options['links']);
47 $this->includeExif = !isset($options['noexif']);
48 $this->checkAll = isset($options['all']);
50 if ( isset($options['wikilang']) ) {
51 $this->wikiCode = $options['wikilang'];
54 if ( isset( $options['whitelist'] ) ) {
55 $this->checks = explode( ',', $options['whitelist'] );
56 } elseif ( isset( $options['blacklist'] ) ) {
57 $this->checks = array_diff(
59 explode( ',', $options['blacklist'] )
62 $this->checks = $this->defaultChecks;
65 if ( isset($options['output']) ) {
66 $this->output = $options['output'];
69 # Some additional checks not enabled by default
70 if ( isset( $options['duplicate'] ) ) {
71 $this->checks[] = 'duplicate';
74 $this->L = new languages( $this->includeExif );
77 protected function getChecks() {
79 $checks['untranslated'] = 'getUntranslatedMessages';
80 $checks['duplicate'] = 'getDuplicateMessages';
81 $checks['obsolete'] = 'getObsoleteMessages';
82 $checks['variables'] = 'getMessagesWithoutVariables';
83 $checks['plural'] = 'getMessagesWithoutPlural';
84 $checks['empty'] = 'getEmptyMessages';
85 $checks['whitespace'] = 'getMessagesWithWhitespace';
86 $checks['xhtml'] = 'getNonXHTMLMessages';
87 $checks['chars'] = 'getMessagesWithWrongChars';
88 $checks['links'] = 'getMessagesWithDubiousLinks';
89 $checks['unbalanced'] = 'getMessagesWithUnbalanced';
93 protected function getDescriptions() {
94 $descriptions = array();
95 $descriptions['untranslated'] = '$1 message(s) of $2 are not translated to $3, but exist in en:';
96 $descriptions['duplicate'] = '$1 message(s) of $2 are translated the same in en and $3:';
97 $descriptions['obsolete'] = '$1 message(s) of $2 do not exist in en or are in the ignore list, but are in $3';
98 $descriptions['variables'] = '$1 message(s) of $2 in $3 don\'t use some variables that en uses:';
99 $descriptions['plural'] = '$1 message(s) of $2 in $3 don\'t use {{plural}} while en uses:';
100 $descriptions['empty'] = '$1 message(s) of $2 in $3 are empty or -:';
101 $descriptions['whitespace'] = '$1 message(s) of $2 in $3 have trailing whitespace:';
102 $descriptions['xhtml'] = '$1 message(s) of $2 in $3 contain illegal XHTML:';
103 $descriptions['chars'] = '$1 message(s) of $2 in $3 include hidden chars which should not be used in the messages:';
104 $descriptions['links'] = '$1 message(s) of $2 in $3 have problematic link(s):';
105 $descriptions['unbalanced'] = '$1 message(s) of $2 in $3 have unbalanced {[]}:';
106 return $descriptions;
109 protected function help() {
111 Run this script to check a specific language file, or all of them.
112 Command line settings are in form --parameter[=value].
114 * lang: Language code (default: the installation default language).
115 * all: Check all customized languages.
116 * help: Show this help.
117 * level: Show the following level (default: 2).
118 * links: Link the message values (default off).
119 * wikilang: For the links, what is the content language of the wiki to display the output in (default en).
120 * whitelist: Do only the following checks (form: code,code).
121 * blacklist: Don't do the following checks (form: code,code).
122 * duplicate: Additionally check for messages which are translated the same to English (default off).
123 * noexif: Don't check for EXIF messages (a bit hard and boring to translate), if you know that they are currently not translated and want to focus on other problems (default off).
124 Check codes (ideally, all of them should result 0; all the checks are executed by default (except duplicate and language specific check blacklists in checkLanguage.inc):
125 * untranslated: Messages which are required to translate, but are not translated.
126 * duplicate: Messages which translation equal to fallback
127 * obsolete: Messages which are untranslatable, but translated.
128 * variables: Messages without variables which should be used.
129 * empty: Empty messages.
130 * whitespace: Messages which have trailing whitespace.
131 * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
132 * chars: Messages with hidden characters.
133 * links: Messages which contains broken links to pages (does not find all).
134 * unbalanced: Messages which contains unequal numbers of opening {[ and closing ]}.
135 Display levels (default: 2):
136 * 0: Skip the checks (useful for checking syntax).
137 * 1: Show only the stub headers and number of wrong messages, without list of messages.
138 * 2: Show only the headers and the message keys, without the message values.
139 * 3: Show both the headers and the complete messages, with both keys and values.
144 public function execute() {
146 if ( $this->level > 0 ) {
147 switch ($this->output) {
155 throw new MWException( "Invalid output type $this->output");
160 protected function doChecks() {
161 $ignoredCodes = array( 'en', 'enRTL' );
163 $this->results = array();
165 if ( $this->checkAll ) {
166 foreach ( $this->L->getLanguages() as $language ) {
167 if ( !in_array($language, $ignoredCodes) ) {
168 $this->results[$language] = $this->checkLanguage( $language );
172 if ( in_array($this->code, $ignoredCodes) ) {
173 throw new MWException("Cannot check code $this->code.");
175 $this->results[$this->code] = $this->checkLanguage( $this->code );
180 protected function getCheckBlacklist() {
181 global $checkBlacklist;
182 return $checkBlacklist;
185 protected function checkLanguage( $code ) {
187 if ( $this->level === 0 ) {
188 $this->L->getMessages( $code );
193 $checkFunctions = $this->getChecks();
194 $checkBlacklist = $this->getCheckBlacklist();
195 foreach ( $this->checks as $check ) {
196 if ( isset($checkBlacklist[$code]) &&
197 in_array($check, $checkBlacklist[$code]) ) {
198 $result[$check] = array();
202 $callback = array( $this->L, $checkFunctions[$check] );
203 if ( !is_callable($callback ) ) {
204 throw new MWException( "Unkown check $check." );
206 $results[$check] = call_user_func( $callback , $code );
212 protected function formatKey( $key, $code ) {
213 if ( $this->doLinks ) {
214 $displayKey = ucfirst( $key );
215 if ( $code == $this->wikiCode ) {
216 return "[[MediaWiki:$displayKey|$key]]";
218 return "[[MediaWiki:$displayKey/$code|$key]]";
225 protected function outputText() {
226 foreach ( $this->results as $code => $results ) {
227 $translated = $this->L->getMessages( $code );
228 $translated = count( $translated['translated'] );
229 $translatable = $this->L->getGeneralMessages();
230 $translatable = count( $translatable['translatable'] );
231 foreach ( $results as $check => $messages ) {
232 $count = count( $messages );
234 $search = array( '$1', '$2', '$3' );
235 $replace = array( $count, $check == 'untranslated' ? $translatable: $translated, $code );
236 $descriptions = $this->getDescriptions();
237 echo "\n" . str_replace( $search, $replace, $descriptions[$check] ) . "\n";
238 if ( $this->level == 1 ) {
239 echo "[messages are hidden]\n";
241 foreach ( $messages as $key => $value ) {
242 $displayKey = $this->formatKey( $key, $code );
243 if ( $this->level == 2 ) {
244 echo "* $displayKey\n";
246 echo "* $displayKey: '$value'\n";
256 * Globals: $wgContLang, $IP
258 function outputWiki() {
259 global $wgContLang, $IP;
261 $rows[] = '! Language !! Code !! Total !! ' . implode( ' !! ', $this->checks );
262 foreach ( $this->results as $code => $results ) {
263 $detailTextForLang = "==$code==\n";
266 $detailTextForLangChecks = array();
267 foreach ( $results as $check => $messages ) {
268 $count = count( $messages );
271 $messageDetails = array();
272 foreach ( $messages as $key => $details ) {
273 $displayKey = $this->formatKey( $key, $code );
274 $messageDetails[] = $displayKey;
276 $detailTextForLangChecks[] = "===$code-$check===\n* " . implode( ', ', $messageDetails );
277 $numbers[] = "'''[[#$code-$check|$count]]'''";
284 if ( count( $detailTextForLangChecks ) ) {
285 $detailText .= $detailTextForLang . implode( "\n", $detailTextForLangChecks ) . "\n";
288 if ( !$problems ) { continue; } // Don't list languages without problems
289 $language = $wgContLang->getLanguageName( $code );
290 $rows[] = "| $language || $code || $problems || " . implode( ' || ', $numbers );
293 $tableRows = implode( "\n|-\n", $rows );
295 $version = SpecialVersion::getVersion( $IP );
297 '''Check results are for:''' <code>$version</code>
300 {| class="sortable wikitable" border="2" cellpadding="4" cellspacing="0" style="background-color: #F9F9F9; border: 1px #AAAAAA solid; border-collapse: collapse; clear:both;"
310 class CheckExtensionsCLI extends CheckLanguageCLI {
313 public function __construct( Array $options, $extension ) {
314 if ( isset( $options['help'] ) ) {
319 if ( isset($options['lang']) ) {
320 $this->code = $options['lang'];
322 global $wgLanguageCode;
323 $this->code = $wgLanguageCode;
326 if ( isset($options['level']) ) {
327 $this->level = $options['level'];
330 $this->doLinks = isset($options['links']);
332 if ( isset($options['wikilang']) ) {
333 $this->wikiCode = $options['wikilang'];
336 if ( isset( $options['whitelist'] ) ) {
337 $this->checks = explode( ',', $options['whitelist'] );
338 } elseif ( isset( $options['blacklist'] ) ) {
339 $this->checks = array_diff(
340 $this->defaultChecks,
341 explode( ',', $options['blacklist'] )
344 $this->checks = $this->defaultChecks;
347 if ( isset($options['output']) ) {
348 $this->output = $options['output'];
351 # Some additional checks not enabled by default
352 if ( isset( $options['duplicate'] ) ) {
353 $this->checks[] = 'duplicate';
356 if( $extension == 'all' ) {
357 $this->extensions = array();
358 foreach( MessageGroups::singleton()->getGroups() as $group ) {
359 if( strpos( $group->getId(), 'ext-' ) === 0 && !$group->isMeta() ) {
360 $this->extensions[] = new extensionLanguages( $group );
364 $group = MessageGroups::getGroup( 'ext-' . $extension );
366 $extension = new extensionLanguages( $group );
367 $this->extensions = array( $extension );
369 print "No such extension $extension.\n";
370 $this->extensions = array();
375 protected function help() {
377 Run this script to check the status of a specific language in extensions, or all of them.
378 Command line settings are in form --parameter[=value], except for the first one.
380 * First parameter (mandatory): Extension name, or "all" for all the extensions.
381 * lang: Language code (default: the installation default language).
382 * help: Show this help.
383 * level: Show the following level (default: 2).
384 * links: Link the message values (default off).
385 * wikilang: For the links, what is the content language of the wiki to display the output in (default en).
386 * whitelist: Do only the following checks (form: code,code).
387 * blacklist: Don't do the following checks (form: code,code).
388 * duplicate: Additionally check for messages which are translated the same to English (default off).
389 Check codes (ideally, all of them should result 0; all the checks are executed by default (except duplicate and language specific check blacklists in checkLanguage.inc):
390 * untranslated: Messages which are required to translate, but are not translated.
391 * duplicate: Messages which translation equal to fallback
392 * obsolete: Messages which are untranslatable, but translated.
393 * variables: Messages without variables which should be used.
394 * empty: Empty messages.
395 * whitespace: Messages which have trailing whitespace.
396 * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
397 * chars: Messages with hidden characters.
398 * links: Messages which contains broken links to pages (does not find all).
399 * unbalanced: Messages which contains unequal numbers of opening {[ and closing ]}.
400 Display levels (default: 2):
401 * 0: Skip the checks (useful for checking syntax).
402 * 1: Show only the stub headers and number of wrong messages, without list of messages.
403 * 2: Show only the headers and the message keys, without the message values.
404 * 3: Show both the headers and the complete messages, with both keys and values.
409 public function execute() {
413 protected function checkLanguage( $code ) {
414 foreach( $this->extensions as $extension ) {
415 echo $extension->name() . ":\n";
417 $this->L = $extension;
418 $this->results = array();
419 $this->results[$code] = parent::checkLanguage( $code );
421 if( $this->level > 0 ) {
422 switch( $this->output ) {
430 throw new MWException( "Invalid output type $this->output" );
439 # Blacklist some checks for some languages
440 $checkBlacklist = array(
441 #'code' => array( 'check1', 'check2' ... )
442 'gan' => array( 'plural' ),
443 'hak' => array( 'plural' ),
444 'ja' => array( 'plural' ), // Does not use plural
445 'ka' => array( 'plural' ),
446 'kk-arab' => array( 'plural' ),
447 'kk-cyrl' => array( 'plural' ),
448 'kk-latn' => array( 'plural' ),
449 'ko' => array( 'plural' ),
450 'mn' => array( 'plural' ),
451 'ms' => array( 'plural' ),
452 'my' => array( 'chars' ), // Uses a lot zwnj
453 'sq' => array( 'plural' ),
454 'tet' => array( 'plural' ),
455 'th' => array( 'plural' ),
456 'wuu' => array( 'plural' ),
457 'xmf' => array( 'plural' ),
458 'yue' => array( 'plural' ),
459 'zh' => array( 'plural' ),
460 'zh-classical' => array( 'plural' ),
461 'zh-cn' => array( 'plural' ),
462 'zh-hans' => array( 'plural' ),
463 'zh-hant' => array( 'plural' ),
464 'zh-hk' => array( 'plural' ),
465 'zh-sg' => array( 'plural' ),
466 'zh-tw' => array( 'plural' ),
467 'zh-yue' => array( 'plural' ),