You are here

function _feedapi_node_unique in FeedAPI 6

Same name and namespace in other branches
  1. 5 feedapi_node/feedapi_node.module \_feedapi_node_unique()

Determine whether a given feed item already exists or not.

Parameters

$feed_item: Feed item object

$feed_nid: Feed ID

Return value

TRUE if the item is new, FALSE if not.

File

feedapi_node/feedapi_node.module, line 446
Handle how the feed items are represented as a content Handle the processing of the feed items

Code

function _feedapi_node_unique($feed_item, $feed_nid, $settings) {

  // If FeedAPI Mapper 2.x is available, delegate decision.
  if (function_exists('feedapi_mapper_unique')) {
    $feed_node = node_load($feed_nid);
    $result = feedapi_mapper_unique($feed_node, 'feedapi_node', $feed_item);
    if ($result !== FALSE) {
      if (empty($result)) {
        return TRUE;
      }
      if (!$settings['x_dedupe']) {

        // Filter out unneeded result, only the items from the same feeds are needed
        foreach ($result as $field => $dup_items) {
          foreach ($dup_items as $item_id => $feed_nids) {
            if (!in_array($feed_nid, $feed_nids)) {
              unset($result[$field][$item_id]);
            }
          }
          if (count($result[$field]) == 0) {
            unset($result[$field]);
          }
        }
        if (empty($result)) {
          return TRUE;
        }

        // This is an item id
        return array_pop(array_keys(array_pop($result)));
      }
      else {
        foreach ($result as $dup_items) {
          foreach ($dup_items as $item_id => $feed_nids) {
            $feed_item->feedapi_node->duplicates[$item_id] = array_merge($feed_nids, (array) $feed_item->feedapi_node->duplicates[$item_id]);
          }
        }
        return $item_id;
      }
    }
  }

  // If we reach this point feedapi_mapper_unique() is not available or no unique elements have been defined.
  // Falls back to URL/GUID for determining whether item is unique.
  if (empty($feed_item->options->original_url)) {
    unset($feed_item->options->original_url);
  }
  if (empty($feed_item->options->guid)) {
    unset($feed_item->options->guid);
  }

  // Feed item is duplicate, if URL or GUID are duplicate or if they are both missing.
  if (isset($feed_item->options->original_url)) {
    $nid = db_result(db_query("SELECT fni.nid FROM {feedapi_node_item} fni JOIN {feedapi_node_item_feed} ff ON ff.feed_item_nid = fni.nid WHERE fni.url = '%s' AND ff.feed_nid = %d", $feed_item->options->original_url, $feed_nid));
    if ($nid !== FALSE) {
      return $nid;
    }
  }
  if (isset($feed_item->options->guid)) {
    $nid = db_result(db_query("SELECT fni.nid FROM {feedapi_node_item} fni JOIN {feedapi_node_item_feed} ff ON ff.feed_item_nid = fni.nid WHERE fni.guid = '%s' AND ff.feed_nid = %d", $feed_item->options->guid, $feed_nid));
    if ($nid !== FALSE) {
      return $nid;
    }
  }

  // If cross feed de-dupeing is enabled, check now whether there is a duplicate item on other feeds.
  // If so, store duplicates in array.
  // There is *usually* only one. However, there might be more than one.
  // Todo: don't link to feed items whose feed is not x_dedupe enabled.
  if ($settings['x_dedupe']) {
    if (isset($feed_item->options->original_url)) {
      $result = db_query("SELECT fni.nid, ff.feed_nid FROM {feedapi_node_item} fni JOIN {feedapi_node_item_feed} ff ON ff.feed_item_nid = fni.nid WHERE ff.feed_nid <> %d AND fni.url = '%s'", $feed_nid, $feed_item->options->original_url);
      while ($existing_feed_item = db_fetch_object($result)) {
        $feed_item->feedapi_node->duplicates[$existing_feed_item->nid][] = $existing_feed_item->feed_nid;
      }
    }
    if (!isset($feed_item->feedapi_node->duplicates) && isset($feed_item->options->guid)) {
      $result = db_query("SELECT fni.nid, ff.feed_nid FROM {feedapi_node_item} fni JOIN {feedapi_node_item_feed} ff ON ff.feed_item_nid = fni.nid WHERE ff.feed_nid <> %d AND fni.guid = '%s'", $feed_nid, $feed_item->options->guid);
      while ($existing_feed_item = db_fetch_object($result)) {
        $feed_item->feedapi_node->duplicates[$existing_feed_item->nid][] = $existing_feed_item->feed_nid;
      }
    }
  }
  if (isset($feed_item->options->original_url) || isset($feed_item->options->guid)) {
    return TRUE;
  }

  // Neither GUID, nor URL present: no unique item.
  return FALSE;
}