Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
GroupReviewActionApi.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use MediaWiki\Api\ApiBase;
7use MediaWiki\Api\ApiMain;
8use MediaWiki\Language\LanguageNameUtils;
9use Wikimedia\ParamValidator\ParamValidator;
10
17class GroupReviewActionApi extends ApiBase {
19 protected static $right = 'translate-groupreview';
20
21 public function __construct(
22 ApiMain $main,
23 string $action,
24 private readonly LanguageNameUtils $languageNameUtils,
25 private readonly MessageGroupReviewStore $messageGroupReviewStore,
26 ) {
27 parent::__construct( $main, $action );
28 }
29
30 public function execute() {
31 $user = $this->getUser();
32 $requestParams = $this->extractRequestParams();
33
34 $group = MessageGroups::getGroup( $requestParams['group'] );
35 $code = $requestParams['language'];
36
37 if ( !$group || MessageGroups::isDynamic( $group ) ) {
38 $this->dieWithError( [ 'apierror-badparameter', 'group' ] );
39 }
40 $stateConfig = $group->getMessageGroupStates()->getStates();
41 if ( !$stateConfig ) {
42 $this->dieWithError( 'apierror-translate-groupreviewdisabled', 'disabled' );
43 }
44
45 $this->checkUserRightsAny( self::$right );
46
47 $block = $user->getBlock();
48 if ( $block ) {
49 $this->dieBlocked( $block );
50 }
51
52 $languages = $this->languageNameUtils->getLanguageNames();
53 if ( !isset( $languages[$code] ) ) {
54 $this->dieWithError( [ 'apierror-badparameter', 'language' ] );
55 }
56
57 $targetState = $requestParams['state'];
58 if ( !isset( $stateConfig[$targetState] ) ) {
59 $this->dieWithError( 'apierror-translate-invalidstate', 'invalidstate' );
60 }
61
62 if ( is_array( $stateConfig[$targetState] )
63 && isset( $stateConfig[$targetState]['right'] )
64 ) {
65 $this->checkUserRightsAny( $stateConfig[$targetState]['right'] );
66 }
67
68 $this->messageGroupReviewStore->changeState( $group, $code, $targetState, $user );
69
70 $output = [ 'review' => [
71 'group' => $group->getId(),
72 'language' => $code,
73 'state' => $targetState,
74 ] ];
75
76 $this->getResult()->addValue( null, $this->getModuleName(), $output );
77 }
78
79 public function isWriteMode(): bool {
80 return true;
81 }
82
83 public function needsToken(): string {
84 return 'csrf';
85 }
86
87 protected function getAllowedParams(): array {
88 return [
89 'group' => [
90 ParamValidator::PARAM_TYPE => 'string',
91 ParamValidator::PARAM_REQUIRED => true,
92 ],
93 'language' => [
94 ParamValidator::PARAM_TYPE => 'string',
95 ParamValidator::PARAM_DEFAULT => 'en',
96 ],
97 'state' => [
98 ParamValidator::PARAM_TYPE => 'string',
99 ParamValidator::PARAM_REQUIRED => true,
100 ],
101 ];
102 }
103
104 protected function getExamplesMessages(): array {
105 return [
106 'action=groupreview&group=page-Example&language=de&state=ready&token=foo'
107 => 'apihelp-groupreview-example-1',
108 ];
109 }
110}
API module for switching workflow states for message groups.
Provides methods to get and change the state of a message group.
static getGroup(string $id)
Fetch a message group by id.