b8d5e71703bf6048c4b33ff90afc1e2d429ab064
[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 * @package MediaWiki
39 * @subpackage Maintenance
40 */
41
42 /** This script run from the commandline */
43 require_once( dirname(__FILE__).'/../parserTests.inc' );
44 require_once( dirname(__FILE__).'/../commandLine.inc' );
45
46 if( isset($options['help']) ) { usage(); wfDie(); }
47
48 $wgLanguageCode = ucfirstlcrest($wgLanguageCode);
49 /** Language messages we will use as reference. By default 'en' */
50 $referenceMessages = $wgAllMessagesEn;
51 $referenceLanguage = 'En';
52 $referenceFilename = 'Language'.$referenceLanguage.'.php';
53 /** Language messages we will test. */
54 $testMessages = array();
55 $testLanguage = '';
56 /** whereas we use an external language file */
57 $externalRef = false;
58
59 # FUNCTIONS
60 /** @todo more informations !! */
61 function usage() {
62 echo 'php DiffLanguage.php [lang [file]] [--color=(yes|no|light)]'."\n";
63 }
64
65 /** Return a given string with first letter upper case, the rest lowercase */
66 function ucfirstlcrest($string) {
67 return strtoupper(substr($string,0,1)).strtolower(substr($string,1));
68 }
69
70 /**
71 * Return a $wgAllmessages array shipped in MediaWiki
72 * @param string $languageCode Formated language code
73 * @return array The MediaWiki default $wgAllMessages array requested
74 */
75 function getMediawikiMessages($languageCode = 'En') {
76
77 $foo = "wgAllMessages$languageCode";
78 global $$foo;
79 global $wgSkinNamesEn; // potentially unused global declaration?
80
81 // it might already be loaded in LocalSettings.php
82 if(!isset($$foo)) {
83 global $IP;
84 $langFile = $IP.'/languages/classes/Language'.$languageCode.'.php';
85 if (file_exists( $langFile ) ) {
86 print "Including $langFile\n";
87 global $wgNamespaceNamesEn; // potentially unused global declaration?
88 include($langFile);
89 } else wfDie("ERROR: The file $langFile does not exist !\n");
90 }
91 return $$foo;
92 }
93
94 /**
95 * Return a $wgAllmessages array in a given file. Language of the array
96 * need to be given cause we can not detect which language it provides
97 * @param string $filename Filename of the file containing a message array
98 * @param string $languageCode Language of the external array
99 * @return array A $wgAllMessages array from an external file.
100 */
101 function getExternalMessages($filename, $languageCode) {
102 print "Including external file $filename.\n";
103 include($filename);
104 $foo = "wgAllMessages$languageCode";
105 return $$foo;
106 }
107
108 # MAIN ENTRY
109 if ( isset($args[0]) ) {
110 $lang = ucfirstlcrest($args[0],1);
111
112 // eventually against another language file we will use as reference instead
113 // of the default english language.
114 if( isset($args[1])) {
115 // we assume the external file contain an array of messages for the
116 // lang we are testing
117 $referenceMessages = getExternalMessages( $args[1], $lang );
118 $referenceLanguage = $lang;
119 $referenceFilename = $args[1];
120 $externalRef = true;
121 }
122
123 // Load datas from MediaWiki
124 $testMessages = getMediawikiMessages($lang);
125 $testLanguage = $lang;
126 } else {
127 usage();
128 wfDie();
129 }
130
131 /** parsertest is used to do differences */
132 $myParserTest = new ParserTest();
133
134 # Get all references messages and check if they exist in the tested language
135 $i = 0;
136
137 $msg = "MW Language{$testLanguage}.php against ";
138 if($externalRef) { $msg .= 'external file '; }
139 else { $msg .= 'internal file '; }
140 $msg .= $referenceFilename.' ('.$referenceLanguage."):\n----\n";
141 echo $msg;
142
143 // process messages
144 foreach($referenceMessages as $index => $ref)
145 {
146 // message is not localized
147 if(!(isset($testMessages[$index]))) {
148 $i++;
149 print "'$index' => \"$ref\",\n";
150 // Messages in the same language differs
151 } elseif( ($lang == $referenceLanguage) AND ($testMessages[$index] != $ref)) {
152 print "\n$index differs:\n";
153 print $myParserTest->quickDiff($testMessages[$index],$ref,'tested','reference');
154 }
155 }
156
157 echo "\n----\n".$msg;
158 echo "$referenceLanguage language is complete at ".number_format((100 - $i/count($wgAllMessagesEn) * 100),2)."%\n";
159 echo "$i unlocalised messages of the ".count($wgAllMessagesEn)." messages available.\n";
160 ?>