3 * @ingroup MaintenanceLanguage
6 class CheckLanguageCLI {
7 protected $code = null;
9 protected $doLinks = false;
10 protected $linksPrefix = '';
11 protected $wikiCode = 'en';
12 protected $checkAll = false;
13 protected $output = 'plain';
14 protected $checks = array();
17 protected $results = array();
19 private $includeExif = false;
23 * @param $options Options for script.
25 public function __construct( Array $options ) {
26 if ( isset( $options['help'] ) ) {
31 if ( isset( $options['lang'] ) ) {
32 $this->code = $options['lang'];
34 global $wgLanguageCode;
35 $this->code = $wgLanguageCode;
38 if ( isset( $options['level'] ) ) {
39 $this->level = $options['level'];
42 $this->doLinks = isset( $options['links'] );
43 $this->includeExif = !isset( $options['noexif'] );
44 $this->checkAll = isset( $options['all'] );
46 if ( isset( $options['prefix'] ) ) {
47 $this->linksPrefix = $options['prefix'];
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(
58 isset( $options['easy'] ) ? $this->easyChecks() : $this->defaultChecks(),
59 explode( ',', $options['blacklist'] )
61 } elseif ( isset( $options['easy'] ) ) {
62 $this->checks = $this->easyChecks();
64 $this->checks = $this->defaultChecks();
67 if ( isset( $options['output'] ) ) {
68 $this->output = $options['output'];
71 $this->L = new languages( $this->includeExif );
75 * Get the default checks.
76 * @return A list of the default checks.
78 protected function defaultChecks() {
80 'untranslated', 'duplicate', 'obsolete', 'variables', 'empty', 'plural',
81 'whitespace', 'xhtml', 'chars', 'links', 'unbalanced', 'namespace',
82 'projecttalk', 'magic', 'magic-old', 'magic-over', 'magic-case',
83 'special', 'special-old',
88 * Get the checks which check other things than messages.
89 * @return A list of the non-message checks.
91 protected function nonMessageChecks() {
93 'namespace', 'projecttalk', 'magic', 'magic-old', 'magic-over',
94 'magic-case', 'special', 'special-old',
99 * Get the checks that can easily be treated by non-speakers of the language.
100 * @return A list of the easy checks.
102 protected function easyChecks() {
104 'duplicate', 'obsolete', 'empty', 'whitespace', 'xhtml', 'chars', 'magic-old',
105 'magic-over', 'magic-case', 'special-old',
111 * @return An array of all check names mapped to their function names.
113 protected function getChecks() {
115 'untranslated' => 'getUntranslatedMessages',
116 'duplicate' => 'getDuplicateMessages',
117 'obsolete' => 'getObsoleteMessages',
118 'variables' => 'getMessagesWithMismatchVariables',
119 'plural' => 'getMessagesWithoutPlural',
120 'empty' => 'getEmptyMessages',
121 'whitespace' => 'getMessagesWithWhitespace',
122 'xhtml' => 'getNonXHTMLMessages',
123 'chars' => 'getMessagesWithWrongChars',
124 'links' => 'getMessagesWithDubiousLinks',
125 'unbalanced' => 'getMessagesWithUnbalanced',
126 'namespace' => 'getUntranslatedNamespaces',
127 'projecttalk' => 'getProblematicProjectTalks',
128 'magic' => 'getUntranslatedMagicWords',
129 'magic-old' => 'getObsoleteMagicWords',
130 'magic-over' => 'getOverridingMagicWords',
131 'magic-case' => 'getCaseMismatchMagicWords',
132 'special' => 'getUntraslatedSpecialPages',
133 'special-old' => 'getObsoleteSpecialPages',
138 * Get total count for each check non-messages check.
139 * @return An array of all check names mapped to a two-element array:
140 * function name to get the total count and language code or null
143 protected function getTotalCount() {
145 'namespace' => array( 'getNamespaceNames', 'en' ),
146 'projecttalk' => null,
147 'magic' => array( 'getMagicWords', 'en' ),
148 'magic-old' => array( 'getMagicWords', null ),
149 'magic-over' => array( 'getMagicWords', null ),
150 'magic-case' => array( 'getMagicWords', null ),
151 'special' => array( 'getSpecialPageAliases', 'en' ),
152 'special-old' => array( 'getSpecialPageAliases', null ),
157 * Get all check descriptions.
158 * @return An array of all check names mapped to their descriptions.
160 protected function getDescriptions() {
162 'untranslated' => '$1 message(s) of $2 are not translated to $3, but exist in en:',
163 'duplicate' => '$1 message(s) of $2 are translated the same in en and $3:',
164 'obsolete' => '$1 message(s) of $2 do not exist in en or are in the ignore list, but exist in $3:',
165 'variables' => '$1 message(s) of $2 in $3 don\'t match the variables used in en:',
166 'plural' => '$1 message(s) of $2 in $3 don\'t use {{plural}} while en uses:',
167 'empty' => '$1 message(s) of $2 in $3 are empty or -:',
168 'whitespace' => '$1 message(s) of $2 in $3 have trailing whitespace:',
169 'xhtml' => '$1 message(s) of $2 in $3 contain illegal XHTML:',
170 'chars' => '$1 message(s) of $2 in $3 include hidden chars which should not be used in the messages:',
171 'links' => '$1 message(s) of $2 in $3 have problematic link(s):',
172 'unbalanced' => '$1 message(s) of $2 in $3 have unbalanced {[]}:',
173 'namespace' => '$1 namespace name(s) of $2 are not translated to $3, but exist in en:',
174 'projecttalk' => '$1 namespace name(s) and alias(es) in $3 are project talk namespaces without the parameter:',
175 'magic' => '$1 magic word(s) of $2 are not translated to $3, but exist in en:',
176 'magic-old' => '$1 magic word(s) of $2 do not exist in en, but exist in $3:',
177 'magic-over' => '$1 magic word(s) of $2 in $3 do not contain the original en word(s):',
178 'magic-case' => '$1 magic word(s) of $2 in $3 change the case-sensitivity of the original en word:',
179 'special' => '$1 special page alias(es) of $2 are not translated to $3, but exist in en:',
180 'special-old' => '$1 special page alias(es) of $2 do not exist in en, but exist in $3:',
186 * @return The help string.
188 protected function help() {
190 Run this script to check a specific language file, or all of them.
191 Command line settings are in form --parameter[=value].
193 * lang: Language code (default: the installation default language).
194 * all: Check all customized languages.
195 * help: Show this help.
196 * level: Show the following display level (default: 2).
197 * links: Link the message values (default off).
198 * prefix: prefix to add to links.
199 * wikilang: For the links, what is the content language of the wiki to display the output in (default en).
200 * whitelist: Do only the following checks (form: code,code).
201 * blacklist: Don't do the following checks (form: code,code).
202 * easy: Do only the easy checks, which can be treated by non-speakers of the language.
203 * 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).
204 Check codes (ideally, all of them should result 0; all the checks are executed by default (except language-specific check blacklists in checkLanguage.inc):
205 * untranslated: Messages which are required to translate, but are not translated.
206 * duplicate: Messages which translation equal to fallback
207 * obsolete: Messages which are untranslatable or do not exist, but are translated.
208 * variables: Messages without variables which should be used, or with variables which shouldn't be used.
209 * empty: Empty messages and messages that contain only -.
210 * whitespace: Messages which have trailing whitespace.
211 * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
212 * chars: Messages with hidden characters.
213 * links: Messages which contains broken links to pages (does not find all).
214 * unbalanced: Messages which contains unequal numbers of opening {[ and closing ]}.
215 * namespace: Namespace names that were not translated.
216 * projecttalk: Namespace names and aliases where the project talk does not contain $1.
217 * magic: Magic words that were not translated.
218 * magic-old: Magic words which do not exist.
219 * magic-over: Magic words that override the original English word.
220 * magic-case: Magic words whose translation changes the case-sensitivity of the original English word.
221 * special: Special page names that were not translated.
222 * special-old: Special page names which do not exist.
223 Display levels (default: 2):
224 * 0: Skip the checks (useful for checking syntax).
225 * 1: Show only the stub headers and number of wrong messages, without list of messages.
226 * 2: Show only the headers and the message keys, without the message values.
227 * 3: Show both the headers and the complete messages, with both keys and values.
233 * Execute the script.
235 public function execute() {
237 if ( $this->level > 0 ) {
238 switch ( $this->output ) {
246 throw new MWException( "Invalid output type $this->output" );
252 * Execute the checks.
254 protected function doChecks() {
255 $ignoredCodes = array( 'en', 'enRTL' );
257 $this->results = array();
259 if ( $this->checkAll ) {
260 foreach ( $this->L->getLanguages() as $language ) {
261 if ( !in_array( $language, $ignoredCodes ) ) {
262 $this->results[$language] = $this->checkLanguage( $language );
266 if ( in_array( $this->code, $ignoredCodes ) ) {
267 throw new MWException( "Cannot check code $this->code." );
269 $this->results[$this->code] = $this->checkLanguage( $this->code );
275 * Get the check blacklist.
276 * @return The list of checks which should not be executed.
278 protected function getCheckBlacklist() {
279 global $checkBlacklist;
280 return $checkBlacklist;
285 * @param $code The language code.
286 * @return The results.
288 protected function checkLanguage( $code ) {
290 if ( $this->level === 0 ) {
291 $this->L->getMessages( $code );
296 $checkFunctions = $this->getChecks();
297 $checkBlacklist = $this->getCheckBlacklist();
298 foreach ( $this->checks as $check ) {
299 if ( isset( $checkBlacklist[$code] ) &&
300 in_array( $check, $checkBlacklist[$code] ) ) {
301 $results[$check] = array();
305 $callback = array( $this->L, $checkFunctions[$check] );
306 if ( !is_callable( $callback ) ) {
307 throw new MWException( "Unkown check $check." );
309 $results[$check] = call_user_func( $callback, $code );
316 * Format a message key.
317 * @param $key The message key.
318 * @param $code The language code.
319 * @return The formatted message key.
321 protected function formatKey( $key, $code ) {
322 if ( $this->doLinks ) {
323 $displayKey = ucfirst( $key );
324 if ( $code == $this->wikiCode ) {
325 return "[[{$this->linksPrefix}MediaWiki:$displayKey|$key]]";
327 return "[[{$this->linksPrefix}MediaWiki:$displayKey/$code|$key]]";
335 * Output the checks results as plain text.
336 * @return The checks results as plain text.
338 protected function outputText() {
339 foreach ( $this->results as $code => $results ) {
340 $translated = $this->L->getMessages( $code );
341 $translated = count( $translated['translated'] );
342 foreach ( $results as $check => $messages ) {
343 $count = count( $messages );
345 if ( $check == 'untranslated' ) {
346 $translatable = $this->L->getGeneralMessages();
347 $total = count( $translatable['translatable'] );
348 } elseif ( in_array( $check, $this->nonMessageChecks() ) ) {
349 $totalCount = $this->getTotalCount();
350 $totalCount = $totalCount[$check];
351 $callback = array( $this->L, $totalCount[0] );
352 $callCode = $totalCount[1] ? $totalCount[1] : $code;
353 $total = count( call_user_func( $callback, $callCode ) );
355 $total = $translated;
357 $search = array( '$1', '$2', '$3' );
358 $replace = array( $count, $total, $code );
359 $descriptions = $this->getDescriptions();
360 echo "\n" . str_replace( $search, $replace, $descriptions[$check] ) . "\n";
361 if ( $this->level == 1 ) {
362 echo "[messages are hidden]\n";
364 foreach ( $messages as $key => $value ) {
365 if( !in_array( $check, $this->nonMessageChecks() ) ) {
366 $key = $this->formatKey( $key, $code );
368 if ( $this->level == 2 || empty( $value ) ) {
371 echo "* $key: '$value'\n";
381 * Output the checks results as wiki text.
382 * @return The checks results as wiki text.
384 function outputWiki() {
385 global $wgContLang, $IP;
387 $rows[] = '! Language !! Code !! Total !! ' . implode( ' !! ', array_diff( $this->checks, $this->nonMessageChecks() ) );
388 foreach ( $this->results as $code => $results ) {
389 $detailTextForLang = "==$code==\n";
392 $detailTextForLangChecks = array();
393 foreach ( $results as $check => $messages ) {
394 if( in_array( $check, $this->nonMessageChecks() ) ) {
397 $count = count( $messages );
400 $messageDetails = array();
401 foreach ( $messages as $key => $details ) {
402 $displayKey = $this->formatKey( $key, $code );
403 $messageDetails[] = $displayKey;
405 $detailTextForLangChecks[] = "=== $code-$check ===\n* " . implode( ', ', $messageDetails );
406 $numbers[] = "'''[[#$code-$check|$count]]'''";
413 if ( count( $detailTextForLangChecks ) ) {
414 $detailText .= $detailTextForLang . implode( "\n", $detailTextForLangChecks ) . "\n";
418 # Don't list languages without problems
421 $language = $wgContLang->getLanguageName( $code );
422 $rows[] = "| $language || $code || $problems || " . implode( ' || ', $numbers );
425 $tableRows = implode( "\n|-\n", $rows );
427 $version = SpecialVersion::getVersion( 'nodb' );
429 '''Check results are for:''' <code>$version</code>
432 {| class="sortable wikitable" border="2" cellpadding="4" cellspacing="0" style="background-color: #F9F9F9; border: 1px #AAAAAA solid; border-collapse: collapse; clear: both;"
442 * Check if there are any results for the checks, in any language.
443 * @return True if there are any results, false if not.
445 protected function isEmpty() {
446 foreach( $this->results as $code => $results ) {
447 foreach( $results as $check => $messages ) {
448 if( !empty( $messages ) ) {
457 class CheckExtensionsCLI extends CheckLanguageCLI {
462 * @param $options Options for script.
463 * @param $extension The extension name (or names).
465 public function __construct( Array $options, $extension ) {
466 if ( isset( $options['help'] ) ) {
471 if ( isset( $options['lang'] ) ) {
472 $this->code = $options['lang'];
474 global $wgLanguageCode;
475 $this->code = $wgLanguageCode;
478 if ( isset( $options['level'] ) ) {
479 $this->level = $options['level'];
482 $this->doLinks = isset( $options['links'] );
484 if ( isset( $options['wikilang'] ) ) {
485 $this->wikiCode = $options['wikilang'];
488 if ( isset( $options['whitelist'] ) ) {
489 $this->checks = explode( ',', $options['whitelist'] );
490 } elseif ( isset( $options['blacklist'] ) ) {
491 $this->checks = array_diff(
492 isset( $options['easy'] ) ? $this->easyChecks() : $this->defaultChecks(),
493 explode( ',', $options['blacklist'] )
495 } elseif ( isset( $options['easy'] ) ) {
496 $this->checks = $this->easyChecks();
498 $this->checks = $this->defaultChecks();
501 if ( isset( $options['output'] ) ) {
502 $this->output = $options['output'];
505 # Some additional checks not enabled by default
506 if ( isset( $options['duplicate'] ) ) {
507 $this->checks[] = 'duplicate';
510 $this->extensions = array();
511 $extensions = new PremadeMediawikiExtensionGroups();
512 $extensions->addAll();
513 if ( $extension == 'all' ) {
514 foreach ( MessageGroups::singleton()->getGroups() as $group ) {
515 if ( strpos( $group->getId(), 'ext-' ) === 0 && !$group->isMeta() ) {
516 $this->extensions[] = new extensionLanguages( $group );
519 } elseif ( $extension == 'wikimedia' ) {
520 $wikimedia = MessageGroups::getGroup( 'ext-0-wikimedia' );
521 foreach ( $wikimedia->wmfextensions() as $extension ) {
522 $group = MessageGroups::getGroup( $extension );
523 $this->extensions[] = new extensionLanguages( $group );
525 } elseif ( $extension == 'flaggedrevs' ) {
526 foreach ( MessageGroups::singleton()->getGroups() as $group ) {
527 if ( strpos( $group->getId(), 'ext-flaggedrevs-' ) === 0 && !$group->isMeta() ) {
528 $this->extensions[] = new extensionLanguages( $group );
532 $extensions = explode( ',', $extension );
533 foreach ( $extensions as $extension ) {
534 $group = MessageGroups::getGroup( 'ext-' . $extension );
536 $extension = new extensionLanguages( $group );
537 $this->extensions[] = $extension;
539 print "No such extension $extension.\n";
546 * Get the default checks.
547 * @return A list of the default checks.
549 protected function defaultChecks() {
551 'untranslated', 'duplicate', 'obsolete', 'variables', 'empty', 'plural',
552 'whitespace', 'xhtml', 'chars', 'links', 'unbalanced',
557 * Get the checks which check other things than messages.
558 * @return A list of the non-message checks.
560 protected function nonMessageChecks() {
565 * Get the checks that can easily be treated by non-speakers of the language.
566 * @return A list of the easy checks.
568 protected function easyChecks() {
570 'duplicate', 'obsolete', 'empty', 'whitespace', 'xhtml', 'chars',
576 * @return The help string.
578 protected function help() {
580 Run this script to check the status of a specific language in extensions, or all of them.
581 Command line settings are in form --parameter[=value], except for the first one.
583 * 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.
584 * lang: Language code (default: the installation default language).
585 * help: Show this help.
586 * level: Show the following display level (default: 2).
587 * links: Link the message values (default off).
588 * wikilang: For the links, what is the content language of the wiki to display the output in (default en).
589 * whitelist: Do only the following checks (form: code,code).
590 * blacklist: Do not perform the following checks (form: code,code).
591 * easy: Do only the easy checks, which can be treated by non-speakers of the language.
592 Check codes (ideally, all of them should result 0; all the checks are executed by default (except language-specific check blacklists in checkLanguage.inc):
593 * untranslated: Messages which are required to translate, but are not translated.
594 * duplicate: Messages which translation equal to fallback
595 * obsolete: Messages which are untranslatable, but translated.
596 * variables: Messages without variables which should be used, or with variables which should not be used.
597 * empty: Empty messages.
598 * whitespace: Messages which have trailing whitespace.
599 * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
600 * chars: Messages with hidden characters.
601 * links: Messages which contains broken links to pages (does not find all).
602 * unbalanced: Messages which contains unequal numbers of opening {[ and closing ]}.
603 Display levels (default: 2):
604 * 0: Skip the checks (useful for checking syntax).
605 * 1: Show only the stub headers and number of wrong messages, without list of messages.
606 * 2: Show only the headers and the message keys, without the message values.
607 * 3: Show both the headers and the complete messages, with both keys and values.
613 * Execute the script.
615 public function execute() {
620 * Check a language and show the results.
621 * @param $code The language code.
623 protected function checkLanguage( $code ) {
624 foreach( $this->extensions as $extension ) {
625 $this->L = $extension;
626 $this->results = array();
627 $this->results[$code] = parent::checkLanguage( $code );
629 if( !$this->isEmpty() ) {
630 echo $extension->name() . ":\n";
632 if( $this->level > 0 ) {
633 switch( $this->output ) {
641 throw new MWException( "Invalid output type $this->output" );
651 # Blacklist some checks for some languages
652 $checkBlacklist = array(
653 #'code' => array( 'check1', 'check2' ... )
654 'az' => array( 'plural' ),
655 'bo' => array( 'plural' ),
656 'dz' => array( 'plural' ),
657 'id' => array( 'plural' ),
658 'fa' => array( 'plural' ),
659 'gan' => array( 'plural' ),
660 'gan-hans' => array( 'plural' ),
661 'gan-hant' => array( 'plural' ),
662 'gn' => array( 'plural' ),
663 'hak' => array( 'plural' ),
664 'hu' => array( 'plural' ),
665 'ja' => array( 'plural' ), // Does not use plural
666 'jv' => array( 'plural' ),
667 'ka' => array( 'plural' ),
668 'kk-arab' => array( 'plural' ),
669 'kk-cyrl' => array( 'plural' ),
670 'kk-latn' => array( 'plural' ),
671 'km' => array( 'plural' ),
672 'kn' => array( 'plural' ),
673 'ko' => array( 'plural' ),
674 'lzh' => array( 'plural' ),
675 'mn' => array( 'plural' ),
676 'ms' => array( 'plural' ),
677 'my' => array( 'plural', 'chars' ), // Uses a lot zwnj
678 'sah' => array( 'plural' ),
679 'sq' => array( 'plural' ),
680 'tet' => array( 'plural' ),
681 'th' => array( 'plural' ),
682 'to' => array( 'plural' ),
683 'tr' => array( 'plural' ),
684 'vi' => array( 'plural' ),
685 'wuu' => array( 'plural' ),
686 'xmf' => array( 'plural' ),
687 'yo' => array( 'plural' ),
688 'yue' => array( 'plural' ),
689 'zh' => array( 'plural' ),
690 'zh-classical' => array( 'plural' ),
691 'zh-cn' => array( 'plural' ),
692 'zh-hans' => array( 'plural' ),
693 'zh-hant' => array( 'plural' ),
694 'zh-hk' => array( 'plural' ),
695 'zh-sg' => array( 'plural' ),
696 'zh-tw' => array( 'plural' ),
697 'zh-yue' => array( 'plural' ),