cleanflaglist.script 6.2 KB

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