28 private const RIGHT =
'translate-manage';
30 public function execute():
void {
31 $this->checkUserRightsAny( self::RIGHT );
32 $params = $this->extractRequestParams();
34 $groupId = $params[
'groupId'];
35 $op = $params[
'operation'];
36 $msgKey = $params[
'messageKey'];
37 $name = $params[
'changesetName'] ?? MessageChangeStorage::DEFAULT_NAME;
38 $changesetModifiedTime = $params[
'changesetModified'];
41 if ( !MessageChangeStorage::isValidCdbName( $name ) ) {
43 [
'apierror-translate-invalid-changeset-name', wfEscapeWikiText( $name ) ],
47 $cdbPath = MessageChangeStorage::getCdbPath( $name );
49 if ( !MessageChangeStorage::isModifiedSince( $cdbPath, $changesetModifiedTime ) ) {
51 $this->dieWithError( [
'apierror-translate-changeset-modified' ] );
54 if ( $op ===
'rename' ) {
55 if ( !isset( $params[
'renameMessageKey'] ) ) {
56 $this->dieWithError( [
'apierror-missingparam',
'renameMessageKey' ] );
58 $keyToRename = $params[
'renameMessageKey'];
61 $sourceChanges = MessageChangeStorage::getGroupChanges( $cdbPath, $groupId );
62 if ( $sourceChanges->getAllModifications() === [] ) {
63 $this->dieWithError( [
'apierror-translate-smg-nochanges' ] );
66 $group = MessageGroups::getGroup( $groupId );
67 if ( $group ===
null ) {
68 $this->dieWithError(
'apierror-translate-invalidgroup',
'invalidgroup' );
72 if ( $op ===
'rename' ) {
74 $group, $sourceChanges, $msgKey, $keyToRename, $group->getSourceLanguage()
76 } elseif ( $op ===
'new' ) {
77 $this->
handleNew( $sourceChanges, $msgKey, $group->getSourceLanguage() );
80 [
'apierror-translate-invalid-operation', wfEscapeWikiText( $op ),
81 wfEscapeWikiText( implode(
'/', [
'new' ,
'rename' ] ) ) ],
85 }
catch ( Exception $ex ) {
89 'newMsgKey' => $msgKey,
90 'msgKey' => $keyToRename,
91 'groupId' => $group->getId(),
92 'group' => $group->getLabel(),
93 'groupSourceLang' => $group->getSourceLanguage(),
98 "Error while running: ManageMessageGroupsActionApi::execute. Inputs: \n" .
99 FormatJson::encode( $data,
true )
106 MessageChangeStorage::writeGroupChanges( $sourceChanges, $groupId, $cdbPath );
108 $this->getResult()->addValue(
null, $this->getModuleName(), [
119 string $sourceLanguage
121 $languages = $sourceChanges->getLanguages();
123 foreach ( $languages as $code ) {
124 $msgState = $renameMsgState =
null;
126 $isSourceLang = $sourceLanguage === $code;
127 if ( $isSourceLang ) {
128 $this->handleSourceRename( $sourceChanges, $code, $msgKey, $keyToRename );
135 $msg = $sourceChanges->
findMessage( $code, $msgKey, [
136 MessageSourceChange::ADDITION,
137 MessageSourceChange::RENAME
146 $msg = $msg ?? $sourceChanges->
findMessage( $code, $keyToRename, [
147 MessageSourceChange::DELETION,
148 MessageSourceChange::CHANGE,
149 MessageSourceChange::RENAME
152 if ( $msg ===
null ) {
158 $code, $keyToRename, [ MessageSourceChange::RENAME, MessageSourceChange::DELETION ],
165 if ( $renameMsg ===
null || !isset( $renameMsg[
'content'] ) ) {
166 $title = Title::newFromText(
167 TranslateUtils::title( $keyToRename, $code, $group->
getNamespace() ),
171 $renameContent = TranslateUtils::getContentForTitle( $title,
true ) ??
'';
174 'key' => $keyToRename,
175 'content' => $renameContent
180 if ( $renameMsgState ===
null ) {
181 $renameMsgState = MessageSourceChange::NONE;
186 if ( $msgState === MessageSourceChange::RENAME ) {
187 $msgState = $sourceChanges->
breakRename( $code, $msg[
'key'] );
192 if ( $renameMsgState === MessageSourceChange::RENAME ) {
193 $renameMsgState = $sourceChanges->
breakRename( $code, $renameMsg[
'key'] );
194 } elseif ( $renameMsgState !== MessageSourceChange::NONE ) {
199 $msg[
'key'] = $msgKey;
203 $similarity = $stringComparator->getSimilarity(
205 $renameMsg[
'content']
207 $sourceChanges->
addRename( $code, $msg, $renameMsg, $similarity );
208 $sourceChanges->setRenameState( $code, $msgKey, $msgState );
209 $sourceChanges->setRenameState( $code, $keyToRename, $renameMsgState );
213 protected function handleSourceRename(
214 MessageSourceChange $sourceChanges,
219 $msgState = $renameMsgState = null;
221 $msg = $sourceChanges->findMessage(
222 $code, $msgKey, [ MessageSourceChange::ADDITION, MessageSourceChange::RENAME ], $msgState
225 $renameMsg = $sourceChanges->findMessage(
228 [ MessageSourceChange::DELETION, MessageSourceChange::RENAME ],
232 if ( $msg ===
null || $renameMsg ===
null ) {
233 $this->dieWithError(
'apierror-translate-rename-key-invalid' );
236 if ( $msgState === MessageSourceChange::RENAME ) {
238 $msgState = $sourceChanges->breakRename( $code, $msg[
'key'] );
241 if ( $renameMsgState === MessageSourceChange::RENAME ) {
243 $renameMsgState = $sourceChanges->breakRename( $code, $renameMsg[
'key'] );
247 if ( $msgState !== MessageSourceChange::ADDITION ||
248 $renameMsgState !== MessageSourceChange::DELETION ) {
249 $this->dieWithError( [
250 'apierror-translate-rename-state-invalid',
251 wfEscapeWikiText( $msgState ), wfEscapeWikiText( $renameMsgState )
256 $sourceChanges->removeAdditions( $code, [ $msgKey ] );
257 $sourceChanges->removeDeletions( $code, [ $keyToRename ] );
260 $stringComparator =
new SimpleStringComparator();
261 $similarity = $stringComparator->getSimilarity(
265 $renameMsg[
'content']
267 $sourceChanges->addRename( $code, $msg, $renameMsg, $similarity );
279 foreach ( $languages as $code ) {
281 $code, $msgKey, [ MessageSourceChange::RENAME ], $msgState
284 if ( $code === $sourceLang && $msg ===
null ) {
285 $this->dieWithError(
'apierror-translate-addition-key-invalid' );
288 if ( $code === $sourceLang && $msgState !== MessageSourceChange::RENAME ) {
289 $this->dieWithError(
'apierror-translate-rename-msg-new' );
293 if ( $msg ===
null ) {
302 protected function getAllowedParams(): array {
305 ParamValidator::PARAM_TYPE =>
'string',
306 ParamValidator::PARAM_REQUIRED => true,
308 'renameMessageKey' => [
309 ParamValidator::PARAM_TYPE =>
'string',
310 ParamValidator::PARAM_REQUIRED => false,
313 ParamValidator::PARAM_TYPE =>
'string',
314 ParamValidator::PARAM_REQUIRED => true,
317 ParamValidator::PARAM_TYPE => [
'rename',
'new' ],
318 ParamValidator::PARAM_ISMULTI => false,
319 ParamValidator::PARAM_REQUIRED => true,
322 ParamValidator::PARAM_TYPE =>
'string',
325 'changesetModified' => [
326 ParamValidator::PARAM_TYPE =>
'integer',
327 ParamValidator::PARAM_REQUIRED => true,
332 public function isInternal(): bool {
336 public function needsToken(): string {
return[ 'Translate:ConfigHelper'=> static function():ConfigHelper { return new ConfigHelper();}, 'Translate:CsvTranslationImporter'=> static function(MediaWikiServices $services):CsvTranslationImporter { return new CsvTranslationImporter( $services->getWikiPageFactory());}, 'Translate:EntitySearch'=> static function(MediaWikiServices $services):EntitySearch { return new EntitySearch($services->getMainWANObjectCache(), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), MessageGroups::singleton(), $services->getNamespaceInfo(), $services->get( 'Translate:MessageIndex'), $services->getTitleParser(), $services->getTitleFormatter());}, 'Translate:ExternalMessageSourceStateImporter'=> static function(MediaWikiServices $services):ExternalMessageSourceStateImporter { return new ExternalMessageSourceStateImporter($services->getMainConfig(), $services->get( 'Translate:GroupSynchronizationCache'), $services->getJobQueueGroup(), LoggerFactory::getInstance( 'Translate.GroupSynchronization'), MessageIndex::singleton());}, 'Translate:GroupSynchronizationCache'=> static function(MediaWikiServices $services):GroupSynchronizationCache { return new GroupSynchronizationCache( $services->get( 'Translate:PersistentCache'));}, 'Translate:MessageBundleStore'=> static function(MediaWikiServices $services):MessageBundleStore { return new MessageBundleStore(new RevTagStore(), $services->getJobQueueGroup(), $services->getLanguageNameUtils(), $services->get( 'Translate:MessageIndex'));}, 'Translate:MessageGroupReview'=> static function(MediaWikiServices $services):MessageGroupReview { return new MessageGroupReview($services->getDBLoadBalancer(), $services->getHookContainer());}, 'Translate:MessageIndex'=> static function(MediaWikiServices $services):MessageIndex { $params=$services->getMainConfig() ->get( 'TranslateMessageIndex');if(is_string( $params)) { $params=(array) $params;} $class=array_shift( $params);return new $class( $params);}, 'Translate:ParsingPlaceholderFactory'=> static function():ParsingPlaceholderFactory { return new ParsingPlaceholderFactory();}, 'Translate:PersistentCache'=> static function(MediaWikiServices $services):PersistentCache { return new PersistentDatabaseCache($services->getDBLoadBalancer(), $services->getJsonCodec());}, 'Translate:ProgressStatsTableFactory'=> static function(MediaWikiServices $services):ProgressStatsTableFactory { return new ProgressStatsTableFactory($services->getLinkRenderer(), $services->get( 'Translate:ConfigHelper'));}, 'Translate:SubpageListBuilder'=> static function(MediaWikiServices $services):SubpageListBuilder { return new SubpageListBuilder($services->get( 'Translate:TranslatableBundleFactory'), $services->getLinkBatchFactory());}, 'Translate:TranslatableBundleFactory'=> static function(MediaWikiServices $services):TranslatableBundleFactory { return new TranslatableBundleFactory($services->get( 'Translate:TranslatablePageStore'), $services->get( 'Translate:MessageBundleStore'));}, 'Translate:TranslatableBundleMover'=> static function(MediaWikiServices $services):TranslatableBundleMover { return new TranslatableBundleMover($services->getMovePageFactory(), $services->getJobQueueGroup(), $services->getLinkBatchFactory(), $services->get( 'Translate:TranslatableBundleFactory'), $services->get( 'Translate:SubpageListBuilder'), $services->getMainConfig() ->get( 'TranslatePageMoveLimit'));}, 'Translate:TranslatablePageParser'=> static function(MediaWikiServices $services):TranslatablePageParser { return new TranslatablePageParser($services->get( 'Translate:ParsingPlaceholderFactory'));}, 'Translate:TranslatablePageStore'=> static function(MediaWikiServices $services):TranslatablePageStore { return new TranslatablePageStore($services->get( 'Translate:MessageIndex'), $services->getJobQueueGroup(), new RevTagStore(), $services->getDBLoadBalancer());}, 'Translate:TranslationStashReader'=> static function(MediaWikiServices $services):TranslationStashReader { $db=$services->getDBLoadBalancer() ->getConnectionRef(DB_REPLICA);return new TranslationStashStorage( $db);}, 'Translate:TranslationStatsDataProvider'=> static function(MediaWikiServices $services):TranslationStatsDataProvider { return new TranslationStatsDataProvider(new ServiceOptions(TranslationStatsDataProvider::CONSTRUCTOR_OPTIONS, $services->getMainConfig()), $services->getObjectFactory());}, 'Translate:TranslationUnitStoreFactory'=> static function(MediaWikiServices $services):TranslationUnitStoreFactory { return new TranslationUnitStoreFactory( $services->getDBLoadBalancer());}, 'Translate:TranslatorActivity'=> static function(MediaWikiServices $services):TranslatorActivity { $query=new TranslatorActivityQuery($services->getMainConfig(), $services->getDBLoadBalancer());return new TranslatorActivity($services->getMainObjectStash(), $query, $services->getJobQueueGroup());}, 'Translate:TtmServerFactory'=> static function(MediaWikiServices $services):TtmServerFactory { $config=$services->getMainConfig();$default=$config->get( 'TranslateTranslationDefaultService');if( $default===false) { $default=null;} return new TtmServerFactory( $config->get( 'TranslateTranslationServices'), $default);}]
@phpcs-require-sorted-array