You are here

public function AcquiaContenthubCommands::contenthubResetEntities in Acquia Content Hub 8

Deletes all entities of a particular type from Content Hub, reindex subscription and exports all deleted entities again to allow for Elasticsearch to map correct field types.

@option bundle The Entity Bundle.

@command acquia:contenthub-reset-entities @aliases ach-reset,acquia-contenthub-reset-entities

Parameters

$entity_type: The entity type.

$api: API Key

$secret: Secret Key

Throws

\Exception

File

src/Commands/AcquiaContenthubCommands.php, line 698

Class

AcquiaContenthubCommands
A Drush commandfile.

Namespace

Drupal\acquia_contenthub\Commands

Code

public function contenthubResetEntities($entity_type, $api, $secret, array $options = [
  'bundle' => NULL,
]) {
  if (empty($entity_type)) {
    throw new \Exception(dt('You need to provide at least the entity type of the entities you want to reset.'), LogLevel::CANCEL);
  }

  // Defining the bundle.
  $bundle = $options['bundle'];

  /** @var \Drupal\acquia_contenthub\Client\ClientManager $client_manager */
  $client_manager = \Drupal::service('acquia_contenthub.client_manager');
  $warning_message = "Are you sure you want to remotely DELETE entities of type = %s %s, REINDEX subscription and RE-EXPORT deleted entities in this Content Hub Subscription?\n" . "*************************************************************************************\n" . "PROCEED WITH CAUTION. THIS ACTION WILL REBUILD THE ELASTIC SEARCH INDEX IN YOUR CONTENT HUB SUBSCRIPTION.\n" . "This command will rebuild your index from the data currently stored in Content Hub. Make sure to first 'unpublish' all the entities that contain undesired\n" . "field definitions, otherwise you will rebuild essentially the same index you have today. Entities containing fields with new definitions can be\n" . "published after the index has been rebuilt.\n    For more information, check https://docs.acquia.com/content-hub.\n" . "*************************************************************************************\n" . "Are you sure you want to proceed?\n";
  $warning_bundle = sprintf("and bundle = %s", $bundle);
  $warning_message = sprintf($warning_message, $entity_type, $warning_bundle);
  if ($this
    ->io()
    ->confirm($warning_message)) {

    // If API/Secret Keys have been given, reset the connection to use those
    // keys instead of the ones set in the configuration.
    if (!empty($api) && !empty($secret)) {
      $client_manager
        ->resetConnection([
        'api' => $api,
        'secret' => $secret,
      ]);
    }

    // Verify that this site has a registered webhook before proceeding.

    /** @var \Drupal\acquia_contenthub\ContentHubSubscription $subscription */
    $subscription = \Drupal::service('acquia_contenthub.acquia_contenthub_subscription');
    if (!$subscription
      ->isWebhookSet()) {
      throw new \Exception(dt("Error trying to re-set and reindex entities. You are required to have a registered webhook in this site before proceeding."));
    }

    // Execute the 'reindex' command.
    if ($client_manager
      ->isConnected()) {

      /** @var \Drupal\acquia_contenthub\Controller\ContentHubReindex $reindex */
      $reindex = \Drupal::service('acquia_contenthub.acquia_contenthub_reindex');

      // Only continue with the command if we didn't send a reindex request yet.
      // If we already sent a reindex request, then we are just waiting for it
      // to be completed.
      if (!$reindex
        ->isReindexSent()) {

        // If the reindex has been completed then execute the batch process to
        // re-export entities.
        if ($reindex
          ->isReindexFinished()) {
          $reindex
            ->reExportEntitiesAfterReindex();

          // Start the batch process.
          drush_backend_batch_process();
        }
        else {

          // Check whether there are entities in Content Hub that are not
          // owned by this site.
          $entities = $reindex
            ->getExportedEntitiesNotOwnedByThisSite($entity_type);
          if (count($entities) > 0) {
            $header = [
              'UUID',
              'Origin',
              'Modified',
              'Entity Type',
            ];
            $this
              ->output()
              ->writeln(dt("You cannot perform this operation because there are entities exported to Content Hub that do not belong to this site. Please go to the origins listed (sites) and delete those entities (by doing 'ach-del <UUID>') before proceeding to reindex your subscription. Below is a list of those entities that cannot be deleted from this site:\n"));
            array_unshift($entities, $header);
            $this
              ->output()
              ->writeln(print_r($entities, TRUE));
            throw new \Exception(dt("Error trying to re-index your subscription. You have entities that do not belong to this site."));
          }

          // Delete entities and reindex subscription.
          $success = $reindex
            ->setExportedEntitiesToReindex($entity_type, $bundle);
          if ($success && $reindex
            ->isReindexSent()) {
            $this
              ->output()
              ->writeln("Your Subscription is being re-indexed. All clients who have registered to received webhooks will be notified with a reindex webhook when the process has been completed.\n");
          }
          elseif (!$success && $reindex
            ->isReindexFailed()) {
            throw new \Exception(dt("Error trying to re-index your subscription. You might require elevated keys to perform this operation."));
          }
          elseif (!$success && $reindex
            ->isReindexNone()) {
            $bundle_msg = empty($bundle) ? '' : dt("and bundle = @bundle", [
              '@bundle' => $bundle,
            ]);
            throw new \Exception(dt("You are trying to reset entities of type = '@entity_type' @bundle_msg but none of them have been exported.", [
              '@entity_type' => $entity_type,
              '@bundle_msg' => $bundle_msg,
            ]));
          }
        }
      }
      else {
        throw new \Exception(dt('You have already sent a Reindex Request to Content Hub. Please wait until the current reindex is processed and finalized before you can send another reindex request again.'));
      }
    }
    else {
      throw new \Exception(dt('Error trying to connect to the Content Hub. Make sure this site is registered to Content hub.'));
    }
  }
}