cleanflaglist.script 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. use Drush\Drush;
  7. $this->output()->writeln("Flagging collection cleaning");
  8. $this->output()->writeln("options : [delete-all | delete-nullname | delete-missing | delete-dups]");
  9. // $this->output()->writeln("Hello world!");
  10. $this->output()->writeln("The extra options/arguments to this command were:");
  11. $this->output()->writeln(print_r($extra, true));
  12. $delete_all = fasle;
  13. $delete_nullname = false;
  14. $delete_missing = false;
  15. $delete_dups = false;
  16. if ($extra[0] === "delete-all") {
  17. $delete_all = true;
  18. }
  19. if ($extra[0] === "delete-missing") {
  20. $delete_missing = true;
  21. }
  22. if ($extra[0] === "delete-nullname") {
  23. $delete_nullname = true;
  24. }
  25. if ($extra[0] === "delete-dups") {
  26. $delete_dups = true;
  27. }
  28. $database = \Drupal::database();
  29. // remove flagging collection with null name
  30. $this->output()->writeln("");
  31. $this->output()->writeln("Flagging collection With NULL name cleaning");
  32. $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
  33. ->isNull('fcfd.name')
  34. ->fields('fcfd', ['id', 'relatedflag']);
  35. $fcfd_result = $fcfd_query->execute();
  36. $this->output()->writeln($fcfd_query->countQuery()->execute()->fetchField() . " flagging_collection with null name");
  37. foreach ($fcfd_result as $fc) {
  38. // $this->output()->writeln($fc->id . " flagging_collection with null name");
  39. // $this->output()->writeln(print_r($fc, true));
  40. // get flag list items
  41. $flifd_query = $database->select('flag_list_item_field_data', 'flifd')
  42. ->condition('flifd.flag_list', $fc->id)
  43. ->fields('flifd', ['id', 'baseflag', 'flag_list']);
  44. $flifd_result = $flifd_query->execute();
  45. $this->output()->writeln($flifd_query->countQuery()->execute()->fetchField() . " items for fc " . $fc->id . ' with baseflag ' . $fc->relatedflag);
  46. // $this->output()->writeln(print_r($flifd_result->fetchAssoc(), true) . " items");
  47. delete_flagingcollection($fc, $database, $delete_all ? true : $delete_nullname);
  48. }
  49. // searching for missing related flags
  50. $this->output()->writeln("");
  51. $this->output()->writeln("Missing related flag Flagging collection cleaning");
  52. $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
  53. ->fields('fcfd', ['id', 'relatedflag']);
  54. $fcfd_result = $fcfd_query->execute();
  55. $this->output()->writeln($fcfd_query->countQuery()->execute()->fetchField() . " flagging_collections");
  56. $fc_missing_relatedflag = 0;
  57. foreach ($fcfd_result as $fc) {
  58. $flag_query = $database->select('config', 'c')
  59. ->condition('c.name', 'flag.flag.' . $fc->relatedflag)
  60. ->fields('c', ['name']);
  61. $conf_result = $flag_query->execute();
  62. $count = $flag_query->countQuery()->execute()->fetchField();
  63. if(!$count){
  64. $this->output()->writeln(' flagging_collection' . $fc->id . ' has ' . $count . " relatedflag");
  65. $fc_missing_relatedflag ++;
  66. delete_flagingcollection($fc, $database, $delete_all ? true : $delete_missing);
  67. }
  68. }
  69. $this->output()->writeln($fc_missing_relatedflag .' flagging_collection with missing related_flag');
  70. // searching for duplicates relatedflags
  71. $this->output()->writeln("");
  72. $this->output()->writeln("Duplicate relatedflag flagging_collection cleaning");
  73. $dup_query = $database->select('flagging_collection_field_data', 'fcfd')
  74. ->fields('fcfd', ['relatedflag'])
  75. ->groupBy('fcfd.relatedflag')
  76. ->having('COUNT(*) > 1');
  77. $dup_query->addExpression('COUNT(*)', 'count');
  78. // $results = $query->execute()->fetchAll();
  79. $dup_result = $dup_query->execute();
  80. $this->output()->writeln($dup_query->countQuery()->execute()->fetchField() . " duplicate related flags");
  81. foreach ($dup_result as $dup){
  82. // $this->output()->writeln($dup->relatedflag . " is dup");
  83. $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
  84. ->condition('fcfd.relatedflag', $dup->relatedflag)
  85. ->fields('fcfd', ['id', 'relatedflag']);
  86. $fcfd_result = $fcfd_query->execute();
  87. $count = $fcfd_query->countQuery()->execute()->fetchField();
  88. $this->output()->writeln($dup->relatedflag . ' relatedflag has ' . $count . ' flagging collection');
  89. foreach ($fcfd_result as $fc) {
  90. delete_flagingcollection($fc, $database, $delete_all ? true : $delete_dups);
  91. }
  92. }
  93. // MAIN DELETE FUNCTION
  94. function delete_flagingcollection($fc, $database, $delete){
  95. if($delete){
  96. // $this is not working in function
  97. // $this->output()->writeln("Deleting flagging collection " . $fc->id);
  98. // get flag list items
  99. $flifd_query = $database->select('flag_list_item_field_data', 'flifd')
  100. ->condition('flifd.flag_list', $fc->id)
  101. ->fields('flifd', ['id', 'baseflag', 'flag_list']);
  102. $flifd_result = $flifd_query->execute();
  103. foreach ($flifd_result as $item) {
  104. // delete items
  105. $database->delete('flag_list_item')
  106. ->condition('id', $item->id)
  107. ->execute();
  108. $database->delete('flag_list_item_field_data')
  109. ->condition('id', $item->id)
  110. ->execute();
  111. }
  112. # delete the flag_collection
  113. $database->delete('flagging_collection')
  114. ->condition('id', $fc->id)
  115. ->execute();
  116. $database->delete('flagging_collection_revision')
  117. ->condition('id', $fc->id)
  118. ->execute();
  119. $database->delete('flagging_collection_field_revision')
  120. ->condition('id', $fc->id)
  121. ->execute();
  122. # delete related flag
  123. // $flag_query = $database->select('config', 'c')
  124. // ->condition('c.name', 'flag.flag.' . $fc->relatedflag)
  125. // ->fields('c', ['name']);
  126. // $conf_result = $flag_query->execute();
  127. // $this->output()->writeln(print_r($conf_result->fetchAssoc(), true));
  128. $database->delete('config')
  129. ->condition('name', 'flag.flag.' . $fc->relatedflag)
  130. ->execute();
  131. $database->delete('config')
  132. ->condition('name', 'system.action.flag_action.'.$fc->relatedflag.'_flag')
  133. ->execute();
  134. $database->delete('config')
  135. ->condition('name', 'system.action.flag_action.'.$fc->relatedflag.'_unflag')
  136. ->execute();
  137. $database->delete('flagging_collection_field_data')
  138. ->condition('id', $fc->id)
  139. ->execute();
  140. } else {
  141. // $this is not working in function
  142. // $this->output()->writeln("In order to actually delete fault flagging collection data please use delete option");
  143. }
  144. }