You are here

public function FeedsProcessor::clear in Feeds 8.2

Remove all stored results or stored results up to a certain time for a source.

Parameters

FeedsSource $source: Source information for this expiry. Implementers should only delete items pertaining to this source. The preferred way of determining whether an item pertains to a certain souce is by using $source->feed_nid. It is the processor's responsibility to store the feed_nid of an imported item in the processing stage.

File

lib/Drupal/feeds/Plugin/FeedsProcessor.php, line 329
Contains FeedsProcessor and related classes.

Class

FeedsProcessor
Abstract class, defines interface for processors.

Namespace

Drupal\feeds\Plugin

Code

public function clear(FeedsSource $source) {
  $state = $source
    ->state(FEEDS_PROCESS_CLEAR);

  // Build base select statement.
  $info = $this
    ->entityInfo();
  $select = db_select($info['base_table'], 'e');
  $select
    ->addField('e', $info['entity_keys']['id'], 'entity_id');
  $select
    ->join('feeds_item', 'fi', "e.{$info['entity_keys']['id']} = fi.entity_id AND fi.entity_type = '{$this->entityType()}'");
  $select
    ->condition('fi.id', $this->id);
  $select
    ->condition('fi.feed_nid', $source->feed_nid);

  // If there is no total, query it.
  if (!$state->total) {
    $state->total = $select
      ->countQuery()
      ->execute()
      ->fetchField();
  }

  // Delete a batch of entities.
  $entities = $select
    ->range(0, $this
    ->getLimit())
    ->execute();
  $entity_ids = array();
  foreach ($entities as $entity) {
    $entity_ids[$entity->entity_id] = $entity->entity_id;
  }
  $this
    ->entityDeleteMultiple($entity_ids);

  // Report progress, take into account that we may not have deleted as
  // many items as we have counted at first.
  if (count($entity_ids)) {
    $state->deleted += count($entity_ids);
    $state
      ->progress($state->total, $state->deleted);
  }
  else {
    $state
      ->progress($state->total, $state->total);
  }

  // Report results when done.
  if ($source
    ->progressClearing() == FEEDS_BATCH_COMPLETE) {
    if ($state->deleted) {
      $message = format_plural($state->deleted, 'Deleted @number @entity', 'Deleted @number @entities', array(
        '@number' => $state->deleted,
        '@entity' => strtolower($info['label']),
        '@entities' => strtolower($info['label plural']),
      ));
      $source
        ->log('clear', $message, array(), WATCHDOG_INFO);
      drupal_set_message($message);
    }
    else {
      drupal_set_message(t('There are no @entities to be deleted.', array(
        '@entities' => $info['label plural'],
      )));
    }
  }
}