bug fixes
[lhc/web/wiklou.git] / maintenance / transstat.php
1 <?php
2 /**
3 * Statistics about the localisation.
4 *
5 * @package MediaWiki
6 * @subpackage Maintenance
7 *
8 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
9 * @author Ashar Voultoiz <thoane@altern.org>
10 *
11 * Output is posted from time to time on:
12 * http://meta.wikimedia.org/wiki/Localization_statistics
13 */
14
15 require_once( 'commandLine.inc' );
16 require_once( 'languages.inc' );
17
18 if ( isset( $options['help'] ) ) {
19 showUsage();
20 }
21 # Default output is WikiText
22 if ( !isset( $options['output'] ) ) {
23 $options['output'] = 'wiki';
24 }
25
26 /** Print a usage message*/
27 function showUsage() {
28 print <<<END
29 Usage: php transstat.php [--help] [--output=csv|text|wiki]
30 --help : this helpful message
31 --output : select an output engine one of:
32 * 'csv' : Comma Separated Values.
33 * 'wiki' : MediaWiki syntax (default).
34 * 'text' : Text with tabs.
35 Example: php maintenance/transstat.php --output=text
36
37 END;
38 exit();
39 }
40
41 /** A general output object. Need to be overriden */
42 class statsOutput {
43 function formatPercent( $subset, $total, $revert = false, $accuracy = 2 ) {
44 return @sprintf( '%.' . $accuracy . 'f%%', 100 * $subset / $total );
45 }
46
47 # Override the following methods
48 function heading() {
49 }
50 function footer() {
51 }
52 function blockstart() {
53 }
54 function blockend() {
55 }
56 function element( $in, $heading = false ) {
57 }
58 }
59
60 /** Outputs WikiText */
61 class wikiStatsOutput extends statsOutput {
62 function heading() {
63 echo "'''Note:''' These statistics can be generated by running <code>php maintenance/transstat.php</code>.\n\n";
64 echo "For additional information on specific languages (the message names, the actual problems, etc.), run <code>php maintenance/checkLanguage.php --lang=foo</code>.\n\n";
65 echo "{| border=2 cellpadding=4 cellspacing=0 style=\"background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse;\" width=100%\n";
66 }
67 function footer() {
68 echo "|}\n";
69 }
70 function blockstart() {
71 echo "|-\n";
72 }
73 function blockend() {
74 echo '';
75 }
76 function element( $in, $heading = false ) {
77 echo ($heading ? '!' : '|') . " $in\n";
78 }
79 function formatPercent( $subset, $total, $revert = false, $accuracy = 2 ) {
80 $v = @round(255 * $subset / $total);
81 if ( $revert ) {
82 $v = 255 - $v;
83 }
84 if ( $v < 128 ) {
85 # Red to Yellow
86 $red = 'FF';
87 $green = sprintf( '%02X', 2 * $v );
88 } else {
89 # Yellow to Green
90 $red = sprintf('%02X', 2 * ( 255 - $v ) );
91 $green = 'FF';
92 }
93 $blue = '00';
94 $color = $red . $green . $blue;
95
96 $percent = statsOutput::formatPercent( $subset, $total, $revert, $accuracy );
97 return 'bgcolor="#'. $color .'" | '. $percent;
98 }
99 }
100
101 /** Output text. To be used on a terminal for example. */
102 class textStatsOutput extends statsOutput {
103 function element( $in, $heading = false ) {
104 echo $in."\t";
105 }
106 function blockend() {
107 echo "\n";
108 }
109 }
110
111 /** csv output. Some people love excel */
112 class csvStatsOutput extends statsOutput {
113 function element( $in, $heading = false ) {
114 echo $in . ";";
115 }
116 function blockend() {
117 echo "\n";
118 }
119 }
120
121 # Select an output engine
122 switch ( $options['output'] ) {
123 case 'wiki':
124 $wgOut = new wikiStatsOutput();
125 break;
126 case 'text':
127 $wgOut = new textStatsOutput();
128 break;
129 case 'csv':
130 $wgOut = new csvStatsOutput();
131 break;
132 default:
133 showUsage();
134 }
135
136 # Languages
137 $wgLanguages = new languages();
138
139 # Header
140 $wgOut->heading();
141 $wgOut->blockstart();
142 $wgOut->element( 'Language', true );
143 $wgOut->element( 'Translated', true );
144 $wgOut->element( '%', true );
145 $wgOut->element( 'Obsolete', true );
146 $wgOut->element( '%', true );
147 $wgOut->element( 'Problematic', true );
148 $wgOut->element( '%', true );
149 $wgOut->blockend();
150
151 $wgGeneralMessages = $wgLanguages->getGeneralMessages();
152 $wgRequiredMessagesNumber = count( $wgGeneralMessages['required'] );
153
154 foreach ( $wgLanguages->getLanguages() as $code ) {
155 # Don't check English or RTL English
156 if ( $code == 'en' || $code == 'enRTL' ) {
157 continue;
158 }
159
160 # Calculate the numbers
161 $language = $wgContLang->getLanguageName( $code );
162 $messages = $wgLanguages->getMessages( $code );
163 $messagesNumber = count( $messages['translated'] );
164 $requiredMessagesNumber = count( $messages['required'] );
165 $requiredMessagesPercent = $wgOut->formatPercent( $requiredMessagesNumber, $wgRequiredMessagesNumber );
166 $obsoleteMessagesNumber = count( $messages['obsolete'] );
167 $obsoleteMessagesPercent = $wgOut->formatPercent( $obsoleteMessagesNumber, $messagesNumber, true );
168 $messagesWithoutVariables = $wgLanguages->getMessagesWithoutVariables( $code );
169 $emptyMessages = $wgLanguages->getEmptyMessages( $code );
170 $messagesWithWhitespace = $wgLanguages->getMessagesWithWhitespace( $code );
171 $nonXHTMLMessages = $wgLanguages->getNonXHTMLMessages( $code );
172 $messagesWithWrongChars = $wgLanguages->getMessagesWithWrongChars( $code );
173 $problematicMessagesNumber = count( array_unique( array_merge( $messagesWithoutVariables, $emptyMessages, $messagesWithWhitespace, $nonXHTMLMessages, $messagesWithWrongChars ) ) );
174 $problematicMessagesPercent = $wgOut->formatPercent( $problematicMessagesNumber, $messagesNumber, true );
175
176 # Output them
177 $wgOut->blockstart();
178 $wgOut->element( "$language ($code)" );
179 $wgOut->element( "$requiredMessagesNumber/$wgRequiredMessagesNumber" );
180 $wgOut->element( $requiredMessagesPercent );
181 $wgOut->element( "$obsoleteMessagesNumber/$messagesNumber" );
182 $wgOut->element( $obsoleteMessagesPercent );
183 $wgOut->element( "$problematicMessagesNumber/$messagesNumber" );
184 $wgOut->element( $problematicMessagesPercent );
185 $wgOut->blockend();
186 }
187
188 # Footer
189 $wgOut->footer();
190
191 ?>