123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- use Drush\Drush;
- $this->output()->writeln("Flagging collection cleaning");
- $this->output()->writeln("options : [delete-all | delete-nullname | delete-missing | delete-dups]");
- $this->output()->writeln("The extra options/arguments to this command were:");
- $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 (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();
-
- $this->output()->writeln("");
- $this->output()->writeln("Flagging collection With NULL name cleaning");
- $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
- ->isNull('fcfd.name')
- ->fields('fcfd', ['id', 'relatedflag']);
- $fcfd_result = $fcfd_query->execute();
- $this->output()->writeln($fcfd_query->countQuery()->execute()->fetchField() . " flagging_collection with null name");
- foreach ($fcfd_result as $fc) {
-
-
-
- $flifd_query = $database->select('flag_list_item_field_data', 'flifd')
- ->condition('flifd.flag_list', $fc->id)
- ->fields('flifd', ['id', 'baseflag', 'flag_list']);
- $flifd_result = $flifd_query->execute();
- $this->output()->writeln($flifd_query->countQuery()->execute()->fetchField() . " items for fc " . $fc->id . ' with baseflag ' . $fc->relatedflag);
-
- delete_flagingcollection($fc, $database, $delete_all ? true : $delete_nullname);
- }
- $this->output()->writeln("");
- $this->output()->writeln("Missing related flag Flagging collection cleaning");
- $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
- ->fields('fcfd', ['name', '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){
- $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');
- $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');
- $dup_result = $dup_query->execute();
- $this->output()->writeln($dup_query->countQuery()->execute()->fetchField() . " duplicate related flags");
- foreach ($dup_result as $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) {
- delete_flagingcollection($fc, $database, $delete_all ? true : $delete_dups);
- }
- }
- function delete_flagingcollection($fc, $database, $delete){
- if($delete){
-
-
-
- $flifd_query = $database->select('flag_list_item_field_data', 'flifd')
- ->condition('flifd.flag_list', $fc->id)
- ->fields('flifd', ['id', 'baseflag', 'flag_list']);
- $flifd_result = $flifd_query->execute();
- foreach ($flifd_result as $item) {
-
- $database->delete('flag_list_item')
- ->condition('id', $item->id)
- ->execute();
- $database->delete('flag_list_item_field_data')
- ->condition('id', $item->id)
- ->execute();
- }
-
- $database->delete('flagging_collection')
- ->condition('id', $fc->id)
- ->execute();
- $database->delete('flagging_collection_revision')
- ->condition('id', $fc->id)
- ->execute();
- $database->delete('flagging_collection_field_revision')
- ->condition('id', $fc->id)
- ->execute();
-
-
-
-
-
-
- $database->delete('config')
- ->condition('name', 'flag.flag.' . $fc->relatedflag)
- ->execute();
- $database->delete('config')
- ->condition('name', 'system.action.flag_action.'.$fc->relatedflag.'_flag')
- ->execute();
- $database->delete('config')
- ->condition('name', 'system.action.flag_action.'.$fc->relatedflag.'_unflag')
- ->execute();
-
- $database->delete('flagging_collection_field_data')
- ->condition('id', $fc->id)
- ->execute();
- } else {
-
-
- }
- }
|