Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
QueryTranslationStatsActionApi.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
6use ApiBase;
7use ApiMain;
8use Wikimedia\ParamValidator\ParamValidator;
9use Wikimedia\ParamValidator\TypeDef\IntegerDef;
10
18class QueryTranslationStatsActionApi extends ApiBase {
19 private TranslationStatsDataProvider $dataProvider;
20
21 public function __construct( ApiMain $mainModule, $moduleName, TranslationStatsDataProvider $dataProvider ) {
22 parent::__construct( $mainModule, $moduleName );
23 $this->dataProvider = $dataProvider;
24 }
25
26 public function execute() {
27 $params = $this->extractRequestParams();
28 $graphOpts = new TranslationStatsGraphOptions();
29 $graphOpts->bindArray( $params );
30
31 $language = $this->getLanguage();
32
33 [ $labels, $data ] = $this->dataProvider->getGraphData( $graphOpts, $language );
34 $output = [
35 'labels' => $labels,
36 'data' => $data
37 ];
38
39 $this->getResult()->addValue( null, $this->getModuleName(), $output );
40 }
41
42 protected function getAllowedParams() {
43 return [
44 'count' => [
45 ParamValidator::PARAM_TYPE => $this->dataProvider->getGraphTypes(),
46 ParamValidator::PARAM_REQUIRED => true,
47 ],
48 'days' => [
49 ParamValidator::PARAM_TYPE => 'integer',
50 ParamValidator::PARAM_REQUIRED => true,
51 ParamValidator::PARAM_DEFAULT => 30,
52 IntegerDef::PARAM_MIN => 1,
53 IntegerDef::PARAM_MAX => 10000,
54 ApiBase::PARAM_RANGE_ENFORCE => true
55 ],
56 'group' => [
57 ParamValidator::PARAM_TYPE => 'string',
58 ParamValidator::PARAM_ISMULTI => true
59 ],
60 'language' => [
61 ParamValidator::PARAM_TYPE => 'string',
62 ParamValidator::PARAM_ISMULTI => true
63 ],
64 'scale' => [
65 ParamValidator::PARAM_TYPE => TranslationStatsGraphOptions::VALID_SCALES,
66 ParamValidator::PARAM_DEFAULT => 'days'
67 ],
68 'start' => [
69 ParamValidator::PARAM_TYPE => 'timestamp'
70 ]
71 ];
72 }
73
74 protected function getExamplesMessages() {
75 return [
76 'action=translationstats&count=edits&days=30'
77 => 'apihelp-translationstats-example-1',
78 'action=translationstats&count=edits&days=30&language=en|fr'
79 => 'apihelp-translationstats-example-2'
80 ];
81 }
82}