Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 79 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
TranslatePerLanguageStats | |
0.00% |
0 / 79 |
|
0.00% |
0 / 8 |
992 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
preQuery | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
42 | |||
indexOf | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
72 | |||
labels | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTimestamp | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
makeLabel | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
combineTwoArrays | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
formatTimestamp | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\Statistics; |
5 | |
6 | use Language; |
7 | use MediaWiki\Extension\Translate\MessageGroupProcessing\MessageGroups; |
8 | use MediaWiki\Extension\Translate\Utilities\Utilities; |
9 | use MediaWiki\MediaWikiServices; |
10 | use Wikimedia\Rdbms\IDatabase; |
11 | |
12 | /** |
13 | * Graph which provides statistics on active users and number of translations. |
14 | * @ingroup Stats |
15 | * @license GPL-2.0-or-later |
16 | * @since 2010.07 |
17 | */ |
18 | class TranslatePerLanguageStats extends TranslationStatsBase { |
19 | /** @var array For client side group by time period */ |
20 | protected array $seenUsers = []; |
21 | protected array $groups = []; |
22 | private Language $dateFormatter; |
23 | private array $formatCache = []; |
24 | |
25 | public function __construct( TranslationStatsGraphOptions $opts ) { |
26 | parent::__construct( $opts ); |
27 | // This query is slow. Set a lower limit, but allow seeing one year at once. |
28 | $opts->boundValue( 'days', 1, 400 ); |
29 | // TODO: inject |
30 | $this->dateFormatter = MediaWikiServices::getInstance()->getContentLanguage(); |
31 | } |
32 | |
33 | public function preQuery( |
34 | IDatabase $database, |
35 | &$tables, |
36 | &$fields, |
37 | &$conds, |
38 | &$type, |
39 | &$options, |
40 | &$joins, |
41 | $start, |
42 | $end |
43 | ) { |
44 | global $wgTranslateMessageNamespaces; |
45 | |
46 | $tables = [ 'recentchanges' ]; |
47 | $fields = [ 'rc_timestamp' ]; |
48 | $joins = []; |
49 | |
50 | $conds = [ |
51 | 'rc_namespace' => $wgTranslateMessageNamespaces, |
52 | 'rc_bot' => 0, |
53 | 'rc_type != ' . RC_LOG, |
54 | ]; |
55 | |
56 | $timeConds = self::makeTimeCondition( $database, 'rc_timestamp', $start, $end ); |
57 | $conds = array_merge( $conds, $timeConds ); |
58 | |
59 | $options = [ 'ORDER BY' => 'rc_timestamp' ]; |
60 | |
61 | $this->groups = array_map( [ MessageGroups::class, 'normalizeId' ], $this->opts->getGroups() ); |
62 | |
63 | $namespaces = self::namespacesFromGroups( $this->groups ); |
64 | if ( count( $namespaces ) ) { |
65 | $conds['rc_namespace'] = $namespaces; |
66 | } |
67 | |
68 | $languages = []; |
69 | foreach ( $this->opts->getLanguages() as $code ) { |
70 | $languages[] = 'rc_title ' . $database->buildLike( $database->anyString(), "/$code" ); |
71 | } |
72 | if ( count( $languages ) ) { |
73 | $conds[] = $database->makeList( $languages, LIST_OR ); |
74 | } |
75 | |
76 | $fields[] = 'rc_title'; |
77 | |
78 | if ( $this->groups ) { |
79 | $fields[] = 'rc_namespace'; |
80 | } |
81 | |
82 | if ( $this->opts->getValue( 'count' ) === 'users' ) { |
83 | $fields[] = 'rc_actor'; |
84 | } |
85 | |
86 | $type .= '-perlang'; |
87 | } |
88 | |
89 | public function indexOf( $row ) { |
90 | if ( $this->opts->getValue( 'count' ) === 'users' ) { |
91 | $date = $this->formatTimestamp( $row->rc_timestamp ); |
92 | |
93 | if ( isset( $this->seenUsers[$date][$row->rc_actor] ) ) { |
94 | return false; |
95 | } |
96 | |
97 | $this->seenUsers[$date][$row->rc_actor] = true; |
98 | } |
99 | |
100 | // Do not consider language-less pages. |
101 | if ( !str_contains( $row->rc_title, '/' ) ) { |
102 | return false; |
103 | } |
104 | |
105 | // No filters, just one key to track. |
106 | if ( !$this->groups && !$this->opts->getLanguages() ) { |
107 | return [ 'all' ]; |
108 | } |
109 | |
110 | // The key-building needs to be in sync with ::labels(). |
111 | [ $key, $code ] = Utilities::figureMessage( $row->rc_title ); |
112 | |
113 | $groups = []; |
114 | $codes = []; |
115 | |
116 | if ( $this->groups ) { |
117 | // Get list of keys that the message belongs to, and filter |
118 | // out those which are not requested. |
119 | $groups = Utilities::messageKeyToGroups( (int)$row->rc_namespace, $key ); |
120 | $groups = array_intersect( $this->groups, $groups ); |
121 | } |
122 | |
123 | if ( $this->opts->getLanguages() ) { |
124 | $codes = [ $code ]; |
125 | } |
126 | |
127 | return $this->combineTwoArrays( $groups, $codes ); |
128 | } |
129 | |
130 | public function labels() { |
131 | return $this->combineTwoArrays( $this->groups, $this->opts->getLanguages() ); |
132 | } |
133 | |
134 | public function getTimestamp( $row ) { |
135 | return $row->rc_timestamp; |
136 | } |
137 | |
138 | /** |
139 | * Makes a label for variable. If group or language code filters, or both |
140 | * are used, combine those in a pretty way. |
141 | * @param string $group Group name. |
142 | * @param string $code Language code. |
143 | * @return string Label. |
144 | */ |
145 | protected function makeLabel( $group, $code ) { |
146 | if ( $group || $code ) { |
147 | return "$group@$code"; |
148 | } else { |
149 | return 'all'; |
150 | } |
151 | } |
152 | |
153 | /** |
154 | * Cross-product of two lists with string results, where either |
155 | * list can be empty. |
156 | * @param string[] $groups Group names. |
157 | * @param string[] $codes Language codes. |
158 | * @return string[] Labels. |
159 | */ |
160 | protected function combineTwoArrays( $groups, $codes ) { |
161 | if ( !count( $groups ) ) { |
162 | $groups[] = false; |
163 | } |
164 | |
165 | if ( !count( $codes ) ) { |
166 | $codes[] = false; |
167 | } |
168 | |
169 | $items = []; |
170 | foreach ( $groups as $group ) { |
171 | foreach ( $codes as $code ) { |
172 | $items[] = $this->makeLabel( $group, $code ); |
173 | } |
174 | } |
175 | |
176 | return $items; |
177 | } |
178 | |
179 | /** |
180 | * Returns unique index for given item in the scale being used. |
181 | * Called a lot, so performance intensive. |
182 | * @param string $timestamp Timestamp in mediawiki format. |
183 | * @return string |
184 | */ |
185 | protected function formatTimestamp( $timestamp ) { |
186 | switch ( $this->opts->getValue( 'scale' ) ) { |
187 | case 'hours': |
188 | $cut = 4; |
189 | break; |
190 | case 'days': |
191 | $cut = 6; |
192 | break; |
193 | case 'months': |
194 | $cut = 8; |
195 | break; |
196 | case 'years': |
197 | $cut = 10; |
198 | break; |
199 | default: |
200 | // Get the prefix that uniquely identifies a day in the MW timestamp format |
201 | $index = substr( $timestamp, 0, -6 ); |
202 | // Date formatting is really slow, so do it at most once per day. This is not |
203 | // adjusted for user timestamp, so it's safe to assume day boundaries follow UTC. |
204 | $this->formatCache[$index] ??= $this->dateFormatter->sprintfDate( $this->getDateFormat(), $timestamp ); |
205 | return $this->formatCache[$index]; |
206 | } |
207 | |
208 | return substr( $timestamp, 0, -$cut ); |
209 | } |
210 | } |