Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
ManageGroupSynchronizationCacheActionApi.php
1<?php
2declare( strict_types = 1 );
3
5
6use Exception;
7use MediaWiki\Api\ApiBase;
8use MediaWiki\Api\ApiMain;
11use MediaWiki\Json\FormatJson;
12use MediaWiki\Logger\LoggerFactory;
13use Psr\Log\LoggerInterface;
14use Wikimedia\ParamValidator\ParamValidator;
15
24 private const RIGHT = 'translate-manage';
25 private const VALID_OPS = [ 'resolveMessage', 'resolveGroup' ];
26 private GroupSynchronizationCache $groupSyncCache;
27 private LoggerInterface $groupSyncLog;
28
29 public function __construct(
30 ApiMain $mainModule,
31 string $moduleName,
32 GroupSynchronizationCache $groupSyncCache
33 ) {
34 parent::__construct( $mainModule, $moduleName );
35 $this->groupSyncCache = $groupSyncCache;
36 $this->groupSyncLog = LoggerFactory::getInstance( LogNames::GROUP_SYNCHRONIZATION );
37 }
38
39 public function execute() {
40 $this->checkUserRightsAny( self::RIGHT );
41 $block = $this->getUser()->getBlock();
42 if ( $block && $block->isSitewide() ) {
43 $this->dieBlocked( $block );
44 }
45
46 $params = $this->extractRequestParams();
47 $operation = $params['operation'];
48 $groupId = $params['group'];
49 $titleStr = $params['title'] ?? null;
50
51 $group = MessageGroups::getGroup( $groupId );
52 if ( $group === null ) {
53 $this->dieWithError( 'apierror-translate-invalidgroup', 'invalidgroup' );
54 }
55
56 try {
57 if ( $operation === 'resolveMessage' ) {
58 if ( $titleStr === null ) {
59 $this->dieWithError( [ 'apierror-missingparam', 'title' ] );
60 }
61 $this->markAsResolved( $groupId, $titleStr );
62 } elseif ( $operation === 'resolveGroup' ) {
63 $this->markAsResolved( $groupId );
64 }
65 } catch ( Exception $e ) {
66 $data = [
67 'requestParams' => $params,
68 'exceptionMessage' => $e->getMessage()
69 ];
70
71 $this->groupSyncLog->error(
72 "Error while running: ManageGroupSynchronizationCacheActionApi::execute. Details: \n" .
73 FormatJson::encode( $data, true )
74 );
75
76 $this->dieWithError(
77 [
78 'apierror-translate-operation-error',
79 wfEscapeWikiText( $e->getMessage() )
80 ]
81 );
82 }
83 }
84
85 private function markAsResolved( string $groupId, ?string $messageTitle = null ): void {
86 if ( $messageTitle === null ) {
87 $currentGroupStatus = $this->groupSyncCache->markGroupAsResolved( $groupId );
88 $this->groupSyncLog->info(
89 '{user} resolved group {groupId}.',
90 [
91 'user' => $this->getUser()->getName(),
92 'groupId' => $groupId
93 ]
94 );
95 } else {
96 $this->groupSyncCache->markMessageAsResolved( $groupId, $messageTitle );
97 $currentGroupStatus = $this->groupSyncCache->syncGroupErrors( $groupId );
98 $this->groupSyncLog->info(
99 '{user} resolved message {messageTitle} in group {groupId}.',
100 [
101 'user' => $this->getUser()->getName(),
102 'groupId' => $groupId,
103 'messageTitle' => $messageTitle
104 ]
105 );
106 }
107
108 $this->getResult()->addValue( null, $this->getModuleName(), [
109 'success' => 1,
110 'data' => [
111 'groupRemainingMessageCount' => count( $currentGroupStatus->getRemainingMessages() )
112 ]
113 ] );
114 }
115
116 protected function getAllowedParams() {
117 return [
118 'operation' => [
119 ParamValidator::PARAM_TYPE => self::VALID_OPS,
120 ParamValidator::PARAM_ISMULTI => false,
121 ParamValidator::PARAM_REQUIRED => true,
122 ],
123 'title' => [
124 ParamValidator::PARAM_TYPE => 'string',
125 ParamValidator::PARAM_REQUIRED => false
126 ],
127 'group' => [
128 ParamValidator::PARAM_TYPE => 'string',
129 ParamValidator::PARAM_REQUIRED => true
130 ]
131 ];
132 }
133
134 public function isInternal() {
135 return true;
136 }
137
138 public function needsToken() {
139 return 'csrf';
140 }
141}
Constants for log channel names used in this extension.
Definition LogNames.php:13
const GROUP_SYNCHRONIZATION
Channel for message group synchronization.
Definition LogNames.php:18
Factory class for accessing message groups individually by id or all of them as a list.
Finds external changes for file based message groups.