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