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