cleanflaglist.script 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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, $database, $delete);
  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($fc->id . ' flagging_collection has ' . $count . " relatedflag");
  52. $fc_missing_relatedflag ++;
  53. delete_flagingcollection($fc, $database, $delete);
  54. }
  55. }
  56. $this->output()->writeln($fc_missing_relatedflag .' flagging_collection with missing related_flag');
  57. // searching for duplicates relatedflags
  58. $this->output()->writeln("");
  59. $this->output()->writeln("Duplicate relatedflag flagging_collection cleaning");
  60. $dup_query = $database->select('flagging_collection_field_data', 'fcfd')
  61. ->fields('fcfd', ['relatedflag'])
  62. ->groupBy('fcfd.relatedflag')
  63. ->having('COUNT(*) > 1');
  64. $dup_query->addExpression('COUNT(*)', 'count');
  65. // $results = $query->execute()->fetchAll();
  66. $dup_result = $dup_query->execute();
  67. $this->output()->writeln($dup_query->countQuery()->execute()->fetchField() . " duplicate related flags");
  68. foreach ($dup_result as $dup){
  69. // $this->output()->writeln($dup->relatedflag . " is dup");
  70. $fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
  71. ->condition('fcfd.relatedflag', $dup->relatedflag)
  72. ->fields('fcfd', ['id', 'relatedflag']);
  73. $fcfd_result = $fcfd_query->execute();
  74. $count = $fcfd_query->countQuery()->execute()->fetchField();
  75. $this->output()->writeln($dup->relatedflag . ' relatedflag has ' . $count . ' flagging collection');
  76. foreach ($fcfd_result as $fc) {
  77. delete_flagingcollection($fc, $database, $delete);
  78. }
  79. }
  80. // MAIN DELETE FUNCTION
  81. function delete_flagingcollection($fc, $database, $delete){
  82. if($delete){
  83. // $this is not working in function
  84. // $this->output()->writeln("Deleting flagging collection " . $fc->id);
  85. // get flag list items
  86. $flifd_query = $database->select('flag_list_item_field_data', 'flifd')
  87. ->condition('flifd.flag_list', $fc->id)
  88. ->fields('flifd', ['id', 'baseflag', 'flag_list']);
  89. $flifd_result = $flifd_query->execute();
  90. foreach ($flifd_result as $item) {
  91. // delete items
  92. $database->delete('flag_list_item')
  93. ->condition('id', $item->id)
  94. ->execute();
  95. $database->delete('flag_list_item_field_data')
  96. ->condition('id', $item->id)
  97. ->execute();
  98. }
  99. # delete the flag_collection
  100. $database->delete('flagging_collection')
  101. ->condition('id', $fc->id)
  102. ->execute();
  103. $database->delete('flagging_collection_revision')
  104. ->condition('id', $fc->id)
  105. ->execute();
  106. $database->delete('flagging_collection_field_revision')
  107. ->condition('id', $fc->id)
  108. ->execute();
  109. # delete related flag
  110. // $flag_query = $database->select('config', 'c')
  111. // ->condition('c.name', 'flag.flag.' . $fc->relatedflag)
  112. // ->fields('c', ['name']);
  113. // $conf_result = $flag_query->execute();
  114. // $this->output()->writeln(print_r($conf_result->fetchAssoc(), true));
  115. $database->delete('config')
  116. ->condition('name', 'flag.flag.' . $fc->relatedflag)
  117. ->execute();
  118. $database->delete('flagging_collection_field_data')
  119. ->condition('id', $fc->id)
  120. ->execute();
  121. } else {
  122. // $this is not working in function
  123. // $this->output()->writeln("In order to actually delete fault flagging collection data please use delete option");
  124. }
  125. }