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 $messageGroupReview;
23
24 public function __construct(
25 ApiMain $main,
26 string $action,
27 LanguageNameUtils $languageNameUtils,
28 MessageGroupReview $messageGroupReview
29 ) {
30 parent::__construct( $main, $action );
31 $this->languageNameUtils = $languageNameUtils;
32 $this->messageGroupReview = $messageGroupReview;
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 if ( $user->getBlock() ) {
53 $this->dieBlocked( $user->getBlock() );
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->messageGroupReview->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 'token' => [
106 ParamValidator::PARAM_TYPE => 'string',
107 ParamValidator::PARAM_REQUIRED => true,
108 ],
109 ];
110 }
111
112 protected function getExamplesMessages(): array {
113 return [
114 'action=groupreview&group=page-Example&language=de&state=ready&token=foo'
115 => 'apihelp-groupreview-example-1',
116 ];
117 }
118}
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.