Copy querycache: encoding and a table definition change
[lhc/web/wiklou.git] / maintenance / 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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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( 'commandLine.inc' );
44
45 $wgLanguageCode = ucfirstlcrest($wgLanguageCode);
46 /** Language messages we will use as reference. By default 'en' */
47 $referenceMessages = $wgAllMessagesEn;
48 $referenceLanguage = 'En';
49 $referenceFilename = 'Language'.$referenceLanguage.'.php';
50 /** Language messages we will test. */
51 $testMessages = array();
52 $testLanguage = '';
53 /** whereas we use an external language file */
54 $externalRef = false;
55
56 # FUNCTIONS
57 /** @todo more informations !! */
58 function usage() {
59 echo "php DiffLanguage.php [lang [file]]\n";
60 }
61
62 /** Return a given string with first letter upper case, the rest lowercase */
63 function ucfirstlcrest($string) {
64 return strtoupper(substr($string,0,1)).strtolower(substr($string,1));
65 }
66
67 /**
68 * Return a $wgAllmessages array shipped in MediaWiki
69 * @param string $languageCode Formated language code
70 * @return array The MediaWiki default $wgAllMessages array requested
71 */
72 function getMediawikiMessages($languageCode = 'En') {
73
74 $foo = "wgAllMessages$languageCode";
75 global $$foo;
76
77 // it might already be loaded in LocalSettings.php
78 if(!isset($$foo)) {
79 global $IP;
80 $langFile = $IP.'/languages/Language'.$languageCode.'.php';
81 if (file_exists( $langFile ) ) {
82 print "Including $langFile\n";
83 include($langFile);
84 } else die("ERROR: The file $langFile does not exist !\n");
85 }
86 return $$foo;
87 }
88
89 /**
90 * Return a $wgAllmessages array in a given file. Language of the array
91 * need to be given cause we can not detect which language it provides
92 * @param string $filename Filename of the file containing a message array
93 * @param string $languageCode Language of the external array
94 * @return array A $wgAllMessages array from an external file.
95 */
96 function getExternalMessages($filename, $languageCode) {
97 print "Including external file $filename.\n";
98 include($filename);
99 $foo = "wgAllMessages$languageCode";
100 return $$foo;
101 }
102
103 # MAIN ENTRY
104 if ( isset($args[0]) ) {
105 $lang = ucfirstlcrest($args[0],1);
106
107 // eventually against another language file we will use as reference instead
108 // of the default english language.
109 if( isset($args[1])) {
110 // we assume the external file contain an array of messages for the
111 // lang we are testing
112 $referenceMessages = getExternalMessages( $args[1], $lang );
113 $referenceLanguage = $lang;
114 $referenceFilename = $args[1];
115 $externalRef = true;
116 }
117
118 // Load datas from MediaWiki
119 $testMessages = getMediawikiMessages($lang);
120 $testLanguage = $lang;
121 } else {
122 usage();
123 die();
124 }
125
126
127 # Get all references messages and check if they exist in the tested language
128 $i = 0;
129
130 $msg = "$testLanguage MediaWiki file against ";
131 if($externalRef) { $msg .= 'external file '; }
132 else { $msg .= 'internal file '; }
133 $msg .= $referenceFilename.' ('.$referenceLanguage."):\n----\n";
134
135 echo $msg;
136 foreach($referenceMessages as $index => $localized)
137 {
138 if(!(isset($testMessages[$index]))) {
139 $i++;
140 print "'$index' => \"$localized\",\n";
141 }
142 }
143 echo "\n----\n".$msg;
144 echo "$referenceLanguage language is complete at ".number_format((100 - $i/count($wgAllMessagesEn) * 100),2)."%\n";
145 echo "$i unlocalised messages of the ".count($wgAllMessagesEn)." messages available.\n";
146 print_r($time);
147 ?>