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;
11use MediaWiki\MediaWikiServices;
12use Wikimedia\Rdbms\IDatabase;
13
22 protected array $seenUsers = [];
23 protected array $groups = [];
24 private Language $dateFormatter;
25 private array $formatCache = [];
26 protected MessageIndex $messageIndex;
27
28 public function __construct( TranslationStatsGraphOptions $opts ) {
29 parent::__construct( $opts );
30 // This query is slow. Set a lower limit, but allow seeing one year at once.
31 $opts->boundValue( 'days', 1, 400 );
32 // TODO: inject
33 $this->dateFormatter = MediaWikiServices::getInstance()->getContentLanguage();
34 $this->messageIndex = Services::getInstance()->getMessageIndex();
35 }
36
37 public function preQuery(
38 IDatabase $database,
39 &$tables,
40 &$fields,
41 &$conds,
42 &$type,
43 &$options,
44 &$joins,
45 $start,
46 $end
47 ) {
48 global $wgTranslateMessageNamespaces;
49
50 $tables = [ 'recentchanges' ];
51 $fields = [ 'rc_timestamp' ];
52 $joins = [];
53
54 $conds = [
55 'rc_namespace' => $wgTranslateMessageNamespaces,
56 'rc_bot' => 0,
57 'rc_type != ' . RC_LOG,
58 ];
59
60 $timeConds = self::makeTimeCondition( $database, 'rc_timestamp', $start, $end );
61 $conds = array_merge( $conds, $timeConds );
62
63 $options = [ 'ORDER BY' => 'rc_timestamp' ];
64
65 $this->groups = array_map( [ MessageGroups::class, 'normalizeId' ], $this->opts->getGroups() );
66
67 $namespaces = self::namespacesFromGroups( $this->groups );
68 if ( count( $namespaces ) ) {
69 $conds['rc_namespace'] = $namespaces;
70 }
71
72 $languages = [];
73 foreach ( $this->opts->getLanguages() as $code ) {
74 $languages[] = 'rc_title ' . $database->buildLike( $database->anyString(), "/$code" );
75 }
76 if ( count( $languages ) ) {
77 $conds[] = $database->makeList( $languages, LIST_OR );
78 }
79
80 $fields[] = 'rc_title';
81
82 if ( $this->groups ) {
83 $fields[] = 'rc_namespace';
84 }
85
86 if ( $this->opts->getValue( 'count' ) === 'users' ) {
87 $fields[] = 'rc_actor';
88 }
89
90 $type .= '-perlang';
91 }
92
93 public function indexOf( $row ) {
94 if ( $this->opts->getValue( 'count' ) === 'users' ) {
95 $date = $this->formatTimestamp( $row->rc_timestamp );
96
97 if ( isset( $this->seenUsers[$date][$row->rc_actor] ) ) {
98 return false;
99 }
100
101 $this->seenUsers[$date][$row->rc_actor] = true;
102 }
103
104 // Do not consider language-less pages.
105 if ( !str_contains( $row->rc_title, '/' ) ) {
106 return false;
107 }
108
109 // No filters, just one key to track.
110 if ( !$this->groups && !$this->opts->getLanguages() ) {
111 return [ 'all' ];
112 }
113
114 // The key-building needs to be in sync with ::labels().
115 [ $key, $code ] = Utilities::figureMessage( $row->rc_title );
116
117 $groups = [];
118 $codes = [];
119
120 if ( $this->groups ) {
121 // Get list of keys that the message belongs to, and filter
122 // out those which are not requested.
123 $groups = $this->messageIndex->getGroupIdsForDatabaseTitle( (int)$row->rc_namespace, $key );
124 $groups = array_intersect( $this->groups, $groups );
125 }
126
127 if ( $this->opts->getLanguages() ) {
128 $codes = [ $code ];
129 }
130
131 return $this->combineTwoArrays( $groups, $codes );
132 }
133
134 public function labels() {
135 return $this->combineTwoArrays( $this->groups, $this->opts->getLanguages() );
136 }
137
138 public function getTimestamp( $row ) {
139 return $row->rc_timestamp;
140 }
141
149 protected function makeLabel( $group, $code ) {
150 if ( $group || $code ) {
151 return "$group@$code";
152 } else {
153 return 'all';
154 }
155 }
156
164 protected function combineTwoArrays( $groups, $codes ) {
165 if ( !count( $groups ) ) {
166 $groups[] = false;
167 }
168
169 if ( !count( $codes ) ) {
170 $codes[] = false;
171 }
172
173 $items = [];
174 foreach ( $groups as $group ) {
175 foreach ( $codes as $code ) {
176 $items[] = $this->makeLabel( $group, $code );
177 }
178 }
179
180 return $items;
181 }
182
189 protected function formatTimestamp( $timestamp ) {
190 switch ( $this->opts->getValue( 'scale' ) ) {
191 case 'hours':
192 $cut = 4;
193 break;
194 case 'days':
195 $cut = 6;
196 break;
197 case 'months':
198 $cut = 8;
199 break;
200 case 'years':
201 $cut = 10;
202 break;
203 default:
204 // Get the prefix that uniquely identifies a day in the MW timestamp format
205 $index = substr( $timestamp, 0, -6 );
206 // Date formatting is really slow, so do it at most once per day. This is not
207 // adjusted for user timestamp, so it's safe to assume day boundaries follow UTC.
208 $this->formatCache[$index] ??= $this->dateFormatter->sprintfDate( $this->getDateFormat(), $timestamp );
209 return $this->formatCache[$index];
210 }
211
212 return substr( $timestamp, 0, -$cut );
213 }
214}
Factory class for accessing message groups individually by id or all of them as a list.
figureMessage()
Recommended to use getCode and getKey instead.
Creates a database of keys in all groups, so that namespace and key can be used to get the groups the...
Minimal service container.
Definition Services.php:58
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