cleanflaglist.script 5.7 KB

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