Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / maintenance / language / diffLanguage.php
1 <?php
2 # MediaWiki web-based config/installation
3 # Copyright (C) 2004 Ashar Voultoiz <thoane@altern.org> and others
4 # http://www.mediawiki.org/
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
20
21 /**
22 * Usage: php DiffLanguage.php [lang [file]]
23 *
24 * lang: Enter the language code following "Language" of the LanguageXX.php you
25 * want to check. If using linux you might need to follow case aka Zh and not
26 * zh.
27 *
28 * file: A php language file you want to include to compare mediawiki
29 * Language{Lang}.php against (for example Special:Allmessages PHP output).
30 *
31 * The goal is to get a list of messages not yet localised in a languageXX.php
32 * file using the language.php file as reference.
33 *
34 * The script then print a list of wgAllMessagesXX keys that aren't localised, a
35 * percentage of messages correctly localised and the number of messages to be
36 * translated.
37 *
38 * @addtogroup Maintenance
39 */
40
41 /** This script run from the commandline */
42 require_once( dirname(__FILE__).'/../parserTests.inc' );
43 require_once( dirname(__FILE__).'/../commandLine.inc' );
44
45 if( isset($options['help']) ) { usage(); wfDie(); }
46
47 $wgLanguageCode = ucfirstlcrest($wgLanguageCode);
48 /** Language messages we will use as reference. By default 'en' */
49 $referenceMessages = $wgAllMessagesEn;
50 $referenceLanguage = 'En';
51 $referenceFilename = 'Language'.$referenceLanguage.'.php';
52 /** Language messages we will test. */
53 $testMessages = array();
54 $testLanguage = '';
55 /** whereas we use an external language file */
56 $externalRef = false;
57
58 # FUNCTIONS
59 /** @todo more informations !! */
60 function usage() {
61 echo 'php DiffLanguage.php [lang [file]] [--color=(yes|no|light)]'."\n";
62 }
63
64 /** Return a given string with first letter upper case, the rest lowercase */
65 function ucfirstlcrest($string) {
66 return strtoupper(substr($string,0,1)).strtolower(substr($string,1));
67 }
68
69 /**
70 * Return a $wgAllmessages array shipped in MediaWiki
71 * @param string $languageCode Formated language code
72 * @return array The MediaWiki default $wgAllMessages array requested
73 */
74 function getMediawikiMessages($languageCode = 'En') {
75
76 $foo = "wgAllMessages$languageCode";
77 global $$foo;
78 global $wgSkinNamesEn; // potentially unused global declaration?
79
80 // it might already be loaded in LocalSettings.php
81 if(!isset($$foo)) {
82 global $IP;
83 $langFile = $IP.'/languages/classes/Language'.$languageCode.'.php';
84 if (file_exists( $langFile ) ) {
85 print "Including $langFile\n";
86 global $wgNamespaceNamesEn; // potentially unused global declaration?
87 include($langFile);
88 } else wfDie("ERROR: The file $langFile does not exist !\n");
89 }
90 return $$foo;
91 }
92
93 /**
94 * Return a $wgAllmessages array in a given file. Language of the array
95 * need to be given cause we can not detect which language it provides
96 * @param string $filename Filename of the file containing a message array
97 * @param string $languageCode Language of the external array
98 * @return array A $wgAllMessages array from an external file.
99 */
100 function getExternalMessages($filename, $languageCode) {
101 print "Including external file $filename.\n";
102 include($filename);
103 $foo = "wgAllMessages$languageCode";
104 return $$foo;
105 }
106
107 # MAIN ENTRY
108 if ( isset($args[0]) ) {
109 $lang = ucfirstlcrest($args[0],1);
110
111 // eventually against another language file we will use as reference instead
112 // of the default english language.
113 if( isset($args[1])) {
114 // we assume the external file contain an array of messages for the
115 // lang we are testing
116 $referenceMessages = getExternalMessages( $args[1], $lang );
117 $referenceLanguage = $lang;
118 $referenceFilename = $args[1];
119 $externalRef = true;
120 }
121
122 // Load datas from MediaWiki
123 $testMessages = getMediawikiMessages($lang);
124 $testLanguage = $lang;
125 } else {
126 usage();
127 wfDie();
128 }
129
130 /** parsertest is used to do differences */
131 $myParserTest = new ParserTest();
132
133 # Get all references messages and check if they exist in the tested language
134 $i = 0;
135
136 $msg = "MW Language{$testLanguage}.php against ";
137 if($externalRef) { $msg .= 'external file '; }
138 else { $msg .= 'internal file '; }
139 $msg .= $referenceFilename.' ('.$referenceLanguage."):\n----\n";
140 echo $msg;
141
142 // process messages
143 foreach($referenceMessages as $index => $ref)
144 {
145 // message is not localized
146 if(!(isset($testMessages[$index]))) {
147 $i++;
148 print "'$index' => \"$ref\",\n";
149 // Messages in the same language differs
150 } elseif( ($lang == $referenceLanguage) AND ($testMessages[$index] != $ref)) {
151 print "\n$index differs:\n";
152 print $myParserTest->quickDiff($testMessages[$index],$ref,'tested','reference');
153 }
154 }
155
156 echo "\n----\n".$msg;
157 echo "$referenceLanguage language is complete at ".number_format((100 - $i/count($wgAllMessagesEn) * 100),2)."%\n";
158 echo "$i unlocalised messages of the ".count($wgAllMessagesEn)." messages available.\n";
159 ?>