From e735d2d21e1045a9652dcd92427f2934b91180ec Mon Sep 17 00:00:00 2001 From: bach Date: Tue, 4 Jul 2023 15:39:23 +0200 Subject: [PATCH] created custom drush commands to clean missing related flags and duplicates related flags --- cleanflaglist.script | 67 ++++++--- .../custom/materio_flag/drush.services.yml | 6 + .../src/Commands/MaterioFlagCleaning.php | 130 ++++++++++++++++++ 3 files changed, 185 insertions(+), 18 deletions(-) create mode 100644 web/modules/custom/materio_flag/drush.services.yml create mode 100644 web/modules/custom/materio_flag/src/Commands/MaterioFlagCleaning.php diff --git a/cleanflaglist.script b/cleanflaglist.script index c544670b..1a36051f 100755 --- a/cleanflaglist.script +++ b/cleanflaglist.script @@ -19,27 +19,42 @@ $this->output()->writeln(print_r($extra, true)); $delete_all = false; $delete_nullname = false; $delete_missing = false; +$recreate_missing = false; $delete_dups = false; +$recreate_dups = false; -if ($extra[0] === "delete-all") { - $delete_all = true; -} - -if ($extra[0] === "delete-missing") { - $delete_missing = true; -} - -if ($extra[0] === "delete-nullname") { - $delete_nullname = true; -} - -if ($extra[0] === "delete-dups") { - $delete_dups = true; +if (count($extra)) { + switch ($extra[0]) { + case "delete-all": + $delete_all = true; + break; + case "delete-missing": + $delete_missing = true; + break; + case "recreate-missing": + $recreate_missing = true; + break; + case "delete-nullname": + $delete_nullname = true; + break; + case "delete-dups": + $delete_dups = true; + break; + case "recreate-dups": + $recreate_dups = true; + break; + } } $database = \Drupal::database(); +// _ _ _ _ +// | \ | |_ _| | | _ __ __ _ _ __ ___ ___ +// | \| | | | | | | | '_ \ / _` | '_ ` _ \ / _ \ +// | |\ | |_| | | | | | | | (_| | | | | | | __/ +// |_| \_|\__,_|_|_| |_| |_|\__,_|_| |_| |_|\___| + // remove flagging collection with null name $this->output()->writeln(""); $this->output()->writeln("Flagging collection With NULL name cleaning"); @@ -66,12 +81,17 @@ foreach ($fcfd_result as $fc) { delete_flagingcollection($fc, $database, $delete_all ? true : $delete_nullname); } - +// __ __ _ _ ____ _ _ _ _____ _ +// | \/ (_)___ ___(_)_ __ __ _ | _ \ ___| | __ _| |_ ___ __| | | ___| | __ _ __ _ +// | |\/| | / __/ __| | '_ \ / _` | | |_) / _ \ |/ _` | __/ _ \/ _` | | |_ | |/ _` |/ _` | +// | | | | \__ \__ \ | | | | (_| | | _ < __/ | (_| | || __/ (_| | | _| | | (_| | (_| | +// |_| |_|_|___/___/_|_| |_|\__, | |_| \_\___|_|\__,_|\__\___|\__,_| |_| |_|\__,_|\__, | +// |___/ |___/ // searching for missing related flags $this->output()->writeln(""); $this->output()->writeln("Missing related flag Flagging collection cleaning"); $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd') - ->fields('fcfd', ['id', 'relatedflag']); + ->fields('fcfd', ['name', 'id', 'relatedflag']); $fcfd_result = $fcfd_query->execute(); $this->output()->writeln($fcfd_query->countQuery()->execute()->fetchField() . " flagging_collections"); @@ -84,13 +104,24 @@ foreach ($fcfd_result as $fc) { $conf_result = $flag_query->execute(); $count = $flag_query->countQuery()->execute()->fetchField(); if(!$count){ - $this->output()->writeln(' flagging_collection' . $fc->id . ' has ' . $count . " relatedflag"); + $this->output()->writeln(' flagging_collection ' . $fc->name . ' ('. $fc->id . ') has ' . $count . " relatedflag"); $fc_missing_relatedflag ++; delete_flagingcollection($fc, $database, $delete_all ? true : $delete_missing); + if ($recreate_missing) { + $FlagListsService = new FlagListsService(); + $flaggingcollection = $FlagListsService->getFlaggingCollectionById($fc->id); + $this->output()->writeln(' flagging_collection ' . $flaggingcollection->getName()); + } } } $this->output()->writeln($fc_missing_relatedflag .' flagging_collection with missing related_flag'); +// ____ _ _ _ +// | _ \ _ _ _ __ | (_) ___ __ _| |_ ___ ___ +// | | | | | | | '_ \| | |/ __/ _` | __/ _ \/ __| +// | |_| | |_| | |_) | | | (_| (_| | || __/\__ \ +// |____/ \__,_| .__/|_|_|\___\__,_|\__\___||___/ +// |_| // searching for duplicates relatedflags $this->output()->writeln(""); $this->output()->writeln("Duplicate relatedflag flagging_collection cleaning"); @@ -190,4 +221,4 @@ function delete_flagingcollection($fc, $database, $delete){ // $this->output()->writeln("In order to actually delete fault flagging collection data please use delete option"); } -} \ No newline at end of file +} diff --git a/web/modules/custom/materio_flag/drush.services.yml b/web/modules/custom/materio_flag/drush.services.yml new file mode 100644 index 00000000..f1283c76 --- /dev/null +++ b/web/modules/custom/materio_flag/drush.services.yml @@ -0,0 +1,6 @@ +services: + MaterioFlagCleaning.commands: + class: \Drupal\materio_flag\Commands\MaterioFlagCleaning + arguments: ['@entity_type.manager', '@flaglists', '@current_user'] + tags: + - { name: drush.command } \ No newline at end of file diff --git a/web/modules/custom/materio_flag/src/Commands/MaterioFlagCleaning.php b/web/modules/custom/materio_flag/src/Commands/MaterioFlagCleaning.php new file mode 100644 index 00000000..e2b220df --- /dev/null +++ b/web/modules/custom/materio_flag/src/Commands/MaterioFlagCleaning.php @@ -0,0 +1,130 @@ +get('entity_type.manager'), + // $container->get('flaglists'), + // $container->get('current_user') + // ); + // } + + /** + * Constructs a new MaterioFlagController object. + */ + public function __construct(EntityTypeManagerInterface $entity_type_manager, FlagListsService $flag_lists_service, AccountProxyInterface $account) { + $this->entityTypeManager = $entity_type_manager; + $this->flagListsService = $flag_lists_service; + $this->user = $account; + } + + /** + * Try to recreate missing related flags. + * + * @command materio_flag:recreate_missing_related_flags + * @aliases mf-rmrf + */ + public function recreate_missing_related_flags() { + $database = \Drupal::database(); + + $this->output()->writeln(""); + $this->output()->writeln("Missing related flag Flagging collection cleaning"); + $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd') + ->fields('fcfd', ['id', 'relatedflag']); + + $fcfd_result = $fcfd_query->execute(); + $this->output()->writeln($fcfd_query->countQuery()->execute()->fetchField() . " flagging_collections"); + + $fc_missing_relatedflag = 0; + foreach ($fcfd_result as $fc) { + $flag_query = $database->select('config', 'c') + ->condition('c.name', 'flag.flag.' . $fc->relatedflag) + ->fields('c', ['name']); + // $conf_result = $flag_query->execute(); + $count = $flag_query->countQuery()->execute()->fetchField(); + if(!$count){ + $fc_missing_relatedflag ++; + $flagList = $this->flagListsService->getFlaggingCollectionById($fc->id); + $this->output()->writeln(' flagging_collection ' . $flagList->getName() . ' ('. $fc->id . ') has ' . $count . " relatedflag"); + $flagList->save(); + } + } + $this->output()->writeln($fc_missing_relatedflag .' flagging_collection with missing related_flag'); + + } + + /** + * Clean multiple flag list for one related flag. + * + * @command materio_flag:clean_duplicates_related_flags + * @aliases mf-cdrf + */ + public function clean_duplicates_related_flags() { + // searching for duplicates relatedflags + $database = \Drupal::database(); + + $this->output()->writeln(""); + $this->output()->writeln("Duplicate relatedflag flagging_collection cleaning"); + $dup_query = $database->select('flagging_collection_field_data', 'fcfd') + ->fields('fcfd', ['relatedflag']) + ->groupBy('fcfd.relatedflag') + ->having('COUNT(*) > 1'); + $dup_query->addExpression('COUNT(*)', 'count'); + + // $results = $query->execute()->fetchAll(); + + $dup_result = $dup_query->execute(); + $this->output()->writeln($dup_query->countQuery()->execute()->fetchField() . " duplicate related flags"); + + foreach ($dup_result as $dup){ + // $this->output()->writeln($dup->relatedflag . " is dup"); + $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd') + ->condition('fcfd.relatedflag', $dup->relatedflag) + ->fields('fcfd', ['id', 'relatedflag']); + $fcfd_result = $fcfd_query->execute(); + $count = $fcfd_query->countQuery()->execute()->fetchField(); + + $this->output()->writeln($dup->relatedflag . ' relatedflag has ' . $count . ' flagging collection'); + + foreach ($fcfd_result as $fc) { + $flagList = $this->flagListsService->getFlaggingCollectionById($fc->id); + $flagList->save(); + // delete_flagingcollection($fc, $database, $delete_all ? true : $delete_dups); + } + + } + } +} + + + + +