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