3 * Helper class for checkLanguage.php script.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup MaintenanceLanguage
25 * @ingroup MaintenanceLanguage
27 class CheckLanguageCLI {
28 protected $code = null;
30 protected $doLinks = false;
31 protected $linksPrefix = '';
32 protected $wikiCode = 'en';
33 protected $checkAll = false;
34 protected $output = 'plain';
35 protected $checks = array();
38 protected $results = array();
40 private $includeExif = false;
44 * @param $options array Options for script.
46 public function __construct( array $options ) {
47 if ( isset( $options['help'] ) ) {
52 if ( isset( $options['lang'] ) ) {
53 $this->code = $options['lang'];
55 global $wgLanguageCode;
56 $this->code = $wgLanguageCode;
59 if ( isset( $options['level'] ) ) {
60 $this->level = $options['level'];
63 $this->doLinks = isset( $options['links'] );
64 $this->includeExif = !isset( $options['noexif'] );
65 $this->checkAll = isset( $options['all'] );
67 if ( isset( $options['prefix'] ) ) {
68 $this->linksPrefix = $options['prefix'];
71 if ( isset( $options['wikilang'] ) ) {
72 $this->wikiCode = $options['wikilang'];
75 if ( isset( $options['whitelist'] ) ) {
76 $this->checks = explode( ',', $options['whitelist'] );
77 } elseif ( isset( $options['blacklist'] ) ) {
78 $this->checks = array_diff(
79 isset( $options['easy'] ) ? $this->easyChecks() : $this->defaultChecks(),
80 explode( ',', $options['blacklist'] )
82 } elseif ( isset( $options['easy'] ) ) {
83 $this->checks = $this->easyChecks();
85 $this->checks = $this->defaultChecks();
88 if ( isset( $options['output'] ) ) {
89 $this->output = $options['output'];
92 $this->L = new languages( $this->includeExif );
96 * Get the default checks.
97 * @return array A list of the default checks.
99 protected function defaultChecks() {
101 'untranslated', 'duplicate', 'obsolete', 'variables', 'empty', 'plural',
102 'whitespace', 'xhtml', 'chars', 'links', 'unbalanced', 'namespace',
103 'projecttalk', 'magic', 'magic-old', 'magic-over', 'magic-case',
104 'special', 'special-old',
109 * Get the checks which check other things than messages.
110 * @return array A list of the non-message checks.
112 protected function nonMessageChecks() {
114 'namespace', 'projecttalk', 'magic', 'magic-old', 'magic-over',
115 'magic-case', 'special', 'special-old',
120 * Get the checks that can easily be treated by non-speakers of the language.
121 * @return Array A list of the easy checks.
123 protected function easyChecks() {
125 'duplicate', 'obsolete', 'empty', 'whitespace', 'xhtml', 'chars', 'magic-old',
126 'magic-over', 'magic-case', 'special-old',
132 * @return array An array of all check names mapped to their function names.
134 protected function getChecks() {
136 'untranslated' => 'getUntranslatedMessages',
137 'duplicate' => 'getDuplicateMessages',
138 'obsolete' => 'getObsoleteMessages',
139 'variables' => 'getMessagesWithMismatchVariables',
140 'plural' => 'getMessagesWithoutPlural',
141 'empty' => 'getEmptyMessages',
142 'whitespace' => 'getMessagesWithWhitespace',
143 'xhtml' => 'getNonXHTMLMessages',
144 'chars' => 'getMessagesWithWrongChars',
145 'links' => 'getMessagesWithDubiousLinks',
146 'unbalanced' => 'getMessagesWithUnbalanced',
147 'namespace' => 'getUntranslatedNamespaces',
148 'projecttalk' => 'getProblematicProjectTalks',
149 'magic' => 'getUntranslatedMagicWords',
150 'magic-old' => 'getObsoleteMagicWords',
151 'magic-over' => 'getOverridingMagicWords',
152 'magic-case' => 'getCaseMismatchMagicWords',
153 'special' => 'getUntraslatedSpecialPages',
154 'special-old' => 'getObsoleteSpecialPages',
159 * Get total count for each check non-messages check.
160 * @return array An array of all check names mapped to a two-element array:
161 * function name to get the total count and language code or null
164 protected function getTotalCount() {
166 'namespace' => array( 'getNamespaceNames', 'en' ),
167 'projecttalk' => null,
168 'magic' => array( 'getMagicWords', 'en' ),
169 'magic-old' => array( 'getMagicWords', null ),
170 'magic-over' => array( 'getMagicWords', null ),
171 'magic-case' => array( 'getMagicWords', null ),
172 'special' => array( 'getSpecialPageAliases', 'en' ),
173 'special-old' => array( 'getSpecialPageAliases', null ),
178 * Get all check descriptions.
179 * @return array An array of all check names mapped to their descriptions.
181 protected function getDescriptions() {
183 'untranslated' => '$1 message(s) of $2 are not translated to $3, but exist in en:',
184 'duplicate' => '$1 message(s) of $2 are translated the same in en and $3:',
186 '$1 message(s) of $2 do not exist in en or are in the ignore list, but exist in $3:',
187 'variables' => '$1 message(s) of $2 in $3 don\'t match the variables used in en:',
188 'plural' => '$1 message(s) of $2 in $3 don\'t use {{plural}} while en uses:',
189 'empty' => '$1 message(s) of $2 in $3 are empty or -:',
190 'whitespace' => '$1 message(s) of $2 in $3 have trailing whitespace:',
191 'xhtml' => '$1 message(s) of $2 in $3 contain illegal XHTML:',
193 '$1 message(s) of $2 in $3 include hidden chars which should not be used in the messages:',
194 'links' => '$1 message(s) of $2 in $3 have problematic link(s):',
195 'unbalanced' => '$1 message(s) of $2 in $3 have unbalanced {[]}:',
196 'namespace' => '$1 namespace name(s) of $2 are not translated to $3, but exist in en:',
198 '$1 namespace name(s) and alias(es) in $3 are project talk namespaces without the parameter:',
199 'magic' => '$1 magic word(s) of $2 are not translated to $3, but exist in en:',
200 'magic-old' => '$1 magic word(s) of $2 do not exist in en, but exist in $3:',
201 'magic-over' => '$1 magic word(s) of $2 in $3 do not contain the original en word(s):',
203 '$1 magic word(s) of $2 in $3 change the case-sensitivity of the original en word:',
204 'special' => '$1 special page alias(es) of $2 are not translated to $3, but exist in en:',
205 'special-old' => '$1 special page alias(es) of $2 do not exist in en, but exist in $3:',
211 * @return string The help string.
213 protected function help() {
215 Run this script to check a specific language file, or all of them.
216 Command line settings are in form --parameter[=value].
218 --help: Show this help.
219 --lang: Language code (default: the installation default language).
220 --all: Check all customized languages.
221 --level: Show the following display level (default: 2):
222 * 0: Skip the checks (useful for checking syntax).
223 * 1: Show only the stub headers and number of wrong messages, without
225 * 2: Show only the headers and the message keys, without the message
227 * 3: Show both the headers and the complete messages, with both keys and
229 --links: Link the message values (default off).
230 --prefix: prefix to add to links.
231 --wikilang: For the links, what is the content language of the wiki to
232 display the output in (default en).
233 --noexif: Do not check for Exif messages (a bit hard and boring to
234 translate), if you know what they are currently not translated and want
235 to focus on other problems (default off).
236 --whitelist: Do only the following checks (form: code,code).
237 --blacklist: Do not do the following checks (form: code,code).
238 --easy: Do only the easy checks, which can be treated by non-speakers of
241 Check codes (ideally, all of them should result 0; all the checks are executed
242 by default (except language-specific check blacklists in checkLanguage.inc):
243 * untranslated: Messages which are required to translate, but are not
245 * duplicate: Messages which translation equal to fallback.
246 * obsolete: Messages which are untranslatable or do not exist, but are
248 * variables: Messages without variables which should be used, or with
249 variables which should not be used.
250 * empty: Empty messages and messages that contain only -.
251 * whitespace: Messages which have trailing whitespace.
252 * xhtml: Messages which are not well-formed XHTML (checks only few common
254 * chars: Messages with hidden characters.
255 * links: Messages which contains broken links to pages (does not find all).
256 * unbalanced: Messages which contains unequal numbers of opening {[ and
258 * namespace: Namespace names that were not translated.
259 * projecttalk: Namespace names and aliases where the project talk does not
261 * magic: Magic words that were not translated.
262 * magic-old: Magic words which do not exist.
263 * magic-over: Magic words that override the original English word.
264 * magic-case: Magic words whose translation changes the case-sensitivity of
265 the original English word.
266 * special: Special page names that were not translated.
267 * special-old: Special page names which do not exist.
273 * Execute the script.
275 public function execute() {
277 if ( $this->level > 0 ) {
278 switch ( $this->output ) {
286 throw new MWException( "Invalid output type $this->output" );
292 * Execute the checks.
294 protected function doChecks() {
295 $ignoredCodes = array( 'en', 'enRTL' );
297 $this->results = array();
299 if ( $this->checkAll ) {
300 foreach ( $this->L->getLanguages() as $language ) {
301 if ( !in_array( $language, $ignoredCodes ) ) {
302 $this->results[$language] = $this->checkLanguage( $language );
306 if ( in_array( $this->code, $ignoredCodes ) ) {
307 throw new MWException( "Cannot check code $this->code." );
309 $this->results[$this->code] = $this->checkLanguage( $this->code );
313 $results = $this->results;
314 foreach ( $results as $code => $checks ) {
315 foreach ( $checks as $check => $messages ) {
316 foreach ( $messages as $key => $details ) {
317 if ( $this->isCheckBlacklisted( $check, $code, $key ) ) {
318 unset( $this->results[$code][$check][$key] );
326 * Get the check blacklist.
327 * @return array The list of checks which should not be executed.
329 protected function getCheckBlacklist() {
330 static $blacklist = null;
332 if ( $blacklist !== null ) {
336 global $checkBlacklist;
338 $blacklist = $checkBlacklist;
340 wfRunHooks( 'LocalisationChecksBlacklist', array( &$blacklist ) );
346 * Verify whether a check is blacklisted.
348 * @param string $check Check name
349 * @param string $code Language code
350 * @param string|bool $message Message name, or False for a whole language
351 * @return bool Whether the check is blacklisted
353 protected function isCheckBlacklisted( $check, $code, $message ) {
354 $blacklist = $this->getCheckBlacklist();
356 foreach ( $blacklist as $item ) {
357 if ( isset( $item['check'] ) && $check !== $item['check'] ) {
361 if ( isset( $item['code'] ) && !in_array( $code, $item['code'] ) ) {
365 if ( isset( $item['message'] ) &&
366 ( $message === false || !in_array( $message, $item['message'] ) )
379 * @param $code string The language code.
380 * @throws MWException
381 * @return array The results.
383 protected function checkLanguage( $code ) {
386 if ( $this->level === 0 ) {
387 $this->L->getMessages( $code );
392 $checkFunctions = $this->getChecks();
393 foreach ( $this->checks as $check ) {
394 if ( $this->isCheckBlacklisted( $check, $code, false ) ) {
395 $results[$check] = array();
399 $callback = array( $this->L, $checkFunctions[$check] );
400 if ( !is_callable( $callback ) ) {
401 throw new MWException( "Unkown check $check." );
403 $results[$check] = call_user_func( $callback, $code );
410 * Format a message key.
411 * @param $key string The message key.
412 * @param $code string The language code.
413 * @return string The formatted message key.
415 protected function formatKey( $key, $code ) {
416 if ( $this->doLinks ) {
417 $displayKey = ucfirst( $key );
418 if ( $code == $this->wikiCode ) {
419 return "[[{$this->linksPrefix}MediaWiki:$displayKey|$key]]";
421 return "[[{$this->linksPrefix}MediaWiki:$displayKey/$code|$key]]";
429 * Output the checks results as plain text.
431 protected function outputText() {
432 foreach ( $this->results as $code => $results ) {
433 $translated = $this->L->getMessages( $code );
434 $translated = count( $translated['translated'] );
435 foreach ( $results as $check => $messages ) {
436 $count = count( $messages );
438 if ( $check == 'untranslated' ) {
439 $translatable = $this->L->getGeneralMessages();
440 $total = count( $translatable['translatable'] );
441 } elseif ( in_array( $check, $this->nonMessageChecks() ) ) {
442 $totalCount = $this->getTotalCount();
443 $totalCount = $totalCount[$check];
444 $callback = array( $this->L, $totalCount[0] );
445 $callCode = $totalCount[1] ? $totalCount[1] : $code;
446 $total = count( call_user_func( $callback, $callCode ) );
448 $total = $translated;
450 $search = array( '$1', '$2', '$3' );
451 $replace = array( $count, $total, $code );
452 $descriptions = $this->getDescriptions();
453 echo "\n" . str_replace( $search, $replace, $descriptions[$check] ) . "\n";
454 if ( $this->level == 1 ) {
455 echo "[messages are hidden]\n";
457 foreach ( $messages as $key => $value ) {
458 if ( !in_array( $check, $this->nonMessageChecks() ) ) {
459 $key = $this->formatKey( $key, $code );
461 if ( $this->level == 2 || empty( $value ) ) {
464 echo "* $key: '$value'\n";
474 * Output the checks results as wiki text.
476 function outputWiki() {
478 $rows[] = '! Language !! Code !! Total !! ' .
479 implode( ' !! ', array_diff( $this->checks, $this->nonMessageChecks() ) );
480 foreach ( $this->results as $code => $results ) {
481 $detailTextForLang = "==$code==\n";
484 $detailTextForLangChecks = array();
485 foreach ( $results as $check => $messages ) {
486 if ( in_array( $check, $this->nonMessageChecks() ) ) {
489 $count = count( $messages );
492 $messageDetails = array();
493 foreach ( $messages as $key => $details ) {
494 $displayKey = $this->formatKey( $key, $code );
495 $messageDetails[] = $displayKey;
497 $detailTextForLangChecks[] = "=== $code-$check ===\n* " . implode( ', ', $messageDetails );
498 $numbers[] = "'''[[#$code-$check|$count]]'''";
504 if ( count( $detailTextForLangChecks ) ) {
505 $detailText .= $detailTextForLang . implode( "\n", $detailTextForLangChecks ) . "\n";
509 # Don't list languages without problems
512 $language = Language::fetchLanguageName( $code );
513 $rows[] = "| $language || $code || $problems || " . implode( ' || ', $numbers );
516 $tableRows = implode( "\n|-\n", $rows );
518 $version = SpecialVersion::getVersion( 'nodb' );
519 // @codingStandardsIgnoreStart Long line.
521 '''Check results are for:''' <code>$version</code>
524 {| class="sortable wikitable" border="2" cellpadding="4" cellspacing="0" style="background-color: #F9F9F9; border: 1px #AAAAAA solid; border-collapse: collapse; clear: both;"
531 // @codingStandardsIgnoreEnd
535 * Check if there are any results for the checks, in any language.
536 * @return bool True if there are any results, false if not.
538 protected function isEmpty() {
539 foreach ( $this->results as $results ) {
540 foreach ( $results as $messages ) {
541 if ( !empty( $messages ) ) {
552 * @ingroup MaintenanceLanguage
554 class CheckExtensionsCLI extends CheckLanguageCLI {
559 * @param $options array Options for script.
560 * @param $extension string The extension name (or names).
562 public function __construct( array $options, $extension ) {
563 if ( isset( $options['help'] ) ) {
568 if ( isset( $options['lang'] ) ) {
569 $this->code = $options['lang'];
571 global $wgLanguageCode;
572 $this->code = $wgLanguageCode;
575 if ( isset( $options['level'] ) ) {
576 $this->level = $options['level'];
579 $this->doLinks = isset( $options['links'] );
581 if ( isset( $options['wikilang'] ) ) {
582 $this->wikiCode = $options['wikilang'];
585 if ( isset( $options['whitelist'] ) ) {
586 $this->checks = explode( ',', $options['whitelist'] );
587 } elseif ( isset( $options['blacklist'] ) ) {
588 $this->checks = array_diff(
589 isset( $options['easy'] ) ? $this->easyChecks() : $this->defaultChecks(),
590 explode( ',', $options['blacklist'] )
592 } elseif ( isset( $options['easy'] ) ) {
593 $this->checks = $this->easyChecks();
595 $this->checks = $this->defaultChecks();
598 if ( isset( $options['output'] ) ) {
599 $this->output = $options['output'];
602 # Some additional checks not enabled by default
603 if ( isset( $options['duplicate'] ) ) {
604 $this->checks[] = 'duplicate';
607 $this->extensions = array();
608 $extensions = new PremadeMediawikiExtensionGroups();
609 $extensions->addAll();
610 if ( $extension == 'all' ) {
611 foreach ( MessageGroups::singleton()->getGroups() as $group ) {
612 if ( strpos( $group->getId(), 'ext-' ) === 0 && !$group->isMeta() ) {
613 $this->extensions[] = new extensionLanguages( $group );
616 } elseif ( $extension == 'wikimedia' ) {
617 $wikimedia = MessageGroups::getGroup( 'ext-0-wikimedia' );
618 foreach ( $wikimedia->wmfextensions() as $extension ) {
619 $group = MessageGroups::getGroup( $extension );
620 $this->extensions[] = new extensionLanguages( $group );
622 } elseif ( $extension == 'flaggedrevs' ) {
623 foreach ( MessageGroups::singleton()->getGroups() as $group ) {
624 if ( strpos( $group->getId(), 'ext-flaggedrevs-' ) === 0 && !$group->isMeta() ) {
625 $this->extensions[] = new extensionLanguages( $group );
629 $extensions = explode( ',', $extension );
630 foreach ( $extensions as $extension ) {
631 $group = MessageGroups::getGroup( 'ext-' . $extension );
633 $extension = new extensionLanguages( $group );
634 $this->extensions[] = $extension;
636 print "No such extension $extension.\n";
643 * Get the default checks.
644 * @return array A list of the default checks.
646 protected function defaultChecks() {
648 'untranslated', 'duplicate', 'obsolete', 'variables', 'empty', 'plural',
649 'whitespace', 'xhtml', 'chars', 'links', 'unbalanced',
654 * Get the checks which check other things than messages.
655 * @return array A list of the non-message checks.
657 protected function nonMessageChecks() {
662 * Get the checks that can easily be treated by non-speakers of the language.
663 * @return arrayA list of the easy checks.
665 protected function easyChecks() {
667 'duplicate', 'obsolete', 'empty', 'whitespace', 'xhtml', 'chars',
673 * @return string The help string.
675 protected function help() {
677 Run this script to check the status of a specific language in extensions, or
678 all of them. Command line settings are in form --parameter[=value], except for
681 * First parameter (mandatory): Extension name, multiple extension names
682 (separated by commas), "all" for all the extensions, "wikimedia" for
683 extensions used by Wikimedia or "flaggedrevs" for all FLaggedRevs
685 * lang: Language code (default: the installation default language).
686 * help: Show this help.
687 * level: Show the following display level (default: 2).
688 * links: Link the message values (default off).
689 * wikilang: For the links, what is the content language of the wiki to
690 display the output in (default en).
691 * whitelist: Do only the following checks (form: code,code).
692 * blacklist: Do not perform the following checks (form: code,code).
693 * easy: Do only the easy checks, which can be treated by non-speakers of
696 Check codes (ideally, all of them should result 0; all the checks are executed
697 by default (except language-specific check blacklists in checkLanguage.inc):
698 * untranslated: Messages which are required to translate, but are not
700 * duplicate: Messages which translation equal to fallback.
701 * obsolete: Messages which are untranslatable, but translated.
702 * variables: Messages without variables which should be used, or with
703 variables which should not be used.
704 * empty: Empty messages.
705 * whitespace: Messages which have trailing whitespace.
706 * xhtml: Messages which are not well-formed XHTML (checks only few common
708 * chars: Messages with hidden characters.
709 * links: Messages which contains broken links to pages (does not find all).
710 * unbalanced: Messages which contains unequal numbers of opening {[ and
713 Display levels (default: 2):
714 * 0: Skip the checks (useful for checking syntax).
715 * 1: Show only the stub headers and number of wrong messages, without list
717 * 2: Show only the headers and the message keys, without the message
719 * 3: Show both the headers and the complete messages, with both keys and
726 * Execute the script.
728 public function execute() {
733 * Check a language and show the results.
734 * @param $code string The language code.
735 * @throws MWException
737 protected function checkLanguage( $code ) {
738 foreach ( $this->extensions as $extension ) {
739 $this->L = $extension;
740 $this->results = array();
741 $this->results[$code] = parent::checkLanguage( $code );
743 if ( !$this->isEmpty() ) {
744 echo $extension->name() . ":\n";
746 if ( $this->level > 0 ) {
747 switch ( $this->output ) {
755 throw new MWException( "Invalid output type $this->output" );
765 // Blacklist some checks for some languages or some messages
766 // Possible keys of the sub arrays are: 'check', 'code' and 'message'.
767 $checkBlacklist = array(
770 'code' => array( 'az', 'bo', 'cdo', 'dz', 'id', 'fa', 'gan', 'gan-hans',
771 'gan-hant', 'gn', 'hak', 'hu', 'ja', 'jv', 'ka', 'kk-arab',
772 'kk-cyrl', 'kk-latn', 'km', 'kn', 'ko', 'lzh', 'mn', 'ms',
773 'my', 'sah', 'sq', 'tet', 'th', 'to', 'tr', 'vi', 'wuu', 'xmf',
774 'yo', 'yue', 'zh', 'zh-classical', 'zh-cn', 'zh-hans',
775 'zh-hant', 'zh-hk', 'zh-sg', 'zh-tw', 'zh-yue'
780 'code' => array( 'my' ),