Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatorActivityQuery.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
6use ActorMigration;
7use Config;
8use Wikimedia\Rdbms\ILoadBalancer;
9
18 public const USER_NAME = 0;
19 public const USER_TRANSLATIONS = 1;
20 public const USER_LAST_ACTIVITY = 2;
21 private Config $options;
22 private ILoadBalancer $loadBalancer;
23
24 public function __construct( Config $options, ILoadBalancer $loadBalancer ) {
25 $this->options = $options;
26 $this->loadBalancer = $loadBalancer;
27 }
28
35 public function inLanguage( string $code ): array {
36 $dbr = $this->loadBalancer->getConnection( DB_REPLICA, 'vslow' );
37
38 $actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' );
39
40 $tables = [ 'page', 'revision' ] + $actorQuery['tables'];
41 $fields = [
42 'rev_user_text' => $actorQuery['fields']['rev_user_text'],
43 'MAX(rev_timestamp) as lastedit',
44 'count(page_id) as count',
45 ];
46 $conds = [
47 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code ),
48 'page_namespace' => $this->options->get( 'TranslateMessageNamespaces' ),
49 ];
50 $options = [
51 'GROUP BY' => $actorQuery['fields']['rev_user_text'],
52 'ORDER BY' => 'NULL',
53 ];
54 $joins = [
55 'revision' => [ 'JOIN', 'page_id=rev_page' ],
56 ] + $actorQuery['joins'];
57
58 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $joins );
59
60 $data = [];
61 foreach ( $res as $row ) {
62 // Warning: user names may be numbers that get cast to ints in array keys
63 $data[] = [
64 self::USER_NAME => $row->rev_user_text,
65 self::USER_TRANSLATIONS => (int)$row->count,
66 self::USER_LAST_ACTIVITY => $row->lastedit,
67 ];
68 }
69
70 return $data;
71 }
72
81 public function inAllLanguages(): array {
82 $dbr = $this->loadBalancer->getConnection( DB_REPLICA, 'vslow' );
83
84 $actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' );
85
86 $tables = [ 'page', 'revision' ] + $actorQuery['tables'];
87 $fields = [
88 'rev_user_text' => $actorQuery['fields']['rev_user_text'],
89 'substring_index(page_title, \'/\', -1) as lang',
90 'MAX(rev_timestamp) as lastedit',
91 'count(page_id) as count',
92 ];
93 $conds = [
94 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $dbr->anyString() ),
95 'page_namespace' => $this->options->get( 'TranslateMessageNamespaces' ),
96 ];
97 $options = [
98 'GROUP BY' => [ 'lang', $actorQuery['fields']['rev_user_text'] ],
99 'ORDER BY' => 'NULL',
100 ];
101
102 $joins = [
103 'revision' => [ 'JOIN', 'page_id=rev_page' ],
104 ] + $actorQuery['joins'];
105
106 $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $joins );
107
108 $data = [];
109 foreach ( $res as $row ) {
110 // Warning: user names may be numbers that get cast to ints in array keys
111 $data[$row->lang][] = [
112 self::USER_NAME => $row->rev_user_text,
113 self::USER_TRANSLATIONS => (int)$row->count,
114 self::USER_LAST_ACTIVITY => $row->lastedit,
115 ];
116 }
117
118 return $data;
119 }
120}
inLanguage(string $code)
Fetch the translators for a language.