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', 'duplicate', '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 $this->L = new languages( $this->includeExif );
72 protected function getChecks() {
74 $checks['untranslated'] = 'getUntranslatedMessages';
75 $checks['duplicate'] = 'getDuplicateMessages';
76 $checks['obsolete'] = 'getObsoleteMessages';
77 $checks['variables'] = 'getMessagesWithoutVariables';
78 $checks['plural'] = 'getMessagesWithoutPlural';
79 $checks['empty'] = 'getEmptyMessages';
80 $checks['whitespace'] = 'getMessagesWithWhitespace';
81 $checks['xhtml'] = 'getNonXHTMLMessages';
82 $checks['chars'] = 'getMessagesWithWrongChars';
83 $checks['links'] = 'getMessagesWithDubiousLinks';
84 $checks['unbalanced'] = 'getMessagesWithUnbalanced';
88 protected function getDescriptions() {
89 $descriptions = array();
90 $descriptions['untranslated'] = '$1 message(s) of $2 are not translated to $3, but exist in en:';
91 $descriptions['duplicate'] = '$1 message(s) of $2 are translated the same in en and $3:';
92 $descriptions['obsolete'] = '$1 message(s) of $2 do not exist in en or are in the ignore list, but are in $3';
93 $descriptions['variables'] = '$1 message(s) of $2 in $3 don\'t use some variables that en uses:';
94 $descriptions['plural'] = '$1 message(s) of $2 in $3 don\'t use {{plural}} while en uses:';
95 $descriptions['empty'] = '$1 message(s) of $2 in $3 are empty or -:';
96 $descriptions['whitespace'] = '$1 message(s) of $2 in $3 have trailing whitespace:';
97 $descriptions['xhtml'] = '$1 message(s) of $2 in $3 contain illegal XHTML:';
98 $descriptions['chars'] = '$1 message(s) of $2 in $3 include hidden chars which should not be used in the messages:';
99 $descriptions['links'] = '$1 message(s) of $2 in $3 have problematic link(s):';
100 $descriptions['unbalanced'] = '$1 message(s) of $2 in $3 have unbalanced {[]}:';
101 return $descriptions;
104 protected function help() {
106 Run this script to check a specific language file, or all of them.
107 Command line settings are in form --parameter[=value].
109 * lang: Language code (default: the installation default language).
110 * all: Check all customized languages.
111 * help: Show this help.
112 * level: Show the following level (default: 2).
113 * links: Link the message values (default off).
114 * wikilang: For the links, what is the content language of the wiki to display the output in (default en).
115 * whitelist: Do only the following checks (form: code,code).
116 * blacklist: Don't do the following checks (form: code,code).
117 * 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).
118 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):
119 * untranslated: Messages which are required to translate, but are not translated.
120 * duplicate: Messages which translation equal to fallback
121 * obsolete: Messages which are untranslatable, but translated.
122 * variables: Messages without variables which should be used.
123 * empty: Empty messages.
124 * whitespace: Messages which have trailing whitespace.
125 * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
126 * chars: Messages with hidden characters.
127 * links: Messages which contains broken links to pages (does not find all).
128 * unbalanced: Messages which contains unequal numbers of opening {[ and closing ]}.
129 Display levels (default: 2):
130 * 0: Skip the checks (useful for checking syntax).
131 * 1: Show only the stub headers and number of wrong messages, without list of messages.
132 * 2: Show only the headers and the message keys, without the message values.
133 * 3: Show both the headers and the complete messages, with both keys and values.
138 public function execute() {
140 if ( $this->level > 0 ) {
141 switch ($this->output) {
149 throw new MWException( "Invalid output type $this->output");
154 protected function doChecks() {
155 $ignoredCodes = array( 'en', 'enRTL' );
157 $this->results = array();
159 if ( $this->checkAll ) {
160 foreach ( $this->L->getLanguages() as $language ) {
161 if ( !in_array($language, $ignoredCodes) ) {
162 $this->results[$language] = $this->checkLanguage( $language );
166 if ( in_array($this->code, $ignoredCodes) ) {
167 throw new MWException("Cannot check code $this->code.");
169 $this->results[$this->code] = $this->checkLanguage( $this->code );
174 protected function getCheckBlacklist() {
175 global $checkBlacklist;
176 return $checkBlacklist;
179 protected function checkLanguage( $code ) {
181 if ( $this->level === 0 ) {
182 $this->L->getMessages( $code );
187 $checkFunctions = $this->getChecks();
188 $checkBlacklist = $this->getCheckBlacklist();
189 foreach ( $this->checks as $check ) {
190 if ( isset($checkBlacklist[$code]) &&
191 in_array($check, $checkBlacklist[$code]) ) {
192 $result[$check] = array();
196 $callback = array( $this->L, $checkFunctions[$check] );
197 if ( !is_callable($callback ) ) {
198 throw new MWException( "Unkown check $check." );
200 $results[$check] = call_user_func( $callback , $code );
206 protected function formatKey( $key, $code ) {
207 if ( $this->doLinks ) {
208 $displayKey = ucfirst( $key );
209 if ( $code == $this->wikiCode ) {
210 return "[[MediaWiki:$displayKey|$key]]";
212 return "[[MediaWiki:$displayKey/$code|$key]]";
219 protected function outputText() {
220 foreach ( $this->results as $code => $results ) {
221 $translated = $this->L->getMessages( $code );
222 $translated = count( $translated['translated'] );
223 $translatable = $this->L->getGeneralMessages();
224 $translatable = count( $translatable['translatable'] );
225 foreach ( $results as $check => $messages ) {
226 $count = count( $messages );
228 $search = array( '$1', '$2', '$3' );
229 $replace = array( $count, $check == 'untranslated' ? $translatable: $translated, $code );
230 $descriptions = $this->getDescriptions();
231 echo "\n" . str_replace( $search, $replace, $descriptions[$check] ) . "\n";
232 if ( $this->level == 1 ) {
233 echo "[messages are hidden]\n";
235 foreach ( $messages as $key => $value ) {
236 $displayKey = $this->formatKey( $key, $code );
237 if ( $this->level == 2 ) {
238 echo "* $displayKey\n";
240 echo "* $displayKey: '$value'\n";
250 * Globals: $wgContLang, $IP
252 function outputWiki() {
253 global $wgContLang, $IP;
255 $rows[] = '! Language !! Code !! Total !! ' . implode( ' !! ', $this->checks );
256 foreach ( $this->results as $code => $results ) {
257 $detailTextForLang = "==$code==\n";
260 $detailTextForLangChecks = array();
261 foreach ( $results as $check => $messages ) {
262 $count = count( $messages );
265 $messageDetails = array();
266 foreach ( $messages as $key => $details ) {
267 $displayKey = $this->formatKey( $key, $code );
268 $messageDetails[] = $displayKey;
270 $detailTextForLangChecks[] = "===$code-$check===\n* " . implode( ', ', $messageDetails );
271 $numbers[] = "'''[[#$code-$check|$count]]'''";
278 if ( count( $detailTextForLangChecks ) ) {
279 $detailText .= $detailTextForLang . implode( "\n", $detailTextForLangChecks ) . "\n";
282 if ( !$problems ) { continue; } // Don't list languages without problems
283 $language = $wgContLang->getLanguageName( $code );
284 $rows[] = "| $language || $code || $problems || " . implode( ' || ', $numbers );
287 $tableRows = implode( "\n|-\n", $rows );
289 $version = SpecialVersion::getVersion( $IP );
291 '''Check results are for:''' <code>$version</code>
294 {| class="sortable wikitable" border="2" cellpadding="4" cellspacing="0" style="background-color: #F9F9F9; border: 1px #AAAAAA solid; border-collapse: collapse; clear:both;"
303 protected function isEmpty() {
305 foreach( $this->results as $code => $results ) {
306 foreach( $results as $check => $messages ) {
307 if( !empty( $messages ) ) {
320 class CheckExtensionsCLI extends CheckLanguageCLI {
323 public function __construct( Array $options, $extension ) {
324 if ( isset( $options['help'] ) ) {
329 if ( isset($options['lang']) ) {
330 $this->code = $options['lang'];
332 global $wgLanguageCode;
333 $this->code = $wgLanguageCode;
336 if ( isset($options['level']) ) {
337 $this->level = $options['level'];
340 $this->doLinks = isset($options['links']);
342 if ( isset($options['wikilang']) ) {
343 $this->wikiCode = $options['wikilang'];
346 if ( isset( $options['whitelist'] ) ) {
347 $this->checks = explode( ',', $options['whitelist'] );
348 } elseif ( isset( $options['blacklist'] ) ) {
349 $this->checks = array_diff(
350 $this->defaultChecks,
351 explode( ',', $options['blacklist'] )
354 $this->checks = $this->defaultChecks;
357 if ( isset($options['output']) ) {
358 $this->output = $options['output'];
361 # Some additional checks not enabled by default
362 if ( isset( $options['duplicate'] ) ) {
363 $this->checks[] = 'duplicate';
366 $this->extensions = array();
367 $extensions = new PremadeMediawikiExtensionGroups();
368 $extensions->addAll();
369 if( $extension == 'all' ) {
370 foreach( MessageGroups::singleton()->getGroups() as $group ) {
371 if( strpos( $group->getId(), 'ext-' ) === 0 && !$group->isMeta() ) {
372 $this->extensions[] = new extensionLanguages( $group );
375 } elseif( $extension == 'wikimedia' ) {
376 $wikimedia = MessageGroups::getGroup( 'ext-0-wikimedia' );
377 foreach( $wikimedia->wmfextensions() as $extension ) {
378 $group = MessageGroups::getGroup( $extension );
379 $this->extensions[] = new extensionLanguages( $group );
382 $extensions = explode( ',', $extension );
383 foreach( $extensions as $extension ) {
384 $group = MessageGroups::getGroup( 'ext-' . $extension );
386 $extension = new extensionLanguages( $group );
387 $this->extensions[] = $extension;
389 print "No such extension $extension.\n";
395 protected function help() {
397 Run this script to check the status of a specific language in extensions, or all of them.
398 Command line settings are in form --parameter[=value], except for the first one.
400 * First parameter (mandatory): Extension name, multiple extension names (separated by commas), "all" for all the extensions or "wikimedia" for extensions used by Wikimedia.
401 * lang: Language code (default: the installation default language).
402 * help: Show this help.
403 * level: Show the following level (default: 2).
404 * links: Link the message values (default off).
405 * wikilang: For the links, what is the content language of the wiki to display the output in (default en).
406 * whitelist: Do only the following checks (form: code,code).
407 * blacklist: Do not perform the following checks (form: code,code).
408 * duplicate: Additionally check for messages which are translated the same to English (default off).
409 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):
410 * untranslated: Messages which are required to translate, but are not translated.
411 * duplicate: Messages which translation equal to fallback
412 * obsolete: Messages which are untranslatable, but translated.
413 * variables: Messages without variables which should be used.
414 * empty: Empty messages.
415 * whitespace: Messages which have trailing whitespace.
416 * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
417 * chars: Messages with hidden characters.
418 * links: Messages which contains broken links to pages (does not find all).
419 * unbalanced: Messages which contains unequal numbers of opening {[ and closing ]}.
420 Display levels (default: 2):
421 * 0: Skip the checks (useful for checking syntax).
422 * 1: Show only the stub headers and number of wrong messages, without list of messages.
423 * 2: Show only the headers and the message keys, without the message values.
424 * 3: Show both the headers and the complete messages, with both keys and values.
429 public function execute() {
433 protected function checkLanguage( $code ) {
434 foreach( $this->extensions as $extension ) {
435 $this->L = $extension;
436 $this->results = array();
437 $this->results[$code] = parent::checkLanguage( $code );
439 if( !$this->isEmpty() ) {
440 echo $extension->name() . ":\n";
442 if( $this->level > 0 ) {
443 switch( $this->output ) {
451 throw new MWException( "Invalid output type $this->output" );
461 # Blacklist some checks for some languages
462 $checkBlacklist = array(
463 #'code' => array( 'check1', 'check2' ... )
464 'gan' => array( 'plural' ),
465 'gn' => array( 'plural' ),
466 'hak' => array( 'plural' ),
467 'hu' => array( 'plural' ),
468 'ja' => array( 'plural' ), // Does not use plural
469 'ka' => array( 'plural' ),
470 'kk-arab' => array( 'plural' ),
471 'kk-cyrl' => array( 'plural' ),
472 'kk-latn' => array( 'plural' ),
473 'ko' => array( 'plural' ),
474 'mn' => array( 'plural' ),
475 'ms' => array( 'plural' ),
476 'my' => array( 'chars' ), // Uses a lot zwnj
477 'sah' => array( 'plural' ),
478 'sq' => array( 'plural' ),
479 'tet' => array( 'plural' ),
480 'th' => array( 'plural' ),
481 'wuu' => array( 'plural' ),
482 'xmf' => array( 'plural' ),
483 'yue' => array( 'plural' ),
484 'zh' => array( 'plural' ),
485 'zh-classical' => array( 'plural' ),
486 'zh-cn' => array( 'plural' ),
487 'zh-hans' => array( 'plural' ),
488 'zh-hant' => array( 'plural' ),
489 'zh-hk' => array( 'plural' ),
490 'zh-sg' => array( 'plural' ),
491 'zh-tw' => array( 'plural' ),
492 'zh-yue' => array( 'plural' ),