You are here

public function FeedsAtomRDFProcessor::process in Feeds Atom 7

Same name and namespace in other branches
  1. 6 plugins/FeedsAtomRDFProcessor.inc \FeedsAtomRDFProcessor::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/FeedsAtomRDFProcessor.inc, line 21
Contains the feeds atom RDF processor class.

Class

FeedsAtomRDFProcessor
Creates nodes from feed items.

Code

public function process(FeedsSource $source, FeedsParserResult $parser_result) {
  $state = $source
    ->state(FEEDS_PROCESS);
  while ($item = $parser_result
    ->shiftItem()) {
    if (!($entity_id = $this
      ->existingEntityId($source, $parser_result)) || $this->config['update_existing'] != FEEDS_SKIP_EXISTING) {

      // Only proceed if item has actually changed.
      $hash = $this
        ->hash($item);
      if (!empty($entity_id) && $hash == $this
        ->getHash($entity_id)) {
        continue;
      }
      try {

        // Assemble node, map item to it, save.
        if (empty($entity_id)) {
          $entity = $this
            ->newEntity($source);
          $this
            ->newItemInfo($entity, $source->feed_nid, $hash);
        }
        else {
          $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;
        }
        if (!empty($item['deleted'])) {
          $this
            ->entityDeleteMultiple(array(
            $entity_id,
          ));
          $state->deleted++;
        }
        else {
          $this
            ->map($source, $parser_result, $entity);

          // Set this boolean so that validate and save operations know this is
          // coming from Feeds Atom. This property is also declared in
          // feeds_atom_entity_property_info_alter() making the property available to Rules.
          $entity->feeds_atom_import = 1;
          $this
            ->entityValidate($entity);

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

          // Track progress.
          if (empty($entity_id)) {
            $state->created++;
          }
          else {
            $state->updated++;
          }
        }
      } catch (Exception $e) {
        $state->failed++;
        drupal_set_message($e
          ->getMessage(), 'warning');
        $message = $e
          ->getMessage();
        $message .= '<h3>Original item</h3>';
        $message .= '<pre>' . var_export($item, TRUE) . '</pre>';
        $message .= '<h3>Entity</h3>';
        $message .= '<pre>' . var_export($entity, TRUE) . '</pre>';
        $source
          ->log('import', $message, array(), WATCHDOG_ERROR);
      }
    }
  }

  // Set messages if we're done.
  if ($source
    ->progressImporting() != FEEDS_BATCH_COMPLETE) {
    return;
  }
  $info = $this
    ->entityInfo();
  $tokens = array(
    '@entity' => strtolower($info['label']),
    '@entities' => strtolower($info['label plural']),
  );
  $messages = array();
  if ($state->deleted) {
    $messages[] = array(
      'message' => format_plural($state->deleted, 'Deleted @number @entity.', 'Deleted @number @entities.', array(
        '@number' => $state->deleted,
      ) + $tokens),
    );
  }
  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->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);
  }
}