Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 143 |
|
0.00% |
0 / 16 |
CRAP | |
0.00% |
0 / 1 |
StatsTable | |
0.00% |
0 / 143 |
|
0.00% |
0 / 16 |
1482 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
element | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
getBackgroundColor | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
getMainColumnHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setMainColumnHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
createColumnHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addExtraColumn | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getOtherColumnHeaders | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
createHeader | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
makeTotalRow | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
makeNumberColumns | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
20 | |||
makeWorkflowStateCell | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
56 | |||
formatPercentage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getGroupLabel | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
makeGroupLink | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
isExcluded | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
72 |
1 | <?php |
2 | declare( strict_types=1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\Statistics; |
5 | |
6 | use HtmlArmor; |
7 | use Language; |
8 | use MediaWiki\Extension\Translate\MessageProcessing\MessageGroupMetadata; |
9 | use MediaWiki\Extension\Translate\Utilities\ConfigHelper; |
10 | use MediaWiki\Html\Html; |
11 | use MediaWiki\Linker\LinkRenderer; |
12 | use Message; |
13 | use MessageGroup; |
14 | use MessageGroupStats; |
15 | use MessageLocalizer; |
16 | use SpecialPage; |
17 | use TitleValue; |
18 | use Xml; |
19 | |
20 | /** |
21 | * Implements generation of HTML stats table. |
22 | * @author Siebrand Mazeland |
23 | * @author Niklas Laxström |
24 | * @license GPL-2.0-or-later |
25 | */ |
26 | class StatsTable { |
27 | protected TitleValue $translate; |
28 | private LinkRenderer $linkRenderer; |
29 | private ConfigHelper $configHelper; |
30 | private MessageLocalizer $messageLocalizer; |
31 | protected Language $language; |
32 | protected string $mainColumnHeader; |
33 | /** @var Message[] */ |
34 | protected array $extraColumns = []; |
35 | private MessageGroupMetadata $messageGroupMetadata; |
36 | |
37 | public function __construct( |
38 | LinkRenderer $linkRenderer, |
39 | ConfigHelper $configHelper, |
40 | MessageLocalizer $messageLocalizer, |
41 | Language $language, |
42 | MessageGroupMetadata $messageGroupMetadata |
43 | ) { |
44 | $this->translate = SpecialPage::getTitleValueFor( 'Translate' ); |
45 | $this->linkRenderer = $linkRenderer; |
46 | $this->configHelper = $configHelper; |
47 | $this->messageLocalizer = $messageLocalizer; |
48 | $this->language = $language; |
49 | $this->messageGroupMetadata = $messageGroupMetadata; |
50 | } |
51 | |
52 | /** |
53 | * Statistics table element (heading or regular cell) |
54 | * @param string $in Element contents. |
55 | * @param string $bgcolor Backround color in ABABAB format. |
56 | * @param string $sort Value used for sorting. |
57 | * @return string Html td element. |
58 | */ |
59 | public function element( string $in, string $bgcolor = '', string $sort = '' ): string { |
60 | $attributes = []; |
61 | |
62 | if ( $sort ) { |
63 | $attributes['data-sort-value'] = $sort; |
64 | } |
65 | |
66 | if ( $bgcolor ) { |
67 | $attributes['style'] = 'background-color: #' . $bgcolor; |
68 | } |
69 | |
70 | $element = Html::element( 'td', $attributes, $in ); |
71 | |
72 | return $element; |
73 | } |
74 | |
75 | public function getBackgroundColor( float $percentage, bool $fuzzy = false ): string { |
76 | if ( $fuzzy ) { |
77 | // Steeper scale for fuzzy |
78 | // (0), [0-2), [2-4), ... [12-100) |
79 | $index = min( 7, ceil( 50 * $percentage ) ); |
80 | $colors = [ |
81 | '', 'fedbd7', 'fecec8', 'fec1b9', |
82 | 'fcb5ab', 'fba89d', 'f89b8f', 'f68d81' |
83 | ]; |
84 | return $colors[ $index ]; |
85 | } |
86 | |
87 | // https://gka.github.io/palettes/#colors=#36c,#eaf3ff|steps=20|bez=1|coL=1 |
88 | // Color groups for (0-10], (10-20], ... (90-100], (100) |
89 | $index = (int)floor( $percentage * 10 ); |
90 | $colors = [ |
91 | 'eaf3ff', 'e2ebfc', 'dae3fa', 'd2dbf7', 'c9d4f5', |
92 | 'c1ccf2', 'b8c4ef', 'b1bced', 'a8b4ea', '9fade8', |
93 | '96a6e5' |
94 | ]; |
95 | |
96 | return $colors[ $index ]; |
97 | } |
98 | |
99 | private function getMainColumnHeader(): string { |
100 | return $this->mainColumnHeader; |
101 | } |
102 | |
103 | public function setMainColumnHeader( Message $msg ): void { |
104 | $this->mainColumnHeader = $this->createColumnHeader( $msg ); |
105 | } |
106 | |
107 | /** @return string HTML */ |
108 | private function createColumnHeader( Message $msg ): string { |
109 | return Html::element( 'th', [], $msg->text() ); |
110 | } |
111 | |
112 | public function addExtraColumn( Message $column ): void { |
113 | $this->extraColumns[] = $column; |
114 | } |
115 | |
116 | /** @return Message[] */ |
117 | private function getOtherColumnHeaders(): array { |
118 | return array_merge( [ |
119 | $this->messageLocalizer->msg( 'translate-total' ), |
120 | $this->messageLocalizer->msg( 'translate-untranslated' ), |
121 | $this->messageLocalizer->msg( 'translate-percentage-complete' ), |
122 | $this->messageLocalizer->msg( 'translate-percentage-proofread' ), |
123 | $this->messageLocalizer->msg( 'translate-percentage-fuzzy' ), |
124 | ], $this->extraColumns ); |
125 | } |
126 | |
127 | /** @return string HTML */ |
128 | public function createHeader(): string { |
129 | // Create table header |
130 | $out = Html::openElement( |
131 | 'table', |
132 | [ 'class' => 'statstable' ] |
133 | ); |
134 | |
135 | $out .= "\n\t" . Html::openElement( 'thead' ); |
136 | $out .= "\n\t" . Html::openElement( 'tr' ); |
137 | |
138 | $out .= "\n\t\t" . $this->getMainColumnHeader(); |
139 | foreach ( $this->getOtherColumnHeaders() as $label ) { |
140 | $out .= "\n\t\t" . $this->createColumnHeader( $label ); |
141 | } |
142 | $out .= "\n\t" . Html::closeElement( 'tr' ); |
143 | $out .= "\n\t" . Html::closeElement( 'thead' ); |
144 | $out .= "\n\t" . Html::openElement( 'tbody' ); |
145 | |
146 | return $out; |
147 | } |
148 | |
149 | /** |
150 | * Makes a row with aggregate numbers. |
151 | * @param Message $message |
152 | * @param array $stats ( total, translate, fuzzy ) |
153 | * @return string HTML |
154 | */ |
155 | public function makeTotalRow( Message $message, array $stats ): string { |
156 | $out = "\t" . Html::openElement( 'tr' ); |
157 | $out .= "\n\t\t" . Html::element( 'td', [], $message->text() ); |
158 | $out .= $this->makeNumberColumns( $stats ); |
159 | $out .= "\n\t" . Xml::closeElement( 'tr' ) . "\n"; |
160 | |
161 | return $out; |
162 | } |
163 | |
164 | /** |
165 | * Makes partial row from completion numbers |
166 | * @return string HTML |
167 | */ |
168 | public function makeNumberColumns( array $stats ): string { |
169 | $total = $stats[MessageGroupStats::TOTAL]; |
170 | $translated = $stats[MessageGroupStats::TRANSLATED]; |
171 | $fuzzy = $stats[MessageGroupStats::FUZZY]; |
172 | $proofread = $stats[MessageGroupStats::PROOFREAD]; |
173 | |
174 | if ( $total === null ) { |
175 | $na = "\n\t\t" . Html::element( 'td', [ 'data-sort-value' => -1 ], '...' ); |
176 | $nap = "\n\t\t" . $this->element( '...', 'AFAFAF', '-1' ); |
177 | $out = $na . $na . $nap . $nap; |
178 | |
179 | return $out; |
180 | } |
181 | |
182 | $out = "\n\t\t" . Html::element( 'td', |
183 | [ 'data-sort-value' => $total ], |
184 | $this->language->formatNum( $total ) ); |
185 | |
186 | $out .= "\n\t\t" . Html::element( 'td', |
187 | [ 'data-sort-value' => $total - $translated ], |
188 | $this->language->formatNum( $total - $translated ) ); |
189 | |
190 | if ( $total === 0 ) { |
191 | $transRatio = 0; |
192 | $fuzzyRatio = 0; |
193 | $proofRatio = 0; |
194 | } else { |
195 | $transRatio = $translated / $total; |
196 | $fuzzyRatio = $fuzzy / $total; |
197 | $proofRatio = $translated ? $proofread / $translated : 0; |
198 | } |
199 | |
200 | $out .= "\n\t\t" . $this->element( $this->formatPercentage( $transRatio, 'floor' ), |
201 | $this->getBackgroundColor( $transRatio ), |
202 | sprintf( '%1.5f', $transRatio ) ); |
203 | |
204 | $out .= "\n\t\t" . $this->element( $this->formatPercentage( $proofRatio, 'floor' ), |
205 | $this->getBackgroundColor( $proofRatio ), |
206 | sprintf( '%1.5f', $proofRatio ) ); |
207 | |
208 | $out .= "\n\t\t" . $this->element( $this->formatPercentage( $fuzzyRatio, 'ceil' ), |
209 | $this->getBackgroundColor( $fuzzyRatio, true ), |
210 | sprintf( '%1.5f', $fuzzyRatio ) ); |
211 | |
212 | return $out; |
213 | } |
214 | |
215 | public function makeWorkflowStateCell( ?string $state, MessageGroup $group, string $language ): string { |
216 | if ( $state === null || $group->getSourceLanguage() === $language ) { |
217 | return "\n\t\t" . $this->element( '', '', '-1' ); |
218 | } |
219 | |
220 | $stateConfig = $group->getMessageGroupStates()->getStates(); |
221 | $sortValue = '-1'; |
222 | $stateColor = ''; |
223 | if ( isset( $stateConfig[$state] ) ) { |
224 | $sortIndex = array_flip( array_keys( $stateConfig ) ); |
225 | $sortValue = $sortIndex[$state] + 1; |
226 | |
227 | if ( is_string( $stateConfig[$state] ) ) { |
228 | // BC for old configuration format |
229 | $stateColor = $stateConfig[$state]; |
230 | } elseif ( isset( $stateConfig[$state]['color'] ) ) { |
231 | $stateColor = $stateConfig[$state]['color']; |
232 | } |
233 | } |
234 | |
235 | $stateMessage = $this->messageLocalizer->msg( "translate-workflow-state-$state" ); |
236 | $stateText = $stateMessage->isBlank() ? $state : $stateMessage->text(); |
237 | |
238 | return "\n\t\t" . $this->element( |
239 | $stateText, |
240 | $stateColor, |
241 | (string)$sortValue |
242 | ); |
243 | } |
244 | |
245 | /** |
246 | * Makes a nice print from plain float. |
247 | * @param int|float $num |
248 | * @param string $to floor or ceil |
249 | * @return string Plain text |
250 | */ |
251 | public function formatPercentage( $num, string $to = 'floor' ): string { |
252 | $num = $to === 'floor' ? floor( 100 * $num ) : ceil( 100 * $num ); |
253 | $fmt = $this->language->formatNum( $num ); |
254 | |
255 | return $this->messageLocalizer->msg( 'percent', $fmt )->text(); |
256 | } |
257 | |
258 | /** |
259 | * Gets the name of group with some extra formatting. |
260 | * @return string HTML |
261 | */ |
262 | private function getGroupLabel( MessageGroup $group ): string { |
263 | $groupLabel = htmlspecialchars( $group->getLabel() ); |
264 | |
265 | // Bold for meta groups. |
266 | if ( $group->isMeta() ) { |
267 | $groupLabel = Html::rawElement( 'b', [], $groupLabel ); |
268 | } |
269 | |
270 | return $groupLabel; |
271 | } |
272 | |
273 | /** |
274 | * Gets the name of group linked to translation tool. |
275 | * @param MessageGroup $group |
276 | * @param string $code Language code |
277 | * @param array $params Any extra query parameters. |
278 | * @return string HTML |
279 | */ |
280 | public function makeGroupLink( MessageGroup $group, string $code, array $params ): string { |
281 | $queryParameters = $params + [ |
282 | 'group' => $group->getId(), |
283 | 'language' => $code |
284 | ]; |
285 | |
286 | return $this->linkRenderer->makeLink( |
287 | $this->translate, |
288 | new HtmlArmor( $this->getGroupLabel( $group ) ), |
289 | [], |
290 | $queryParameters |
291 | ); |
292 | } |
293 | |
294 | /** |
295 | * Check whether translations in given group in given language |
296 | * has been disabled. |
297 | * @param MessageGroup $group Message group |
298 | * @param string $code Language code |
299 | */ |
300 | public function isExcluded( MessageGroup $group, string $code ): bool { |
301 | $excluded = null; |
302 | $groupId = $group->getId(); |
303 | |
304 | $checks = [ |
305 | $groupId, |
306 | strtok( $groupId, '-' ), |
307 | '*' |
308 | ]; |
309 | |
310 | $disabledLanguages = $this->configHelper->getDisabledTargetLanguages(); |
311 | |
312 | foreach ( $checks as $check ) { |
313 | if ( isset( $disabledLanguages[$check] ) && isset( $disabledLanguages[$check][$code] ) ) { |
314 | $excluded = $disabledLanguages[$check][$code]; |
315 | } |
316 | |
317 | if ( $excluded !== null ) { |
318 | break; |
319 | } |
320 | } |
321 | |
322 | $languages = $group->getTranslatableLanguages(); |
323 | if ( $languages !== null && !isset( $languages[$code] ) ) { |
324 | $excluded = true; |
325 | } |
326 | |
327 | if ( $this->messageGroupMetadata->isExcluded( $groupId, $code ) ) { |
328 | $excluded = true; |
329 | } |
330 | |
331 | return (bool)$excluded; |
332 | } |
333 | } |