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\Languages\LanguageNameUtils;
9use Wikimedia\ParamValidator\ParamValidator;
10
17class GroupReviewActionApi extends ApiBase {
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}
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.