summaryrefslogtreecommitdiff
path: root/maintenance/transstat.php
blob: e54a668c7d4a4ff6272a6ebccde11e9695f1decf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
 * @package MediaWiki
 * @subpackage Maintenance
 *
 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
 * @author Ashar Voultoiz <thoane@altern.org>
 * @bug 2499
 *
 * Output is posted from time to time on:
 * http://meta.wikimedia.org/wiki/Localization_statistics
 */

/** */
require_once('commandLine.inc');
require_once('languages.inc');

if( isset($options['help']) ) { usage(); wfDie(); }
// default output is WikiText
if( !isset($options['output']) ) { $options['output']='wiki'; }


/** Print a usage message*/
function usage() {
print <<<END
Usage: php transstat.php [--help] [--output:csv|text|wiki] [--showdupes]
         --help : this helpful message
      --showold : show old messages that are not in Messages.php
       --output : select an output engine one of:
                    * 'csv'  : Comma Separated Values.
                    * 'none' : Nothing, usefull with --showdupes
                    * 'wiki' : MediaWiki syntax (default).
                    * 'text' : Text with tabs.
Example: php transstat.php --showdupes --output=none


END;
}


/** A general output object. Need to be overriden */
class statsOutput {
	var $output; // buffer that contain the text
	function statsOutput() { $this->output='';}
	function getContent() { return $this->output;}

	function formatPercent($subset, $total, $revert=false, $accuracy=2) {
		return @sprintf( '%.' . $accuracy . 'f%%', 100 * $subset / $total );
	}

	// Override the next methods
	function heading() {}
	function footer() {}
	function blockstart() {}
	function blockend() {}
	function element($in, $heading=false) {}
}

/** Outputs nothing ! */
class noneStatsOutput extends statsOutput {
	function getContent() { return NULL;}
}

/** Outputs WikiText */
class wikiStatsOutput extends statsOutput {
	function heading() {
		$this->output .= "{| border=2 cellpadding=4 cellspacing=0 style=\"background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse;\" width=100%\n";
	}
	function footer()     { $this->output .= "|}\n"; }
	function blockstart() { $this->output .= "|-\n"; }
	function blockend()   { $this->output .= ''; }
	function element($in, $heading = false) {
		$this->output .= ($heading ? '!' : '|') . " $in\n";
	}
	function formatPercent($subset, $total, $revert=false, $accuracy=2) {
		$v = @round(255 * $subset / $total);
		if($revert) $v = 255 - $v;
		if($v < 128) {
			// red to yellow
			$red = 'FF';
			$green = sprintf('%02X', 2*$v);
		} else {
			// yellow to green
			$red   = sprintf('%02X', 2*(255 -$v) );
			$green = 'FF';
		}
		$blue  = '00';
		$color = $red.$green.$blue;

		$percent = statsOutput::formatPercent($subset, $total, $revert, $accuracy);
		return 'bgcolor="#'.$color.'" | '.$percent;
	}
}

/** Output text. To be used on a terminal for example. */
class textStatsOutput extends statsOutput {
	function element($in, $heading = false) {
		$this->output .= $in."\t";
	}
	function blockend(){ $this->output .="\n";}
}

/** csv output. Some people love excel */
class csvStatsOutput extends statsOutput {
	function element($in, $heading = false) {
		$this->output .= $in.";";
	}
	function blockend(){ $this->output .="\n";}
}


function redundant(&$arr, $langcode) {
	global $wgAllMessagesEn;

	$redundant = 0;
	foreach(array_keys($arr) as $key) {
		if ( @$wgAllMessagesEn[$key] === null ) {
			global $options;
			if( isset($options['showold']) ) {
				print "Deprecated [$langcode]: $key\n";
			}
			++$redundant;
		}
	}
	return $redundant;
}

// Select an output engine
switch ($options['output']) {
	case 'csv':
		$out = new csvStatsOutput(); break;
	case 'none':
		$out = new noneStatsOutput(); break;
	case 'text':
		$out = new textStatsOutput(); break;
	case 'wiki':
		$out = new wikiStatsOutput(); break;
	default:
		usage(); wfDie();
	break;
}

$langTool = new languages();

//  Load message and compute stuff
$msgs = array();
foreach($langTool->getList() as $langcode) {
	// Since they aren't loaded by default..
	require_once( 'languages/Language' . $langcode . '.php' );
	$arr = 'wgAllMessages'.$langcode;
	if(@is_array($$arr)) {
		$msgs[$wgContLang->lcfirst($langcode)] = array(
			'total' => count($$arr),
			'redundant' => redundant($$arr, $langcode),
		);
	} else {
		$msgs[$wgContLang->lcfirst($langcode)] = array(
			'total' => 0,
			'redundant' => 0,
		);
	}
}

// Top entry
$out->heading();
$out->blockstart();
$out->element('Language', true);
$out->element('Translated', true);
$out->element('%', true);
$out->element('Untranslated', true);
$out->element('%', true);
$out->element('Redundant', true);
$out->element('%', true);
$out->blockend();

// Generate rows
foreach($msgs as $lang => $stats) {
	$out->blockstart();
	// Language
	$out->element($wgContLang->getLanguageName(strtr($lang, '_', '-')) . " ($lang)");
	// Translated
	$out->element($stats['total'] . '/' . $msgs['en']['total']);
	// % Translated
	$out->element($out->formatPercent($stats['total'], $msgs['en']['total']));
	// Untranslated
	$out->element($msgs['en']['total'] - $stats['total']);
	// % Untranslated
	$out->element($out->formatPercent($msgs['en']['total'] - $stats['total'], $msgs['en']['total'], true));
	// Redundant & % Redundant
	if($stats['redundant'] =='NC') {
		$out->element('NC');
		$out->element('NC');
	} else {
		$out->element($stats['redundant'] . '/' . $stats['total']);
		$out->element($out->formatPercent($stats['redundant'],  $stats['total'],true));
	}
	$out->blockend();
}
$out->footer();

// Final output
echo $out->getContent();
?>