cleanflaglist.script 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. //
  3. // This example demonstrates how to write a drush
  4. // script. These scripts are run with the php-script command.
  5. //
  6. // drush php-script cleanflaglist.script
  7. use Drush\Drush;
  8. $this->output()->writeln("Flagging collection cleaning");
  9. $this->output()->writeln("options : [delete-all | delete-nullname | delete-missing | delete-dups]");
  10. // $this->output()->writeln("Hello world!");
  11. $this->output()->writeln("The extra options/arguments to this command were:");
  12. $this->output()->writeln(print_r($extra, true));
  13. $delete_all = false;
  14. $delete_nullname = false;
  15. $delete_missing = false;
  16. $recreate_missing = false;
  17. $delete_dups = false;
  18. $recreate_dups = false;
  19. if (count($extra)) {
  20. switch ($extra[0]) {
  21. case "delete-all":
  22. $delete_all = true;
  23. break;
  24. case "delete-missing":
  25. $delete_missing = true;
  26. break;
  27. case "recreate-missing":
  28. $recreate_missing = true;
  29. break;
  30. case "delete-nullname":
  31. $delete_nullname = true;
  32. break;
  33. case "delete-dups":
  34. $delete_dups = true;
  35. break;
  36. case "recreate-dups":
  37. $recreate_dups = true;
  38. break;
  39. }
  40. }
  41. $database = \Drupal::database();
  42. // _ _ _ _
  43. // | \ | |_ _| | | _ __ __ _ _ __ ___ ___
  44. // | \| | | | | | | | '_ \ / _` | '_ ` _ \ / _ \
  45. // | |\ | |_| | | | | | | | (_| | | | | | | __/
  46. // |_| \_|\__,_|_|_| |_| |_|\__,_|_| |_| |_|\___|
  47. // remove flagging collection with null name
  48. $this->output()->writeln("");
  49. $this->output()->writeln("Flagging collection With NULL name cleaning");
  50. $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
  51. ->isNull('fcfd.name')
  52. ->fields('fcfd', ['id', 'relatedflag']);
  53. $fcfd_result = $fcfd_query->execute();
  54. $this->output()->writeln($fcfd_query->countQuery()->execute()->fetchField() . " flagging_collection with null name");
  55. foreach ($fcfd_result as $fc) {
  56. // $this->output()->writeln($fc->id . " flagging_collection with null name");
  57. // $this->output()->writeln(print_r($fc, true));
  58. // get flag list items
  59. $flifd_query = $database->select('flag_list_item_field_data', 'flifd')
  60. ->condition('flifd.flag_list', $fc->id)
  61. ->fields('flifd', ['id', 'baseflag', 'flag_list']);
  62. $flifd_result = $flifd_query->execute();
  63. $this->output()->writeln($flifd_query->countQuery()->execute()->fetchField() . " items for fc " . $fc->id . ' with baseflag ' . $fc->relatedflag);
  64. // $this->output()->writeln(print_r($flifd_result->fetchAssoc(), true) . " items");
  65. delete_flagingcollection($fc, $database, $delete_all ? true : $delete_nullname);
  66. }
  67. // __ __ _ _ ____ _ _ _ _____ _
  68. // | \/ (_)___ ___(_)_ __ __ _ | _ \ ___| | __ _| |_ ___ __| | | ___| | __ _ __ _
  69. // | |\/| | / __/ __| | '_ \ / _` | | |_) / _ \ |/ _` | __/ _ \/ _` | | |_ | |/ _` |/ _` |
  70. // | | | | \__ \__ \ | | | | (_| | | _ < __/ | (_| | || __/ (_| | | _| | | (_| | (_| |
  71. // |_| |_|_|___/___/_|_| |_|\__, | |_| \_\___|_|\__,_|\__\___|\__,_| |_| |_|\__,_|\__, |
  72. // |___/ |___/
  73. // searching for missing related flags
  74. $this->output()->writeln("");
  75. $this->output()->writeln("Missing related flag Flagging collection cleaning");
  76. $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
  77. ->fields('fcfd', ['name', 'id', 'relatedflag']);
  78. $fcfd_result = $fcfd_query->execute();
  79. $this->output()->writeln($fcfd_query->countQuery()->execute()->fetchField() . " flagging_collections");
  80. $fc_missing_relatedflag = 0;
  81. foreach ($fcfd_result as $fc) {
  82. $flag_query = $database->select('config', 'c')
  83. ->condition('c.name', 'flag.flag.' . $fc->relatedflag)
  84. ->fields('c', ['name']);
  85. $conf_result = $flag_query->execute();
  86. $count = $flag_query->countQuery()->execute()->fetchField();
  87. if(!$count){
  88. $this->output()->writeln(' flagging_collection ' . $fc->name . ' ('. $fc->id . ') has ' . $count . " relatedflag");
  89. $fc_missing_relatedflag ++;
  90. delete_flagingcollection($fc, $database, $delete_all ? true : $delete_missing);
  91. if ($recreate_missing) {
  92. $FlagListsService = new FlagListsService();
  93. $flaggingcollection = $FlagListsService->getFlaggingCollectionById($fc->id);
  94. $this->output()->writeln(' flagging_collection ' . $flaggingcollection->getName());
  95. }
  96. }
  97. }
  98. $this->output()->writeln($fc_missing_relatedflag .' flagging_collection with missing related_flag');
  99. // ____ _ _ _
  100. // | _ \ _ _ _ __ | (_) ___ __ _| |_ ___ ___
  101. // | | | | | | | '_ \| | |/ __/ _` | __/ _ \/ __|
  102. // | |_| | |_| | |_) | | | (_| (_| | || __/\__ \
  103. // |____/ \__,_| .__/|_|_|\___\__,_|\__\___||___/
  104. // |_|
  105. // searching for duplicates relatedflags
  106. $this->output()->writeln("");
  107. $this->output()->writeln("Duplicate relatedflag flagging_collection cleaning");
  108. $dup_query = $database->select('flagging_collection_field_data', 'fcfd')
  109. ->fields('fcfd', ['relatedflag'])
  110. ->groupBy('fcfd.relatedflag')
  111. ->having('COUNT(*) > 1');
  112. $dup_query->addExpression('COUNT(*)', 'count');
  113. // $results = $query->execute()->fetchAll();
  114. $dup_result = $dup_query->execute();
  115. $this->output()->writeln($dup_query->countQuery()->execute()->fetchField() . " duplicate related flags");
  116. foreach ($dup_result as $dup){
  117. // $this->output()->writeln($dup->relatedflag . " is dup");
  118. $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
  119. ->condition('fcfd.relatedflag', $dup->relatedflag)
  120. ->fields('fcfd', ['id', 'relatedflag']);
  121. $fcfd_result = $fcfd_query->execute();
  122. $count = $fcfd_query->countQuery()->execute()->fetchField();
  123. $this->output()->writeln($dup->relatedflag . ' relatedflag has ' . $count . ' flagging collection');
  124. foreach ($fcfd_result as $fc) {
  125. delete_flagingcollection($fc, $database, $delete_all ? true : $delete_dups);
  126. }
  127. }
  128. // MAIN DELETE FUNCTION
  129. function delete_flagingcollection($fc, $database, $delete){
  130. if($delete){
  131. // $this is not working in function
  132. // $this->output()->writeln("Deleting flagging collection " . $fc->id);
  133. // get flag list items
  134. $flifd_query = $database->select('flag_list_item_field_data', 'flifd')
  135. ->condition('flifd.flag_list', $fc->id)
  136. ->fields('flifd', ['id', 'baseflag', 'flag_list']);
  137. $flifd_result = $flifd_query->execute();
  138. foreach ($flifd_result as $item) {
  139. // delete items
  140. $database->delete('flag_list_item')
  141. ->condition('id', $item->id)
  142. ->execute();
  143. $database->delete('flag_list_item_field_data')
  144. ->condition('id', $item->id)
  145. ->execute();
  146. }
  147. # delete the flag_collection
  148. $database->delete('flagging_collection')
  149. ->condition('id', $fc->id)
  150. ->execute();
  151. $database->delete('flagging_collection_revision')
  152. ->condition('id', $fc->id)
  153. ->execute();
  154. $database->delete('flagging_collection_field_revision')
  155. ->condition('id', $fc->id)
  156. ->execute();
  157. # delete related flag
  158. // $flag_query = $database->select('config', 'c')
  159. // ->condition('c.name', 'flag.flag.' . $fc->relatedflag)
  160. // ->fields('c', ['name']);
  161. // $conf_result = $flag_query->execute();
  162. // $this->output()->writeln(print_r($conf_result->fetchAssoc(), true));
  163. $database->delete('config')
  164. ->condition('name', 'flag.flag.' . $fc->relatedflag)
  165. ->execute();
  166. $database->delete('config')
  167. ->condition('name', 'system.action.flag_action.'.$fc->relatedflag.'_flag')
  168. ->execute();
  169. $database->delete('config')
  170. ->condition('name', 'system.action.flag_action.'.$fc->relatedflag.'_unflag')
  171. ->execute();
  172. $database->delete('flagging_collection_field_data')
  173. ->condition('id', $fc->id)
  174. ->execute();
  175. } else {
  176. // $this is not working in function
  177. // $this->output()->writeln("In order to actually delete fault flagging collection data please use delete option");
  178. }
  179. }