29 protected static string $right =
'translate-manage';
31 public function __construct(
34 private readonly JobQueueGroup $jobQueueGroup,
38 parent::__construct( $main, $action );
41 public function execute():
void {
42 $this->checkUserRightsAny( self::$right );
43 $block = $this->getUser()->getBlock();
44 if ( $block && $block->isSitewide() ) {
45 $this->dieBlocked( $block );
48 $params = $this->extractRequestParams();
49 $action = $params[
'do'];
51 if ( $action ===
'associate' || $action ===
'dissociate' ) {
53 if ( !isset( $params[
'groups'] ) ) {
54 $this->dieWithError( [
'apierror-missingparam',
'groups' ] );
57 $subgroupIds = array_map(
'trim', $params[
'groups'] );
59 if ( !isset( $params[
'aggregategroup'] ) ) {
60 $this->dieWithError( [
'apierror-missingparam',
'aggregategroup' ] );
63 $aggregateGroupId = $params[
'aggregategroup'];
66 if ( $action ===
'associate' ) {
68 $groupIdsToLog = $this->aggregateGroupManager->associate( $aggregateGroupId, $subgroupIds );
69 $output[
'groupUrls'] = [];
70 foreach ( $groupIdsToLog as $subgroupId ) {
71 $output[
'groupUrls'][ $subgroupId ] =
72 $this->aggregateGroupManager->getTargetTitleByGroupId( $subgroupId )->getFullURL();
75 $groupIdsToLog = $this->aggregateGroupManager->disassociate( $aggregateGroupId, $subgroupIds );
82 $this->dieWithException( $e );
86 'aggregategroup' => $this->messageGroupMetadata->get( $aggregateGroupId,
'name' ),
87 'aggregategroup-id' => $aggregateGroupId,
90 foreach ( $groupIdsToLog as $subgroupId ) {
91 $title = $this->aggregateGroupManager->getTargetTitleByGroupId( $subgroupId );
92 $entry =
new ManualLogEntry(
'pagetranslation', $action );
93 $entry->setPerformer( $this->getUser() );
94 $entry->setTarget( $title );
95 $entry->setParameters( $logParams );
97 $logId = $entry->insert();
98 $entry->publish( $logId );
100 } elseif ( $action ===
'remove' ) {
101 if ( !isset( $params[
'aggregategroup'] ) ) {
102 $this->dieWithError( [
'apierror-missingparam',
'aggregategroup' ] );
105 $aggregateGroupId = $params[
'aggregategroup'];
109 'apierror-translate-invalidaggregategroupname',
110 'invalidaggregategroupname'
114 $this->messageGroupMetadata->deleteGroup( $params[
'aggregategroup'] );
117 'Aggregate group {groupId} has been deleted.',
118 [
'groupId' => $aggregateGroupId ]
120 } elseif ( $action ===
'add' ) {
121 if ( !isset( $params[
'groupname'] ) ) {
122 $this->dieWithError( [
'apierror-missingparam',
'groupname' ] );
124 $name = trim( $params[
'groupname'] );
125 if ( strlen( $name ) === 0 ) {
127 'apierror-translate-invalidaggregategroupname',
128 'invalidaggregategroupname'
132 $desc = trim( $params[
'groupdescription'] );
133 $languageCode = trim( $params[
'groupsourcelanguagecode'] );
134 $languageCode = $languageCode === AggregateMessageGroup::UNDETERMINED_LANGUAGE_CODE ?
135 null : $languageCode;
137 $aggregateGroupId = $this->aggregateGroupManager->add( $name, $desc, $languageCode );
139 $this->dieWithException( $e );
143 $output[
'groups'] = $this->getIncludableGroups();
144 $output[
'aggregategroupId'] = $aggregateGroupId;
146 } elseif ( $action ===
'update' ) {
147 if ( !isset( $params[
'groupname'] ) ) {
148 $this->dieWithError( [
'apierror-missingparam',
'groupname' ] );
150 $name = trim( $params[
'groupname'] );
151 if ( strlen( $name ) === 0 ) {
153 'apierror-translate-invalidaggregategroupname',
154 'invalidaggregategroupname'
158 $aggregateGroupId = $params[
'aggregategroup'];
159 $oldName = $this->messageGroupMetadata->get( $aggregateGroupId,
'name' );
163 if ( $exists && $oldName !== $name ) {
167 $desc = trim( $params[
'groupdescription'] );
169 $newLanguageCode = trim( $params[
'groupsourcelanguagecode'] );
171 $oldDesc = $this->messageGroupMetadata->get( $aggregateGroupId,
'description' );
172 $currentLanguageCode = $this->messageGroupMetadata->get( $aggregateGroupId,
'sourcelanguagecode' );
175 $newLanguageCode !== AggregateMessageGroup::UNDETERMINED_LANGUAGE_CODE &&
176 $newLanguageCode !== $currentLanguageCode
178 $groupsWithDifferentLanguage =
179 $this->getGroupsWithDifferentLanguage( $aggregateGroupId, $newLanguageCode );
181 if ( count( $groupsWithDifferentLanguage ) ) {
182 $this->dieWithError( [
183 'translate-error-aggregategroup-source-language-mismatch',
184 implode(
', ', $groupsWithDifferentLanguage ),
186 count( $groupsWithDifferentLanguage )
193 && $oldDesc === $desc
194 && $newLanguageCode === $currentLanguageCode
196 $this->dieWithError(
'apierror-translate-invalidupdate',
'invalidupdate' );
198 $this->messageGroupMetadata->set( $aggregateGroupId,
'name', $name );
199 $this->messageGroupMetadata->set( $aggregateGroupId,
'description', $desc );
200 if ( $newLanguageCode === AggregateMessageGroup::UNDETERMINED_LANGUAGE_CODE ) {
201 $this->messageGroupMetadata->clearMetadata( $aggregateGroupId, [
'sourcelanguagecode' ] );
203 $this->messageGroupMetadata->set( $aggregateGroupId,
'sourcelanguagecode', $newLanguageCode );
208 $output[
'result'] =
'ok';
209 $this->getResult()->addValue(
null, $this->getModuleName(), $output );
211 MessageGroups::singleton()->recache();
212 $this->jobQueueGroup->push( RebuildMessageIndexJob::newJob() );
222 private function getGroupsWithDifferentLanguage(
223 string $aggregateGroupId,
224 string $sourceLanguageCode
226 $groupsWithDifferentLanguage = [];
227 $subgroups = $this->messageGroupMetadata->getSubgroups( $aggregateGroupId );
228 foreach ( $subgroups as $group ) {
230 $messageGroupLanguage = $messageGroup->getSourceLanguage();
231 if ( $messageGroupLanguage !== $sourceLanguageCode ) {
232 $groupsWithDifferentLanguage[] = $messageGroup->getLabel();
236 return $groupsWithDifferentLanguage;
239 public function isWriteMode():
bool {
243 public function needsToken():
string {
247 protected function getAllowedParams(): array {
250 ParamValidator::PARAM_TYPE => [
'associate',
'dissociate',
'remove',
'add',
'update' ],
251 ParamValidator::PARAM_REQUIRED =>
true,
253 'aggregategroup' => [
254 ParamValidator::PARAM_TYPE =>
'string',
258 ParamValidator::PARAM_TYPE =>
'string',
259 ParamValidator::PARAM_ISMULTI =>
true,
262 ParamValidator::PARAM_TYPE =>
'string',
264 'groupdescription' => [
265 ParamValidator::PARAM_TYPE =>
'string',
266 ParamValidator::PARAM_DEFAULT =>
'',
268 'groupsourcelanguagecode' => [
269 ParamValidator::PARAM_TYPE =>
'string',
270 ParamValidator::PARAM_DEFAULT => AggregateMessageGroup::UNDETERMINED_LANGUAGE_CODE,
275 protected function getExamplesMessages(): array {
277 'action=aggregategroups&do=associate&groups=groupId1|groupId2&aggregategroup=aggregateGroupId'
278 =>
'apihelp-aggregategroups-example-1',
283 private function getIncludableGroups(): array {
286 foreach ( $groups as $group ) {
287 if ( $this->aggregateGroupManager->supportsAggregation( $group ) ) {
288 $pages[$group->getId()] = $group->getLabel( $this->getContext() );