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;
10use TitleParser;
11
19 private TitleParser $titleParser;
20 private ?array $allStats;
21
22 public function __construct( TitleParser $titleParser ) {
23 $this->titleParser = $titleParser;
24 }
25
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
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}
This file contains the class for core message collections implementation.
Wrapper for message definitions, just to beauty the code.
This class abstracts MessagePrefix statistics calculation and storing.
forAll(string ... $prefixedMessagesKeys)
Returns statistics for the message keys provided.
This class abstract MessageGroup statistics calculation and storing.
const FUZZY
Array index.
const TRANSLATED
Array index.