* Recreating the checkExtensions script, using data in the Translate extension, and...
authorRotem Liss <rotem@users.mediawiki.org>
Fri, 9 May 2008 14:16:59 +0000 (14:16 +0000)
committerRotem Liss <rotem@users.mediawiki.org>
Fri, 9 May 2008 14:16:59 +0000 (14:16 +0000)
* Moving the CheckLanguageCLI class to checkLanguage.inc.
* Several fixes in checkLanguage script.

maintenance/language/checkExtensions.php
maintenance/language/checkLanguage.inc
maintenance/language/checkLanguage.php
maintenance/language/languages.inc

index 1cfb0de..f3c6ea9 100644 (file)
 <?php
 /**
- * Copyright (C) 2007 Ashar Voultoiz <hashar@altern.org>
- *
- * Based on dumpBackup:
- * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
- *
- * http://www.mediawiki.org
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
+ * Check the extensions language files.
  *
  * @addtogroup Maintenance
  */
 
-#
-# Lacking documentation. Examples:
-# php checkExtensions.php /opt/mw/extensions/CentralAuth/CentralAuth.i18n.php wgCentralAuthMessages
-# php checkExtensions.php --extdir /opt/mw/extensions/
-#
-# BUGS: cant guess registered extensions :)
-# TODO: let users set parameters to configure checklanguage.inc
-
-// Filename for the extension i18n files database:
-define( 'EXT_I18N_DB', 'i18n.db' );
-
-$optionsWithArgs = array( 'extdir', 'lang' );
-
 require_once( dirname(__FILE__).'/../commandLine.inc' );
 require_once( 'languages.inc' );
 require_once( 'checkLanguage.inc' );
 
-
-class extensionLanguages extends languages {
-       private $mExt18nFilename, $mExtArrayName ;
-       private $mExtArray;
-
-       function __construct( $ext18nFilename, $extArrayName ) {
-               $this->mExt18nFilename = $ext18nFilename;
-               $this->mExtArrayName = $extArrayName;
-
-               $this->mIgnoredMessages = array();
-               $this->mOptionalMessages = array();
-
-               if ( file_exists( $this->mExt18nFilename ) ) {
-                       require_once( $this->mExt18nFilename );
-
-                       $foundarray = false;
-                       if( isset( ${$this->mExtArrayName} ) )  {
-                               // File provided in the db file
-                               $foundarray = ${$this->mExtArrayName};
-                       } else {
-
-                               /* For extensions included elsewhere. For some reason other extensions
-                                * break with the global statement, so recheck here.
-                                */
-                               global ${$this->mExtArrayName};
-                               if( is_array( ${$this->mExtArrayName} ) )  {
-                                       $foundarray = ${$this->mExtArrayName};
-                               }
-
-                               /* we might have been given a function name, test it too */
-                               if( function_exists( $this->mExtArrayName  ) ) {
-                                       // Load data
-                                       $funcName = $this->mExtArrayName ;
-                                       $foundarray = $funcName();
-                               }
-
-                               if(!$foundarray) {
-                                       // Provided array could not be found we try to guess it.
-
-                                       # Using the extension path ($m[1]) and filename ($m[2]):
-                                       $m = array();
-                                       preg_match( '%.*/(.*)/(.*).i18n\.php%', $this->mExt18nFilename, $m);
-                                       $arPathCandidate = 'wg' . $m[1].'Messages';
-                                       $arFileCandidate = 'wg' . $m[2].'Messages';
-                                       $funcCandidate = "ef{$m[2]}Messages";
-
-                                       // Try them:
-                                       if( isset($$arPathCandidate) && is_array( $$arPathCandidate ) ) {
-                                               print "warning> messages from guessed path array \$$arPathCandidate.\n";
-                                               $foundarray = $$arPathCandidate;
-                                       } elseif( isset($$arFileCandidate) && is_array( $$arFileCandidate ) ) {
-                                               print "warning> messages from guessed file array \$$arFileCandidate.\n";
-                                               $foundarray = $$arFileCandidate;
-                                       } elseif( function_exists( $funcCandidate ) ) {
-                                               print "warning> messages build from guessed function {$funcCandidate}().\n";
-                                               $foundarray = $funcCandidate();
-                                       }
-                               }
-
-                               # We are unlucky, return empty stuff
-                               if(!$foundarray) {
-                                       print "ERROR> failed to guess an array to use.\n";
-                                       $this->mExtArray = null;
-                                       $this->mLanguages = null;
-                                       return;
-                               }
-                       }
-
-                       $this->mExtArray = $foundarray ;
-                       $this->mLanguages = array_keys( $this->mExtArray );
-               } else {
-                       wfDie( "File $this->mExt18nFilename not found\n" );
-               }
-       }
-
-       protected function loadRawMessages( $code ) {
-               if ( isset( $this->mRawMessages[$code] ) ) {
-                       return;
-               }
-               if( isset( $this->mExtArray[$code] ) ) {
-                       $this->mRawMessages[$code] = $this->mExtArray[$code] ;
-               } else {
-                       $this->mRawMessages[$code] = array();
-               }
-       }
-
-       public function getLanguages() {
-               return $this->mLanguages;
-       }
-}
-
-/**
- * @param $filename Filename containing the extension i18n
- * @param $arrayname The name of the array in the filename
- * @param $filter Optional, restrict check to a given language code (default; null)
- */
-function checkExtensionLanguage( $filename, $arrayname, $filter = null ) {
-       $extLanguages = new extensionLanguages($filename, $arrayname);
-
-       $langs = $extLanguages->getLanguages();
-       if( !$langs ) {
-               print "ERROR> \$$arrayname array does not exist.\n";
-               return false;
-       }
-
-       $nErrors = 0;
-       if( $filter ) {
-               $nErrors += checkLanguage( $extLanguages, $filter );
-       } else {
-               print "Will check ". count($langs) . " languages : " . implode(' ', $langs) .".\n";
-               foreach( $langs as $lang ) {
-                       if( $lang == 'en' ) {
-                               #print "Skipped english language\n";
-                               continue;
-                       }
-
-                       $nErrors += checkLanguage( $extLanguages, $lang );
-               }
-       }
-
-       return $nErrors;
-}
-
-/**
- * Read the db file, parse it, start the check.
- */
-function checkExtensionRepository( $extdir, $db ) {
-       $fh = fopen( $extdir. '/' . $db, 'r' );
-
-       $line_number = 0;
-       while( $line = fgets( $fh ) ) {
-               $line_number++;
-
-               // Ignore comments
-               if( preg_match( '/^#/', $line ) ) {
-                       continue;
-               }
-
-               // Load data from i18n database
-               $data = split( ' ', chop($line) );
-               $i18n_file = @$data[0];
-               $arrayname = @$data[1];
-
-               print "------------------------------------------------------\n";
-               print "Checking $i18n_file (\$$arrayname).\n";
-
-               // Check data
-               if( !file_exists( $extdir . '/' . $i18n_file ) ) {
-                       print "ERROR> $i18n_file not found ($db:$line_number).\n";
-                       continue;
-               }
-#              if( $arrayname == '' ) {
-#                      print "warning> no array name for $i18n_file ($db:$line_number).\n";
-#              }
-
-               $i18n_file = $extdir . '/' . $i18n_file ;
-
-               global $myLang;
-               $nErrors = checkExtensionLanguage( $i18n_file, $arrayname, $myLang );
-               if($nErrors == 1 ) {
-                       print "\nFound $nErrors error for this extension.\n";
-               } elseif($nErrors) {
-                       print "\nFound $nErrors errors for this extension.\n";
-               } else {
-                       print "Looks OK.\n";
-               }
-
-               print "\n";
-       }
-}
-
-
-function usage() {
-// Usage
-print <<<END
-Usage:
-    php checkExtensions.php <filename> <arrayname>
-    php checkExtensions.php --extdir <extension repository>
-
-Common option:
-    --lang <language code> : only check the given language.
-
+if( !class_exists( 'MessageGroups' ) ) {
+       echo <<<END
+Please add the Translate extension to LocalSettings.php, and enable the extension groups:
+       require_once( 'extensions/Translate/Translate.php' );
+       \$wgTranslateEC = array_keys( \$wgTranslateAC );
 
 END;
-die;
-}
-
-// Play with options and arguments
-$myLang = isset($options['lang']) ? $options['lang'] : null;
-
-if( isset( $options['extdir'] ) ) {
-       $extdb = $options['extdir'] . '/' . EXT_I18N_DB ;
-
-       if( file_exists( $extdb ) ) {
-               checkExtensionRepository( $options['extdir'], EXT_I18N_DB );
-       } else {
-               print "$extdb does not exist\n";
-       }
-
-} else {
-       // Check arguments
-       if ( isset( $argv[0] ) ) {
-
-               if (file_exists( $argv[0] ) ) {
-                       $filename = $argv[0];
-               } else {
-                       print "Unable to open file '{$argv[0]}'\n";
-                       usage();
-               }
-
-               if ( isset( $argv[1] ) ) {
-                       $arrayname = $argv[1];
-               } else {
-                       print "You must give an array name to be checked\n";
-                       usage();
-               }
-
-               global $myLang;
-               checkExtensionLanguage( $filename, $arrayname, $myLang );
-       } else {
-               usage();
-       }
+       exit(-1);
 }
 
-
+$cli = new CheckExtensionsCLI( $options, $argv[0] );
+$cli->execute();
index 4213afd..0571bda 100644 (file)
@@ -1,5 +1,433 @@
 <?php
 
+class CheckLanguageCLI {
+       protected $code  = null;
+       protected $level = 2;
+       protected $doLinks = false;
+       protected $wikiCode = 'en';
+       protected $checkAll = false;
+       protected $output = 'plain';
+       protected $checks = array();
+       protected $L = null;
+
+       protected $defaultChecks = array(
+               'untranslated', 'obsolete', 'variables', 'empty', 'plural',
+               'whitespace', 'xhtml', 'chars', 'links', 'unbalanced'
+       );
+
+       protected $results = array();
+
+       private $includeExif = false;
+
+       /**
+        * 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).
+       * 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: 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).
+       * 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 (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 (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.
+       * 2: Show only the headers and the message keys, without the message values.
+       * 3: Show both the headers and the complete messages, with both keys and values.
+
+ENDS;
+       }
+
+       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");
+                       }
+               }
+       }
+
+       protected function doChecks() {
+               $ignoredCodes = array( 'en', 'enRTL' );
+
+               $this->results = array();
+               # Check the language
+               if ( $this->checkAll ) {
+                       foreach ( $this->L->getLanguages() as $language ) {
+                               if ( !in_array($language, $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 );
+                       }
+               }
+       }
+
+       protected function getCheckBlacklist() {
+               global $checkBlacklist;
+               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 formatKey( $key, $code ) {
+               if ( $this->doLinks ) {
+                       $displayKey = ucfirst( $key );
+                       if ( $code == $this->wikiCode ) {
+                               return "[[MediaWiki:$displayKey|$key]]";
+                       } else {
+                               return "[[MediaWiki:$displayKey/$code|$key]]";
+                       }
+               } else {
+                       return $key;
+               }
+       }
+
+       protected function outputText() {
+               foreach ( $this->results as $code => $results ) {
+                       $translated = $this->L->getMessages( $code );
+                       $translated = count( $translated['translated'] );
+                       $translatable = $this->L->getGeneralMessages();
+                       $translatable = count( $translatable['translatable'] );
+                       foreach ( $results as $check => $messages ) {
+                               $count = count( $messages );
+                               if ( $count ) {
+                                       $search = array( '$1', '$2', '$3' );
+                                       $replace = array( $count, $check == 'untranslated' ? $translatable: $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 ) {
+                                                       $displayKey = $this->formatKey( $key, $code );
+                                                       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 ) {
+                                               $displayKey = $this->formatKey( $key, $code );
+                                               $messageDetails[] = $displayKey;
+                                       }
+                                       $detailTextForLangChecks[] = "===$code-$check===\n* " . implode( ', ', $messageDetails );
+                                       $numbers[] = "'''[[#$code-$check|$count]]'''";
+                               } else {
+                                       $numbers[] = $count;
+                               }
+
+                       }
+
+                       if ( count( $detailTextForLangChecks ) ) {
+                               $detailText .= $detailTextForLang . implode( "\n", $detailTextForLangChecks ) . "\n";
+                       }
+
+                       if ( !$problems ) { continue; } // Don't list languages without problems
+                       $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;
+       }
+}
+
+class CheckExtensionsCLI extends CheckLanguageCLI {
+       private $extensions;
+
+       public function __construct( Array $options, $extension ) {
+               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']);
+
+               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';
+               }
+
+               if( $extension == 'all' ) {
+                       $this->extensions = array();
+                       foreach( MessageGroups::singleton()->getGroups() as $group ) {
+                               if( strpos( $group->getId(), 'ext-' ) === 0 && !$group->isMeta() ) {
+                                       $this->extensions[] = new extensionLanguages( $group );
+                               }
+                       }
+               } else {
+                       $group = MessageGroup::getGroup( 'ext-' . $extension );
+                       $extension = new extensionLanguages( $group );
+                       $this->extensions = array( $extension );
+               }
+       }
+
+       protected function help() {
+               return <<<ENDS
+Run this script to check the status of a specific language in extensions, or all of them.
+Command line settings are in form --parameter[=value], except for the first one.
+Parameters:
+       * First parameter (mandatory): Extension name, or "all" for all the extensions.
+       * lang: Language code (default: the installation default language).
+       * 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: 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).
+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 (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.
+       * 2: Show only the headers and the message keys, without the message values.
+       * 3: Show both the headers and the complete messages, with both keys and values.
+
+ENDS;
+       }
+
+       public function execute() {
+               $this->doChecks();
+       }
+
+       protected function checkLanguage( $code ) {
+               foreach( $this->extensions as $extension ) {
+                       echo $extension->name() . ":\n";
+
+                       $this->L = $extension;
+                       $this->results = array();
+                       $this->results[$code] = parent::checkLanguage( $code );
+
+                       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" );
+                               }
+                       }
+
+                       echo "\n";
+               }
+       }
+}
+
 # Blacklist some checks for some languages
 $checkBlacklist = array(
 #'code'        => array( 'check1', 'check2' ... )
index ee44158..daade68 100644 (file)
@@ -6,313 +6,8 @@
  */
 
 require_once( dirname(__FILE__).'/../commandLine.inc' );
+require_once( 'checkLanguage.inc' );
 require_once( 'languages.inc' );
 
 $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).
-       * 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: 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).
-       * 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 (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 (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.
-       * 2: Show only the headers and the message keys, without the message values.
-       * 3: Show both the headers and the complete messages, with both keys and values.
-
-ENDS;
-       }
-
-       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");
-                       }
-               }
-       }
-
-       protected function doChecks() {
-               $ignoredCodes = array( 'en', 'enRTL' );
-
-               $this->results = array();
-               # Check the language
-               if ( $this->checkAll ) {
-                       foreach ( $this->L->getLanguages() as $language ) {
-                               if ( !in_array($language, $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 );
-                       }
-               }
-       }
-
-       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 formatKey( $key, $code ) {
-               if ( $this->doLinks ) {
-                       $displayKey = ucfirst( $key );
-                       if ( $code == $this->wikiCode ) {
-                               return "[[MediaWiki:$displayKey|$key]]";
-                       } else {
-                               return "[[MediaWiki:$displayKey/$code|$key]]";
-                       }
-               } else {
-                       return $key;
-       }                                               }
-
-       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 ) {
-                                                       $displayKey = $this->formatKey( $key, $code );
-                                                       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 ) {
-                                               $displayKey = $this->formatKey( $key, $code );
-                                               $messageDetails[] = $displayKey;
-                                       }
-                                       $detailTextForLangChecks[] = "===$code-$check===\n* " . implode( ', ', $messageDetails );
-                                       $numbers[] = "'''[[#$code-$check|$count]]'''";
-                               } else {
-                                       $numbers[] = $count;
-                               }
-
-                       }
-
-                       if ( count( $detailTextForLangChecks ) ) {
-                               $detailText .= $detailTextForLang . implode( "\n", $detailTextForLangChecks ) . "\n";
-                       }
-
-                       if ( !$problems ) { continue; } // Don't list languages without problems
-                       $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;
-       }
-
-}
index 9472e25..0ed4464 100644 (file)
@@ -418,4 +418,46 @@ class languages {
 
 }
 
-?>
+class extensionLanguages extends languages {
+       private $mMessageGroup; # The message group
+
+       /**
+        * Load the messages group.
+        * @param $group The messages group.
+        */
+       function __construct( MessageGroup $group ) {
+               $this->mMessageGroup = $group;
+
+               $bools = $this->mMessageGroup->getBools();
+               $this->mIgnoredMessages = $bools['ignored'];
+               $this->mOptionalMessages = $bools['optional'];
+       }
+
+       /**
+        * Get the extension name.
+        *
+        * @return The extension name.
+        */
+       public function name() {
+               return $this->mMessageGroup->getLabel();
+       }
+
+       /**
+        * Load the raw messages for a specific language.
+        *
+        * @param $code The language code.
+        */
+       protected function loadRawMessages( $code ) {
+               if ( isset( $this->mRawMessages[$code] ) ) {
+                       return;
+               }
+               $definitions = $this->mMessageGroup->getDefinitions();
+               $this->mRawMessages[$code] = array();
+               foreach( $definitions as $key => $value ) {
+                       $message = $this->mMessageGroup->getMessage( $key, $code );
+                       if( $message !== null ) {
+                               $this->mRawMessages[$code][$key] = $message;
+                       }
+               }
+       }
+}