* Use languages class.
[lhc/web/wiklou.git] / maintenance / transstat.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Maintenance
5 *
6 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
7 * @author Ashar Voultoiz <thoane@altern.org>
8 * @bug 2499
9 *
10 * Output is posted from time to time on:
11 * http://meta.wikimedia.org/wiki/Localization_statistics
12 */
13
14 /** */
15 require_once('commandLine.inc');
16 require_once('languages.inc');
17
18 if( isset($options['help']) ) { usage(); die(); }
19 // default output is WikiText
20 if( !isset($options['output']) ) { $options['output']='wiki'; }
21
22
23 /** Print a usage message*/
24 function usage() {
25 print <<<END
26 Usage: php transstat.php [--help] [--output:csv|text|wiki] [--noredundant]
27 --help : this helpful message
28 --output : select an output engine one of:
29 * 'csv' : Comma Separated Values.
30 * 'wiki' : MediaWiki syntax.
31 * 'text' : Text with tabs.
32 Default output is 'wiki'
33 --noredundant : do NOT calculate redundant (takes some time)
34
35
36 END;
37 }
38
39
40 /** A general output object. Need to be overriden */
41 class statsOutput {
42 var $output; // buffer that contain the text
43 function statsOutput() { $this->output='';}
44 function getContent() { return $this->output;}
45
46 function formatPercent($subset, $total, $revert=false, $accuracy=2) {
47 return @sprintf( '%.' . $accuracy . 'f%%', 100 * $subset / $total );
48 }
49
50 // Override the next methods
51 function heading() {}
52 function footer() {}
53 function blockstart() {}
54 function blockend() {}
55 function element($in, $heading=false) {}
56 }
57
58
59 /** Outputs WikiText */
60 class wikiStatsOutput extends statsOutput {
61 function heading() {
62 $this->output .= "{| border=2 cellpadding=4 cellspacing=0 style=\"background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse;\" width=100%\n";
63 }
64 function footer() { $this->output .= "|}\n"; }
65 function blockstart() { $this->output .= "|-\n"; }
66 function blockend() { $this->output .= ''; }
67 function element($in, $heading = false) {
68 $this->output .= ($heading ? '!' : '|') . " $in\n";
69 }
70 function formatPercent($subset, $total, $revert=false, $accuracy=2) {
71 $v = @round(255 * $subset / $total);
72 if($revert) $v = 255 - $v;
73 if($v < 128) {
74 // red to yellow
75 $red = 'FF';
76 $green = sprintf('%02X', 2*$v);
77 } else {
78 // yellow to green
79 $red = sprintf('%02X', 2*(255 -$v) );
80 $green = 'FF';
81 }
82 $blue = '00';
83 $color = $red.$green.$blue;
84
85 $percent = statsOutput::formatPercent($subset, $total, $revert, $accuracy);
86 return 'bgcolor="#'.$color.'" | '.$percent;
87 }
88 }
89
90 /** Output text. To be used on a terminal for example. */
91 class textStatsOutput extends statsOutput {
92 function element($in, $heading = false) {
93 $this->output .= $in."\t";
94 }
95 function blockend(){ $this->output .="\n";}
96 }
97
98 /** csv output. Some people love excel */
99 class csvStatsOutput extends statsOutput {
100 function element($in, $heading = false) {
101 $this->output .= $in.";";
102 }
103 function blockend(){ $this->output .="\n";}
104 }
105
106
107 /** FIXME: This takes an obscene amount of time */
108 if(isset($options['noredundant'])) {
109 function redundant(&$arr) { return 'NC'; }
110 } else {
111 function redundant(&$arr) {
112 global $wgAllMessagesEn;
113
114 $redundant = 0;
115 foreach(array_keys($arr) as $key) {
116 if ( ! array_key_exists( $key, $wgAllMessagesEn) )
117 ++$redundant;
118 }
119 return $redundant;
120 }
121 }
122
123 // Select an output engine
124 switch ($options['output']) {
125 case 'csv':
126 $out = new csvStatsOutput(); break;
127 case 'text':
128 $out = new textStatsOutput(); break;
129 case 'wiki':
130 $out = new wikiStatsOutput(); break;
131 default:
132 usage(); die();
133 break;
134 }
135
136 $langTool = new languages();
137
138 // Load message and compute stuff
139 $msgs = array();
140 foreach($langTool->getList() as $langcode) {
141 // Since they aren't loaded by default..
142 require_once( 'languages/Language' . $langcode . '.php' );
143 $arr = 'wgAllMessages'.$langcode;
144 if(@is_array($$arr)) {
145 $msgs[$wgContLang->lcfirst($langcode)] = array(
146 'total' => count($$arr),
147 'redundant' => redundant($$arr),
148 );
149 } else {
150 $msgs[$wgContLang->lcfirst($langcode)] = array(
151 'total' => 0,
152 'redundant' => 0,
153 );
154 }
155 }
156
157 // Top entry
158 $out->heading();
159 $out->blockstart();
160 $out->element('Language', true);
161 $out->element('Translated', true);
162 $out->element('%', true);
163 $out->element('Untranslated', true);
164 $out->element('%', true);
165 $out->element('Redundant', true);
166 $out->element('%', true);
167 $out->blockend();
168
169 // Generate rows
170 foreach($msgs as $lang => $stats) {
171 $out->blockstart();
172 // Language
173 $out->element($wgContLang->getLanguageName(strtr($lang, '_', '-')) . " ($lang)");
174 // Translated
175 $out->element($stats['total'] . '/' . $msgs['en']['total']);
176 // % Translated
177 $out->element($out->formatPercent($stats['total'], $msgs['en']['total']));
178 // Untranslated
179 $out->element($msgs['en']['total'] - $stats['total']);
180 // % Untranslated
181 $out->element($out->formatPercent($msgs['en']['total'] - $stats['total'], $msgs['en']['total'], true));
182 // Redundant & % Redundant
183 if($stats['redundant'] =='NC') {
184 $out->element('NC');
185 $out->element('NC');
186 } else {
187 $out->element($stats['redundant'] . '/' . $stats['total']);
188 $out->element($out->formatPercent($stats['redundant'], $stats['total'],true));
189 }
190 $out->blockend();
191 }
192 $out->footer();
193
194 // Final output
195 echo $out->getContent();
196 ?>