2022-02-23 11:03:45 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
//
|
|
|
|
// This example demonstrates how to write a drush
|
|
|
|
// script. These scripts are run with the php-script command.
|
|
|
|
//
|
|
|
|
use Drush\Drush;
|
|
|
|
|
|
|
|
$this->output()->writeln("Flagging collection cleaning");
|
|
|
|
|
|
|
|
// $this->output()->writeln("Hello world!");
|
2022-02-23 11:20:06 +01:00
|
|
|
$this->output()->writeln("The extra options/arguments to this command were:");
|
|
|
|
$this->output()->writeln(print_r($extra, true));
|
2022-02-23 11:03:45 +01:00
|
|
|
if ($extra[0] === "delete") {
|
|
|
|
$delete = true;
|
|
|
|
} else {
|
|
|
|
$delete = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$database = \Drupal::database();
|
|
|
|
|
|
|
|
$this->output()->writeln("");
|
|
|
|
$this->output()->writeln("Flagging collection With NULL name cleaning");
|
|
|
|
$fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
|
|
|
|
->isNull('fcfd.name')
|
|
|
|
->fields('fcfd', ['id', 'relatedflag']);
|
|
|
|
|
|
|
|
$fcfd_result = $fcfd_query->execute();
|
|
|
|
$this->output()->writeln($fcfd_query->countQuery()->execute()->fetchField() . " flagging_collection with null name");
|
|
|
|
|
|
|
|
foreach ($fcfd_result as $fc) {
|
|
|
|
// $this->output()->writeln($fc->id . " flagging_collection with null name");
|
|
|
|
// $this->output()->writeln(print_r($fc, true));
|
|
|
|
|
|
|
|
// get flag list items
|
|
|
|
$flifd_query = $database->select('flag_list_item_field_data', 'flifd')
|
|
|
|
->condition('flifd.flag_list', $fc->id)
|
|
|
|
->fields('flifd', ['id', 'baseflag', 'flag_list']);
|
|
|
|
|
|
|
|
$flifd_result = $flifd_query->execute();
|
|
|
|
$this->output()->writeln($flifd_query->countQuery()->execute()->fetchField() . " items for fc " . $fc->id . ' with baseflag ' . $fc->relatedflag);
|
|
|
|
// $this->output()->writeln(print_r($flifd_result->fetchAssoc(), true) . " items");
|
|
|
|
|
2022-02-23 11:25:16 +01:00
|
|
|
delete_flagingcollection($fc, $database, $delete);
|
2022-02-23 11:03:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// searching for missing related flags
|
|
|
|
$this->output()->writeln("");
|
|
|
|
$this->output()->writeln("Missing related flag Flagging collection cleaning");
|
|
|
|
$fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
|
|
|
|
->fields('fcfd', ['id', 'relatedflag']);
|
|
|
|
|
|
|
|
$fcfd_result = $fcfd_query->execute();
|
|
|
|
$this->output()->writeln($fcfd_query->countQuery()->execute()->fetchField() . " flagging_collections");
|
|
|
|
|
|
|
|
$fc_missing_relatedflag = 0;
|
|
|
|
foreach ($fcfd_result as $fc) {
|
|
|
|
$flag_query = $database->select('config', 'c')
|
|
|
|
->condition('c.name', 'flag.flag.' . $fc->relatedflag)
|
|
|
|
->fields('c', ['name']);
|
|
|
|
$conf_result = $flag_query->execute();
|
|
|
|
$count = $flag_query->countQuery()->execute()->fetchField();
|
|
|
|
if(!$count){
|
2022-02-23 14:48:13 +01:00
|
|
|
$this->output()->writeln($fc->id . ' flagging_collection has ' . $count . " relatedflag");
|
2022-02-23 11:03:45 +01:00
|
|
|
$fc_missing_relatedflag ++;
|
2022-02-23 11:25:16 +01:00
|
|
|
delete_flagingcollection($fc, $database, $delete);
|
2022-02-23 11:03:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->output()->writeln($fc_missing_relatedflag .' flagging_collection with missing related_flag');
|
|
|
|
|
2022-02-23 13:59:27 +01:00
|
|
|
// searching for duplicates relatedflags
|
|
|
|
$this->output()->writeln("");
|
|
|
|
$this->output()->writeln("Duplicate relatedflag flagging_collection cleaning");
|
2022-02-23 14:48:13 +01:00
|
|
|
$dup_query = $database->select('flagging_collection_field_data', 'fcfd')
|
|
|
|
->fields('fcfd', ['relatedflag'])
|
2022-02-23 13:59:27 +01:00
|
|
|
->groupBy('fcfd.relatedflag')
|
|
|
|
->having('COUNT(*) > 1');
|
2022-02-23 14:48:13 +01:00
|
|
|
$dup_query->addExpression('COUNT(*)', 'count');
|
2022-02-23 13:59:27 +01:00
|
|
|
|
|
|
|
// $results = $query->execute()->fetchAll();
|
|
|
|
|
2022-02-23 14:48:13 +01:00
|
|
|
$dup_result = $dup_query->execute();
|
|
|
|
$this->output()->writeln($dup_query->countQuery()->execute()->fetchField() . " duplicate related flags");
|
|
|
|
|
|
|
|
foreach ($dup_result as $dup){
|
|
|
|
// $this->output()->writeln($dup->relatedflag . " is dup");
|
|
|
|
$fcfd_query = $database->select('flagging_collection_field_data', 'fcfd')
|
|
|
|
->condition('fcfd.relatedflag', $dup->relatedflag)
|
|
|
|
->fields('fcfd', ['id', 'relatedflag']);
|
|
|
|
$fcfd_result = $fcfd_query->execute();
|
|
|
|
$count = $fcfd_query->countQuery()->execute()->fetchField();
|
|
|
|
|
|
|
|
$this->output()->writeln($dup->relatedflag . ' relatedflag has ' . $count . ' flagging collection');
|
|
|
|
|
|
|
|
foreach ($fcfd_result as $fc) {
|
|
|
|
delete_flagingcollection($fc, $database, $delete);
|
|
|
|
}
|
2022-02-23 13:59:27 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-23 14:48:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-02-23 13:59:27 +01:00
|
|
|
// MAIN DELETE FUNCTION
|
2022-02-23 11:25:16 +01:00
|
|
|
function delete_flagingcollection($fc, $database, $delete){
|
2022-02-23 11:03:45 +01:00
|
|
|
if($delete){
|
2022-02-23 11:16:15 +01:00
|
|
|
// $this is not working in function
|
|
|
|
// $this->output()->writeln("Deleting flagging collection " . $fc->id);
|
2022-02-23 11:03:45 +01:00
|
|
|
// get flag list items
|
|
|
|
$flifd_query = $database->select('flag_list_item_field_data', 'flifd')
|
|
|
|
->condition('flifd.flag_list', $fc->id)
|
|
|
|
->fields('flifd', ['id', 'baseflag', 'flag_list']);
|
|
|
|
|
|
|
|
$flifd_result = $flifd_query->execute();
|
|
|
|
|
|
|
|
foreach ($flifd_result as $item) {
|
|
|
|
// delete items
|
|
|
|
$database->delete('flag_list_item')
|
|
|
|
->condition('id', $item->id)
|
|
|
|
->execute();
|
|
|
|
$database->delete('flag_list_item_field_data')
|
|
|
|
->condition('id', $item->id)
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
# delete the flag_collection
|
|
|
|
$database->delete('flagging_collection')
|
|
|
|
->condition('id', $fc->id)
|
|
|
|
->execute();
|
|
|
|
$database->delete('flagging_collection_revision')
|
|
|
|
->condition('id', $fc->id)
|
|
|
|
->execute();
|
|
|
|
$database->delete('flagging_collection_field_revision')
|
|
|
|
->condition('id', $fc->id)
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
# delete related flag
|
|
|
|
// $flag_query = $database->select('config', 'c')
|
|
|
|
// ->condition('c.name', 'flag.flag.' . $fc->relatedflag)
|
|
|
|
// ->fields('c', ['name']);
|
|
|
|
// $conf_result = $flag_query->execute();
|
|
|
|
// $this->output()->writeln(print_r($conf_result->fetchAssoc(), true));
|
|
|
|
|
|
|
|
$database->delete('config')
|
|
|
|
->condition('name', 'flag.flag.' . $fc->relatedflag)
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
|
|
|
|
$database->delete('flagging_collection_field_data')
|
|
|
|
->condition('id', $fc->id)
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
} else {
|
2022-02-23 11:16:15 +01:00
|
|
|
// $this is not working in function
|
|
|
|
// $this->output()->writeln("In order to actually delete fault flagging collection data please use delete option");
|
2022-02-23 11:03:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|