Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
GroupReviewActionApi | |
0.00% |
0 / 56 |
|
0.00% |
0 / 6 |
210 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
90 | |||
isWriteMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
needsToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAllowedParams | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\MessageGroupProcessing; |
5 | |
6 | use ApiBase; |
7 | use ApiMain; |
8 | use MediaWiki\Languages\LanguageNameUtils; |
9 | use Wikimedia\ParamValidator\ParamValidator; |
10 | |
11 | /** |
12 | * API module for switching workflow states for message groups |
13 | * @author Niklas Laxström |
14 | * @license GPL-2.0-or-later |
15 | * @ingroup API TranslateAPI |
16 | */ |
17 | class GroupReviewActionApi extends ApiBase { |
18 | protected static $right = 'translate-groupreview'; |
19 | /** @var LanguageNameUtils */ |
20 | private $languageNameUtils; |
21 | /** @var MessageGroupReviewStore */ |
22 | private $messageGroupReviewStore; |
23 | |
24 | public function __construct( |
25 | ApiMain $main, |
26 | string $action, |
27 | LanguageNameUtils $languageNameUtils, |
28 | MessageGroupReviewStore $messageGroupReviewStore |
29 | ) { |
30 | parent::__construct( $main, $action ); |
31 | $this->languageNameUtils = $languageNameUtils; |
32 | $this->messageGroupReviewStore = $messageGroupReviewStore; |
33 | } |
34 | |
35 | public function execute() { |
36 | $user = $this->getUser(); |
37 | $requestParams = $this->extractRequestParams(); |
38 | |
39 | $group = MessageGroups::getGroup( $requestParams['group'] ); |
40 | $code = $requestParams['language']; |
41 | |
42 | if ( !$group || MessageGroups::isDynamic( $group ) ) { |
43 | $this->dieWithError( [ 'apierror-badparameter', 'group' ] ); |
44 | } |
45 | $stateConfig = $group->getMessageGroupStates()->getStates(); |
46 | if ( !$stateConfig ) { |
47 | $this->dieWithError( 'apierror-translate-groupreviewdisabled', 'disabled' ); |
48 | } |
49 | |
50 | $this->checkUserRightsAny( self::$right ); |
51 | |
52 | $block = $user->getBlock(); |
53 | if ( $block ) { |
54 | $this->dieBlocked( $block ); |
55 | } |
56 | |
57 | $languages = $this->languageNameUtils->getLanguageNames(); |
58 | if ( !isset( $languages[$code] ) ) { |
59 | $this->dieWithError( [ 'apierror-badparameter', 'language' ] ); |
60 | } |
61 | |
62 | $targetState = $requestParams['state']; |
63 | if ( !isset( $stateConfig[$targetState] ) ) { |
64 | $this->dieWithError( 'apierror-translate-invalidstate', 'invalidstate' ); |
65 | } |
66 | |
67 | if ( is_array( $stateConfig[$targetState] ) |
68 | && isset( $stateConfig[$targetState]['right'] ) |
69 | ) { |
70 | $this->checkUserRightsAny( $stateConfig[$targetState]['right'] ); |
71 | } |
72 | |
73 | $this->messageGroupReviewStore->changeState( $group, $code, $targetState, $user ); |
74 | |
75 | $output = [ 'review' => [ |
76 | 'group' => $group->getId(), |
77 | 'language' => $code, |
78 | 'state' => $targetState, |
79 | ] ]; |
80 | |
81 | $this->getResult()->addValue( null, $this->getModuleName(), $output ); |
82 | } |
83 | |
84 | public function isWriteMode(): bool { |
85 | return true; |
86 | } |
87 | |
88 | public function needsToken(): string { |
89 | return 'csrf'; |
90 | } |
91 | |
92 | protected function getAllowedParams(): array { |
93 | return [ |
94 | 'group' => [ |
95 | ParamValidator::PARAM_TYPE => 'string', |
96 | ParamValidator::PARAM_REQUIRED => true, |
97 | ], |
98 | 'language' => [ |
99 | ParamValidator::PARAM_TYPE => 'string', |
100 | ParamValidator::PARAM_DEFAULT => 'en', |
101 | ], |
102 | 'state' => [ |
103 | ParamValidator::PARAM_TYPE => 'string', |
104 | ParamValidator::PARAM_REQUIRED => true, |
105 | ], |
106 | 'token' => [ |
107 | ParamValidator::PARAM_TYPE => 'string', |
108 | ParamValidator::PARAM_REQUIRED => true, |
109 | ], |
110 | ]; |
111 | } |
112 | |
113 | protected function getExamplesMessages(): array { |
114 | return [ |
115 | 'action=groupreview&group=page-Example&language=de&state=ready&token=foo' |
116 | => 'apihelp-groupreview-example-1', |
117 | ]; |
118 | } |
119 | } |