You are here

public function EntityUsageCommands::recreate in Entity Usage 8.2

Recreate all entity usage statistics.

@command entity-usage:recreate @aliases eu-r,entity-usage-recreate @option use-queue Use a queue instead of a batch process to recreate tracking info. This means usage information won't be accurate until all items in the queue have been processed by cron runs. @option multi-pass Use this command with options --use-queue and --multi-pass if you are experiencing timeouts when populating the queue. This means that every every time the command is passed with both options, queue items will not be created from start, but from where we left off in the previous execution. Run the command several times until all items have been queued. Use --clear-multi-pass to reset the --multi-pass flag. @option clear-multi-pass If --clear-multi-pass is set, all this command will do is to reset the multi-pass flag, so subsequent executions of this command will start fresh.

File

src/Commands/EntityUsageCommands.php, line 67

Class

EntityUsageCommands
Entity Usage drush commands.

Namespace

Drupal\entity_usage\Commands

Code

public function recreate($options = [
  'use-queue' => FALSE,
  'multi-pass' => FALSE,
  'clear-multi-pass' => FALSE,
]) {
  if (!empty($options['clear-multi-pass'])) {
    \Drupal::state()
      ->delete('entity_usage.multi_pass');
    $this
      ->output()
      ->writeln(t('Multi-pass flag has been cleared. Subsequent executions will start from scratch.'));
    return;
  }
  if (!empty($options['multi-pass']) && empty($options['use-queue'])) {
    $this
      ->output()
      ->writeln(t('The --multi-pass option can only be used when the --use-queue flag is specified. Aborting.'));
    return;
  }
  if (!empty($options['multi-pass'])) {
    $this
      ->output()
      ->writeln(t('Multi-pass flag has been set. If this command breaks, try running it again and it will do its best to resume where things were left off. Current state values:'));
    $state_values = \Drupal::state()
      ->get('entity_usage.multi_pass', []);
    array_walk($state_values, function (&$value, $key) {
      $value = "{$key}: {$value}";
    });
    if (empty($state_values)) {
      $state_values = t('Nothing yet!');
    }
    $this
      ->output()
      ->writeln($state_values);
  }
  if (!empty($options['use-queue'])) {
    $to_track = $this->entityUsageConfig
      ->get('track_enabled_source_entity_types');
    foreach ($this->entityTypeManager
      ->getDefinitions() as $entity_type_id => $entity_type) {

      // Only look for entities enabled for tracking on the settings form.
      $track_this_entity_type = FALSE;
      if (!is_array($to_track) && $entity_type
        ->entityClassImplements('\\Drupal\\Core\\Entity\\ContentEntityInterface')) {

        // When no settings are defined, track all content entities by default,
        // except for Files and Users.
        if (!in_array($entity_type_id, [
          'file',
          'user',
        ])) {
          $track_this_entity_type = TRUE;
        }
      }
      elseif (is_array($to_track) && in_array($entity_type_id, $to_track, TRUE)) {
        $track_this_entity_type = TRUE;
      }
      if ($track_this_entity_type) {
        $this
          ->generateQueueItems($entity_type_id, (bool) $options['multi-pass']);
      }
    }
  }
  else {
    $this->batchManager
      ->recreate();
    drush_backend_batch_process();
  }
}