cleanflaglist.script 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. $this->output()->writeln("");
  18. $this->output()->writeln("Flagging collection With NULL name cleaning");
  19. $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
  20. ->isNull('fcfd.name')
  21. ->fields('fcfd', ['id', 'relatedflag']);
  22. $fcfd_result = $fcfd_query->execute();
  23. $this->output()->writeln($fcfd_query->countQuery()->execute()->fetchField() . " flagging_collection with null name");
  24. foreach ($fcfd_result as $fc) {
  25. // $this->output()->writeln($fc->id . " flagging_collection with null name");
  26. // $this->output()->writeln(print_r($fc, true));
  27. // get flag list items
  28. $flifd_query = $database->select('flag_list_item_field_data', 'flifd')
  29. ->condition('flifd.flag_list', $fc->id)
  30. ->fields('flifd', ['id', 'baseflag', 'flag_list']);
  31. $flifd_result = $flifd_query->execute();
  32. $this->output()->writeln($flifd_query->countQuery()->execute()->fetchField() . " items for fc " . $fc->id . ' with baseflag ' . $fc->relatedflag);
  33. // $this->output()->writeln(print_r($flifd_result->fetchAssoc(), true) . " items");
  34. delete_flagingcollection($fc);
  35. }
  36. // searching for missing related flags
  37. $this->output()->writeln("");
  38. $this->output()->writeln("Missing related flag Flagging collection cleaning");
  39. $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
  40. ->fields('fcfd', ['id', 'relatedflag']);
  41. $fcfd_result = $fcfd_query->execute();
  42. $this->output()->writeln($fcfd_query->countQuery()->execute()->fetchField() . " flagging_collections");
  43. $fc_missing_relatedflag = 0;
  44. foreach ($fcfd_result as $fc) {
  45. $flag_query = $database->select('config', 'c')
  46. ->condition('c.name', 'flag.flag.' . $fc->relatedflag)
  47. ->fields('c', ['name']);
  48. $conf_result = $flag_query->execute();
  49. $count = $flag_query->countQuery()->execute()->fetchField();
  50. if(!$count){
  51. $this->output()->writeln($count . " relatedflag");
  52. $fc_missing_relatedflag ++;
  53. delete_flagingcollection($fc);
  54. }
  55. }
  56. $this->output()->writeln($fc_missing_relatedflag .' flagging_collection with missing related_flag');
  57. function delete_flagingcollection($fc){
  58. if($delete){
  59. $this->output()->writeln("Deleting flagging collection " . $fc->id);
  60. // get flag list items
  61. $flifd_query = $database->select('flag_list_item_field_data', 'flifd')
  62. ->condition('flifd.flag_list', $fc->id)
  63. ->fields('flifd', ['id', 'baseflag', 'flag_list']);
  64. $flifd_result = $flifd_query->execute();
  65. foreach ($flifd_result as $item) {
  66. // delete items
  67. $database->delete('flag_list_item')
  68. ->condition('id', $item->id)
  69. ->execute();
  70. $database->delete('flag_list_item_field_data')
  71. ->condition('id', $item->id)
  72. ->execute();
  73. }
  74. # delete the flag_collection
  75. $database->delete('flagging_collection')
  76. ->condition('id', $fc->id)
  77. ->execute();
  78. $database->delete('flagging_collection_revision')
  79. ->condition('id', $fc->id)
  80. ->execute();
  81. $database->delete('flagging_collection_field_revision')
  82. ->condition('id', $fc->id)
  83. ->execute();
  84. # delete related flag
  85. // $flag_query = $database->select('config', 'c')
  86. // ->condition('c.name', 'flag.flag.' . $fc->relatedflag)
  87. // ->fields('c', ['name']);
  88. // $conf_result = $flag_query->execute();
  89. // $this->output()->writeln(print_r($conf_result->fetchAssoc(), true));
  90. $database->delete('config')
  91. ->condition('name', 'flag.flag.' . $fc->relatedflag)
  92. ->execute();
  93. $database->delete('flagging_collection_field_data')
  94. ->condition('id', $fc->id)
  95. ->execute();
  96. } else {
  97. $this->output()->writeln("In order to actually delete fault flagging collection data please use delete option");
  98. }
  99. }