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 $results = array();
18 private $includeExif = false;
22 * @param $options Options for script.
24 public function __construct( Array $options ) {
25 if ( isset( $options['help'] ) ) {
30 if ( isset( $options['lang'] ) ) {
31 $this->code = $options['lang'];
33 global $wgLanguageCode;
34 $this->code = $wgLanguageCode;
37 if ( isset( $options['level'] ) ) {
38 $this->level = $options['level'];
41 $this->doLinks = isset( $options['links'] );
42 $this->includeExif = !isset( $options['noexif'] );
43 $this->checkAll = isset( $options['all'] );
45 if ( isset( $options['wikilang'] ) ) {
46 $this->wikiCode = $options['wikilang'];
49 if ( isset( $options['whitelist'] ) ) {
50 $this->checks = explode( ',', $options['whitelist'] );
51 } elseif ( isset( $options['blacklist'] ) ) {
52 $this->checks = array_diff(
53 isset( $options['easy'] ) ? $this->easyChecks() : $this->defaultChecks(),
54 explode( ',', $options['blacklist'] )
56 } elseif ( isset( $options['easy'] ) ) {
57 $this->checks = $this->easyChecks();
59 $this->checks = $this->defaultChecks();
62 if ( isset( $options['output'] ) ) {
63 $this->output = $options['output'];
66 $this->L = new languages( $this->includeExif );
70 * Get the default checks.
71 * @return A list of the default checks.
73 protected function defaultChecks() {
75 'untranslated', 'duplicate', 'obsolete', 'variables', 'empty', 'plural',
76 'whitespace', 'xhtml', 'chars', 'links', 'unbalanced', 'namespace',
77 'projecttalk', 'magic', 'magic-old', 'magic-over', 'magic-case',
78 'special', 'special-old',
83 * Get the checks which check other things than messages.
84 * @return A list of the non-message checks.
86 protected function nonMessageChecks() {
88 'namespace', 'projecttalk', 'magic', 'magic-old', 'magic-over',
89 'magic-case', 'special', 'special-old',
94 * Get the checks that can easily be treated by non-speakers of the language.
95 * @return A list of the easy checks.
97 protected function easyChecks() {
99 'duplicate', 'obsolete', 'empty', 'whitespace', 'xhtml', 'chars', 'magic-old',
100 'magic-over', 'magic-case', 'special-old',
106 * @return An array of all check names mapped to their function names.
108 protected function getChecks() {
110 'untranslated' => 'getUntranslatedMessages',
111 'duplicate' => 'getDuplicateMessages',
112 'obsolete' => 'getObsoleteMessages',
113 'variables' => 'getMessagesWithMismatchVariables',
114 'plural' => 'getMessagesWithoutPlural',
115 'empty' => 'getEmptyMessages',
116 'whitespace' => 'getMessagesWithWhitespace',
117 'xhtml' => 'getNonXHTMLMessages',
118 'chars' => 'getMessagesWithWrongChars',
119 'links' => 'getMessagesWithDubiousLinks',
120 'unbalanced' => 'getMessagesWithUnbalanced',
121 'namespace' => 'getUntranslatedNamespaces',
122 'projecttalk' => 'getProblematicProjectTalks',
123 'magic' => 'getUntranslatedMagicWords',
124 'magic-old' => 'getObsoleteMagicWords',
125 'magic-over' => 'getOverridingMagicWords',
126 'magic-case' => 'getCaseMismatchMagicWords',
127 'special' => 'getUntraslatedSpecialPages',
128 'special-old' => 'getObsoleteSpecialPages',
133 * Get total count for each check non-messages check.
134 * @return An array of all check names mapped to a two-element array:
135 * function name to get the total count and language code or null
138 protected function getTotalCount() {
140 'namespace' => array( 'getNamespaceNames', 'en' ),
141 'projecttalk' => null,
142 'magic' => array( 'getMagicWords', 'en' ),
143 'magic-old' => array( 'getMagicWords', null ),
144 'magic-over' => array( 'getMagicWords', null ),
145 'magic-case' => array( 'getMagicWords', null ),
146 'special' => array( 'getSpecialPageAliases', 'en' ),
147 'special-old' => array( 'getSpecialPageAliases', null ),
152 * Get all check descriptions.
153 * @return An array of all check names mapped to their descriptions.
155 protected function getDescriptions() {
157 'untranslated' => '$1 message(s) of $2 are not translated to $3, but exist in en:',
158 'duplicate' => '$1 message(s) of $2 are translated the same in en and $3:',
159 'obsolete' => '$1 message(s) of $2 do not exist in en or are in the ignore list, but exist in $3:',
160 'variables' => '$1 message(s) of $2 in $3 don\'t match the variables used in en:',
161 'plural' => '$1 message(s) of $2 in $3 don\'t use {{plural}} while en uses:',
162 'empty' => '$1 message(s) of $2 in $3 are empty or -:',
163 'whitespace' => '$1 message(s) of $2 in $3 have trailing whitespace:',
164 'xhtml' => '$1 message(s) of $2 in $3 contain illegal XHTML:',
165 'chars' => '$1 message(s) of $2 in $3 include hidden chars which should not be used in the messages:',
166 'links' => '$1 message(s) of $2 in $3 have problematic link(s):',
167 'unbalanced' => '$1 message(s) of $2 in $3 have unbalanced {[]}:',
168 'namespace' => '$1 namespace name(s) of $2 are not translated to $3, but exist in en:',
169 'projecttalk' => '$1 namespace name(s) and alias(es) in $3 are project talk namespaces without the parameter:',
170 'magic' => '$1 magic word(s) of $2 are not translated to $3, but exist in en:',
171 'magic-old' => '$1 magic word(s) of $2 do not exist in en, but exist in $3:',
172 'magic-over' => '$1 magic word(s) of $2 in $3 do not contain the original en word(s):',
173 'magic-case' => '$1 magic word(s) of $2 in $3 change the case-sensitivity of the original en word:',
174 'special' => '$1 special page alias(es) of $2 are not translated to $3, but exist in en:',
175 'special-old' => '$1 special page alias(es) of $2 do not exist in en, but exist in $3:',
181 * @return The help string.
183 protected function help() {
185 Run this script to check a specific language file, or all of them.
186 Command line settings are in form --parameter[=value].
188 * lang: Language code (default: the installation default language).
189 * all: Check all customized languages.
190 * help: Show this help.
191 * level: Show the following display level (default: 2).
192 * links: Link the message values (default off).
193 * wikilang: For the links, what is the content language of the wiki to display the output in (default en).
194 * whitelist: Do only the following checks (form: code,code).
195 * blacklist: Don't do the following checks (form: code,code).
196 * easy: Do only the easy checks, which can be treated by non-speakers of the language.
197 * 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).
198 Check codes (ideally, all of them should result 0; all the checks are executed by default (except language-specific check blacklists in checkLanguage.inc):
199 * untranslated: Messages which are required to translate, but are not translated.
200 * duplicate: Messages which translation equal to fallback
201 * obsolete: Messages which are untranslatable or do not exist, but are translated.
202 * variables: Messages without variables which should be used, or with variables which shouldn't be used.
203 * empty: Empty messages and messages that contain only -.
204 * whitespace: Messages which have trailing whitespace.
205 * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
206 * chars: Messages with hidden characters.
207 * links: Messages which contains broken links to pages (does not find all).
208 * unbalanced: Messages which contains unequal numbers of opening {[ and closing ]}.
209 * namespace: Namespace names that were not translated.
210 * projecttalk: Namespace names and aliases where the project talk does not contain $1.
211 * magic: Magic words that were not translated.
212 * magic-old: Magic words which do not exist.
213 * magic-over: Magic words that override the original English word.
214 * magic-case: Magic words whose translation changes the case-sensitivity of the original English word.
215 * special: Special page names that were not translated.
216 * special-old: Special page names which do not exist.
217 Display levels (default: 2):
218 * 0: Skip the checks (useful for checking syntax).
219 * 1: Show only the stub headers and number of wrong messages, without list of messages.
220 * 2: Show only the headers and the message keys, without the message values.
221 * 3: Show both the headers and the complete messages, with both keys and values.
227 * Execute the script.
229 public function execute() {
231 if ( $this->level > 0 ) {
232 switch ( $this->output ) {
240 throw new MWException( "Invalid output type $this->output" );
246 * Execute the checks.
248 protected function doChecks() {
249 $ignoredCodes = array( 'en', 'enRTL' );
251 $this->results = array();
253 if ( $this->checkAll ) {
254 foreach ( $this->L->getLanguages() as $language ) {
255 if ( !in_array( $language, $ignoredCodes ) ) {
256 $this->results[$language] = $this->checkLanguage( $language );
260 if ( in_array( $this->code, $ignoredCodes ) ) {
261 throw new MWException( "Cannot check code $this->code." );
263 $this->results[$this->code] = $this->checkLanguage( $this->code );
269 * Get the check blacklist.
270 * @return The list of checks which should not be executed.
272 protected function getCheckBlacklist() {
273 global $checkBlacklist;
274 return $checkBlacklist;
279 * @param $code The language code.
280 * @return The results.
282 protected function checkLanguage( $code ) {
284 if ( $this->level === 0 ) {
285 $this->L->getMessages( $code );
290 $checkFunctions = $this->getChecks();
291 $checkBlacklist = $this->getCheckBlacklist();
292 foreach ( $this->checks as $check ) {
293 if ( isset( $checkBlacklist[$code] ) &&
294 in_array( $check, $checkBlacklist[$code] ) ) {
295 $result[$check] = array();
299 $callback = array( $this->L, $checkFunctions[$check] );
300 if ( !is_callable( $callback ) ) {
301 throw new MWException( "Unkown check $check." );
303 $results[$check] = call_user_func( $callback, $code );
310 * Format a message key.
311 * @param $key The message key.
312 * @param $code The language code.
313 * @return The formatted message key.
315 protected function formatKey( $key, $code ) {
316 if ( $this->doLinks ) {
317 $displayKey = ucfirst( $key );
318 if ( $code == $this->wikiCode ) {
319 return "[[MediaWiki:$displayKey|$key]]";
321 return "[[MediaWiki:$displayKey/$code|$key]]";
329 * Output the checks results as plain text.
330 * @return The checks results as plain text.
332 protected function outputText() {
333 foreach ( $this->results as $code => $results ) {
334 $translated = $this->L->getMessages( $code );
335 $translated = count( $translated['translated'] );
336 foreach ( $results as $check => $messages ) {
337 $count = count( $messages );
339 if ( $check == 'untranslated' ) {
340 $translatable = $this->L->getGeneralMessages();
341 $total = count( $translatable['translatable'] );
342 } elseif ( in_array( $check, $this->nonMessageChecks() ) ) {
343 $totalCount = $this->getTotalCount();
344 $totalCount = $totalCount[$check];
345 $callback = array( $this->L, $totalCount[0] );
346 $callCode = $totalCount[1] ? $totalCount[1] : $code;
347 $total = count( call_user_func( $callback, $callCode ) );
349 $total = $translated;
351 $search = array( '$1', '$2', '$3' );
352 $replace = array( $count, $total, $code );
353 $descriptions = $this->getDescriptions();
354 echo "\n" . str_replace( $search, $replace, $descriptions[$check] ) . "\n";
355 if ( $this->level == 1 ) {
356 echo "[messages are hidden]\n";
358 foreach ( $messages as $key => $value ) {
359 if( !in_array( $check, $this->nonMessageChecks() ) ) {
360 $key = $this->formatKey( $key, $code );
362 if ( $this->level == 2 || empty( $value ) ) {
365 echo "* $key: '$value'\n";
375 * Output the checks results as wiki text.
376 * @return The checks results as wiki text.
378 function outputWiki() {
379 global $wgContLang, $IP;
381 $rows[] = '! Language !! Code !! Total !! ' . implode( ' !! ', $this->checks );
382 foreach ( $this->results as $code => $results ) {
383 $detailTextForLang = "==$code==\n";
386 $detailTextForLangChecks = array();
387 foreach ( $results as $check => $messages ) {
388 if( in_array( $check, $this->nonMessageChecks() ) ) {
391 $count = count( $messages );
394 $messageDetails = array();
395 foreach ( $messages as $key => $details ) {
396 $displayKey = $this->formatKey( $key, $code );
397 $messageDetails[] = $displayKey;
399 $detailTextForLangChecks[] = "=== $code-$check ===\n* " . implode( ', ', $messageDetails );
400 $numbers[] = "'''[[#$code-$check|$count]]'''";
407 if ( count( $detailTextForLangChecks ) ) {
408 $detailText .= $detailTextForLang . implode( "\n", $detailTextForLangChecks ) . "\n";
412 # Don't list languages without problems
415 $language = $wgContLang->getLanguageName( $code );
416 $rows[] = "| $language || $code || $problems || " . implode( ' || ', $numbers );
419 $tableRows = implode( "\n|-\n", $rows );
421 $version = SpecialVersion::getVersion( 'nodb' );
423 '''Check results are for:''' <code>$version</code>
426 {| class="sortable wikitable" border="2" cellpadding="4" cellspacing="0" style="background-color: #F9F9F9; border: 1px #AAAAAA solid; border-collapse: collapse; clear: both;"
436 * Check if there are any results for the checks, in any language.
437 * @return True if there are any results, false if not.
439 protected function isEmpty() {
440 foreach( $this->results as $code => $results ) {
441 foreach( $results as $check => $messages ) {
442 if( !empty( $messages ) ) {
451 class CheckExtensionsCLI extends CheckLanguageCLI {
456 * @param $options Options for script.
457 * @param $extension The extension name (or names).
459 public function __construct( Array $options, $extension ) {
460 if ( isset( $options['help'] ) ) {
465 if ( isset( $options['lang'] ) ) {
466 $this->code = $options['lang'];
468 global $wgLanguageCode;
469 $this->code = $wgLanguageCode;
472 if ( isset( $options['level'] ) ) {
473 $this->level = $options['level'];
476 $this->doLinks = isset( $options['links'] );
478 if ( isset( $options['wikilang'] ) ) {
479 $this->wikiCode = $options['wikilang'];
482 if ( isset( $options['whitelist'] ) ) {
483 $this->checks = explode( ',', $options['whitelist'] );
484 } elseif ( isset( $options['blacklist'] ) ) {
485 $this->checks = array_diff(
486 isset( $options['easy'] ) ? $this->easyChecks() : $this->defaultChecks(),
487 explode( ',', $options['blacklist'] )
489 } elseif ( isset( $options['easy'] ) ) {
490 $this->checks = $this->easyChecks();
492 $this->checks = $this->defaultChecks();
495 if ( isset( $options['output'] ) ) {
496 $this->output = $options['output'];
499 # Some additional checks not enabled by default
500 if ( isset( $options['duplicate'] ) ) {
501 $this->checks[] = 'duplicate';
504 $this->extensions = array();
505 $extensions = new PremadeMediawikiExtensionGroups();
506 $extensions->addAll();
507 if ( $extension == 'all' ) {
508 foreach ( MessageGroups::singleton()->getGroups() as $group ) {
509 if ( strpos( $group->getId(), 'ext-' ) === 0 && !$group->isMeta() ) {
510 $this->extensions[] = new extensionLanguages( $group );
513 } elseif ( $extension == 'wikimedia' ) {
514 $wikimedia = MessageGroups::getGroup( 'ext-0-wikimedia' );
515 foreach ( $wikimedia->wmfextensions() as $extension ) {
516 $group = MessageGroups::getGroup( $extension );
517 $this->extensions[] = new extensionLanguages( $group );
519 } elseif ( $extension == 'flaggedrevs' ) {
520 foreach ( MessageGroups::singleton()->getGroups() as $group ) {
521 if ( strpos( $group->getId(), 'ext-flaggedrevs-' ) === 0 && !$group->isMeta() ) {
522 $this->extensions[] = new extensionLanguages( $group );
526 $extensions = explode( ',', $extension );
527 foreach ( $extensions as $extension ) {
528 $group = MessageGroups::getGroup( 'ext-' . $extension );
530 $extension = new extensionLanguages( $group );
531 $this->extensions[] = $extension;
533 print "No such extension $extension.\n";
540 * Get the default checks.
541 * @return A list of the default checks.
543 protected function defaultChecks() {
545 'untranslated', 'duplicate', 'obsolete', 'variables', 'empty', 'plural',
546 'whitespace', 'xhtml', 'chars', 'links', 'unbalanced',
551 * Get the checks which check other things than messages.
552 * @return A list of the non-message checks.
554 protected function nonMessageChecks() {
559 * Get the checks that can easily be treated by non-speakers of the language.
560 * @return A list of the easy checks.
562 protected function easyChecks() {
564 'duplicate', 'obsolete', 'empty', 'whitespace', 'xhtml', 'chars',
570 * @return The help string.
572 protected function help() {
574 Run this script to check the status of a specific language in extensions, or all of them.
575 Command line settings are in form --parameter[=value], except for the first one.
577 * First parameter (mandatory): Extension name, multiple extension names (separated by commas), "all" for all the extensions, "wikimedia" for extensions used by Wikimedia or "flaggedrevs" for all FLaggedRevs extension messages.
578 * lang: Language code (default: the installation default language).
579 * help: Show this help.
580 * level: Show the following display level (default: 2).
581 * links: Link the message values (default off).
582 * wikilang: For the links, what is the content language of the wiki to display the output in (default en).
583 * whitelist: Do only the following checks (form: code,code).
584 * blacklist: Do not perform the following checks (form: code,code).
585 * easy: Do only the easy checks, which can be treated by non-speakers of the language.
586 Check codes (ideally, all of them should result 0; all the checks are executed by default (except language-specific check blacklists in checkLanguage.inc):
587 * untranslated: Messages which are required to translate, but are not translated.
588 * duplicate: Messages which translation equal to fallback
589 * obsolete: Messages which are untranslatable, but translated.
590 * variables: Messages without variables which should be used, or with variables which should not be used.
591 * empty: Empty messages.
592 * whitespace: Messages which have trailing whitespace.
593 * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
594 * chars: Messages with hidden characters.
595 * links: Messages which contains broken links to pages (does not find all).
596 * unbalanced: Messages which contains unequal numbers of opening {[ and closing ]}.
597 Display levels (default: 2):
598 * 0: Skip the checks (useful for checking syntax).
599 * 1: Show only the stub headers and number of wrong messages, without list of messages.
600 * 2: Show only the headers and the message keys, without the message values.
601 * 3: Show both the headers and the complete messages, with both keys and values.
607 * Execute the script.
609 public function execute() {
614 * Check a language and show the results.
615 * @param $code The language code.
617 protected function checkLanguage( $code ) {
618 foreach( $this->extensions as $extension ) {
619 $this->L = $extension;
620 $this->results = array();
621 $this->results[$code] = parent::checkLanguage( $code );
623 if( !$this->isEmpty() ) {
624 echo $extension->name() . ":\n";
626 if( $this->level > 0 ) {
627 switch( $this->output ) {
635 throw new MWException( "Invalid output type $this->output" );
645 # Blacklist some checks for some languages
646 $checkBlacklist = array(
647 #'code' => array( 'check1', 'check2' ... )
648 'az' => array( 'plural' ),
649 'bo' => array( 'plural' ),
650 'dz' => array( 'plural' ),
651 'id' => array( 'plural' ),
652 'fa' => array( 'plural' ),
653 'gan' => array( 'plural' ),
654 'gan-hans' => array( 'plural' ),
655 'gan-hant' => array( 'plural' ),
656 'gn' => array( 'plural' ),
657 'hak' => array( 'plural' ),
658 'hu' => array( 'plural' ),
659 'ja' => array( 'plural' ), // Does not use plural
660 'jv' => array( 'plural' ),
661 'ka' => array( 'plural' ),
662 'kk-arab' => array( 'plural' ),
663 'kk-cyrl' => array( 'plural' ),
664 'kk-latn' => array( 'plural' ),
665 'km' => array( 'plural' ),
666 'kn' => array( 'plural' ),
667 'ko' => array( 'plural' ),
668 'lzh' => array( 'plural' ),
669 'mn' => array( 'plural' ),
670 'ms' => array( 'plural' ),
671 'my' => array( 'plural', 'chars' ), // Uses a lot zwnj
672 'sah' => array( 'plural' ),
673 'sq' => array( 'plural' ),
674 'tet' => array( 'plural' ),
675 'th' => array( 'plural' ),
676 'to' => array( 'plural' ),
677 'tr' => array( 'plural' ),
678 'vi' => array( 'plural' ),
679 'wuu' => array( 'plural' ),
680 'xmf' => array( 'plural' ),
681 'yo' => array( 'plural' ),
682 'yue' => array( 'plural' ),
683 'zh' => array( 'plural' ),
684 'zh-classical' => array( 'plural' ),
685 'zh-cn' => array( 'plural' ),
686 'zh-hans' => array( 'plural' ),
687 'zh-hant' => array( 'plural' ),
688 'zh-hk' => array( 'plural' ),
689 'zh-sg' => array( 'plural' ),
690 'zh-tw' => array( 'plural' ),
691 'zh-yue' => array( 'plural' ),