Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatePerLanguageStats.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
8use MediaWiki\MediaWikiServices;
9
18 protected $seenUsers;
19 protected $groups;
20
21 public function __construct( TranslationStatsGraphOptions $opts ) {
22 parent::__construct( $opts );
23 // This query is slow. Set a lower limit, but allow seeing one year at once.
24 $opts->boundValue( 'days', 1, 400 );
25 }
26
27 public function preQuery( &$tables, &$fields, &$conds, &$type, &$options, &$joins, $start, $end ) {
28 global $wgTranslateMessageNamespaces;
29
30 $db = wfGetDB( DB_REPLICA );
31
32 $tables = [ 'recentchanges' ];
33 $fields = [ 'rc_timestamp' ];
34 $joins = [];
35
36 $conds = [
37 'rc_namespace' => $wgTranslateMessageNamespaces,
38 'rc_bot' => 0,
39 'rc_type != ' . RC_LOG,
40 ];
41
42 $timeConds = self::makeTimeCondition( 'rc_timestamp', $start, $end );
43 $conds = array_merge( $conds, $timeConds );
44
45 $options = [ 'ORDER BY' => 'rc_timestamp' ];
46
47 $this->groups = array_map( [ MessageGroups::class, 'normalizeId' ], $this->opts->getGroups() );
48
49 $namespaces = self::namespacesFromGroups( $this->groups );
50 if ( count( $namespaces ) ) {
51 $conds['rc_namespace'] = $namespaces;
52 }
53
54 $languages = [];
55 foreach ( $this->opts->getLanguages() as $code ) {
56 $languages[] = 'rc_title ' . $db->buildLike( $db->anyString(), "/$code" );
57 }
58 if ( count( $languages ) ) {
59 $conds[] = $db->makeList( $languages, LIST_OR );
60 }
61
62 $fields[] = 'rc_title';
63
64 if ( $this->groups ) {
65 $fields[] = 'rc_namespace';
66 }
67
68 if ( $this->opts->getValue( 'count' ) === 'users' ) {
69 $fields[] = 'rc_actor';
70 }
71
72 $type .= '-perlang';
73 }
74
75 public function indexOf( $row ) {
76 if ( $this->opts->getValue( 'count' ) === 'users' ) {
77 $date = $this->formatTimestamp( $row->rc_timestamp );
78
79 if ( isset( $this->seenUsers[$date][$row->rc_actor] ) ) {
80 return false;
81 }
82
83 $this->seenUsers[$date][$row->rc_actor] = true;
84 }
85
86 // Do not consider language-less pages.
87 if ( strpos( $row->rc_title, '/' ) === false ) {
88 return false;
89 }
90
91 // No filters, just one key to track.
92 if ( !$this->groups && !$this->opts->getLanguages() ) {
93 return [ 'all' ];
94 }
95
96 // The key-building needs to be in sync with ::labels().
97 [ $key, $code ] = Utilities::figureMessage( $row->rc_title );
98
99 $groups = [];
100 $codes = [];
101
102 if ( $this->groups ) {
103 /*
104 * Get list of keys that the message belongs to, and filter
105 * out those which are not requested.
106 */
107 $groups = Utilities::messageKeyToGroups( (int)$row->rc_namespace, $key );
108 $groups = array_intersect( $this->groups, $groups );
109 }
110
111 if ( $this->opts->getLanguages() ) {
112 $codes = [ $code ];
113 }
114
115 return $this->combineTwoArrays( $groups, $codes );
116 }
117
118 public function labels() {
119 return $this->combineTwoArrays( $this->groups, $this->opts->getLanguages() );
120 }
121
122 public function getTimestamp( $row ) {
123 return $row->rc_timestamp;
124 }
125
133 protected function makeLabel( $group, $code ) {
134 if ( $group || $code ) {
135 return "$group@$code";
136 } else {
137 return 'all';
138 }
139 }
140
148 protected function combineTwoArrays( $groups, $codes ) {
149 if ( !count( $groups ) ) {
150 $groups[] = false;
151 }
152
153 if ( !count( $codes ) ) {
154 $codes[] = false;
155 }
156
157 $items = [];
158 foreach ( $groups as $group ) {
159 foreach ( $codes as $code ) {
160 $items[] = $this->makeLabel( $group, $code );
161 }
162 }
163
164 return $items;
165 }
166
173 protected function formatTimestamp( $timestamp ) {
174 switch ( $this->opts->getValue( 'scale' ) ) {
175 case 'hours':
176 $cut = 4;
177 break;
178 case 'days':
179 $cut = 6;
180 break;
181 case 'months':
182 $cut = 8;
183 break;
184 case 'years':
185 $cut = 10;
186 break;
187 default:
188 return MediaWikiServices::getInstance()->getContentLanguage()
189 ->sprintfDate( $this->getDateFormat(), $timestamp );
190 }
191
192 return substr( $timestamp, 0, -$cut );
193 }
194}
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(&$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:30