Skip to content

Commit

Permalink
Fix #19927: Fixed console\controllers\MessageController when saving…
Browse files Browse the repository at this point in the history
… translations to database: fixed FK error when adding new string and language at the same time, checking/regenerating all missing messages and dropping messages for unused languages
  • Loading branch information
atrandafir authored Oct 16, 2023
1 parent 5a4c048 commit 961a952
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 65 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.50 under development
------------------------

- Bug #19927: Fixed `console\controllers\MessageController` when saving translations to database: fixed FK error when adding new string and language at the same time, checking/regenerating all missing messages and dropping messages for unused languages (atrandafir)
- Enh #12743: Added new methods `BaseActiveRecord::loadRelations()` and `BaseActiveRecord::loadRelationsFor()` to eager load related models for existing primary model instances (PowerGamer1)


Expand Down
161 changes: 96 additions & 65 deletions framework/console/controllers/MessageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,17 +353,7 @@ protected function saveMessagesToDb($messages, $db, $sourceMessageTable, $messag
foreach ($rows as $row) {
$currentMessages[$row['category']][$row['id']] = $row['message'];
}

$currentLanguages = [];
$rows = (new Query())->select(['language'])->from($messageTable)->groupBy('language')->all($db);
foreach ($rows as $row) {
$currentLanguages[] = $row['language'];
}
$missingLanguages = [];
if (!empty($currentLanguages)) {
$missingLanguages = array_diff($languages, $currentLanguages);
}


$new = [];
$obsolete = [];

Expand All @@ -372,89 +362,130 @@ protected function saveMessagesToDb($messages, $db, $sourceMessageTable, $messag

if (isset($currentMessages[$category])) {
$new[$category] = array_diff($msgs, $currentMessages[$category]);
// obsolete messages per category
$obsolete += array_diff($currentMessages[$category], $msgs);
} else {
$new[$category] = $msgs;
}
}


// obsolete categories
foreach (array_diff(array_keys($currentMessages), array_keys($messages)) as $category) {
$obsolete += $currentMessages[$category];
}

if (!$removeUnused) {
foreach ($obsolete as $pk => $msg) {
// skip already marked unused
if (strncmp($msg, '@@', 2) === 0 && substr($msg, -2) === '@@') {
unset($obsolete[$pk]);
}
}
}

$obsolete = array_keys($obsolete);
}

$this->stdout('Inserting new messages...');
$savedFlag = false;
$insertCount = 0;

foreach ($new as $category => $msgs) {
foreach ($msgs as $msg) {
$savedFlag = true;
$lastPk = $db->schema->insert($sourceMessageTable, ['category' => $category, 'message' => $msg]);
foreach ($languages as $language) {
$db->createCommand()
->insert($messageTable, ['id' => $lastPk['id'], 'language' => $language])
->execute();
}
}
}

if (!empty($missingLanguages)) {
$updatedMessages = [];
$rows = (new Query())->select(['id', 'category', 'message'])->from($sourceMessageTable)->all($db);
foreach ($rows as $row) {
$updatedMessages[$row['category']][$row['id']] = $row['message'];
}
foreach ($updatedMessages as $category => $msgs) {
foreach ($msgs as $id => $msg) {
$savedFlag = true;
foreach ($missingLanguages as $language) {
$db->createCommand()
->insert($messageTable, ['id' => $id, 'language' => $language])
->execute();
}
}
$insertCount++;
$db->schema->insert($sourceMessageTable, ['category' => $category, 'message' => $msg]);
}
}

$this->stdout($savedFlag ? "saved.\n" : "Nothing to save.\n");

$this->stdout($insertCount ? "{$insertCount} saved.\n" : "Nothing to save.\n");

$this->stdout($removeUnused ? 'Deleting obsoleted messages...' : 'Updating obsoleted messages...');

if (empty($obsolete)) {
$this->stdout("Nothing obsoleted...skipped.\n");
return;
}

if ($removeUnused) {
$db->createCommand()
->delete($sourceMessageTable, ['in', 'id', $obsolete])
->execute();
$this->stdout("deleted.\n");
} elseif ($markUnused) {
$rows = (new Query())
->select(['id', 'message'])
->from($sourceMessageTable)
->where(['in', 'id', $obsolete])
->all($db);

foreach ($rows as $row) {
$db->createCommand()->update(
$sourceMessageTable,
['message' => '@@' . $row['message'] . '@@'],
['id' => $row['id']]
)->execute();
if ($obsolete) {
if ($removeUnused) {
$affected = $db->createCommand()
->delete($sourceMessageTable, ['in', 'id', array_keys($obsolete)])
->execute();
$this->stdout("{$affected} deleted.\n");
} elseif ($markUnused) {
$marked=0;
$rows = (new Query())
->select(['id', 'message'])
->from($sourceMessageTable)
->where(['in', 'id', array_keys($obsolete)])
->all($db);

foreach ($rows as $row) {
$marked++;
$db->createCommand()->update(
$sourceMessageTable,
['message' => '@@' . $row['message'] . '@@'],
['id' => $row['id']]
)->execute();
}
$this->stdout("{$marked} updated.\n");
} else {
$this->stdout("kept untouched.\n");
}
$this->stdout("updated.\n");
} else {
$this->stdout("kept untouched.\n");
}

// get fresh message id list
$freshMessagesIds = [];
$rows = (new Query())->select(['id'])->from($sourceMessageTable)->all($db);
foreach ($rows as $row) {
$freshMessagesIds[] = $row['id'];
}

$this->stdout("Generating missing rows...");
$generatedMissingRows = [];

foreach ($languages as $language) {
$count = 0;

// get list of ids of translations for this language
$msgRowsIds = [];
$msgRows = (new Query())->select(['id'])->from($messageTable)->where([
'language'=>$language,
])->all($db);
foreach ($msgRows as $row) {
$msgRowsIds[] = $row['id'];
}

// insert missing
foreach ($freshMessagesIds as $id) {
if (!in_array($id, $msgRowsIds)) {
$db->createCommand()
->insert($messageTable, ['id' => $id, 'language' => $language])
->execute();
$count++;
}
}
if ($count) {
$generatedMissingRows[] = "{$count} for {$language}";
}
}

$this->stdout($generatedMissingRows ? implode(", ", $generatedMissingRows).".\n" : "Nothing to do.\n");

$this->stdout("Dropping unused languages...");
$droppedLanguages=[];

$currentLanguages = [];
$rows = (new Query())->select(['language'])->from($messageTable)->groupBy('language')->all($db);
foreach ($rows as $row) {
$currentLanguages[] = $row['language'];
}

foreach ($currentLanguages as $currentLanguage) {
if (!in_array($currentLanguage, $languages)) {
$deleted=$db->createCommand()->delete($messageTable, "language=:language", [
'language'=>$currentLanguage,
])->execute();
$droppedLanguages[] = "removed {$deleted} rows for $currentLanguage";
}
}

$this->stdout($droppedLanguages ? implode(", ", $droppedLanguages).".\n" : "Nothing to do.\n");
}

/**
Expand Down

0 comments on commit 961a952

Please sign in to comment.