4 * @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 --help: Show this help.
194 --lang: Language code (default: the installation default language).
195 --all: Check all customized languages.
196 --level: Show the following display level (default: 2):
197 * 0: Skip the checks (useful for checking syntax).
198 * 1: Show only the stub headers and number of wrong messages, without list of messages.
199 * 2: Show only the headers and the message keys, without the message values.
200 * 3: Show both the headers and the complete messages, with both keys and values.
201 --links: Link the message values (default off).
202 --prefix: prefix to add to links.
203 --wikilang: For the links, what is the content language of the wiki to display the output in (default en).
204 --noexif: Don't check for EXIF messages (a bit hard and boring to translate), if you know
205 that they are currently not translated and want to focus on other problems (default off).
206 --whitelist: Do only the following checks (form: code,code).
207 --blacklist: Don't do the following checks (form: code,code).
208 --easy: Do only the easy checks, which can be treated by non-speakers of the language.
210 Check codes (ideally, all of them should result 0; all the checks are executed by default (except language-specific check blacklists in checkLanguage.inc):
211 * untranslated: Messages which are required to translate, but are not translated.
212 * duplicate: Messages which translation equal to fallback
213 * obsolete: Messages which are untranslatable or do not exist, but are translated.
214 * variables: Messages without variables which should be used, or with variables which shouldn't be used.
215 * empty: Empty messages and messages that contain only -.
216 * whitespace: Messages which have trailing whitespace.
217 * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
218 * chars: Messages with hidden characters.
219 * links: Messages which contains broken links to pages (does not find all).
220 * unbalanced: Messages which contains unequal numbers of opening {[ and closing ]}.
221 * namespace: Namespace names that were not translated.
222 * projecttalk: Namespace names and aliases where the project talk does not contain $1.
223 * magic: Magic words that were not translated.
224 * magic-old: Magic words which do not exist.
225 * magic-over: Magic words that override the original English word.
226 * magic-case: Magic words whose translation changes the case-sensitivity of the original English word.
227 * special: Special page names that were not translated.
228 * special-old: Special page names which do not exist.
234 * Execute the script.
236 public function execute() {
238 if ( $this->level > 0 ) {
239 switch ( $this->output ) {
247 throw new MWException( "Invalid output type $this->output" );
253 * Execute the checks.
255 protected function doChecks() {
256 $ignoredCodes = array( 'en', 'enRTL' );
258 $this->results = array();
260 if ( $this->checkAll ) {
261 foreach ( $this->L->getLanguages() as $language ) {
262 if ( !in_array( $language, $ignoredCodes ) ) {
263 $this->results[$language] = $this->checkLanguage( $language );
267 if ( in_array( $this->code, $ignoredCodes ) ) {
268 throw new MWException( "Cannot check code $this->code." );
270 $this->results[$this->code] = $this->checkLanguage( $this->code );
276 * Get the check blacklist.
277 * @return The list of checks which should not be executed.
279 protected function getCheckBlacklist() {
280 global $checkBlacklist;
281 return $checkBlacklist;
286 * @param $code The language code.
287 * @return The results.
289 protected function checkLanguage( $code ) {
291 if ( $this->level === 0 ) {
292 $this->L->getMessages( $code );
297 $checkFunctions = $this->getChecks();
298 $checkBlacklist = $this->getCheckBlacklist();
299 foreach ( $this->checks as $check ) {
300 if ( isset( $checkBlacklist[$code] ) &&
301 in_array( $check, $checkBlacklist[$code] ) ) {
302 $results[$check] = array();
306 $callback = array( $this->L, $checkFunctions[$check] );
307 if ( !is_callable( $callback ) ) {
308 throw new MWException( "Unkown check $check." );
310 $results[$check] = call_user_func( $callback, $code );
317 * Format a message key.
318 * @param $key The message key.
319 * @param $code The language code.
320 * @return The formatted message key.
322 protected function formatKey( $key, $code ) {
323 if ( $this->doLinks ) {
324 $displayKey = ucfirst( $key );
325 if ( $code == $this->wikiCode ) {
326 return "[[{$this->linksPrefix}MediaWiki:$displayKey|$key]]";
328 return "[[{$this->linksPrefix}MediaWiki:$displayKey/$code|$key]]";
336 * Output the checks results as plain text.
337 * @return The checks results as plain text.
339 protected function outputText() {
340 foreach ( $this->results as $code => $results ) {
341 $translated = $this->L->getMessages( $code );
342 $translated = count( $translated['translated'] );
343 foreach ( $results as $check => $messages ) {
344 $count = count( $messages );
346 if ( $check == 'untranslated' ) {
347 $translatable = $this->L->getGeneralMessages();
348 $total = count( $translatable['translatable'] );
349 } elseif ( in_array( $check, $this->nonMessageChecks() ) ) {
350 $totalCount = $this->getTotalCount();
351 $totalCount = $totalCount[$check];
352 $callback = array( $this->L, $totalCount[0] );
353 $callCode = $totalCount[1] ? $totalCount[1] : $code;
354 $total = count( call_user_func( $callback, $callCode ) );
356 $total = $translated;
358 $search = array( '$1', '$2', '$3' );
359 $replace = array( $count, $total, $code );
360 $descriptions = $this->getDescriptions();
361 echo "\n" . str_replace( $search, $replace, $descriptions[$check] ) . "\n";
362 if ( $this->level == 1 ) {
363 echo "[messages are hidden]\n";
365 foreach ( $messages as $key => $value ) {
366 if( !in_array( $check, $this->nonMessageChecks() ) ) {
367 $key = $this->formatKey( $key, $code );
369 if ( $this->level == 2 || empty( $value ) ) {
372 echo "* $key: '$value'\n";
382 * Output the checks results as wiki text.
383 * @return The checks results as wiki text.
385 function outputWiki() {
388 $rows[] = '! Language !! Code !! Total !! ' . implode( ' !! ', array_diff( $this->checks, $this->nonMessageChecks() ) );
389 foreach ( $this->results as $code => $results ) {
390 $detailTextForLang = "==$code==\n";
393 $detailTextForLangChecks = array();
394 foreach ( $results as $check => $messages ) {
395 if( in_array( $check, $this->nonMessageChecks() ) ) {
398 $count = count( $messages );
401 $messageDetails = array();
402 foreach ( $messages as $key => $details ) {
403 $displayKey = $this->formatKey( $key, $code );
404 $messageDetails[] = $displayKey;
406 $detailTextForLangChecks[] = "=== $code-$check ===\n* " . implode( ', ', $messageDetails );
407 $numbers[] = "'''[[#$code-$check|$count]]'''";
414 if ( count( $detailTextForLangChecks ) ) {
415 $detailText .= $detailTextForLang . implode( "\n", $detailTextForLangChecks ) . "\n";
419 # Don't list languages without problems
422 $language = $wgContLang->getLanguageName( $code );
423 $rows[] = "| $language || $code || $problems || " . implode( ' || ', $numbers );
426 $tableRows = implode( "\n|-\n", $rows );
428 $version = SpecialVersion::getVersion( 'nodb' );
430 '''Check results are for:''' <code>$version</code>
433 {| class="sortable wikitable" border="2" cellpadding="4" cellspacing="0" style="background-color: #F9F9F9; border: 1px #AAAAAA solid; border-collapse: collapse; clear: both;"
443 * Check if there are any results for the checks, in any language.
444 * @return True if there are any results, false if not.
446 protected function isEmpty() {
447 foreach( $this->results as $results ) {
448 foreach( $results as $messages ) {
449 if( !empty( $messages ) ) {
459 * @ingroup MaintenanceLanguage
461 class CheckExtensionsCLI extends CheckLanguageCLI {
466 * @param $options Options for script.
467 * @param $extension The extension name (or names).
469 public function __construct( Array $options, $extension ) {
470 if ( isset( $options['help'] ) ) {
475 if ( isset( $options['lang'] ) ) {
476 $this->code = $options['lang'];
478 global $wgLanguageCode;
479 $this->code = $wgLanguageCode;
482 if ( isset( $options['level'] ) ) {
483 $this->level = $options['level'];
486 $this->doLinks = isset( $options['links'] );
488 if ( isset( $options['wikilang'] ) ) {
489 $this->wikiCode = $options['wikilang'];
492 if ( isset( $options['whitelist'] ) ) {
493 $this->checks = explode( ',', $options['whitelist'] );
494 } elseif ( isset( $options['blacklist'] ) ) {
495 $this->checks = array_diff(
496 isset( $options['easy'] ) ? $this->easyChecks() : $this->defaultChecks(),
497 explode( ',', $options['blacklist'] )
499 } elseif ( isset( $options['easy'] ) ) {
500 $this->checks = $this->easyChecks();
502 $this->checks = $this->defaultChecks();
505 if ( isset( $options['output'] ) ) {
506 $this->output = $options['output'];
509 # Some additional checks not enabled by default
510 if ( isset( $options['duplicate'] ) ) {
511 $this->checks[] = 'duplicate';
514 $this->extensions = array();
515 $extensions = new PremadeMediawikiExtensionGroups();
516 $extensions->addAll();
517 if ( $extension == 'all' ) {
518 foreach ( MessageGroups::singleton()->getGroups() as $group ) {
519 if ( strpos( $group->getId(), 'ext-' ) === 0 && !$group->isMeta() ) {
520 $this->extensions[] = new extensionLanguages( $group );
523 } elseif ( $extension == 'wikimedia' ) {
524 $wikimedia = MessageGroups::getGroup( 'ext-0-wikimedia' );
525 foreach ( $wikimedia->wmfextensions() as $extension ) {
526 $group = MessageGroups::getGroup( $extension );
527 $this->extensions[] = new extensionLanguages( $group );
529 } elseif ( $extension == 'flaggedrevs' ) {
530 foreach ( MessageGroups::singleton()->getGroups() as $group ) {
531 if ( strpos( $group->getId(), 'ext-flaggedrevs-' ) === 0 && !$group->isMeta() ) {
532 $this->extensions[] = new extensionLanguages( $group );
536 $extensions = explode( ',', $extension );
537 foreach ( $extensions as $extension ) {
538 $group = MessageGroups::getGroup( 'ext-' . $extension );
540 $extension = new extensionLanguages( $group );
541 $this->extensions[] = $extension;
543 print "No such extension $extension.\n";
550 * Get the default checks.
551 * @return A list of the default checks.
553 protected function defaultChecks() {
555 'untranslated', 'duplicate', 'obsolete', 'variables', 'empty', 'plural',
556 'whitespace', 'xhtml', 'chars', 'links', 'unbalanced',
561 * Get the checks which check other things than messages.
562 * @return A list of the non-message checks.
564 protected function nonMessageChecks() {
569 * Get the checks that can easily be treated by non-speakers of the language.
570 * @return A list of the easy checks.
572 protected function easyChecks() {
574 'duplicate', 'obsolete', 'empty', 'whitespace', 'xhtml', 'chars',
580 * @return The help string.
582 protected function help() {
584 Run this script to check the status of a specific language in extensions, or all of them.
585 Command line settings are in form --parameter[=value], except for the first one.
587 * 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.
588 * lang: Language code (default: the installation default language).
589 * help: Show this help.
590 * level: Show the following display level (default: 2).
591 * links: Link the message values (default off).
592 * wikilang: For the links, what is the content language of the wiki to display the output in (default en).
593 * whitelist: Do only the following checks (form: code,code).
594 * blacklist: Do not perform the following checks (form: code,code).
595 * easy: Do only the easy checks, which can be treated by non-speakers of the language.
596 Check codes (ideally, all of them should result 0; all the checks are executed by default (except language-specific check blacklists in checkLanguage.inc):
597 * untranslated: Messages which are required to translate, but are not translated.
598 * duplicate: Messages which translation equal to fallback
599 * obsolete: Messages which are untranslatable, but translated.
600 * variables: Messages without variables which should be used, or with variables which should not be used.
601 * empty: Empty messages.
602 * whitespace: Messages which have trailing whitespace.
603 * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
604 * chars: Messages with hidden characters.
605 * links: Messages which contains broken links to pages (does not find all).
606 * unbalanced: Messages which contains unequal numbers of opening {[ and closing ]}.
607 Display levels (default: 2):
608 * 0: Skip the checks (useful for checking syntax).
609 * 1: Show only the stub headers and number of wrong messages, without list of messages.
610 * 2: Show only the headers and the message keys, without the message values.
611 * 3: Show both the headers and the complete messages, with both keys and values.
617 * Execute the script.
619 public function execute() {
624 * Check a language and show the results.
625 * @param $code The language code.
627 protected function checkLanguage( $code ) {
628 foreach( $this->extensions as $extension ) {
629 $this->L = $extension;
630 $this->results = array();
631 $this->results[$code] = parent::checkLanguage( $code );
633 if( !$this->isEmpty() ) {
634 echo $extension->name() . ":\n";
636 if( $this->level > 0 ) {
637 switch( $this->output ) {
645 throw new MWException( "Invalid output type $this->output" );
655 # Blacklist some checks for some languages
656 $checkBlacklist = array(
657 #'code' => array( 'check1', 'check2' ... )
658 'az' => array( 'plural' ),
659 'bo' => array( 'plural' ),
660 'dz' => array( 'plural' ),
661 'id' => array( 'plural' ),
662 'fa' => array( 'plural' ),
663 'gan' => array( 'plural' ),
664 'gan-hans' => array( 'plural' ),
665 'gan-hant' => array( 'plural' ),
666 'gn' => array( 'plural' ),
667 'hak' => array( 'plural' ),
668 'hu' => array( 'plural' ),
669 'ja' => array( 'plural' ), // Does not use plural
670 'jv' => array( 'plural' ),
671 'ka' => array( 'plural' ),
672 'kk-arab' => array( 'plural' ),
673 'kk-cyrl' => array( 'plural' ),
674 'kk-latn' => array( 'plural' ),
675 'km' => array( 'plural' ),
676 'kn' => array( 'plural' ),
677 'ko' => array( 'plural' ),
678 'lzh' => array( 'plural' ),
679 'mn' => array( 'plural' ),
680 'ms' => array( 'plural' ),
681 'my' => array( 'plural', 'chars' ), // Uses a lot zwnj
682 'sah' => array( 'plural' ),
683 'sq' => array( 'plural' ),
684 'tet' => array( 'plural' ),
685 'th' => array( 'plural' ),
686 'to' => array( 'plural' ),
687 'tr' => array( 'plural' ),
688 'vi' => array( 'plural' ),
689 'wuu' => array( 'plural' ),
690 'xmf' => array( 'plural' ),
691 'yo' => array( 'plural' ),
692 'yue' => array( 'plural' ),
693 'zh' => array( 'plural' ),
694 'zh-classical' => array( 'plural' ),
695 'zh-cn' => array( 'plural' ),
696 'zh-hans' => array( 'plural' ),
697 'zh-hant' => array( 'plural' ),
698 'zh-hk' => array( 'plural' ),
699 'zh-sg' => array( 'plural' ),
700 'zh-tw' => array( 'plural' ),
701 'zh-yue' => array( 'plural' ),