1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
<?php
declare( strict_types = 1 );
namespace MediaWiki\Extension\Translate\Statistics;
use ActorMigration;
use Config;
use MediaWiki\Config\ServiceOptions;
use Wikimedia\Rdbms\ILoadBalancer;
/**
* Gathers translator activity from the database.
*
* @author Niklas Laxström
* @license GPL-2.0-or-later
* @since 2020.04
*/
class TranslatorActivityQuery {
public const USER_NAME = 0;
public const USER_TRANSLATIONS = 1;
public const USER_LAST_ACTIVITY = 2;
/** @var Config|ServiceOptions */
private $options;
/** @var ILoadBalancer */
private $loadBalancer;
public function __construct( $options, ILoadBalancer $loadBalancer ) {
$this->options = $options;
$this->loadBalancer = $loadBalancer;
}
/**
* Fetch the translators for a language
*
* @param string $code Language tag
* @return array<int,array<string|int|string>> Translation stats per user
*/
public function inLanguage( string $code ): array {
$dbr = $this->loadBalancer->getConnection( DB_REPLICA, 'vslow' );
$actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' );
$tables = [ 'page', 'revision' ] + $actorQuery['tables'];
$fields = [
'rev_user_text' => $actorQuery['fields']['rev_user_text'],
'MAX(rev_timestamp) as lastedit',
'count(page_id) as count',
];
$conds = [
'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code ),
'page_namespace' => $this->options->get( 'TranslateMessageNamespaces' ),
];
$options = [
'GROUP BY' => $actorQuery['fields']['rev_user_text'],
'ORDER BY' => 'NULL',
];
$joins = [
'revision' => [ 'JOIN', 'page_id=rev_page' ],
] + $actorQuery['joins'];
$res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $joins );
$data = [];
foreach ( $res as $row ) {
// Warning: user names may be numbers that get casted to ints in array keys
$data[] = [
self::USER_NAME => $row->rev_user_text,
self::USER_TRANSLATIONS => (int)$row->count,
self::USER_LAST_ACTIVITY => $row->lastedit,
];
}
return $data;
}
/**
* Fetch the translators for all languages.
*
* This is faster than doing each language separately.
*
* @return array<string,array<int,array<string|int|string>>> Map of language tags to
* translation stats per user
*/
public function inAllLanguages(): array {
$dbr = $this->loadBalancer->getConnection( DB_REPLICA, 'vslow' );
$actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' );
$tables = [ 'page', 'revision' ] + $actorQuery['tables'];
$fields = [
'rev_user_text' => $actorQuery['fields']['rev_user_text'],
'substring_index(page_title, \'/\', -1) as lang',
'MAX(rev_timestamp) as lastedit',
'count(page_id) as count',
];
$conds = [
'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() ),
'page_namespace' => $this->options->get( 'TranslateMessageNamespaces' ),
];
$options = [
'GROUP BY' => [ 'lang', $actorQuery['fields']['rev_user_text'] ],
'ORDER BY' => 'NULL',
];
$joins = [
'revision' => [ 'JOIN', 'page_id=rev_page' ],
] + $actorQuery['joins'];
$res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $joins );
$data = [];
foreach ( $res as $row ) {
// Warning: user names may be numbers that get casted to ints in array keys
$data[$row->lang][] = [
self::USER_NAME => $row->rev_user_text,
self::USER_TRANSLATIONS => (int)$row->count,
self::USER_LAST_ACTIVITY => $row->lastedit,
];
}
return $data;
}
}
class_alias( TranslatorActivityQuery::class, '\MediaWiki\Extensions\Translate\TranslatorActivityQuery' );
|