Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
MessagePrefixStats | |
0.00% |
0 / 25 |
|
0.00% |
0 / 4 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
forAll | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
30 | |||
isLanguageUnused | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getAllLanguageStats | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\Statistics; |
5 | |
6 | use InvalidArgumentException; |
7 | use MediaWiki\Extension\Translate\MessageLoading\MessageCollection; |
8 | use MediaWiki\Extension\Translate\MessageLoading\MessageDefinitions; |
9 | use MessageGroupStats; |
10 | use TitleParser; |
11 | |
12 | /** |
13 | * This class abstracts MessagePrefix statistics calculation and storing. |
14 | * @author Abijeet Patro |
15 | * @since 2023.02 |
16 | * @license GPL-2.0-or-later |
17 | */ |
18 | class MessagePrefixStats { |
19 | private TitleParser $titleParser; |
20 | private ?array $allStats; |
21 | |
22 | public function __construct( TitleParser $titleParser ) { |
23 | $this->titleParser = $titleParser; |
24 | } |
25 | |
26 | /** |
27 | * Returns statistics for the message keys provided. Assumes that the message keys |
28 | * belong to the same namespace. |
29 | * @param string ...$prefixedMessagesKeys |
30 | * @return array |
31 | */ |
32 | public function forAll( string ...$prefixedMessagesKeys ): array { |
33 | $languages = MessageGroupStats::getLanguages(); |
34 | $stats = []; |
35 | |
36 | if ( !$prefixedMessagesKeys ) { |
37 | throw new InvalidArgumentException( 'Empty prefixed message keys passed as argument' ); |
38 | } |
39 | |
40 | $messagesForDefinition = []; |
41 | foreach ( $prefixedMessagesKeys as $key ) { |
42 | $messageTitle = $this->titleParser->parseTitle( $key ); |
43 | $messageNamespace = $messageTitle->getNamespace(); |
44 | $messagesForDefinition["$messageNamespace:{$messageTitle->getDBkey()}"] = null; |
45 | } |
46 | |
47 | $messageDefinitions = new MessageDefinitions( $messagesForDefinition, false ); |
48 | |
49 | foreach ( $languages as $code ) { |
50 | if ( $this->isLanguageUnused( $code ) ) { |
51 | $collection = MessageCollection::newFromDefinitions( $messageDefinitions, $code ); |
52 | $stats[ $code ] = MessageGroupStats::getStatsForCollection( $collection ); |
53 | } else { |
54 | $stats[ $code ] = MessageGroupStats::getEmptyStats(); |
55 | $stats[ $code ][ MessageGroupStats::TOTAL ] = count( $prefixedMessagesKeys ); |
56 | } |
57 | } |
58 | |
59 | return $stats; |
60 | } |
61 | |
62 | /** Check if there are any stats for the language */ |
63 | private function isLanguageUnused( string $languageCode ): bool { |
64 | $allStats = $this->getAllLanguageStats(); |
65 | $languageStats = $allStats[ $languageCode ] ?? []; |
66 | $translatedStats = $languageStats[ MessageGroupStats::TRANSLATED ]; |
67 | $fuzzyStats = $languageStats[ MessageGroupStats::FUZZY ]; |
68 | |
69 | return $translatedStats !== 0 || $fuzzyStats !== 0; |
70 | } |
71 | |
72 | private function getAllLanguageStats(): array { |
73 | $this->allStats ??= MessageGroupStats::getApproximateLanguageStats(); |
74 | return $this->allStats; |
75 | } |
76 | } |