Debian Codegraph @

maintenance/language/StatOutputs.php

raw symbols SCIP
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 <[email protected]om>
23 * @author Antoine Musso <hashar at free dot fr>
24 */
25
26 use MediaWiki\Specials\SpecialVersion;
27 use Wikimedia\AtEase\AtEase;
28
29 /**
30 * A general output object. Need to be overridden
31 */
32 class StatsOutput {
33 public function formatPercent( $subset, $total, $revert = false, $accuracy = 2 ) {
34 AtEase::suppressWarnings();
35 $return = sprintf( '%.' . $accuracy . 'f%%', 100 * $subset / $total );
36 AtEase::restoreWarnings();
37
38 return $return;
39 }
40
41 public function heading() {
42 }
43
44 public function footer() {
45 }
46
47 public function blockstart() {
48 }
49
50 public function blockend() {
51 }
52
53 public function element( $in, $heading = false ) {
54 }
55 }
56
57 /** Outputs WikiText */
58 class WikiStatsOutput extends StatsOutput {
59 public function heading() {
60 global $wgDummyLanguageCodes;
61 $version = SpecialVersion::getVersion( 'nodb' );
62 echo "'''Statistics are based on:''' <code>" . $version . "</code>\n\n";
63 echo 'English (en) is excluded because it is the default localization';
64 if ( is_array( $wgDummyLanguageCodes ) ) {
65 $dummyCodes = [];
66 foreach ( $wgDummyLanguageCodes as $dummyCode => $correctCode ) {
67 $dummyCodes[] = $this->getServiceContainer()
68 ->getLanguageNameUtils()
69 ->getLanguageName( $dummyCode ) . ' (' . $dummyCode . ')';
70 }
71 echo ', as well as the following languages that are not intended for ' .
72 'system message translations, usually because they redirect to other ' .
73 'language codes: ' . implode( ', ', $dummyCodes );
74 }
75 # dot to end sentence
76 echo ".\n\n";
77 echo '{| class="sortable wikitable" border="2" style="background-color: #F9F9F9; ' .
78 'border: 1px #AAAAAA solid; border-collapse: collapse; clear:both; width:100%;"' . "\n";
79 }
80
81 public function footer() {
82 echo "|}\n";
83 }
84
85 public function blockstart() {
86 echo "|-\n";
87 }
88
89 public function blockend() {
90 echo '';
91 }
92
93 public function element( $in, $heading = false ) {
94 echo ( $heading ? '!' : '|' ) . "$in\n";
95 }
96
97 public function formatPercent( $subset, $total, $revert = false, $accuracy = 2 ) {
98 AtEase::suppressWarnings();
99 $v = round( 255 * $subset / $total );
100 AtEase::restoreWarnings();
101
102 if ( $revert ) {
103 # Weigh reverse with factor 20 so coloring takes effect more quickly as
104 # this option is used solely for reporting 'bad' percentages.
105 $v = $v * 20;
106 if ( $v > 255 ) {
107 $v = 255;
108 }
109 $v = 255 - $v;
110 }
111 if ( $v < 128 ) {
112 # Red to Yellow
113 $red = 'FF';
114 $green = sprintf( '%02X', 2 * $v );
115 } else {
116 # Yellow to Green
117 $red = sprintf( '%02X', 2 * ( 255 - $v ) );
118 $green = 'FF';
119 }
120 $blue = '00';
121 $color = $red . $green . $blue;
122
123 $percent = parent::formatPercent( $subset, $total, $revert, $accuracy );
124
125 return 'style="background-color:#' . $color . ';"|' . $percent;
126 }
127 }
128
129 /** Output text. To be used on a terminal for example. */
130 class TextStatsOutput extends StatsOutput {
131 public function element( $in, $heading = false ) {
132 echo $in . "\t";
133 }
134
135 public function blockend() {
136 echo "\n";
137 }
138 }
139
140 /** csv output. Some people love excel */
141 class CsvStatsOutput extends StatsOutput {
142 public function element( $in, $heading = false ) {
143 echo $in . ";";
144 }
145
146 public function blockend() {
147 echo "\n";
148 }
149 }