Skip to content

Commit 8fc0e1e

Browse files
committed
#1096 Refactor update code and output
1 parent 1995e5b commit 8fc0e1e

1 file changed

Lines changed: 56 additions & 51 deletions

File tree

library/Application/Update/UpdateDoiImportAndConferencesFields.php

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,12 @@
3737
* Updates enrichment fields used by DOI import functionality.
3838
*
3939
* Changes names of enrichment fields for doi based metadata import to new OPUS default.
40-
* Changes names of existing and adds new enrichment fields for conferences.
4140
*
42-
* Checks whether old fields exist and changes their names. If one or more old fields
43-
* are missing, the corresponding new fields are created unless the new fields do
44-
* already exist, too. In this case, an error message for manual revision is generated.
41+
* Changes names of existing and adds new enrichment fields for conferences.
4542
*
4643
* Adapts key names for edited or added translations of the enrichment fields for
4744
* the doi based metadata import and conferences to the new names of the enrichment
4845
* fields.
49-
*
50-
* Checks whether old keys with edited or added translations exist and changes their
51-
* names. If the new translation key already exists with an edited or added translation,
52-
* an error message for manual revision is generated.
5346
*/
5447
class Application_Update_UpdateDoiImportAndConferencesFields extends Application_Update_PluginAbstract
5548
{
@@ -68,7 +61,7 @@ class Application_Update_UpdateDoiImportAndConferencesFields extends Application
6861

6962
public function run()
7063
{
71-
$this->log("Updating enrichment fieds for doi import and conferences as well as their translations");
64+
$this->log('Updating enrichment keys for DOI import and conferences (including their translation keys)...');
7265
// Iterate through the keys
7366
foreach ($this->keyNames as $oldKeyString => $newKeyString) {
7467
$this->log("Updating '$oldKeyString' -> '$newKeyString'");
@@ -82,7 +75,6 @@ public function run()
8275
*
8376
* @param string $oldKeyString
8477
* @param string $newKeyString
85-
* @param object $colors
8678
*/
8779
public function updateEnrichments($oldKeyString, $newKeyString)
8880
{
@@ -93,23 +85,25 @@ public function updateEnrichments($oldKeyString, $newKeyString)
9385
$newField = EnrichmentKey::fetchByName($newKeyString);
9486

9587
if ($oldField === null) {
96-
// If old and new enrichment field don't exist, the new enrichtment field is created
9788
if ($newField === null) {
89+
// If old and new enrichment don't exist, the new enrichment field is created
9890
$newKey = EnrichmentKey::new();
9991
$this->updateEnrichmentKey($newKey, $newKeyString);
100-
$this->getLogger()->info("Old enrichment field '$oldKeyString' doesn't exist. New enrichment field '$newKeyString' didn't exist either and was created.");
101-
// If only the new enrichment field exists, no changes are needed
92+
$this->log("New enrichment '{$newKeyString}' created.");
10293
} else {
103-
$this->getLogger()->info("Old enrichment field '$oldKeyString' doesn't exist. New enrichment field '$newKeyString' exists. Already up-to-date. No changes needed.");
94+
// If only the new enrichment exists, no changes are needed
95+
$this->log("New enrichment field '{$newKeyString}' already exists. No changes needed.");
10496
}
10597
} else {
106-
// If old and new enrichment field exist already, something seems to be wrong and should be checked
10798
if ($newField !== null) {
108-
$this->log($colors->red("Old enrichment field '$oldKeyString' and new one '$newKeyString' exist parallel. Please clean this up manually."));
109-
// If only the old enrichment field exists, it is updated to its new name
99+
// If old and new enrichment exist, something seems to be wrong and should be checked manually
100+
$this->log($colors->red(
101+
"Old ({$oldKeyString}) and new ({$newKeyString}) enrichment exist. Please clean up manually."
102+
));
110103
} else {
104+
// If only the old enrichment field exists, it is updated to its new name
111105
$this->updateEnrichmentKey($oldField, $newKeyString);
112-
$this->getLogger()->info("Old enrichment field '$oldKeyString' did exist. It has been updated to '$newKeyString'.");
106+
$this->log("Enrichment key '{$oldKeyString}' has been changed to '{$newKeyString}'.");
113107
}
114108
}
115109
}
@@ -129,64 +123,75 @@ public function updateEnrichmentKey($enrichmentKey, $newKey)
129123
/**
130124
* Update translations
131125
*
132-
* @param string $oldKeyString
133-
* @param string $newKeyString
134-
* @param object $colors
126+
* @param string $oldKeyString
127+
* @param string $newKeyString
135128
*/
136129
public function updateTranslations($oldKeyString, $newKeyString)
137130
{
138131
$colors = new ConsoleColors();
139132

140-
$manager = new Application_Translate_TranslationManager();
141-
142-
// Identify already existing modified new translation keys
143-
$manager->setFilter($newKeyString);
144-
$newTranslations = $manager->getMergedTranslations();
145-
foreach (array_keys($newTranslations) as $newKey) {
146-
$translation = $manager->getTranslation($newKey);
147-
if (isset($translation['state']) && (in_array($translation['state'], ['edited', 'added']))) {
148-
$modNewKeys[$newKey] = '';
149-
}
150-
}
133+
// Identify already existing modified new translation keys (in database)
134+
$modNewKeys = $this->getModifiedTranslationKeys($newKeyString);
135+
$conflictKeys = [];
151136

152137
// Identify modified old translation keys and update them to their new names
138+
$manager = new Application_Translate_TranslationManager();
153139
$manager->setFilter($oldKeyString);
154-
$oldTranslations = $manager->getMergedTranslations();
140+
$oldTranslations = $manager->getMergedTranslations(); // TODO expensive, seems unnecessary just for keys in DB
141+
155142
foreach (array_keys($oldTranslations) as $oldKey) {
156143
$translation = $manager->getTranslation($oldKey);
157144

158145
if (isset($translation['state']) && (in_array($translation['state'], ['edited', 'added']))) {
159146
$newKey = str_replace($oldKeyString, $newKeyString, $oldKey);
160-
// If the new translation key exists and has been modified, something might be wrong and should be checked
161147
if ($manager->keyExists($newKey)) {
162-
$this->log($colors->yellow("New translation key '$newKey' already exists. Cannot rename old key '$oldKey'. Please check this manually."));
163-
// Unset modified new key to avoid provding redundant information to user
164-
if (array_key_exists($newKey, $modNewKeys)) {
165-
unset($modNewKeys[$newKey]);
166-
}
167-
168-
// If the new translation key doesn't exist, the old one is renamed
148+
// New translation key exists and has been modified, something might be wrong and should be checked
149+
$this->log($colors->yellow(
150+
"New key '{$newKey}' already exists. Cannot rename old key '{$oldKey}'. Please check this manually."
151+
));
152+
$conflictKeys[] = $newKey;
169153
} else {
154+
// If the new translation key doesn't exist, the old one is renamed
170155
$this->updateTranslationKey($oldKey, $translation, $newKey);
171-
$this->getLogger()->info("Translation key '$oldKey' updated successfully to '$newKey'.");
156+
$this->log("Translation key '{$oldKey}' updated to '{$newKey}'.");
172157
}
173-
// If the old translation key doesn't exist, no action is needed
174-
} else {
175-
$this->getLogger()->info("Old translation Key '$oldKey' was not edited. No changes needed.");
176158
}
177159
}
178160

179161
// Provide information on modified new translation keys that already exist so
180-
// the user may check whether something might be wrong or everything is ok.
181-
if (isset($modNewKeys) && count(array_keys($modNewKeys)) > 0) {
182-
foreach (array_keys($modNewKeys) as $modNewKey) {
183-
$this->log($colors->yellow("New translation key '$modNewKey' exists and has already been modified. Old key doesn't exist or hasn't been modified. You should check this manually."));
162+
// the user may check if something is wrong or everything is ok.
163+
// TODO does this make sense? Does it provide useful information?
164+
$remainingKeys = array_diff($modNewKeys, $conflictKeys);
165+
if (count($remainingKeys) > 0) {
166+
$this->log("The following keys with modified translations already existed for '{$newKeyString}'.");
167+
foreach ($remainingKeys as $key) {
168+
$this->log($colors->yellow($key));
184169
}
185-
} elseif (isset($oldTranslations) && count(array_keys($oldTranslations)) === 0) {
186-
$this->getLogger()->info("Translation keys are up-to-date. No changes needed.");
187170
}
188171
}
189172

173+
/**
174+
* @param string $filterKey
175+
* @return array
176+
*/
177+
protected function getModifiedTranslationKeys($filterKey)
178+
{
179+
$manager = new Application_Translate_TranslationManager();
180+
$manager->setFilter($filterKey);
181+
182+
$modifiedKeys = [];
183+
184+
$translations = $manager->getMergedTranslations();
185+
foreach (array_keys($translations) as $key) {
186+
$translation = $manager->getTranslation($key);
187+
if (isset($translation['state']) && (in_array($translation['state'], ['edited', 'added']))) {
188+
$modifiedKeys[] = $key;
189+
}
190+
}
191+
192+
return $modifiedKeys;
193+
}
194+
190195
/**
191196
* Insert new key with translations, delete old key
192197
*

0 commit comments

Comments
 (0)