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