Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
QueryLanguageStatsActionApi
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 7
132
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
 validateTargetParamater
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 loadStatistics
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 makeStatsItem
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheRebuildJob
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
6use ApiQuery;
7use IJobSpecification;
8use JobQueueGroup;
9use MediaWiki\Extension\Translate\MessageGroupProcessing\MessageGroups;
10use MediaWiki\Extension\Translate\Utilities\Utilities;
11use MessageGroupStats;
12use Wikimedia\ParamValidator\ParamValidator;
13
14/**
15 * Api module for querying language stats.
16 * @ingroup API TranslateAPI
17 * @author Niklas Laxström
18 * @license GPL-2.0-or-later
19 */
20class QueryLanguageStatsActionApi extends QueryStatsActionApi {
21    public function __construct(
22        ApiQuery $query,
23        string $moduleName,
24        JobQueueGroup $jobQueueGroup
25    ) {
26        parent::__construct( $query, $moduleName, 'ls', $jobQueueGroup );
27    }
28
29    // ApiStatsQuery methods
30
31    /** @inheritDoc */
32    protected function validateTargetParamater( array $params ): string {
33        $requested = $params[ 'language' ];
34        if ( !Utilities::isSupportedLanguageCode( $requested ) ) {
35            $this->dieWithError( [ 'apierror-translate-invalidlanguage', $requested ] );
36        }
37
38        return $requested;
39    }
40
41    /** @inheritDoc */
42    protected function loadStatistics( string $target, int $flags = 0 ): array {
43        $groupId = $this->getParameter( 'group' );
44        $group = $groupId !== null ? MessageGroups::getGroup( $groupId ) : null;
45        if ( $groupId ) {
46            if ( !$group ) {
47                $this->dieWithError( [ 'apierror-badparameter', 'group' ] );
48            }
49
50            return [ $groupId => MessageGroupStats::forItem( $group->getId(), $target, $flags ) ];
51        } else {
52            return MessageGroupStats::forLanguage( $target, $flags );
53        }
54    }
55
56    /** @inheritDoc */
57    protected function makeStatsItem( string $item, array $stats ): array {
58        $data = $this->makeItem( $stats );
59        $data['group'] = $item;
60
61        return $data;
62    }
63
64    /** @inheritDoc */
65    protected function getCacheRebuildJob( string $target ): IJobSpecification {
66        return RebuildMessageGroupStatsJob::newJob( [ 'languagecode' => $target ] );
67    }
68
69    // Api methods
70
71    /** @inheritDoc */
72    protected function getAllowedParams(): array {
73        $params = parent::getAllowedParams();
74        $params['language'] = [
75            ParamValidator::PARAM_TYPE => 'string',
76            ParamValidator::PARAM_REQUIRED => true,
77        ];
78
79        $params['group'] = [
80            ParamValidator::PARAM_TYPE => 'string',
81        ];
82
83        return $params;
84    }
85
86    /** @inheritDoc */
87    protected function getExamplesMessages(): array {
88        return [
89            'action=query&meta=languagestats&lslanguage=fi'
90                => 'apihelp-query+languagestats-example-1',
91            'action=query&meta=languagestats&lslanguage=fi&group=A'
92                => 'apihelp-query+languagestats-example-2'
93        ];
94    }
95}