You are here

public function FeedsCommentProcessor::process in Feeds Comment Processor 6

Implementation of FeedsProcessor::process().

Overrides FeedsProcessor::process

File

./FeedsCommentProcessor.inc, line 25
Class definition of FeedsCommentProcessor.

Class

FeedsCommentProcessor
Creates comments from feed items.

Code

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

  // Keep track of processed items in this pass.
  $processed = 0;
  if (!$batch
    ->getTotal(FEEDS_PROCESSING)) {
    $batch
      ->setTotal(FEEDS_PROCESSING, $batch
      ->getItemCount());
  }
  while ($item = $batch
    ->shiftItem()) {

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

      // Only proceed if item has actually changed.
      $hash = $this
        ->hash($item);
      if (!empty($cid) && $hash == $this
        ->getHash($cid)) {
        continue;
      }
      $comment = $this
        ->buildComment($cid, $source->feed_nid);
      $comment->feeds_comment_item->hash = $hash;

      // Map and save comment. If errors occur don't stop but report them.
      try {
        $this
          ->map($batch, $comment);
        if (empty($comment->nid)) {
          throw new Exception("Unable create comment with empty NID");
        }
        if ($this->config['authorize']) {
          $account = user_load($comment->uid);
          if (!user_access('post comments')) {
            throw new Exception('User ' . $account->uid . ' not authorized to post comments.');
          }
        }
        $node = node_load($comment->nid);
        if ($node->comment != 2) {
          throw new Exception('Comments are not allowed for this node.');
        }
        _feeds_comment_save((array) $comment);
        if (!empty($cid)) {
          $batch->updated++;
        }
        else {
          $batch->created++;
        }
      } catch (Exception $e) {
        drupal_set_message($e
          ->getMessage(), 'warning');
        watchdog('feeds', $e
          ->getMessage(), array(), WATCHDOG_WARNING);
      }
    }
    $processed++;
    if ($processed >= variable_get('feeds_comment_batch_size', FEEDS_COMMENT_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 comment', 'Created @number comments.', array(
      '@number' => $batch->created,
    )));
  }
  elseif ($batch->updated) {
    drupal_set_message(format_plural($batch->updated, 'Updated @number comment.', 'Updated @number comments.', array(
      '@number' => $batch->updated,
    )));
  }
  else {
    drupal_set_message(t('There are no new comments.'));
  }
  $batch
    ->setProgress(FEEDS_PROCESSING, FEEDS_BATCH_COMPLETE);
}