]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/language/diffLanguage.php
MediaWiki 1.16.5
[autoinstallsdev/mediawiki.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  * @file
39  * @ingroup MaintenanceLanguage
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 $languageCode String: 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                         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 $filename String: filename of the file containing a message array
97  * @param $languageCode String: 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