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