Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessagePrefixStats.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
6use InvalidArgumentException;
9use TitleParser;
10
18 private TitleParser $titleParser;
19 private ?array $allStats;
20
21 public function __construct( TitleParser $titleParser ) {
22 $this->titleParser = $titleParser;
23 }
24
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
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}
This file contains the class for core message collections implementation.
Wrapper for message definitions, just to beauty the code.
This class aims to provide efficient mechanism for fetching translation completion stats.
static getStatsForCollection(MessageCollection $collection)
This class abstracts MessagePrefix statistics calculation and storing.
forAll(string ... $prefixedMessagesKeys)
Returns statistics for the message keys provided.