You are here

public function FeedsProcessor::process in Feeds 7.2

Same name and namespace in other branches
  1. 6 plugins/FeedsProcessor.inc \FeedsProcessor::process()
  2. 7 plugins/FeedsProcessor.inc \FeedsProcessor::process()

Process the result of the parsing stage.

Parameters

FeedsSource $source: Source information about this import.

FeedsParserResult $parser_result: The result of the parsing stage.

File

plugins/FeedsProcessor.inc, line 389
Contains FeedsProcessor and related classes.

Class

FeedsProcessor
Abstract class, defines interface for processors.

Code

public function process(FeedsSource $source, FeedsParserResult $parser_result) {
  $state = $source
    ->state(FEEDS_PROCESS);
  if (!isset($state->removeList) && $parser_result->items) {
    $this
      ->initEntitiesToBeRemoved($source, $state);
  }
  $skip_new = $this->config['insert_new'] == FEEDS_SKIP_NEW;
  $skip_existing = $this->config['update_existing'] == FEEDS_SKIP_EXISTING;
  while ($item = $parser_result
    ->shiftItem()) {

    // Check if this item already exists.
    $entity_id = $this
      ->existingEntityId($source, $parser_result);

    // If it's included in the feed, it must not be removed on clean.
    if ($entity_id) {
      unset($state->removeList[$entity_id]);
    }
    module_invoke_all('feeds_before_update', $source, $item, $entity_id);

    // If it exists, and we are not updating, or if it does not exist, and we
    // are not inserting, pass onto the next item.
    if ($entity_id && $skip_existing || !$entity_id && $skip_new) {
      continue;
    }
    try {
      $hash = $this
        ->hash($item);
      $changed = $hash !== $this
        ->getHash($entity_id);

      // Do not proceed if the item exists, has not changed, and we're not
      // forcing the update.
      if ($entity_id && !$changed && !$this->config['skip_hash_check']) {
        continue;
      }

      // Load an existing entity.
      if ($entity_id) {
        $entity = $this
          ->entityLoad($source, $entity_id);

        // The feeds_item table is always updated with the info for the most
        // recently processed entity. The only carryover is the entity_id.
        $this
          ->newItemInfo($entity, $source->feed_nid, $hash);
        $entity->feeds_item->entity_id = $entity_id;
        $entity->feeds_item->is_new = FALSE;
      }
      else {
        $entity = $this
          ->newEntity($source);
        $this
          ->newItemInfo($entity, $source->feed_nid, $hash);
      }

      // Set property and field values.
      $this
        ->map($source, $parser_result, $entity);

      // Allow modules to alter the entity before validating.
      module_invoke_all('feeds_prevalidate', $source, $entity, $item, $entity_id);
      $this
        ->entityValidate($entity, $source);

      // Allow modules to alter the entity before saving.
      module_invoke_all('feeds_presave', $source, $entity, $item, $entity_id);
      if (module_exists('rules')) {
        rules_invoke_event('feeds_import_' . $source
          ->importer()->id, $entity);
      }

      // Enable modules to skip saving at all.
      if (!empty($entity->feeds_item->skip)) {
        continue;
      }

      // This will throw an exception on failure.
      $this
        ->entitySaveAccess($entity);
      $this
        ->entitySave($entity);

      // Allow modules to perform operations using the saved entity data.
      // $entity contains the updated entity after saving.
      module_invoke_all('feeds_after_save', $source, $entity, $item, $entity_id);

      // Track progress.
      if (empty($entity_id)) {
        $state->created++;
      }
      else {
        $state->updated++;
      }
    } catch (Exception $e) {
      $state->failed++;
      drupal_set_message($e
        ->getMessage(), 'warning');
      list($message, $arguments) = $this
        ->createLogEntry($e, $entity, $item);
      $source
        ->log('import', $message, $arguments, WATCHDOG_ERROR);
    }
  }

  // Set messages if we're done.
  if ($source
    ->progressImporting() != FEEDS_BATCH_COMPLETE) {
    return;
  }

  // Remove not included items if needed.
  // It depends on the implementation of the clean() method what will happen
  // to items that were no longer in the source.
  $this
    ->clean($state);
  $info = $this
    ->entityInfo();
  $tokens = array(
    '@entity' => strtolower($info['label']),
    '@entities' => strtolower($info['label plural']),
  );
  $messages = array();
  if ($state->created) {
    $messages[] = array(
      'message' => format_plural($state->created, 'Created @number @entity.', 'Created @number @entities.', array(
        '@number' => $state->created,
      ) + $tokens),
    );
  }
  if ($state->updated) {
    $messages[] = array(
      'message' => format_plural($state->updated, 'Updated @number @entity.', 'Updated @number @entities.', array(
        '@number' => $state->updated,
      ) + $tokens),
    );
  }
  if ($state->unpublished) {
    $messages[] = array(
      'message' => format_plural($state->unpublished, 'Unpublished @number @entity.', 'Unpublished @number @entities.', array(
        '@number' => $state->unpublished,
      ) + $tokens),
    );
  }
  if ($state->blocked) {
    $messages[] = array(
      'message' => format_plural($state->blocked, 'Blocked @number @entity.', 'Blocked @number @entities.', array(
        '@number' => $state->blocked,
      ) + $tokens),
    );
  }
  if ($state->deleted) {
    $messages[] = array(
      'message' => format_plural($state->deleted, 'Removed @number @entity.', 'Removed @number @entities.', array(
        '@number' => $state->deleted,
      ) + $tokens),
    );
  }
  if ($state->failed) {
    $messages[] = array(
      'message' => format_plural($state->failed, 'Failed importing @number @entity.', 'Failed importing @number @entities.', array(
        '@number' => $state->failed,
      ) + $tokens),
      'level' => WATCHDOG_ERROR,
    );
  }
  if (empty($messages)) {
    $messages[] = array(
      'message' => t('There are no new @entities.', array(
        '@entities' => strtolower($info['label plural']),
      )),
    );
  }
  foreach ($messages as $message) {
    drupal_set_message($message['message']);
    $source
      ->log('import', $message['message'], array(), isset($message['level']) ? $message['level'] : WATCHDOG_INFO);
  }
}