<?php
-/**
- * Check a language.
- *
- * @param $languages The languages object.
- * @param $code The language code (default content language of the wiki running the script on).
- * @param: $links Show wiki links to messages (default false)?
- * @param: $wikiLanguage Language of the wiki to show the output in, if showing links (default en).
- * @param: $checks Checks to do (default all except for duplicates and plural).
- * @return Number of errors found.
- */
-function checkLanguage( $languages, $code = null, $displayLevel = 2, $links = false, $wikiLanguage = 'en', $checks = null ) {
- # Get messages
- $messages = $languages->getMessages( $code );
- $messagesNumber = count( $messages['translated'] );
- # Skip the checks if told so
- if ( $displayLevel == 0 ) {
- return;
- }
-
- # Initialize counts
- $problems = 0;
-
- # Set default language code and checks
- if ( !$code ) {
- global $wgContLang;
- $code = $wgContLang->getCode();
- }
- if ( !$checks ) {
- $checks = array( 'untranslated', 'obsolete', 'variables', 'empty', 'whitespace', 'xhtml', 'chars' );
- }
-
- # Untranslated messages
- if ( in_array( 'untranslated', $checks ) ) {
- $generalMessages = $languages->getGeneralMessages();
- $requiredMessagesNumber = count( $generalMessages['required'] );
- $untranslatedMessages = $languages->getUntranslatedMessages( $code );
- $untranslatedMessagesNumber = count( $untranslatedMessages );
- $languages->outputMessagesList( $untranslatedMessages, $code, "\n$untranslatedMessagesNumber messages of $requiredMessagesNumber are not translated to $code, but exist in en:", $displayLevel, $links, $wikiLanguage );
- $problems += $untranslatedMessagesNumber;
- }
-
- # Duplicate messages
- if ( in_array( 'duplicate', $checks ) ) {
- $duplicateMessages = $languages->getDuplicateMessages( $code );
- $duplicateMessagesNumber = count( $duplicateMessages );
- $languages->outputMessagesList( $duplicateMessages, $code, "\n$duplicateMessagesNumber messages of $messagesNumber are translated the same in en and $code:", $displayLevel, $links, $wikiLanguage );
- $problems += $duplicateMessagesNumber;
- }
-
- # Obsolete messages
- if ( in_array( 'obsolete', $checks ) ) {
- $obsoleteMessages = $messages['obsolete'];
- $obsoleteMessagesNumber = count( $obsoleteMessages );
- $languages->outputMessagesList( $obsoleteMessages, $code, "\n$obsoleteMessagesNumber messages of $messagesNumber do not exist in en (or are in the ignored list), but still exist in $code:", $displayLevel, $links, $wikiLanguage );
- $problems += $obsoleteMessagesNumber;
- }
-
- # Messages without variables
- if ( in_array( 'variables', $checks ) ) {
- $messagesWithoutVariables = $languages->getMessagesWithoutVariables( $code );
- $messagesWithoutVariablesNumber = count( $messagesWithoutVariables );
- $languages->outputMessagesList( $messagesWithoutVariables, $code, "\n$messagesWithoutVariablesNumber messages of $messagesNumber in $code don't use some variables while en uses them:", $displayLevel, $links, $wikiLanguage );
- $problems += $messagesWithoutVariablesNumber;
- }
-
- # Messages without plural
- if ( in_array( 'plural', $checks ) ) {
- $messagesWithoutPlural = $languages->getMessagesWithoutPlural( $code );
- $messagesWithoutPluralNumber = count( $messagesWithoutPlural );
- $languages->outputMessagesList( $messagesWithoutPlural, $code, "\n$messagesWithoutPluralNumber messages of $messagesNumber in $code don't use {{plural}} while en uses it:", $displayLevel, $links, $wikiLanguage );
- $problems += $messagesWithoutPluralNumber;
- }
-
- # Empty messages
- if ( in_array( 'empty', $checks ) ) {
- $emptyMessages = $languages->getEmptyMessages( $code );
- $emptyMessagesNumber = count( $emptyMessages );
- $languages->outputMessagesList( $emptyMessages, $code, "\n$emptyMessagesNumber messages of $messagesNumber in $code are empty or -:", $displayLevel, $links, $wikiLanguage );
- $problems += $emptyMessagesNumber;
- }
-
- # Messages with whitespace
- if ( in_array( 'whitespace', $checks ) ) {
- $messagesWithWhitespace = $languages->getMessagesWithWhitespace( $code );
- $messagesWithWhitespaceNumber = count( $messagesWithWhitespace );
- $languages->outputMessagesList( $messagesWithWhitespace, $code, "\n$messagesWithWhitespaceNumber messages of $messagesNumber in $code have a trailing whitespace:", $displayLevel, $links, $wikiLanguage );
- $problems += $messagesWithWhitespaceNumber;
- }
-
- # Non-XHTML messages
- if ( in_array( 'xhtml', $checks ) ) {
- $nonXHTMLMessages = $languages->getNonXHTMLMessages( $code );
- $nonXHTMLMessagesNumber = count( $nonXHTMLMessages );
- $languages->outputMessagesList( $nonXHTMLMessages, $code, "\n$nonXHTMLMessagesNumber messages of $messagesNumber in $code are not well-formed XHTML:", $displayLevel, $links, $wikiLanguage );
- $problems += $nonXHTMLMessagesNumber;
- }
-
- # Messages with wrong characters
- if ( in_array( 'chars', $checks ) ) {
- $messagesWithWrongChars = $languages->getMessagesWithWrongChars( $code );
- $messagesWithWrongCharsNumber = count( $messagesWithWrongChars );
- $languages->outputMessagesList( $messagesWithWrongChars, $code, "\n$messagesWithWrongCharsNumber messages of $messagesNumber in $code include hidden chars which should not be used in the messages:", $displayLevel, $links, $wikiLanguage );
- $problems += $messagesWithWrongCharsNumber;
- }
-
- return $problems;
-}
+# Blacklist some checks for some languages
+$checkBlacklist = array(
+#'code' => array( 'check1', 'check2' ... )
+'ja' => array( 'plural' ), // Does not use plural
+'my' => array( 'chars' ), // Uses a lot zwnj
+);
require_once( dirname(__FILE__).'/../commandLine.inc' );
require_once( 'languages.inc' );
-require_once( 'checkLanguage.inc' );
-# Show help
-if ( isset( $options['help'] ) ) {
- echo <<<ENDS
+$cli = new CheckLanguageCLI( $options );
+$cli->execute();
+
+class CheckLanguageCLI {
+ private $code = null;
+ private $level = 2;
+ private $doLinks = false;
+ private $wikiCode = 'en';
+ private $includeExif = false;
+ private $checkAll = false;
+ private $output = 'plain';
+ private $checks = array();
+
+ private $defaultChecks = array(
+ 'untranslated', 'obsolete', 'variables', 'empty', 'plural',
+ 'whitespace', 'xhtml', 'chars', 'links', 'unbalanced'
+ );
+
+ private $L = null;
+
+ /**
+ * GLOBALS: $wgLanguageCode;
+ */
+ public function __construct( Array $options ) {
+
+ if ( isset( $options['help'] ) ) {
+ echo $this->help();
+ exit();
+ }
+
+ if ( isset($options['lang']) ) {
+ $this->code = $options['lang'];
+ } else {
+ global $wgLanguageCode;
+ $this->code = $wgLanguageCode;
+ }
+
+ if ( isset($options['level']) ) {
+ $this->level = $options['level'];
+ }
+
+ $this->doLinks = isset($options['links']);
+ $this->includeExif = !isset($options['noexif']);
+ $this->checkAll = isset($options['all']);
+
+ if ( isset($options['wikilang']) ) {
+ $this->wikiCode = $options['wikilang'];
+ }
+
+ if ( isset( $options['whitelist'] ) ) {
+ $this->checks = explode( ',', $options['whitelist'] );
+ } elseif ( isset( $options['blacklist'] ) ) {
+ $this->checks = array_diff(
+ $this->defaultChecks,
+ explode( ',', $options['blacklist'] )
+ );
+ } else {
+ $this->checks = $this->defaultChecks;
+ }
+
+ if ( isset($options['output']) ) {
+ $this->output = $options['output'];
+ }
+
+ # Some additional checks not enabled by default
+ if ( isset( $options['duplicate'] ) ) {
+ $this->checks[] = 'duplicate';
+ }
+
+ $this->L = new languages( $this->includeExif );
+ }
+
+ protected function getChecks() {
+ $checks = array();
+ $checks['untranslated'] = 'getUntranslatedMessages';
+ $checks['duplicate'] = 'getDuplicateMessages';
+ $checks['obsolete'] = 'getObsoleteMessages';
+ $checks['variables'] = 'getMessagesWithoutVariables';
+ $checks['plural'] = 'getMessagesWithoutPlural';
+ $checks['empty'] = 'getEmptyMessages';
+ $checks['whitespace'] = 'getMessagesWithWhitespace';
+ $checks['xhtml'] = 'getNonXHTMLMessages';
+ $checks['chars'] = 'getMessagesWithWrongChars';
+ $checks['links'] = 'getMessagesWithDubiousLinks';
+ $checks['unbalanced'] = 'getMessagesWithUnbalanced';
+ return $checks;
+ }
+
+ protected function getDescriptions() {
+ $descriptions = array();
+ $descriptions['untranslated'] = '$1 message(s) of $2 are not translated to $3, but exist in en:';
+ $descriptions['duplicate'] = '$1 message(s) of $2 are translated the same in en and $3:';
+ $descriptions['obsolete'] = '$1 message(s) of $2 do not exist in en or are in the ignore list, but are in $3';
+ $descriptions['variables'] = '$1 message(s) of $2 in $3 don\'t use some variables that en uses:';
+ $descriptions['plural'] = '$1 message(s) of $2 in $3 don\'t use {{plural}} while en uses:';
+ $descriptions['empty'] = '$1 message(s) of $2 in $3 are empty or -:';
+ $descriptions['whitespace'] = '$1 message(s) of $2 in $3 have trailing whitespace:';
+ $descriptions['xhtml'] = '$1 message(s) of $2 in $3 contain illegal XHTML:';
+ $descriptions['chars'] = '$1 message(s) of $2 in $3 include hidden chars which should not be used in the messages:';
+ $descriptions['links'] = '$1 message(s) of $2 in $3 have problematic link(s):';
+ $descriptions['unbalanced'] = '$1 message(s) of $2 in $3 have unbalanced {[]}:';
+ return $descriptions;
+ }
+
+ protected function help() {
+ return <<<ENDS
Run this script to check a specific language file, or all of them.
+Command line settings are in form --parameter[=value].
Parameters:
- * lang: Language code (default: the installation default language). You can also specify "all" to check all the languages.
+ * lang: Language code (default: the installation default language).
+ * all: Check all customized languages
* help: Show this help.
* level: Show the following level (default: 2).
* links: Link the message values (default off).
* wikilang: For the links, what is the content language of the wiki to display the output in (default en).
- * whitelist: Make only the following checks (form: code,code).
- * blacklist: Don't make the following checks (form: code,code).
+ * whitelist: Do only the following checks (form: code,code).
+ * blacklist: Don't do the following checks (form: code,code).
* duplicate: Additionally check for messages which are translated the same to English (default off).
- * plural: Additionally check for messages that don't use plural while English does (default off).
* 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).
-Check codes (ideally, all of them should result 0; all the checks are executed by default):
+Check codes (ideally, all of them should result 0; all the checks are executed by default (except duplicate and language specific check blacklists in checkLanguage.inc):
* untranslated: Messages which are required to translate, but are not translated.
+ * duplicate: Messages which translation equal to fallback
* obsolete: Messages which are untranslatable, but translated.
* variables: Messages without variables which should be used.
* empty: Empty messages.
* whitespace: Messages which have trailing whitespace.
- * xhtml: Messages which are not well-formed XHTML.
+ * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
* chars: Messages with hidden characters.
+ * links: Messages which contains broken links to pages (does not find all).
+ * unbalanced: Messages which contains unequal numbers of opening {[ and closing ]}.
Display levels (default: 2):
* 0: Skip the checks (useful for checking syntax).
* 1: Show only the stub headers and number of wrong messages, without list of messages.
* 3: Show both the headers and the complete messages, with both keys and values.
ENDS;
- exit();
-}
+ }
-# Get the parameters
-$wgCode = isset( $options['lang'] ) ? $options['lang'] : null;
-$wgDisplayLevel = isset( $options['level'] ) ? $options['level'] : 2;
-$wgLinks = isset( $options['links'] );
-$wgWikiLanguage = isset( $options['wikilang'] ) ? $options['wikilang'] : 'en';
-$wgCheckEXIF = !isset( $options['noexif'] );
-
-# Get the checks
-$wgChecks = array( 'untranslated', 'obsolete', 'variables', 'empty', 'whitespace', 'xhtml', 'chars' );
-if ( isset( $options['whitelist'] ) ) {
- $wgChecks = explode( ',', $options['whitelist'] );
-} elseif ( isset( $options['blacklist'] ) ) {
- $wgChecks = array_diff( $wgChecks, explode( ',', $options['blacklist'] ) );
-}
-if ( isset( $options['duplicate'] ) ) {
- $wgChecks[] = 'duplicate';
-}
-if ( isset( $options['plural'] ) ) {
- $wgChecks[] = 'plural';
-}
+ private $results = array();
+
+ public function execute() {
+ $this->doChecks();
+ if ( $this->level > 0 ) {
+ switch ($this->output) {
+ case 'plain':
+ $this->outputText();
+ break;
+ case 'wiki':
+ $this->outputWiki();
+ break;
+ default:
+ throw new MWException( "Invalid output type $this->output");
+ }
+ }
+ }
-# Get language object
-$wgLanguages = new languages( $wgCheckEXIF );
+ protected function doChecks() {
+ $ignoredCodes = array( 'en', 'enRTL' );
-# Check the language
-if ( $wgCode == 'all' ) {
- foreach ( $wgLanguages->getLanguages() as $language ) {
- if ( $language != 'en' && $language != 'enRTL' ) {
- checkLanguage( $wgLanguages, $language );
+ $this->results = array();
+ # Check the language
+ if ( $this->checkAll ) {
+ foreach ( $this->L->getLanguages() as $language ) {
+ if ( !in_array($this->code, $ignoredCodes) ) {
+ $this->results[$language] = $this->checkLanguage( $language );
+ }
+ }
+ } else {
+ if ( in_array($this->code, $ignoredCodes) ) {
+ throw new MWException("Cannot check code $this->code.");
+ } else {
+ $this->results[$this->code] = $this->checkLanguage( $this->code );
+ }
}
}
-} else {
- # Can't check English or English RTL
- if ( $wgCode == 'en' ) {
- echo "Current selected language is English, which cannot be checked.\n";
- } else if ( $wgCode == 'enRTL' ) {
- echo "Current selected language is RTL English, which cannot be checked.\n";
- } else {
- checkLanguage( $wgLanguages, $wgCode, $wgDisplayLevel, $wgLinks, $wgWikiLanguage, $wgChecks );
+
+ protected function getCheckBlacklist() {
+ static $checkBlacklist = null;
+ if ( $checkBlacklist === null ) {
+ $checkBlacklist = array();
+ require( dirname(__FILE__) . '/checkLanguage.inc' );
+ }
+ return $checkBlacklist;
}
+
+ protected function checkLanguage( $code ) {
+ # Syntax check only
+ if ( $this->level === 0 ) {
+ $this->L->getMessages( $code );
+ return;
+ }
+
+ $results = array();
+ $checkFunctions = $this->getChecks();
+ $checkBlacklist = $this->getCheckBlacklist();
+ foreach ( $this->checks as $check ) {
+ if ( isset($checkBlacklist[$code]) &&
+ in_array($check, $checkBlacklist[$code]) ) {
+ $result[$check] = array();
+ continue;
+ }
+
+ $callback = array( $this->L, $checkFunctions[$check] );
+ if ( !is_callable($callback ) ) {
+ throw new MWException( "Unkown check $check." );
+ }
+ $results[$check] = call_user_func( $callback , $code );
+ }
+
+ return $results;
+ }
+
+ protected function outputText( ) {
+ foreach ( $this->results as $code => $results ) {
+ $translated = $this->L->getMessages( $code );
+ $translated = count( $translated['translated'] );
+ foreach ( $results as $check => $messages ) {
+ $count = count( $messages );
+ if ( $count ) {
+ $search = array( '$1', '$2', '$3' );
+ $replace = array( $count, $translated, $code );
+ $descriptions = $this->getDescriptions();
+ echo "\n" . str_replace( $search, $replace, $descriptions[$check] ) . "\n";
+ if ( $this->level == 1 ) {
+ echo "[messages are hidden]\n";
+ } else {
+ foreach ( $messages as $key => $value ) {
+ if ( $this->doLinks ) {
+ $displayKey = ucfirst( $key );
+ if ( $code == $this->wikiCode ) {
+ $displayKey = "[[MediaWiki:$displayKey|$key]]";
+ } else {
+ $displayKey = "[[MediaWiki:$displayKey/$code|$key]]";
+ }
+ } else {
+ $displayKey = $key;
+ }
+ if ( $this->level == 2 ) {
+ echo "* $displayKey\n";
+ } else {
+ echo "* $displayKey: '$value'\n";
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Globals: $wgContLang, $IP
+ */
+ function outputWiki() {
+ global $wgContLang, $IP;
+ $detailText = '';
+ $rows[] = '! Language !! Code !! Total !! ' . implode( ' !! ', $this->checks );
+ foreach ( $this->results as $code => $results ) {
+ $detailTextForLang = "==$code==\n";
+ $numbers = array();
+ $problems = 0;
+ $detailTextForLangChecks = array();
+ foreach ( $results as $check => $messages ) {
+ $count = count( $messages );
+ if ( $count ) {
+ $problems += $count;
+ $messageDetails = array();
+ foreach ( $messages as $key => $details ) {
+ $messageDetails[] = $key;
+ }
+ $detailTextForLangChecks[] = "===$code-$check===\n* " . implode( ', ', $messageDetails );
+ $numbers[] = "'''[[#$code-$check|$count]]'''";
+ } else {
+ $numbers[] = $count;
+ }
+
+ }
+
+ if ( count( $detailTextForLangChecks ) ) {
+ $detailText .= $detailTextForLang . implode( "\n", $detailTextForLangChecks ) . "\n";
+ }
+
+ $language = $wgContLang->getLanguageName( $code );
+ $rows[] = "| $language || $code || $problems || " . implode( ' || ', $numbers );
+ }
+
+ $tableRows = implode( "\n|-\n", $rows );
+
+ $version = SpecialVersion::getVersion( $IP );
+ echo <<<EOL
+'''Check results are for:''' <code>$version</code>
+
+
+{| class="sortable wikitable" border="2" cellpadding="4" cellspacing="0" style="background-color: #F9F9F9; border: 1px #AAAAAA solid; border-collapse: collapse; clear:both;"
+$tableRows
+|}
+
+$detailText
+
+EOL;
+ }
+
}
* @addtogroup Maintenance
*/
-require_once( 'messageTypes.inc' );
-
class languages {
protected $mLanguages; # List of languages
protected $mRawMessages; # Raw list of the messages in each language
* @param $exif Treat the EXIF messages?
*/
function __construct( $exif = true ) {
- global $wgIgnoredMessages, $wgOptionalMessages, $wgEXIFMessages;
+ require( dirname(__FILE__) . '/messageTypes.inc' );
$this->mIgnoredMessages = $wgIgnoredMessages;
if ( $exif ) {
$this->mOptionalMessages = array_merge( $wgOptionalMessages );
}
/**
- * Load the raw messages for a specific langauge from the messages file.
+ * Load the raw messages for a specific language from the messages file.
*
- * @param $code The langauge code.
+ * @param $code The language code.
*/
protected function loadRawMessages( $code ) {
if ( isset( $this->mRawMessages[$code] ) ) {
}
/**
- * Get all the messages for a specific langauge (not English), without the
+ * Get all the messages for a specific language (not English), without the
* fallback language messages, divided to groups:
* all - all the messages.
* required - messages which should be translated in order to get a complete translation.
* obsolete - messages which should not be translated, either because they are not exist, or they are ignored messages.
* translated - messages which are either required or optional, but translated from English and needed.
*
- * @param $code The langauge code.
+ * @param $code The language code.
*
* @return The messages in this language.
*/
/**
* Get the untranslated messages for a specific language.
*
- * @param $code The langauge code.
+ * @param $code The language code.
*
* @return The untranslated messages for this language.
*/
/**
* Get the duplicate messages for a specific language.
*
- * @param $code The langauge code.
+ * @param $code The language code.
*
* @return The duplicate messages for this language.
*/
return $duplicateMessages;
}
+ public function getObsoleteMessages( $code ) {
+ $this->loadGeneralMessages();
+ $this->loadMessages( $code );
+ return $this->mMessages[$code]['obsolete'];
+ }
+
/**
* Get the messages which do not use some variables.
*
- * @param $code The langauge code.
+ * @param $code The language code.
*
* @return The messages which do not use some variables in this language.
*/
/**
* Get the messages which do not use plural.
*
- * @param $code The langauge code.
+ * @param $code The language code.
*
* @return The messages which do not use plural in this language.
*/
/**
* Get the empty messages.
*
- * @param $code The langauge code.
+ * @param $code The language code.
*
* @return The empty messages for this language.
*/
/**
* Get the messages with trailing whitespace.
*
- * @param $code The langauge code.
+ * @param $code The language code.
*
* @return The messages with trailing whitespace in this language.
*/
/**
* Get the non-XHTML messages.
*
- * @param $code The langauge code.
+ * @param $code The language code.
*
* @return The non-XHTML messages for this language.
*/
/**
* Get the messages which include wrong characters.
*
- * @param $code The langauge code.
+ * @param $code The language code.
*
* @return The messages which include wrong characters in this language.
*/
return $wrongCharsMessages;
}
- /**
- * Output a messages list
- *
- * @param $messages The messages list
- * @param $code The language code
- * @param $text The text to show before the list (optional)
- * @param $level The display level (optional)
- * @param $links Show links (optional)
- * @param $wikilang The langauge of the wiki to display the list in, for the links (optional)
- */
- public function outputMessagesList( $messages, $code, $text = '', $level = 2, $links = false, $wikilang = null ) {
- if ( count( $messages ) == 0 ) {
- return;
- }
- if ( $text ) {
- echo "$text\n";
- }
- if ( $level == 1 ) {
- echo "[messages are hidden]\n";
- } else {
- foreach ( $messages as $key => $value ) {
- if ( $links ) {
- $displayKey = ucfirst( $key );
- if ( !isset( $wikilang ) ) {
- global $wgContLang;
- $wikilang = $wgContLang->getCode();
- }
- if ( $code == $wikilang ) {
- $displayKey = "[[MediaWiki:$displayKey|$key]]";
- } else {
- $displayKey = "[[MediaWiki:$displayKey/$code|$key]]";
- }
- } else {
- $displayKey = $key;
+ public function getMessagesWithDubiousLinks( $code ) {
+ $this->loadGeneralMessages();
+ $this->loadMessages( $code );
+ $tc = Title::legalChars() . '#%{}';
+ $messages = array();
+ foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
+ $matches = array();
+ preg_match_all( "/\[\[([{$tc}]+)(?:\\|(.+?))?]]/sDu", $value, $matches);
+ for ($i = 0; $i < count($matches[0]); $i++ ) {
+ if ( preg_match( "/.*project.*/isDu", $matches[1][$i]) ) {
+ $messages[$key][] = $matches[0][$i];
}
- if ( $level == 2 ) {
- echo "* $displayKey\n";
- } else {
- echo "* $displayKey: '$value'\n";
+ }
+
+
+ if ( isset( $messages[$key] ) ) {
+ $messages[$key] = implode( $messages[$key],", " );
+ }
+ }
+ return $messages;
+ }
+
+ public function getMessagesWithUnbalanced( $code ) {
+ $this->loadGeneralMessages();
+ $this->loadMessages( $code );
+ $messages = array();
+ foreach ( $this->mMessages[$code]['translated'] as $key => $value ) {
+
+ $a = $b = $c = $d = 0;
+ foreach ( preg_split('//', $value) as $char ) {
+ switch ($char) {
+ case '[': $a++; break;
+ case ']': $b++; break;
+ case '{': $c++; break;
+ case '}': $d++; break;
}
}
+
+ if ( $a !== $b || $c !== $d ) {
+ $messages[$key] = "$a, $b, $c, $d";
+ }
+
}
+ return $messages;
}
+
}
?>