new: [taxonomy:normalizeCustomTags] Normalize custome tags to their taxonomy format

New feature accessible on the administrator on-demand action page
pull/8497/head
Sami Mokaddem 2022-07-12 14:13:30 +02:00
parent 26cc86fde2
commit 59221576e5
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
5 changed files with 92 additions and 2 deletions

View File

@ -669,6 +669,7 @@ class ACLComponent extends Component
'view' => array('*'),
'unhideTag' => array('perm_tagger'),
'hideTag' => array('perm_tagger'),
'normalizeCustomTagsToTaxonomyFormat' => [],
),
'templateElements' => array(
'add' => array('perm_template'),

View File

@ -594,4 +594,14 @@ class TaxonomiesController extends AppController
return $taxonomyIds;
}
public function normalizeCustomTagsToTaxonomyFormat()
{
$this->request->allowMethod(['post', 'put']);
$conversionResult = $this->Taxonomy->normalizeCustomTagsToTaxonomyFormat();
$this->Flash->success(__('%s tags successfully converted. %s row updated.', $conversionResult['tag_converted'], $conversionResult['row_updated']));
$this->redirect(array('controller' => 'taxonomies', 'action' => 'index'));
}
}

View File

@ -161,7 +161,12 @@ class Server extends AppModel
'description' => 'If your your database is locked and is not updating, unlock it here.',
'ignore_disabled' => true,
'url' => '/servers/releaseUpdateLock/'
)
),
'normalizeCustomTagsToTaxonomyFormat' => array(
'title' => 'Normalize custom tags to taxonomy format',
'description' => 'Transform all custom tags existing in a taxonomy into the taxonomy version',
'url' => '/taxonomies/normalizeCustomTagsToTaxonomyFormat/'
),
);
public $validEventIndexFilters = array('searchall', 'searchpublished', 'searchorg', 'searchtag', 'searcheventid', 'searchdate', 'searcheventinfo', 'searchthreatlevel', 'searchdistribution', 'searchanalysis', 'searchattribute');

View File

@ -583,7 +583,8 @@ class Tag extends AppModel
$changedTags = $this->AttributeTag->getAffectedRows();
$this->EventTag->updateAll(['tag_id' => $destinationTag['Tag']['id']], ['tag_id' => $sourceTag['Tag']['id']]);
$changedTags += $this->EventTag->getAffectedRows();
$this->GalaxyClusterRelationTag->updateAll(['tag_id' => $destinationTag['Tag']['id']], ['tag_id' => $sourceTag['Tag']['id']]);
$changedTags += $this->GalaxyClusterRelationTag->getAffectedRows();
$this->delete($sourceTag['Tag']['id']);
return [

View File

@ -770,4 +770,77 @@ class Taxonomy extends AppModel
}
return $splits;
}
private function __craftTaxonomiesTags()
{
$taxonomies = $this->find('all', [
'fields' => ['namespace'],
'contain' => ['TaxonomyPredicate' => ['TaxonomyEntry']],
]);
$allTaxonomyTags = [];
foreach ($taxonomies as $taxonomy) {
$namespace = $taxonomy['Taxonomy']['namespace'];
foreach ($taxonomy['TaxonomyPredicate'] as $predicate) {
if (isset($predicate['TaxonomyEntry']) && !empty($predicate['TaxonomyEntry'])) {
foreach ($predicate['TaxonomyEntry'] as $entry) {
$tag = $namespace . ':' . $predicate['value'] . '="' . $entry['value'] . '"';
$allTaxonomyTags[$tag] = true;
}
} else {
$tag = $namespace . ':' . $predicate['value'];
$allTaxonomyTags[$tag] = true;
}
}
}
return $allTaxonomyTags;
}
/**
* normalizeCustomTagsToTaxonomyFormat Transform all custom tags into their taxonomy version.
*
* @return int The number of converted tag
*/
public function normalizeCustomTagsToTaxonomyFormat(): array
{
$tagConverted = 0;
$rowUpdated = 0;
$craftedTags = $this->__craftTaxonomiesTags();
$allTaxonomyTagsByName = Hash::combine($this->getAllTaxonomyTags(false, false, true, false, true), '{n}.Tag.name', '{n}.Tag.id');
$tagsToMigrate = array_diff_key($allTaxonomyTagsByName, $craftedTags);
foreach ($tagsToMigrate as $tagToMigrate_name => $tagToMigrate_id) {
foreach (array_keys($craftedTags) as $craftedTag) {
if (strcasecmp($craftedTag, $tagToMigrate_name) == 0) {
$result = $this->__updateTagToNormalized(intval($tagToMigrate_id), intval($allTaxonomyTagsByName[$craftedTag]));
$tagConverted += 1;
$rowUpdated += $result['changed'];
}
}
}
return [
'tag_converted' => $tagConverted,
'row_updated' => $rowUpdated,
];
}
/**
* __updateTagToNormalized Change the link of element having $source_id tag attached to them for the $target_id one.
* Updated:
* - event_tags
* - attribute_tags
* - galaxy_cluster_relation_tags
*
* Ignored: As this is defined by users, let them do the migration themselves
* - tag_collection_tags
* - template_tags
* - favorite_tags
*
* @param int $source_id
* @param int $target_id
* @return array
* @throws Exception
*/
private function __updateTagToNormalized($source_id, $target_id): array
{
return $this->Tag->mergeTag($source_id, $target_id);
}
}