Browse Source

created custom drush commands to clean missing related flags and duplicates related flags

bach 1 year ago
parent
commit
e735d2d21e

+ 50 - 19
cleanflaglist.script

@@ -19,27 +19,42 @@ $this->output()->writeln(print_r($extra, true));
 $delete_all = false;
 $delete_nullname = false;
 $delete_missing = false;
+$recreate_missing = false;
 $delete_dups = false;
-
-if ($extra[0] === "delete-all") {
-  $delete_all = true;
-}
-
-if ($extra[0] === "delete-missing") {
-  $delete_missing = true;
-}
-
-if ($extra[0] === "delete-nullname") {
-  $delete_nullname = true;
-}
-
-if ($extra[0] === "delete-dups") {
-  $delete_dups = true;
+$recreate_dups = false;
+
+if (count($extra)) {
+  switch ($extra[0]) {
+    case "delete-all":
+      $delete_all = true;
+      break;
+    case "delete-missing":
+      $delete_missing = true;
+      break;
+    case "recreate-missing":
+      $recreate_missing = true;
+      break;
+    case "delete-nullname":
+      $delete_nullname = true;
+      break;
+    case "delete-dups":
+      $delete_dups = true;
+      break;
+    case "recreate-dups":
+      $recreate_dups = true;
+      break;
+  }
 }
 
 
 $database = \Drupal::database();
 
+//   _   _       _ _                              
+//  | \ | |_   _| | |  _ __   __ _ _ __ ___   ___ 
+//  |  \| | | | | | | | '_ \ / _` | '_ ` _ \ / _ \
+//  | |\  | |_| | | | | | | | (_| | | | | | |  __/
+//  |_| \_|\__,_|_|_| |_| |_|\__,_|_| |_| |_|\___|
+                                               
 // remove flagging collection with null name
 $this->output()->writeln("");
 $this->output()->writeln("Flagging collection With NULL name cleaning");
@@ -66,12 +81,17 @@ foreach ($fcfd_result as $fc) {
   delete_flagingcollection($fc, $database, $delete_all ? true : $delete_nullname);
 }
 
-
+//   __  __ _         _               ____      _       _           _   _____ _             
+//  |  \/  (_)___ ___(_)_ __   __ _  |  _ \ ___| | __ _| |_ ___  __| | |  ___| | __ _  __ _ 
+//  | |\/| | / __/ __| | '_ \ / _` | | |_) / _ \ |/ _` | __/ _ \/ _` | | |_  | |/ _` |/ _` |
+//  | |  | | \__ \__ \ | | | | (_| | |  _ <  __/ | (_| | ||  __/ (_| | |  _| | | (_| | (_| |
+//  |_|  |_|_|___/___/_|_| |_|\__, | |_| \_\___|_|\__,_|\__\___|\__,_| |_|   |_|\__,_|\__, |
+//                            |___/                                                   |___/ 
 // 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']);
+  ->fields('fcfd', ['name', 'id', 'relatedflag']);
 
 $fcfd_result = $fcfd_query->execute();
 $this->output()->writeln($fcfd_query->countQuery()->execute()->fetchField() . " flagging_collections");
@@ -84,13 +104,24 @@ foreach ($fcfd_result as $fc) {
   $conf_result = $flag_query->execute();
   $count = $flag_query->countQuery()->execute()->fetchField();
   if(!$count){
-    $this->output()->writeln(' flagging_collection' . $fc->id . ' has ' . $count . " relatedflag");
+    $this->output()->writeln(' flagging_collection ' . $fc->name . ' ('. $fc->id . ') has ' . $count . " relatedflag");
     $fc_missing_relatedflag ++;
     delete_flagingcollection($fc, $database, $delete_all ? true : $delete_missing);
+    if ($recreate_missing) {
+      $FlagListsService = new FlagListsService();
+      $flaggingcollection = $FlagListsService->getFlaggingCollectionById($fc->id);
+      $this->output()->writeln(' flagging_collection ' . $flaggingcollection->getName());
+    }
   }
 }
 $this->output()->writeln($fc_missing_relatedflag .' flagging_collection with missing related_flag');
 
+//   ____              _ _           _            
+//  |  _ \ _   _ _ __ | (_) ___ __ _| |_ ___  ___ 
+//  | | | | | | | '_ \| | |/ __/ _` | __/ _ \/ __|
+//  | |_| | |_| | |_) | | | (_| (_| | ||  __/\__ \
+//  |____/ \__,_| .__/|_|_|\___\__,_|\__\___||___/
+//              |_|                               
 // searching for duplicates relatedflags
 $this->output()->writeln("");
 $this->output()->writeln("Duplicate relatedflag flagging_collection cleaning");
@@ -190,4 +221,4 @@ function delete_flagingcollection($fc, $database, $delete){
     // $this->output()->writeln("In order to actually delete fault flagging collection data please use delete option");
   }
 
-}
+}

+ 6 - 0
web/modules/custom/materio_flag/drush.services.yml

@@ -0,0 +1,6 @@
+services:
+  MaterioFlagCleaning.commands:
+    class: \Drupal\materio_flag\Commands\MaterioFlagCleaning
+    arguments: ['@entity_type.manager', '@flaglists', '@current_user']
+    tags:
+      - { name: drush.command }

+ 130 - 0
web/modules/custom/materio_flag/src/Commands/MaterioFlagCleaning.php

@@ -0,0 +1,130 @@
+<?php
+
+namespace Drupal\materio_flag\Commands;
+
+
+use Drush\Commands\DrushCommands;
+use Drupal\Component\DependencyInjection\ContainerInterface;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
+use Drupal\flag_lists\FlagListsService;
+use Drupal\Core\Session\AccountProxyInterface;
+
+class MaterioFlagCleaning extends DrushCommands {
+
+  /**
+   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
+   */
+  protected $entityTypeManager;
+
+  /**
+   * @var \Drupal\flag_lists\FlagListsService
+   */
+  protected $flagListsService;
+
+  /**
+   * @var \Drupal\user\User
+   */
+  protected $user;
+
+  // /**
+  //  * {@inheritdoc}
+  //  */
+  // public static function create(ContainerInterface $container) {
+  //   return new static(
+  //     $container->get('entity_type.manager'),
+  //     $container->get('flaglists'),
+  //     $container->get('current_user')
+  //   );
+  // }
+
+  /**
+   * Constructs a new MaterioFlagController object.
+   */
+  public function __construct(EntityTypeManagerInterface $entity_type_manager, FlagListsService $flag_lists_service, AccountProxyInterface $account) {
+    $this->entityTypeManager = $entity_type_manager;
+    $this->flagListsService = $flag_lists_service;
+    $this->user = $account;
+  }
+
+  /**
+   * Try to recreate missing related flags.
+   *
+   * @command materio_flag:recreate_missing_related_flags
+   * @aliases mf-rmrf
+   */
+  public function recreate_missing_related_flags() {
+    $database = \Drupal::database();
+
+    $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){
+        $fc_missing_relatedflag ++;
+        $flagList = $this->flagListsService->getFlaggingCollectionById($fc->id);
+        $this->output()->writeln(' flagging_collection ' . $flagList->getName() . ' ('. $fc->id . ') has ' . $count . " relatedflag");
+        $flagList->save();
+      }
+    }
+    $this->output()->writeln($fc_missing_relatedflag .' flagging_collection with missing related_flag');
+
+  }
+
+  /**
+   * Clean multiple flag list for one related flag.
+   *
+   * @command materio_flag:clean_duplicates_related_flags
+   * @aliases mf-cdrf
+   */
+  public function clean_duplicates_related_flags() {
+    // searching for duplicates relatedflags
+    $database = \Drupal::database();
+
+    $this->output()->writeln("");
+    $this->output()->writeln("Duplicate relatedflag flagging_collection cleaning");
+    $dup_query = $database->select('flagging_collection_field_data', 'fcfd')
+      ->fields('fcfd', ['relatedflag'])
+      ->groupBy('fcfd.relatedflag')
+      ->having('COUNT(*) > 1');
+    $dup_query->addExpression('COUNT(*)', 'count');
+
+    // $results = $query->execute()->fetchAll();
+
+    $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) {
+        $flagList = $this->flagListsService->getFlaggingCollectionById($fc->id);
+        $flagList->save();
+        // delete_flagingcollection($fc, $database, $delete_all ? true : $delete_dups);
+      }
+
+    }
+  }
+}
+
+
+
+
+