b04294415765926c98e4a34a3869708a90cd8174
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'
37 /** A general output object. Need to be overriden */
39 var $output; // buffer that contain the text
40 function statsOutput() { $this->output
='';}
41 function getContent() { return $this->output
;}
43 function formatPercent($subset, $total, $revert=false, $accuracy=2) {
44 return @sprintf
( '%.' . $accuracy . 'f%%', 100 * $subset / $total );
47 // Override the next methods
50 function blockstart() {}
51 function blockend() {}
52 function element($in, $heading=false) {}
56 /** Outputs WikiText */
57 class wikiStatsOutput
extends statsOutput
{
59 $this->output
.= "{| border=2 cellpadding=4 cellspacing=0 style=\"background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse;\" width=100%\n";
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";
67 function formatPercent($subset, $total, $revert=false, $accuracy=2) {
68 $v = @round
(255 * $subset / $total);
69 if($revert) $v = 255 - $v;
73 $green = sprintf('%02X', 2*$v);
76 $red = sprintf('%02X', 2*(255 -$v) );
80 $color = $red.$green.$blue;
82 $percent = statsOutput
::formatPercent($subset, $total, $revert, $accuracy);
83 return 'bgcolor="#'.$color.'" | '.$percent;
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";
92 function blockend(){ $this->output
.="\n";}
95 /** csv output. Some people love excel */
96 class csvStatsOutput
extends statsOutput
{
97 function element($in, $heading = false) {
98 $this->output
.= $in.";";
100 function blockend(){ $this->output
.="\n";}
104 function redundant(&$arr) {
105 global $wgAllMessagesEn;
108 foreach(array_keys($arr) as $key) {
109 if ( @$wgAllMessagesEn[$key] === null )
115 // Select an output engine
116 switch ($options['output']) {
118 $out = new csvStatsOutput(); break;
120 $out = new textStatsOutput(); break;
122 $out = new wikiStatsOutput(); break;
128 $langTool = new languages();
130 // Load message and compute stuff
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),
142 $msgs[$wgContLang->lcfirst($langcode)] = array(
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);
162 foreach($msgs as $lang => $stats) {
165 $out->element($wgContLang->getLanguageName(strtr($lang, '_', '-')) . " ($lang)");
167 $out->element($stats['total'] . '/' . $msgs['en']['total']);
169 $out->element($out->formatPercent($stats['total'], $msgs['en']['total']));
171 $out->element($msgs['en']['total'] - $stats['total']);
173 $out->element($out->formatPercent($msgs['en']['total'] - $stats['total'], $msgs['en']['total'], true));
174 // Redundant & % Redundant
175 if($stats['redundant'] =='NC') {
179 $out->element($stats['redundant'] . '/' . $stats['total']);
180 $out->element($out->formatPercent($stats['redundant'], $stats['total'],true));
187 echo $out->getContent();