Merge "mw.language.convertPlural: Check if matching form exists"
[lhc/web/wiklou.git] / maintenance / language / StatOutputs.php
1 <?php
2 /**
3 * Statistic output classes.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup MaintenanceLanguage
22 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
23 * @author Antoine Musso <hashar at free dot fr>
24 */
25
26 /** A general output object. Need to be overriden */
27 class StatsOutput {
28 function formatPercent( $subset, $total, $revert = false, $accuracy = 2 ) {
29 wfSuppressWarnings();
30 $return = sprintf( '%.' . $accuracy . 'f%%', 100 * $subset / $total );
31 wfRestoreWarnings();
32 return $return;
33 }
34
35 # Override the following methods
36 function heading() {
37 }
38
39 function footer() {
40 }
41
42 function blockstart() {
43 }
44
45 function blockend() {
46 }
47
48 function element( $in, $heading = false ) {
49 }
50 }
51
52 /** Outputs WikiText */
53 class WikiStatsOutput extends StatsOutput {
54 function heading() {
55 global $wgDummyLanguageCodes;
56 $version = SpecialVersion::getVersion( 'nodb' );
57 echo "'''Statistics are based on:''' <code>" . $version . "</code>\n\n";
58 echo "'''Note:''' These statistics can be generated by running " .
59 "<code>php maintenance/language/transstat.php</code>.\n\n";
60 echo "For additional information on specific languages (the message names, the actual " .
61 "problems, etc.), run <code>php maintenance/language/checkLanguage.php --lang=foo</code>.\n\n";
62 echo 'English (en) is excluded because it is the default localization';
63 if ( is_array( $wgDummyLanguageCodes ) ) {
64 $dummyCodes = array();
65 foreach ( $wgDummyLanguageCodes as $dummyCode => $correctCode ) {
66 $dummyCodes[] = Language::fetchLanguageName( $dummyCode ) . ' (' . $dummyCode . ')';
67 }
68 echo ', as well as the following languages that are not intended for ' .
69 'system message translations, usually because they redirect to other ' .
70 'language codes: ' . implode( ', ', $dummyCodes );
71 }
72 echo ".\n\n"; # dot to end sentence
73 echo '{| class="sortable wikitable" border="2" style="background-color: #F9F9F9; ' .
74 'border: 1px #AAAAAA solid; border-collapse: collapse; clear:both; width:100%;"' . "\n";
75 }
76
77 function footer() {
78 echo "|}\n";
79 }
80
81 function blockstart() {
82 echo "|-\n";
83 }
84
85 function blockend() {
86 echo '';
87 }
88
89 function element( $in, $heading = false ) {
90 echo ( $heading ? '!' : '|' ) . "$in\n";
91 }
92
93 function formatPercent( $subset, $total, $revert = false, $accuracy = 2 ) {
94 wfSuppressWarnings();
95 $v = round( 255 * $subset / $total );
96 wfRestoreWarnings();
97
98 if ( $revert ) {
99 # Weigh reverse with factor 20 so coloring takes effect more quickly as
100 # this option is used solely for reporting 'bad' percentages.
101 $v = $v * 20;
102 if ( $v > 255 ) {
103 $v = 255;
104 }
105 $v = 255 - $v;
106 }
107 if ( $v < 128 ) {
108 # Red to Yellow
109 $red = 'FF';
110 $green = sprintf( '%02X', 2 * $v );
111 } else {
112 # Yellow to Green
113 $red = sprintf( '%02X', 2 * ( 255 - $v ) );
114 $green = 'FF';
115 }
116 $blue = '00';
117 $color = $red . $green . $blue;
118
119 $percent = parent::formatPercent( $subset, $total, $revert, $accuracy );
120
121 return 'style="background-color:#' . $color . ';"|' . $percent;
122 }
123 }
124
125 /** Output text. To be used on a terminal for example. */
126 class TextStatsOutput extends StatsOutput {
127 function element( $in, $heading = false ) {
128 echo $in . "\t";
129 }
130
131 function blockend() {
132 echo "\n";
133 }
134 }
135
136 /** csv output. Some people love excel */
137 class CsvStatsOutput extends StatsOutput {
138 function element( $in, $heading = false ) {
139 echo $in . ";";
140 }
141
142 function blockend() {
143 echo "\n";
144 }
145 }