Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatePerLanguageStats.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
6use Language;
9use MediaWiki\MediaWikiServices;
10use Wikimedia\Rdbms\IDatabase;
11
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
145 protected function makeLabel( $group, $code ) {
146 if ( $group || $code ) {
147 return "$group@$code";
148 } else {
149 return 'all';
150 }
151 }
152
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
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}
Factory class for accessing message groups individually by id or all of them as a list.
Graph which provides statistics on active users and number of translations.
combineTwoArrays( $groups, $codes)
Cross-product of two lists with string results, where either list can be empty.
formatTimestamp( $timestamp)
Returns unique index for given item in the scale being used.
preQuery(IDatabase $database, &$tables, &$fields, &$conds, &$type, &$options, &$joins, $start, $end)
Query details that the graph must fill.
getTimestamp( $row)
Return the timestamp associated with this result row.
indexOf( $row)
Return the indexes which this result contributes to.
Provides some hand default implementations for TranslationStatsInterface.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:31