* Added wfDie() wrapper, and some manual die(-1), to force the return code
[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(); wfDie(); }
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]
27 --help : this helpful message
28 --output : select an output engine one of:
29 * 'csv' : Comma Separated Values.
30 * 'wiki' : MediaWiki syntax (default).
31 * 'text' : Text with tabs.
32
33 END;
34 }
35
36
37 /** A general output object. Need to be overriden */
38 class statsOutput {
39 var $output; // buffer that contain the text
40 function statsOutput() { $this->output='';}
41 function getContent() { return $this->output;}
42
43 function formatPercent($subset, $total, $revert=false, $accuracy=2) {
44 return @sprintf( '%.' . $accuracy . 'f%%', 100 * $subset / $total );
45 }
46
47 // Override the next methods
48 function heading() {}
49 function footer() {}
50 function blockstart() {}
51 function blockend() {}
52 function element($in, $heading=false) {}
53 }
54
55
56 /** Outputs WikiText */
57 class wikiStatsOutput extends statsOutput {
58 function heading() {
59 $this->output .= "{| border=2 cellpadding=4 cellspacing=0 style=\"background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse;\" width=100%\n";
60 }
61 function footer() { $this->output .= "|}\n"; }
62 function blockstart() { $this->output .= "|-\n"; }
63 function blockend() { $this->output .= ''; }
64 function element($in, $heading = false) {
65 $this->output .= ($heading ? '!' : '|') . " $in\n";
66 }
67 function formatPercent($subset, $total, $revert=false, $accuracy=2) {
68 $v = @round(255 * $subset / $total);
69 if($revert) $v = 255 - $v;
70 if($v < 128) {
71 // red to yellow
72 $red = 'FF';
73 $green = sprintf('%02X', 2*$v);
74 } else {
75 // yellow to green
76 $red = sprintf('%02X', 2*(255 -$v) );
77 $green = 'FF';
78 }
79 $blue = '00';
80 $color = $red.$green.$blue;
81
82 $percent = statsOutput::formatPercent($subset, $total, $revert, $accuracy);
83 return 'bgcolor="#'.$color.'" | '.$percent;
84 }
85 }
86
87 /** Output text. To be used on a terminal for example. */
88 class textStatsOutput extends statsOutput {
89 function element($in, $heading = false) {
90 $this->output .= $in."\t";
91 }
92 function blockend(){ $this->output .="\n";}
93 }
94
95 /** csv output. Some people love excel */
96 class csvStatsOutput extends statsOutput {
97 function element($in, $heading = false) {
98 $this->output .= $in.";";
99 }
100 function blockend(){ $this->output .="\n";}
101 }
102
103
104 function redundant(&$arr) {
105 global $wgAllMessagesEn;
106
107 $redundant = 0;
108 foreach(array_keys($arr) as $key) {
109 if ( @$wgAllMessagesEn[$key] === null )
110 ++$redundant;
111 }
112 return $redundant;
113 }
114
115 // Select an output engine
116 switch ($options['output']) {
117 case 'csv':
118 $out = new csvStatsOutput(); break;
119 case 'text':
120 $out = new textStatsOutput(); break;
121 case 'wiki':
122 $out = new wikiStatsOutput(); break;
123 default:
124 usage(); wfDie();
125 break;
126 }
127
128 $langTool = new languages();
129
130 // Load message and compute stuff
131 $msgs = array();
132 foreach($langTool->getList() as $langcode) {
133 // Since they aren't loaded by default..
134 require_once( 'languages/Language' . $langcode . '.php' );
135 $arr = 'wgAllMessages'.$langcode;
136 if(@is_array($$arr)) {
137 $msgs[$wgContLang->lcfirst($langcode)] = array(
138 'total' => count($$arr),
139 'redundant' => redundant($$arr),
140 );
141 } else {
142 $msgs[$wgContLang->lcfirst($langcode)] = array(
143 'total' => 0,
144 'redundant' => 0,
145 );
146 }
147 }
148
149 // Top entry
150 $out->heading();
151 $out->blockstart();
152 $out->element('Language', true);
153 $out->element('Translated', true);
154 $out->element('%', true);
155 $out->element('Untranslated', true);
156 $out->element('%', true);
157 $out->element('Redundant', true);
158 $out->element('%', true);
159 $out->blockend();
160
161 // Generate rows
162 foreach($msgs as $lang => $stats) {
163 $out->blockstart();
164 // Language
165 $out->element($wgContLang->getLanguageName(strtr($lang, '_', '-')) . " ($lang)");
166 // Translated
167 $out->element($stats['total'] . '/' . $msgs['en']['total']);
168 // % Translated
169 $out->element($out->formatPercent($stats['total'], $msgs['en']['total']));
170 // Untranslated
171 $out->element($msgs['en']['total'] - $stats['total']);
172 // % Untranslated
173 $out->element($out->formatPercent($msgs['en']['total'] - $stats['total'], $msgs['en']['total'], true));
174 // Redundant & % Redundant
175 if($stats['redundant'] =='NC') {
176 $out->element('NC');
177 $out->element('NC');
178 } else {
179 $out->element($stats['redundant'] . '/' . $stats['total']);
180 $out->element($out->formatPercent($stats['redundant'], $stats['total'],true));
181 }
182 $out->blockend();
183 }
184 $out->footer();
185
186 // Final output
187 echo $out->getContent();
188 ?>