Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 82 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
Translator | |
0.00% |
0 / 82 |
|
0.00% |
0 / 9 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getGlobalUserId | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getUser | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addTranslation | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
getLanguages | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
12 | |||
getTranslationsCount | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
getStats | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
getTranslatorsCount | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
6 | |||
getTotalTranslatorsCount | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace ContentTranslation; |
4 | |
5 | use ContentTranslation\Service\UserService; |
6 | use MediaWiki\MediaWikiServices; |
7 | use MediaWiki\User\User; |
8 | |
9 | class Translator { |
10 | |
11 | private User $user; |
12 | |
13 | public function __construct( User $user ) { |
14 | $this->user = $user; |
15 | } |
16 | |
17 | private function getGlobalUserId() { |
18 | /** @var UserService $userService */ |
19 | $userService = MediaWikiServices::getInstance()->getService( 'ContentTranslation.UserService' ); |
20 | |
21 | return $userService->getGlobalUserId( $this->user ); |
22 | } |
23 | |
24 | public function getUser(): User { |
25 | return $this->user; |
26 | } |
27 | |
28 | public function addTranslation( $translationId ) { |
29 | /** @var LoadBalancer $lb */ |
30 | $lb = MediaWikiServices::getInstance()->getService( 'ContentTranslation.LoadBalancer' ); |
31 | $dbw = $lb->getConnection( DB_PRIMARY ); |
32 | $dbw->newReplaceQueryBuilder() |
33 | ->replaceInto( 'cx_translators' ) |
34 | ->uniqueIndexFields( [ 'translator_user_id', 'translator_translation_id' ] ) |
35 | ->row( [ |
36 | 'translator_user_id' => $this->getGlobalUserId(), |
37 | 'translator_translation_id' => $translationId, |
38 | ] ) |
39 | ->caller( __METHOD__ ) |
40 | ->execute(); |
41 | } |
42 | |
43 | public function getLanguages( $type ) { |
44 | // Note: there is no index on translation_last_updated_timestamp |
45 | /** @var LoadBalancer $lb */ |
46 | $lb = MediaWikiServices::getInstance()->getService( 'ContentTranslation.LoadBalancer' ); |
47 | $dbr = $lb->getConnection( DB_REPLICA ); |
48 | |
49 | $baseQuery = $dbr->newSelectQueryBuilder() |
50 | // ->select() below |
51 | ->from( 'cx_translations' ) |
52 | ->join( 'cx_translators', null, 'translator_translation_id = translation_id' ) |
53 | ->where( [ 'translator_user_id' => $this->getGlobalUserId() ] ); |
54 | if ( $type !== null ) { |
55 | $baseQuery->andWhere( [ 'translation_status' => $type ] ); |
56 | } |
57 | $unionQueryBuilder = $dbr->newUnionQueryBuilder(); |
58 | |
59 | $sourceQuery = clone $baseQuery; |
60 | $sourceQuery->select( [ 'code' => 'translation_source_language' ] ); |
61 | $unionQueryBuilder->add( $sourceQuery ); |
62 | $targetQuery = clone $baseQuery; |
63 | $targetQuery->select( [ 'code' => 'translation_target_language' ] ); |
64 | $unionQueryBuilder->add( $targetQuery ); |
65 | |
66 | $res = $unionQueryBuilder->caller( __METHOD__ )->fetchResultSet(); |
67 | |
68 | $result = []; |
69 | foreach ( $res as $row ) { |
70 | $result[] = $row->code; |
71 | } |
72 | |
73 | return $result; |
74 | } |
75 | |
76 | /** |
77 | * Get the number of published translation by current translator. |
78 | * @return int |
79 | */ |
80 | public function getTranslationsCount() { |
81 | /** @var LoadBalancer $lb */ |
82 | $lb = MediaWikiServices::getInstance()->getService( 'ContentTranslation.LoadBalancer' ); |
83 | $dbr = $lb->getConnection( DB_REPLICA ); |
84 | |
85 | $count = $dbr->newSelectQueryBuilder() |
86 | ->select( 'COUNT(*)' ) |
87 | ->from( 'cx_translators' ) |
88 | ->join( 'cx_translations', null, 'translator_translation_id = translation_id' ) |
89 | ->where( [ |
90 | 'translator_user_id' => $this->getGlobalUserId(), |
91 | // And it is published |
92 | Translation::getPublishedCondition( $dbr ) |
93 | ] ) |
94 | ->caller( __METHOD__ ) |
95 | ->fetchField(); |
96 | |
97 | return intval( $count ); |
98 | } |
99 | |
100 | /** |
101 | * Get the stats for all translator counts. |
102 | * @return array |
103 | */ |
104 | public static function getStats() { |
105 | return [ |
106 | 'from' => self::getTranslatorsCount( 'source' ), |
107 | 'to' => self::getTranslatorsCount( 'target' ), |
108 | 'total' => self::getTotalTranslatorsCount(), |
109 | ]; |
110 | } |
111 | |
112 | /** |
113 | * Get the stats for translator count to or from a language. |
114 | * @param string $direction source or target |
115 | * @return int[] Number of translators indexed by language code |
116 | */ |
117 | public static function getTranslatorsCount( $direction ) { |
118 | $directionField = [ |
119 | 'source' => 'translation_source_language', |
120 | 'target' => 'translation_target_language', |
121 | ]; |
122 | |
123 | /** @var LoadBalancer $lb */ |
124 | $lb = MediaWikiServices::getInstance()->getService( 'ContentTranslation.LoadBalancer' ); |
125 | $dbr = $lb->getConnection( DB_REPLICA ); |
126 | |
127 | $rows = $dbr->newSelectQueryBuilder() |
128 | ->select( [ |
129 | 'language' => $directionField[$direction], |
130 | 'translators' => 'COUNT(DISTINCT translation_started_by)', |
131 | ] ) |
132 | ->from( 'cx_translations' ) |
133 | ->where( Translation::getPublishedCondition( $dbr ) ) |
134 | ->groupBy( $directionField[$direction] ) |
135 | ->caller( __METHOD__ ) |
136 | ->fetchResultSet(); |
137 | |
138 | $result = []; |
139 | |
140 | foreach ( $rows as $row ) { |
141 | $result[$row->language] = (int)$row->translators; |
142 | } |
143 | |
144 | return $result; |
145 | } |
146 | |
147 | /** |
148 | * Get the total count of users who published a translation. |
149 | * @return int Number of translators |
150 | */ |
151 | public static function getTotalTranslatorsCount() { |
152 | /** @var LoadBalancer $lb */ |
153 | $lb = MediaWikiServices::getInstance()->getService( 'ContentTranslation.LoadBalancer' ); |
154 | $dbr = $lb->getConnection( DB_REPLICA ); |
155 | |
156 | return $dbr->newSelectQueryBuilder() |
157 | ->select( 'COUNT(DISTINCT translation_started_by)' ) |
158 | ->from( 'cx_translations' ) |
159 | ->where( Translation::getPublishedCondition( $dbr ) ) |
160 | ->caller( __METHOD__ ) |
161 | ->fetchField(); |
162 | } |
163 | } |