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