Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 66 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryContentTranslationLanguageTrend | |
0.00% |
0 / 66 |
|
0.00% |
0 / 5 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
42 | |||
addMissingDates | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
42 | |||
getAllowedParams | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Api module for querying Content translations trend over a period of time. |
4 | * |
5 | * @copyright See AUTHORS.txt |
6 | * @license GPL-2.0-or-later |
7 | */ |
8 | |
9 | namespace ContentTranslation\ActionApi; |
10 | |
11 | /** |
12 | * Api module for querying ContentTranslation stats. |
13 | */ |
14 | |
15 | use ContentTranslation\DateManipulator; |
16 | use ContentTranslation\Translation; |
17 | use MediaWiki\Api\ApiQuery; |
18 | use MediaWiki\Api\ApiQueryBase; |
19 | use MediaWiki\Languages\LanguageNameUtils; |
20 | use Wikimedia\ParamValidator\ParamValidator; |
21 | |
22 | class ApiQueryContentTranslationLanguageTrend extends ApiQueryBase { |
23 | |
24 | private LanguageNameUtils $languageNameUtils; |
25 | |
26 | public function __construct( |
27 | ApiQuery $query, |
28 | string $moduleName, |
29 | LanguageNameUtils $languageNameUtils |
30 | ) { |
31 | parent::__construct( $query, $moduleName ); |
32 | $this->languageNameUtils = $languageNameUtils; |
33 | } |
34 | |
35 | public function execute() { |
36 | $result = $this->getResult(); |
37 | $params = $this->extractRequestParams(); |
38 | $source = $target = null; |
39 | if ( isset( $params['source'] ) && $this->languageNameUtils->isValidBuiltInCode( $params['source'] ) ) { |
40 | $source = $params['source']; |
41 | } |
42 | if ( isset( $params['target'] ) && $this->languageNameUtils->isValidBuiltInCode( $params['target'] ) ) { |
43 | $target = $params['target']; |
44 | } |
45 | $interval = $params['interval']; |
46 | |
47 | $data = [ |
48 | 'translations' => Translation::getTrendByStatus( |
49 | $source, $target, 'published', $interval, null |
50 | ), |
51 | 'drafts' => Translation::getTrendByStatus( $source, $target, 'draft', $interval, null ), |
52 | ]; |
53 | |
54 | if ( $target !== null ) { |
55 | // We can give deletion rates for only local wiki. We cannot give |
56 | // deletion stats for all wikis. |
57 | $data['deletions'] = Translation::getDeletionTrend( $interval ); |
58 | } |
59 | |
60 | $out = $this->addMissingDates( $data, $interval ); |
61 | $result->addValue( [ 'query' ], 'contenttranslationlangtrend', $out ); |
62 | } |
63 | |
64 | /** |
65 | * @param array $data |
66 | * @param string $interval |
67 | * @return array |
68 | */ |
69 | public function addMissingDates( $data, $interval ) { |
70 | $dates = call_user_func_array( 'array_merge', array_map( 'array_keys', $data ) ); |
71 | if ( $dates === [] ) { |
72 | return []; |
73 | } |
74 | |
75 | $dm = new DateManipulator( $interval ); |
76 | $min = $dm->getIntervalIdentifier( min( $dates ) ); |
77 | $max = $dm->getIntervalIdentifier( 0 ); // 0 means now |
78 | |
79 | $steps = $dm->getSteps( $min, $max ); |
80 | |
81 | $counts = []; |
82 | foreach ( array_keys( $data ) as $type ) { |
83 | $counts[$type] = 0; |
84 | } |
85 | |
86 | $out = []; |
87 | foreach ( $steps as $datetime ) { |
88 | foreach ( $data as $type => $column ) { |
89 | $id = $datetime->format( 'U' ); |
90 | $date = $datetime->format( 'Y-m-d' ); |
91 | |
92 | if ( isset( $column[$id] ) ) { |
93 | $column[$id]['date'] = $date; |
94 | $out[$type][] = $column[$id]; |
95 | // @phan-suppress-next-line PhanTypeInvalidDimOffset |
96 | $counts[$type] = $column[$id]['count']; |
97 | } else { |
98 | $out[$type][] = [ |
99 | 'count' => $counts[$type], |
100 | 'delta' => 0, |
101 | 'date' => $date, |
102 | ]; |
103 | } |
104 | } |
105 | } |
106 | |
107 | return $out; |
108 | } |
109 | |
110 | public function getAllowedParams() { |
111 | $allowedParams = [ |
112 | 'source' => [ |
113 | ParamValidator::PARAM_TYPE => 'string', |
114 | ParamValidator::PARAM_REQUIRED => false, |
115 | ], |
116 | 'target' => [ |
117 | ParamValidator::PARAM_TYPE => 'string', |
118 | ParamValidator::PARAM_REQUIRED => false, |
119 | ], |
120 | 'interval' => [ |
121 | ParamValidator::PARAM_DEFAULT => 'week', |
122 | ParamValidator::PARAM_TYPE => [ 'week', 'month' ], |
123 | ] |
124 | ]; |
125 | return $allowedParams; |
126 | } |
127 | |
128 | protected function getExamplesMessages() { |
129 | return [ |
130 | 'action=query&list=contenttranslationlangtrend&source=es&target=ca&interval=week' => |
131 | 'apihelp-query+contenttranslationlangtrend-example-1', |
132 | 'action=query&list=contenttranslationlangtrend' => |
133 | 'apihelp-query+contenttranslationlangtrend-example-2', |
134 | ]; |
135 | } |
136 | } |