You are here

public function FeedsAtomRDFProcessor::process in Feeds Atom 6

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

Implementation of FeedsProcessor::process().

Overrides FeedsNodeProcessor::process

File

plugins/FeedsAtomRDFProcessor.inc, line 16
Contains the feeds atom RDF processor class.

Class

FeedsAtomRDFProcessor
Creates nodes from feed items.

Code

public function process(FeedsImportBatch $batch, FeedsSource $source) {

  // Keep track of processed items in this pass, set total number of items.
  $processed = 0;
  if (!$batch
    ->getTotal(FEEDS_PROCESSING)) {
    $batch
      ->setTotal(FEEDS_PROCESSING, $batch
      ->getItemCount());

    // These two calls to setTotal() were added as a workaround to problems
    // upstream in Feeds. See http://drupal.org/node/1139376
    $batch
      ->setTotal(FEEDS_FETCHING, $batch
      ->getProgress(FEEDS_FETCHING));
    $batch
      ->setTotal(FEEDS_PARSING, $batch
      ->getProgress(FEEDS_PARSING));
  }

  // These variables are set outsite of the while loop to reduce the total
  // number of function calls.
  $feeds_node_batch_size = variable_get('feeds_node_batch_size', FEEDS_NODE_BATCH_SIZE);
  $batch_total = $batch
    ->getTotal(FEEDS_PROCESSING);
  while ($item = $batch
    ->shiftItem()) {

    // If the item already exists and we're flagged to delete it, do that instead.
    // If the item doesn't already exists and we're flagged to delete it, do nothing.
    // This part is added from the parent class.
    if (!empty($item['deleted'])) {
      $nid = $this
        ->existingItemIdGlobal($batch, $source);
      if (!empty($nid)) {
        node_delete($nid);
      }
      continue;
    }

    // Create/update if item does not exist or update existing is enabled.
    if (!($nid = $this
      ->existingItemId($batch, $source)) || $this->config['update_existing'] != FEEDS_SKIP_EXISTING) {

      // Only proceed if item has actually changed.
      $hash = $this
        ->hash($item);
      if (!empty($nid) && $hash == $this
        ->getHash($nid)) {
        continue;
      }
      $node = $this
        ->buildNode($nid, $source->feed_nid);
      $node->feeds_node_item->hash = $hash;

      // Map and save node. If errors occur don't stop but report them.
      try {
        $this
          ->map($batch, $node, $source);
        node_save($node);
        if (!empty($nid)) {
          $batch->updated++;
        }
        else {
          $batch->created++;
        }
      } catch (Exception $e) {
        drupal_set_message($e
          ->getMessage(), 'warning');
        watchdog('feeds', $e
          ->getMessage(), array(), WATCHDOG_WARNING);
      }
    }
    $processed++;

    // setProgress() does not handle well the case in which the feeds_node_batch_size
    // is exactly equal to the number of incoming items.
    // This first if statement is a workaround for that bug with setProgress.
    if ($processed == $batch_total && $processed == $feeds_node_batch_size) {

      // Explicitly set the progress to FEED_BATCH_COMPLETE rather than calculating
      // the value as the next 'else if' will do.
      $batch
        ->setProgress(FEEDS_PROCESSING, FEEDS_BATCH_COMPLETE);
      return;
    }
    else {
      if ($processed >= $feeds_node_batch_size) {
        $batch
          ->setProgress(FEEDS_PROCESSING, $batch->created + $batch->updated);
        return;
      }
    }
  }

  // Set messages.
  if ($batch->created) {
    drupal_set_message(format_plural($batch->created, 'Created @number @type node.', 'Created @number @type nodes.', array(
      '@number' => $batch->created,
      '@type' => node_get_types('name', $this->config['content_type']),
    )));
  }
  elseif ($batch->updated) {
    drupal_set_message(format_plural($batch->updated, 'Updated @number @type node.', 'Updated @number @type nodes.', array(
      '@number' => $batch->updated,
      '@type' => node_get_types('name', $this->config['content_type']),
    )));
  }
  else {
    drupal_set_message(t('There is no new content.'));
  }
  $batch
    ->setProgress(FEEDS_PROCESSING, FEEDS_BATCH_COMPLETE);
}