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