summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'MLEB/Translate/utils/ExternalMessageSourceStateImporter.php')
-rw-r--r--MLEB/Translate/utils/ExternalMessageSourceStateImporter.php96
1 files changed, 69 insertions, 27 deletions
diff --git a/MLEB/Translate/utils/ExternalMessageSourceStateImporter.php b/MLEB/Translate/utils/ExternalMessageSourceStateImporter.php
index 495c3fd7..51674921 100644
--- a/MLEB/Translate/utils/ExternalMessageSourceStateImporter.php
+++ b/MLEB/Translate/utils/ExternalMessageSourceStateImporter.php
@@ -7,56 +7,67 @@
* @license GPL-2.0-or-later
* @since 2016.02
*/
+
+use MediaWiki\Extensions\Translate\MessageSync\MessageSourceChange;
+
class ExternalMessageSourceStateImporter {
- public function importSafe( $changeData ) {
+ /**
+ * @param MessageSourceChange[] $changeData
+ * @return array
+ */
+ public function importSafe( array $changeData ) {
$processed = [];
$skipped = [];
$jobs = [];
$jobs[] = MessageIndexRebuildJob::newJob();
+ /**
+ * @var MessageSourceChange $changesForGroup
+ */
foreach ( $changeData as $groupId => $changesForGroup ) {
+ /**
+ * @var FileBasedMessageGroup
+ */
$group = MessageGroups::getGroup( $groupId );
if ( !$group ) {
unset( $changeData[$groupId] );
continue;
}
+ '@phan-var FileBasedMessageGroup $group';
- $processed[$groupId] = 0;
+ $processed[$groupId] = [];
+ $languages = $changesForGroup->getLanguages();
- foreach ( $changesForGroup as $languageCode => $changesForLanguage ) {
- if ( !self::isSafe( $changesForLanguage ) ) {
+ foreach ( $languages as $language ) {
+ if ( !self::isSafe( $changesForGroup, $language ) ) {
+ // changes other than additions were present
$skipped[$groupId] = true;
continue;
}
- if ( !isset( $changesForLanguage['addition'] ) ) {
+ $additions = $changesForGroup->getAdditions( $language );
+ if ( $additions === [] ) {
continue;
}
- foreach ( $changesForLanguage['addition'] as $addition ) {
- $namespace = $group->getNamespace();
- $name = "{$addition['key']}/$languageCode";
-
- $title = Title::makeTitleSafe( $namespace, $name );
- if ( !$title ) {
- wfWarn( "Invalid title for group $groupId key {$addition['key']}" );
- continue;
- }
+ [ $groupJobs, $groupProcessed ] = $this->createMessageUpdateJobs(
+ $group, $additions, $language
+ );
- $jobs[] = MessageUpdateJob::newJob( $title, $addition['content'] );
- $processed[$groupId]++;
- }
-
- unset( $changeData[$groupId][$languageCode] );
+ $jobs = array_merge( $jobs, $groupJobs );
+ $processed[$groupId][$language] = $groupProcessed;
- $cache = new MessageGroupCache( $groupId, $languageCode );
- $cache->create();
+ $changesForGroup->removeChangesForLanguage( $language );
+ $group->getMessageGroupCache( $language )->create();
}
}
// Remove groups where everything was imported
- $changeData = array_filter( $changeData );
+ $changeData = array_filter( $changeData, function ( MessageSourceChange $change ) {
+ return $change->getAllModifications() !== [];
+ } );
+
// Remove groups with no imports
$processed = array_filter( $processed );
@@ -72,13 +83,44 @@ class ExternalMessageSourceStateImporter {
];
}
- protected static function isSafe( array $changesForLanguage ) {
- foreach ( array_keys( $changesForLanguage ) as $changeType ) {
- if ( $changeType !== 'addition' ) {
- return false;
+ /**
+ * Checks if changes for a language in a group are safe.
+ * @param MessageSourceChange $changesForGroup
+ * @param string $language
+ * @return bool
+ */
+ public static function isSafe( MessageSourceChange $changesForGroup, $language ) {
+ return $changesForGroup->hasOnly( $language, MessageSourceChange::ADDITION );
+ }
+
+ /**
+ * Creates MessagUpdateJobs additions for a language under a group
+ *
+ * @param MessageGroup $group
+ * @param string[][] $additions
+ * @param string $language
+ * @return array
+ */
+ private function createMessageUpdateJobs(
+ MessageGroup $group, array $additions, string $language
+ ) {
+ $groupId = $group->getId();
+ $jobs = [];
+ $processed = 0;
+ foreach ( $additions as $addition ) {
+ $namespace = $group->getNamespace();
+ $name = "{$addition['key']}/$language";
+
+ $title = Title::makeTitleSafe( $namespace, $name );
+ if ( !$title ) {
+ wfWarn( "Invalid title for group $groupId key {$addition['key']}" );
+ continue;
}
+
+ $jobs[] = MessageUpdateJob::newJob( $title, $addition['content'] );
+ $processed++;
}
- return true;
+ return [ $jobs, $processed ];
}
}