Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryTranslatorStats
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 5
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
20
 addMissingMonths
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
20
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Api module for querying translation statistics for a translator.
4 *
5 * @copyright See AUTHORS.txt
6 * @license GPL-2.0-or-later
7 */
8
9namespace ContentTranslation\ActionApi;
10
11use ContentTranslation\DateManipulator;
12use ContentTranslation\Service\UserService;
13use ContentTranslation\Translation;
14use Exception;
15use MediaWiki\Api\ApiQuery;
16use MediaWiki\Api\ApiQueryBase;
17use MediaWiki\User\User;
18use Wikimedia\ParamValidator\ParamValidator;
19
20class ApiQueryTranslatorStats extends ApiQueryBase {
21
22    private UserService $userService;
23
24    public function __construct(
25        ApiQuery $query,
26        string $moduleName,
27        UserService $userService
28    ) {
29        parent::__construct( $query, $moduleName );
30        $this->userService = $userService;
31    }
32
33    public function execute() {
34        $params = $this->extractRequestParams();
35        $user = $this->getUser();
36
37        if ( isset( $params['translator'] ) ) {
38            $user = User::newFromName( $params['translator'] );
39        }
40
41        if ( !$user ) {
42            $this->dieWithError( 'apierror-cx-invalidtranslator', 'invalidtranslator' );
43        }
44
45        try {
46            $translatorId = $this->userService->getGlobalUserId( $user );
47        } catch ( Exception $e ) {
48            $this->dieWithError( 'apierror-cx-invalidtranslator', 'invalidtranslator' );
49        }
50
51        $publishedStats = Translation::getTrendByStatus(
52            null, null, 'published', 'month', $translatorId
53        );
54
55        $trend = $this->addMissingMonths( $publishedStats );
56
57        $result = [
58            'translator' => $user->getName(),
59            'translatorId' => $translatorId,
60            'publishTrend' => $trend,
61        ];
62        $this->getResult()->addValue( null, $this->getModuleName(), $result );
63    }
64
65    private function addMissingMonths( $data ) {
66        $dates = array_keys( $data );
67
68        $dm = new DateManipulator( 'month' );
69        // If there is no data, just use zero (represents now)
70        // to avoid passing empty array into min method
71        $min = $dm->getIntervalIdentifier( !$dates ? 0 : min( $dates ) );
72        $max = $dm->getIntervalIdentifier( 0 ); // Now
73
74        $steps = $dm->getSteps( $min, $max );
75
76        $out = [];
77        $count = 0;
78
79        foreach ( $steps as $datetime ) {
80            $id = $datetime->format( 'U' );
81            $date = $datetime->format( 'Y-m-d' );
82
83            if ( isset( $data[ $id ] ) ) {
84                $out[ $date ] = $data[ $id ];
85                $count = $data[ $id ][ 'count' ];
86            } else {
87                $out[ $date ] = [
88                    'count' => $count,
89                    'delta' => 0
90                ];
91            }
92        }
93
94        return $out;
95    }
96
97    public function getAllowedParams() {
98        return [
99            'translator' => [
100                ParamValidator::PARAM_TYPE => 'string',
101            ]
102        ];
103    }
104
105    protected function getExamplesMessages() {
106        return [
107            'action=query&list=cxtranslatorstats&translator=TranslatorName' =>
108                'apihelp-query+cxtranslatorstats-example-1',
109        ];
110    }
111}