4 * @subpackage Maintenance
6 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
7 * @author Ashar Voultoiz <thoane@altern.org>
10 * Output is posted from time to time on:
11 * http://meta.wikimedia.org/wiki/Localization_statistics
15 require_once('commandLine.inc');
16 require_once('languages.inc');
18 if( isset($options['help']) ) { usage(); die(); }
19 // default output is WikiText
20 if( !isset($options['output']) ) { $options['output']='wiki'; }
23 /** Print a usage message*/
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)
40 /** A general output object. Need to be overriden */
42 var $output; // buffer that contain the text
43 function statsOutput() { $this->output
='';}
44 function getContent() { return $this->output
;}
46 function formatPercent($subset, $total, $revert=false, $accuracy=2) {
47 return @sprintf
( '%.' . $accuracy . 'f%%', 100 * $subset / $total );
50 // Override the next methods
53 function blockstart() {}
54 function blockend() {}
55 function element($in, $heading=false) {}
59 /** Outputs WikiText */
60 class wikiStatsOutput
extends statsOutput
{
62 $this->output
.= "{| border=2 cellpadding=4 cellspacing=0 style=\"background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse;\" width=100%\n";
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";
70 function formatPercent($subset, $total, $revert=false, $accuracy=2) {
71 $v = @round
(255 * $subset / $total);
72 if($revert) $v = 255 - $v;
76 $green = sprintf('%02X', 2*$v);
79 $red = sprintf('%02X', 2*(255 -$v) );
83 $color = $red.$green.$blue;
85 $percent = statsOutput
::formatPercent($subset, $total, $revert, $accuracy);
86 return 'bgcolor="#'.$color.'" | '.$percent;
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";
95 function blockend(){ $this->output
.="\n";}
98 /** csv output. Some people love excel */
99 class csvStatsOutput
extends statsOutput
{
100 function element($in, $heading = false) {
101 $this->output
.= $in.";";
103 function blockend(){ $this->output
.="\n";}
107 /** FIXME: This takes an obscene amount of time */
108 if(isset($options['noredundant'])) {
109 function redundant(&$arr) { return 'NC'; }
111 function redundant(&$arr) {
112 global $wgAllMessagesEn;
115 foreach(array_keys($arr) as $key) {
116 if ( ! array_key_exists( $key, $wgAllMessagesEn) )
123 // Select an output engine
124 switch ($options['output']) {
126 $out = new csvStatsOutput(); break;
128 $out = new textStatsOutput(); break;
130 $out = new wikiStatsOutput(); break;
136 $langTool = new languages();
138 // Load message and compute stuff
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),
150 $msgs[$wgContLang->lcfirst($langcode)] = array(
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);
170 foreach($msgs as $lang => $stats) {
173 $out->element($wgContLang->getLanguageName(strtr($lang, '_', '-')) . " ($lang)");
175 $out->element($stats['total'] . '/' . $msgs['en']['total']);
177 $out->element($out->formatPercent($stats['total'], $msgs['en']['total']));
179 $out->element($msgs['en']['total'] - $stats['total']);
181 $out->element($out->formatPercent($msgs['en']['total'] - $stats['total'], $msgs['en']['total'], true));
182 // Redundant & % Redundant
183 if($stats['redundant'] =='NC') {
187 $out->element($stats['redundant'] . '/' . $stats['total']);
188 $out->element($out->formatPercent($stats['redundant'], $stats['total'],true));
195 echo $out->getContent();