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';
20 private $languageNameUtils;
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}
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.