documentation
[lhc/web/wiklou.git] / maintenance / DiffLanguage.php
index b015591..98befd9 100644 (file)
 # MediaWiki web-based config/installation
 # Copyright (C) 2004 Ashar Voultoiz <thoane@altern.org> and others
 # 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 
+# 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.,
 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 # http://www.gnu.org/copyleft/gpl.html
 
+/**
+ * Usage: php DiffLanguage.php [lang [file]]
+ *
+ * lang: Enter the language code following "Language" of the LanguageXX.php you
+ * want to check. If using linux you might need to follow case aka Zh and not
+ * zh.
+ *
+ * file: A php language file you want to include to compare mediawiki
+ * Language{Lang}.php against (for example Special:Allmessages PHP output).
+ *
+ * The goal is to get a list of messages not yet localised in a languageXX.php
+ * file using the language.php file as reference.
+ * 
+ * The script then print a list of wgAllMessagesXX keys that aren't localised, a
+ * percentage of messages correctly localised and the number of messages to be
+ * translated.
+ * 
+ * @package MediaWiki
+ * @subpackage Maintenance
+ */
 
-# This script is an alpha version.
-#
-# The goal is to get a list of messages not yet localised in a
-# languageXX.php file using the language.php file as reference.
-#
-# Usage:
-# php DiffLanguage.php
-#
-# Enter the language code following "Language" of the LanguageXX.php
-# you want to check. If using linux you might need to follow case aka
-# Zh and not zh.
-#
-# The script then print a list of wgAllMessagesXX keys that aren't
-# localised, a percentage of messages correctly localised and the
-# number of messages to be translated.
-
-require_once( "commandLine.inc" );
+/** This script run from the commandline */
+require_once( 'commandLine.inc' );
 
-$wgLanguageCode = strtoupper(substr($wgLanguageCode,0,1)).strtolower(substr($wgLanguageCode,1));
+$wgLanguageCode = ucfirstlcrest($wgLanguageCode);
+/** Language messages we will use as reference. By default 'en' */
+$referenceMessages = $wgAllMessagesEn;
+$referenceLanguage = 'En';
+$referenceFilename = 'Language'.$referenceLanguage.'.php';
+/** Language messages we will test. */ 
+$testMessages = array();
+$testLanguage = '';
+/** whereas we use an external language file */
+$externalRef = false;
 
-# read command line argument
-if ( isset($args[0]) ) {
-       $lang = $args[0];
+# FUNCTIONS
+/** @todo more informations !! */
+function usage() {
+       echo "php DiffLanguage.php [lang [file]]\n";    
+}
 
-# or prompt a simple menu
-} else {
-       $loop = true;
-       do {
-               @ob_end_flush();
-               print "Enter the language you want to check [$wgLanguageCode]:";
-               $input = readconsole();
-               
-               # set the input to current language
-               if($input == "") {
-                       $input = $wgLanguageCode;
-               }
-               
-               # convert to 1st char upper, rest lower case
-               $input = strtoupper(substr($input,0,1)).strtolower(substr($input,1));
-               
-               # try to get the file           
-               if( file_exists("../languages/Language$input.php") ) {
-                       $loop = false;
-                       $lang = $input;
-               } else {
-                       print "ERROR: The file Language$input.php doesn't exist !\n";
-               }
-               
-       } while ($loop);
-       
+/** Return a given string with first letter upper case, the rest lowercase */
+function ucfirstlcrest($string) {
+       return strtoupper(substr($string,0,1)).strtolower(substr($string,1));
 }
 
-/* TODO
-       Need to check case of the $lang : 1st char upper 2nd char lower
-*/
+/**
+ * Return a $wgAllmessages array shipped in MediaWiki
+ * @param string $languageCode Formated language code
+ * @return array The MediaWiki default $wgAllMessages array requested
+ */
+function getMediawikiMessages($languageCode = 'En') {
 
+       $foo = "wgAllMessages$languageCode";
+       global $$foo;
 
-# include the language if it's not the already loaded one
-if($lang != $wgLanguageCode) {
-       print "Including language file for $lang.\n";
-       include_once("Language{$lang}.php");
+       // it might already be loaded in LocalSettings.php
+       if(!isset($$foo)) {
+               global $IP;
+               $langFile = $IP.'/languages/Language'.$languageCode.'.php';
+               if (file_exists( $langFile ) ) {
+                       print "Including $langFile\n";
+                       include($langFile);
+               } else die("ERROR: The file $langFile does not exist !\n");
+       }
+       return $$foo;
 }
 
-/* ugly hack to load the correct array, if you have a better way
-to achieve the same goal, recode it ! */
-$foo = "wgAllMessages$lang";
-$testme = &$$foo;
-/* end of ugly hack */
+/**
+ * Return a $wgAllmessages array in a given file. Language of the array
+ * need to be given cause we can not detect which language it provides
+ * @param string $filename Filename of the file containing a message array
+ * @param string $languageCode Language of the external array
+ * @return array A $wgAllMessages array from an external file.
+ */
+function getExternalMessages($filename, $languageCode) {
+       print "Including external file $filename.\n";
+       include($filename);
+       $foo = "wgAllMessages$languageCode";
+       return $$foo;
+}
+
+# MAIN ENTRY
+if ( isset($args[0]) ) {
+       $lang = ucfirstlcrest($args[0],1);
+
+       // eventually against another language file we will use as reference instead
+       // of the default english language.
+       if( isset($args[1])) {
+               // we assume the external file contain an array of messages for the
+               // lang we are testing
+               $referenceMessages = getExternalMessages( $args[1], $lang );
+               $referenceLanguage = $lang;
+               $referenceFilename = $args[1];
+               $externalRef = true;
+       }
+
+       // Load datas from MediaWiki
+       $testMessages = getMediawikiMessages($lang);
+       $testLanguage = $lang;          
+} else {
+       usage();
+       die();
+}
 
 
 # Get all references messages and check if they exist in the tested language
 $i = 0;
-print "\nChecking $lang localisation file against reference (en):\n----\n";
-foreach($wgAllMessagesEn as $index => $localized)
+
+$msg = "$testLanguage MediaWiki file against ";
+if($externalRef) { $msg .= 'external file '; }
+else { $msg .= 'internal file '; }
+$msg .= $referenceFilename.' ('.$referenceLanguage."):\n----\n";
+
+echo $msg;
+foreach($referenceMessages as $index => $localized)
 {
-       if(!(isset($testme[$index]))) {
+       if(!(isset($testMessages[$index]))) {
                $i++;
-
-               echo "$index\n";
+               print "'$index' => \"$localized\",\n";
        }
 }
-echo "----\n";
-echo "$lang language is complete at ".number_format((100 - $i/count($wgAllMessagesEn) * 100),2)."%\n";
-echo "$i unlocalised message of the ".count($wgAllMessagesEn)." messages available.\n";
+echo "\n----\n".$msg;
+echo "$referenceLanguage language is complete at ".number_format((100 - $i/count($wgAllMessagesEn) * 100),2)."%\n";
+echo "$i unlocalised messages of the ".count($wgAllMessagesEn)." messages available.\n";
+print_r($time);
+?>
\ No newline at end of file