You are here

public function FeedsNodeProcessor::process in Feeds 6

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

Implementation of FeedsProcessor::process().

Overrides FeedsProcessor::process

File

plugins/FeedsNodeProcessor.inc, line 25
Class definition of FeedsNodeProcessor.

Class

FeedsNodeProcessor
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;
  $batch_size = variable_get('feeds_node_batch_size', FEEDS_NODE_BATCH_SIZE);
  if (!$batch
    ->getTotal(FEEDS_PROCESSING)) {
    $batch
      ->setTotal(FEEDS_PROCESSING, count($batch->items));
  }
  while ($item = $batch
    ->shiftItem()) {

    // 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);
        if ($this->config['authorize']) {
          if (empty($node->nid)) {
            $op = 'create';
          }
          else {
            $op = 'update';
          }
          $account = user_load($node->uid);
          if (!node_access($op, $node, $account)) {
            throw new Exception('User ' . $account->uid . ' not authorized to ' . $op . ' content type ' . $node->type);
          }
        }
        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++;
    if ($processed >= $batch_size) {
      $total = $batch
        ->getTotal(FEEDS_PROCESSING);
      $batch
        ->setProgress(FEEDS_PROCESSING, $total - count($batch->items));
      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']),
    )));
  }
  if ($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']),
    )));
  }
  if (!$batch->created && !$batch->updated) {
    drupal_set_message(t('There is no new content.'));
  }
  $batch
    ->setProgress(FEEDS_PROCESSING, FEEDS_BATCH_COMPLETE);
}