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