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:',
185 'obsolete' => '$1 message(s) of $2 do not exist in en or are in the ignore list, but exist in $3:',
186 'variables' => '$1 message(s) of $2 in $3 don\'t match the variables used in en:',
187 'plural' => '$1 message(s) of $2 in $3 don\'t use {{plural}} while en uses:',
188 'empty' => '$1 message(s) of $2 in $3 are empty or -:',
189 'whitespace' => '$1 message(s) of $2 in $3 have trailing whitespace:',
190 'xhtml' => '$1 message(s) of $2 in $3 contain illegal XHTML:',
191 'chars' => '$1 message(s) of $2 in $3 include hidden chars which should not be used in the messages:',
192 'links' => '$1 message(s) of $2 in $3 have problematic link(s):',
193 'unbalanced' => '$1 message(s) of $2 in $3 have unbalanced {[]}:',
194 'namespace' => '$1 namespace name(s) of $2 are not translated to $3, but exist in en:',
195 'projecttalk' => '$1 namespace name(s) and alias(es) in $3 are project talk namespaces without the parameter:',
196 'magic' => '$1 magic word(s) of $2 are not translated to $3, but exist in en:',
197 'magic-old' => '$1 magic word(s) of $2 do not exist in en, but exist in $3:',
198 'magic-over' => '$1 magic word(s) of $2 in $3 do not contain the original en word(s):',
199 'magic-case' => '$1 magic word(s) of $2 in $3 change the case-sensitivity of the original en word:',
200 'special' => '$1 special page alias(es) of $2 are not translated to $3, but exist in en:',
201 'special-old' => '$1 special page alias(es) of $2 do not exist in en, but exist in $3:',
207 * @return string The help string.
209 protected function help() {
211 Run this script to check a specific language file, or all of them.
212 Command line settings are in form --parameter[=value].
214 --help: Show this help.
215 --lang: Language code (default: the installation default language).
216 --all: Check all customized languages.
217 --level: Show the following display level (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.
222 --links: Link the message values (default off).
223 --prefix: prefix to add to links.
224 --wikilang: For the links, what is the content language of the wiki to display the output in (default en).
225 --noexif: Do not check for Exif messages (a bit hard and boring to translate), if you know
226 that they are currently not translated and want to focus on other problems (default off).
227 --whitelist: Do only the following checks (form: code,code).
228 --blacklist: Do not do the following checks (form: code,code).
229 --easy: Do only the easy checks, which can be treated by non-speakers of the language.
231 Check codes (ideally, all of them should result 0; all the checks are executed by default (except language-specific check blacklists in checkLanguage.inc):
232 * untranslated: Messages which are required to translate, but are not translated.
233 * duplicate: Messages which translation equal to fallback
234 * obsolete: Messages which are untranslatable or do not exist, but are translated.
235 * variables: Messages without variables which should be used, or with variables which should not be used.
236 * empty: Empty messages and messages that contain only -.
237 * whitespace: Messages which have trailing whitespace.
238 * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
239 * chars: Messages with hidden characters.
240 * links: Messages which contains broken links to pages (does not find all).
241 * unbalanced: Messages which contains unequal numbers of opening {[ and closing ]}.
242 * namespace: Namespace names that were not translated.
243 * projecttalk: Namespace names and aliases where the project talk does not contain $1.
244 * magic: Magic words that were not translated.
245 * magic-old: Magic words which do not exist.
246 * magic-over: Magic words that override the original English word.
247 * magic-case: Magic words whose translation changes the case-sensitivity of the original English word.
248 * special: Special page names that were not translated.
249 * special-old: Special page names which do not exist.
255 * Execute the script.
257 public function execute() {
259 if ( $this->level > 0 ) {
260 switch ( $this->output ) {
268 throw new MWException( "Invalid output type $this->output" );
274 * Execute the checks.
276 protected function doChecks() {
277 $ignoredCodes = array( 'en', 'enRTL' );
279 $this->results = array();
281 if ( $this->checkAll ) {
282 foreach ( $this->L->getLanguages() as $language ) {
283 if ( !in_array( $language, $ignoredCodes ) ) {
284 $this->results[$language] = $this->checkLanguage( $language );
288 if ( in_array( $this->code, $ignoredCodes ) ) {
289 throw new MWException( "Cannot check code $this->code." );
291 $this->results[$this->code] = $this->checkLanguage( $this->code );
297 * Get the check blacklist.
298 * @return array The list of checks which should not be executed.
300 protected function getCheckBlacklist() {
301 global $checkBlacklist;
302 return $checkBlacklist;
307 * @param $code string The language code.
308 * @throws MWException
309 * @return array The results.
311 protected function checkLanguage( $code ) {
314 if ( $this->level === 0 ) {
315 $this->L->getMessages( $code );
319 $checkFunctions = $this->getChecks();
320 $checkBlacklist = $this->getCheckBlacklist();
321 foreach ( $this->checks as $check ) {
322 if ( isset( $checkBlacklist[$code] ) &&
323 in_array( $check, $checkBlacklist[$code] ) ) {
324 $results[$check] = array();
328 $callback = array( $this->L, $checkFunctions[$check] );
329 if ( !is_callable( $callback ) ) {
330 throw new MWException( "Unkown check $check." );
332 $results[$check] = call_user_func( $callback, $code );
339 * Format a message key.
340 * @param $key string The message key.
341 * @param $code string The language code.
342 * @return string The formatted message key.
344 protected function formatKey( $key, $code ) {
345 if ( $this->doLinks ) {
346 $displayKey = ucfirst( $key );
347 if ( $code == $this->wikiCode ) {
348 return "[[{$this->linksPrefix}MediaWiki:$displayKey|$key]]";
350 return "[[{$this->linksPrefix}MediaWiki:$displayKey/$code|$key]]";
358 * Output the checks results as plain text.
360 protected function outputText() {
361 foreach ( $this->results as $code => $results ) {
362 $translated = $this->L->getMessages( $code );
363 $translated = count( $translated['translated'] );
364 foreach ( $results as $check => $messages ) {
365 $count = count( $messages );
367 if ( $check == 'untranslated' ) {
368 $translatable = $this->L->getGeneralMessages();
369 $total = count( $translatable['translatable'] );
370 } elseif ( in_array( $check, $this->nonMessageChecks() ) ) {
371 $totalCount = $this->getTotalCount();
372 $totalCount = $totalCount[$check];
373 $callback = array( $this->L, $totalCount[0] );
374 $callCode = $totalCount[1] ? $totalCount[1] : $code;
375 $total = count( call_user_func( $callback, $callCode ) );
377 $total = $translated;
379 $search = array( '$1', '$2', '$3' );
380 $replace = array( $count, $total, $code );
381 $descriptions = $this->getDescriptions();
382 echo "\n" . str_replace( $search, $replace, $descriptions[$check] ) . "\n";
383 if ( $this->level == 1 ) {
384 echo "[messages are hidden]\n";
386 foreach ( $messages as $key => $value ) {
387 if ( !in_array( $check, $this->nonMessageChecks() ) ) {
388 $key = $this->formatKey( $key, $code );
390 if ( $this->level == 2 || empty( $value ) ) {
393 echo "* $key: '$value'\n";
403 * Output the checks results as wiki text.
405 function outputWiki() {
407 $rows[] = '! Language !! Code !! Total !! ' . implode( ' !! ', array_diff( $this->checks, $this->nonMessageChecks() ) );
408 foreach ( $this->results as $code => $results ) {
409 $detailTextForLang = "==$code==\n";
412 $detailTextForLangChecks = array();
413 foreach ( $results as $check => $messages ) {
414 if ( in_array( $check, $this->nonMessageChecks() ) ) {
417 $count = count( $messages );
420 $messageDetails = array();
421 foreach ( $messages as $key => $details ) {
422 $displayKey = $this->formatKey( $key, $code );
423 $messageDetails[] = $displayKey;
425 $detailTextForLangChecks[] = "=== $code-$check ===\n* " . implode( ', ', $messageDetails );
426 $numbers[] = "'''[[#$code-$check|$count]]'''";
433 if ( count( $detailTextForLangChecks ) ) {
434 $detailText .= $detailTextForLang . implode( "\n", $detailTextForLangChecks ) . "\n";
438 # Don't list languages without problems
441 $language = Language::fetchLanguageName( $code );
442 $rows[] = "| $language || $code || $problems || " . implode( ' || ', $numbers );
445 $tableRows = implode( "\n|-\n", $rows );
447 $version = SpecialVersion::getVersion( 'nodb' );
449 '''Check results are for:''' <code>$version</code>
452 {| class="sortable wikitable" border="2" cellpadding="4" cellspacing="0" style="background-color: #F9F9F9; border: 1px #AAAAAA solid; border-collapse: collapse; clear: both;"
462 * Check if there are any results for the checks, in any language.
463 * @return bool True if there are any results, false if not.
465 protected function isEmpty() {
466 foreach ( $this->results as $results ) {
467 foreach ( $results as $messages ) {
468 if ( !empty( $messages ) ) {
478 * @ingroup MaintenanceLanguage
480 class CheckExtensionsCLI extends CheckLanguageCLI {
485 * @param $options array Options for script.
486 * @param $extension string The extension name (or names).
488 public function __construct( array $options, $extension ) {
489 if ( isset( $options['help'] ) ) {
494 if ( isset( $options['lang'] ) ) {
495 $this->code = $options['lang'];
497 global $wgLanguageCode;
498 $this->code = $wgLanguageCode;
501 if ( isset( $options['level'] ) ) {
502 $this->level = $options['level'];
505 $this->doLinks = isset( $options['links'] );
507 if ( isset( $options['wikilang'] ) ) {
508 $this->wikiCode = $options['wikilang'];
511 if ( isset( $options['whitelist'] ) ) {
512 $this->checks = explode( ',', $options['whitelist'] );
513 } elseif ( isset( $options['blacklist'] ) ) {
514 $this->checks = array_diff(
515 isset( $options['easy'] ) ? $this->easyChecks() : $this->defaultChecks(),
516 explode( ',', $options['blacklist'] )
518 } elseif ( isset( $options['easy'] ) ) {
519 $this->checks = $this->easyChecks();
521 $this->checks = $this->defaultChecks();
524 if ( isset( $options['output'] ) ) {
525 $this->output = $options['output'];
528 # Some additional checks not enabled by default
529 if ( isset( $options['duplicate'] ) ) {
530 $this->checks[] = 'duplicate';
533 $this->extensions = array();
534 $extensions = new PremadeMediawikiExtensionGroups();
535 $extensions->addAll();
536 if ( $extension == 'all' ) {
537 foreach ( MessageGroups::singleton()->getGroups() as $group ) {
538 if ( strpos( $group->getId(), 'ext-' ) === 0 && !$group->isMeta() ) {
539 $this->extensions[] = new extensionLanguages( $group );
542 } elseif ( $extension == 'wikimedia' ) {
543 $wikimedia = MessageGroups::getGroup( 'ext-0-wikimedia' );
544 foreach ( $wikimedia->wmfextensions() as $extension ) {
545 $group = MessageGroups::getGroup( $extension );
546 $this->extensions[] = new extensionLanguages( $group );
548 } elseif ( $extension == 'flaggedrevs' ) {
549 foreach ( MessageGroups::singleton()->getGroups() as $group ) {
550 if ( strpos( $group->getId(), 'ext-flaggedrevs-' ) === 0 && !$group->isMeta() ) {
551 $this->extensions[] = new extensionLanguages( $group );
555 $extensions = explode( ',', $extension );
556 foreach ( $extensions as $extension ) {
557 $group = MessageGroups::getGroup( 'ext-' . $extension );
559 $extension = new extensionLanguages( $group );
560 $this->extensions[] = $extension;
562 print "No such extension $extension.\n";
569 * Get the default checks.
570 * @return array A list of the default checks.
572 protected function defaultChecks() {
574 'untranslated', 'duplicate', 'obsolete', 'variables', 'empty', 'plural',
575 'whitespace', 'xhtml', 'chars', 'links', 'unbalanced',
580 * Get the checks which check other things than messages.
581 * @return array A list of the non-message checks.
583 protected function nonMessageChecks() {
588 * Get the checks that can easily be treated by non-speakers of the language.
589 * @return arrayA list of the easy checks.
591 protected function easyChecks() {
593 'duplicate', 'obsolete', 'empty', 'whitespace', 'xhtml', 'chars',
599 * @return string The help string.
601 protected function help() {
603 Run this script to check the status of a specific language in extensions, or all of them.
604 Command line settings are in form --parameter[=value], except for the first one.
606 * 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.
607 * lang: Language code (default: the installation default language).
608 * help: Show this help.
609 * level: Show the following display level (default: 2).
610 * links: Link the message values (default off).
611 * wikilang: For the links, what is the content language of the wiki to display the output in (default en).
612 * whitelist: Do only the following checks (form: code,code).
613 * blacklist: Do not perform the following checks (form: code,code).
614 * easy: Do only the easy checks, which can be treated by non-speakers of the language.
615 Check codes (ideally, all of them should result 0; all the checks are executed by default (except language-specific check blacklists in checkLanguage.inc):
616 * untranslated: Messages which are required to translate, but are not translated.
617 * duplicate: Messages which translation equal to fallback
618 * obsolete: Messages which are untranslatable, but translated.
619 * variables: Messages without variables which should be used, or with variables which should not be used.
620 * empty: Empty messages.
621 * whitespace: Messages which have trailing whitespace.
622 * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
623 * chars: Messages with hidden characters.
624 * links: Messages which contains broken links to pages (does not find all).
625 * unbalanced: Messages which contains unequal numbers of opening {[ and closing ]}.
626 Display levels (default: 2):
627 * 0: Skip the checks (useful for checking syntax).
628 * 1: Show only the stub headers and number of wrong messages, without list of messages.
629 * 2: Show only the headers and the message keys, without the message values.
630 * 3: Show both the headers and the complete messages, with both keys and values.
636 * Execute the script.
638 public function execute() {
643 * Check a language and show the results.
644 * @param $code string The language code.
645 * @throws MWException
647 protected function checkLanguage( $code ) {
648 foreach ( $this->extensions as $extension ) {
649 $this->L = $extension;
650 $this->results = array();
651 $this->results[$code] = parent::checkLanguage( $code );
653 if ( !$this->isEmpty() ) {
654 echo $extension->name() . ":\n";
656 if ( $this->level > 0 ) {
657 switch ( $this->output ) {
665 throw new MWException( "Invalid output type $this->output" );
675 # Blacklist some checks for some languages
676 $checkBlacklist = array(
677 #'code' => array( 'check1', 'check2' ... )
678 'az' => array( 'plural' ),
679 'bo' => array( 'plural' ),
680 'dz' => array( 'plural' ),
681 'id' => array( 'plural' ),
682 'fa' => array( 'plural' ),
683 'gan' => array( 'plural' ),
684 'gan-hans' => array( 'plural' ),
685 'gan-hant' => array( 'plural' ),
686 'gn' => array( 'plural' ),
687 'hak' => array( 'plural' ),
688 'hu' => array( 'plural' ),
689 'ja' => array( 'plural' ), // Does not use plural
690 'jv' => array( 'plural' ),
691 'ka' => array( 'plural' ),
692 'kk-arab' => array( 'plural' ),
693 'kk-cyrl' => array( 'plural' ),
694 'kk-latn' => array( 'plural' ),
695 'km' => array( 'plural' ),
696 'kn' => array( 'plural' ),
697 'ko' => array( 'plural' ),
698 'lzh' => array( 'plural' ),
699 'mn' => array( 'plural' ),
700 'ms' => array( 'plural' ),
701 'my' => array( 'plural', 'chars' ), // Uses a lot zwnj
702 'sah' => array( 'plural' ),
703 'sq' => array( 'plural' ),
704 'tet' => array( 'plural' ),
705 'th' => array( 'plural' ),
706 'to' => array( 'plural' ),
707 'tr' => array( 'plural' ),
708 'vi' => array( 'plural' ),
709 'wuu' => array( 'plural' ),
710 'xmf' => array( 'plural' ),
711 'yo' => array( 'plural' ),
712 'yue' => array( 'plural' ),
713 'zh' => array( 'plural' ),
714 'zh-classical' => array( 'plural' ),
715 'zh-cn' => array( 'plural' ),
716 'zh-hans' => array( 'plural' ),
717 'zh-hant' => array( 'plural' ),
718 'zh-hk' => array( 'plural' ),
719 'zh-sg' => array( 'plural' ),
720 'zh-tw' => array( 'plural' ),
721 'zh-yue' => array( 'plural' ),