cleanflaglist.script 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 is not working in function
  60. // $this->output()->writeln("Deleting flagging collection " . $fc->id);
  61. // get flag list items
  62. $flifd_query = $database->select('flag_list_item_field_data', 'flifd')
  63. ->condition('flifd.flag_list', $fc->id)
  64. ->fields('flifd', ['id', 'baseflag', 'flag_list']);
  65. $flifd_result = $flifd_query->execute();
  66. foreach ($flifd_result as $item) {
  67. // delete items
  68. $database->delete('flag_list_item')
  69. ->condition('id', $item->id)
  70. ->execute();
  71. $database->delete('flag_list_item_field_data')
  72. ->condition('id', $item->id)
  73. ->execute();
  74. }
  75. # delete the flag_collection
  76. $database->delete('flagging_collection')
  77. ->condition('id', $fc->id)
  78. ->execute();
  79. $database->delete('flagging_collection_revision')
  80. ->condition('id', $fc->id)
  81. ->execute();
  82. $database->delete('flagging_collection_field_revision')
  83. ->condition('id', $fc->id)
  84. ->execute();
  85. # delete related flag
  86. // $flag_query = $database->select('config', 'c')
  87. // ->condition('c.name', 'flag.flag.' . $fc->relatedflag)
  88. // ->fields('c', ['name']);
  89. // $conf_result = $flag_query->execute();
  90. // $this->output()->writeln(print_r($conf_result->fetchAssoc(), true));
  91. $database->delete('config')
  92. ->condition('name', 'flag.flag.' . $fc->relatedflag)
  93. ->execute();
  94. $database->delete('flagging_collection_field_data')
  95. ->condition('id', $fc->id)
  96. ->execute();
  97. } else {
  98. // $this is not working in function
  99. // $this->output()->writeln("In order to actually delete fault flagging collection data please use delete option");
  100. }
  101. }