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