From cbe0081a8c7e955870a374b5232a0f0487adb176 Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Tue, 20 Jun 2006 09:44:52 +0000
Subject: [PATCH] - Patch #61433 by Steve Dondley: added support for GUIDs to
 prevent duplicate posts being inserted.

---
 database/updates.inc                 | 16 ++++++++++++++++
 modules/aggregator.module            | 17 ++++++++++-------
 modules/aggregator/aggregator.module | 17 ++++++++++-------
 3 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/database/updates.inc b/database/updates.inc
index b1110f2aecb2..b5aeea6c6170 100644
--- a/database/updates.inc
+++ b/database/updates.inc
@@ -2071,3 +2071,19 @@ function system_update_185() {
   return $ret;
 }
 
+function system_update_186() {
+  // Make use of guid in feed items
+  $ret = array();
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      $ret[] = update_sql("ALTER TABLE {aggregator_item} ADD guid varchar(255) AFTER timestamp ;");
+      break;
+    case 'pgsql':
+      db_add_column($ret, 'aggregator_item', 'guid', 'varchar(255)');
+      break;
+  }
+  return $ret;
+}
+
+
diff --git a/modules/aggregator.module b/modules/aggregator.module
index 1625ad4e859f..e9fe10d0259f 100644
--- a/modules/aggregator.module
+++ b/modules/aggregator.module
@@ -829,7 +829,7 @@ function aggregator_parse_feed(&$data, $feed) {
   $items = array_reverse($items);
 
   foreach ($items as $item) {
-    unset($title, $link, $author, $description);
+    unset($title, $link, $author, $description, $guid);
 
     // Prepare the item:
     foreach ($item as $key => $value) {
@@ -856,8 +856,8 @@ function aggregator_parse_feed(&$data, $feed) {
     if ($item['LINK']) {
       $link = $item['LINK'];
     }
-    elseif ($item['GUID'] && (strncmp($item['GUID'], 'http://', 7) == 0)) {
-      $link = $item['GUID'];
+    if ($item['GUID']) {
+      $guid = $item['GUID'];
     }
     else {
       $link = $feed['link'];
@@ -902,14 +902,17 @@ function aggregator_parse_feed(&$data, $feed) {
     ** pass along it's ID such that we can update it if needed.
     */
 
-    if ($link && $link != $feed['link'] && $link != $feed['url']) {
+    if ($guid) {
+      $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND guid = '%s'", $feed['fid'], $guid));
+    }
+    else if ($link && $link != $feed['link'] && $link != $feed['url']) {
       $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND link = '%s'", $feed['fid'], $link));
     }
     else {
       $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND title = '%s'", $feed['fid'], $title));
     }
 
-    aggregator_save_item(array('iid' => $entry->iid, 'fid' => $feed['fid'], 'timestamp' => $timestamp, 'title' => $title, 'link' => $link, 'author' => $item['AUTHOR'], 'description' => $item['DESCRIPTION']));
+    aggregator_save_item(array('iid' => $entry->iid, 'fid' => $feed['fid'], 'timestamp' => $timestamp, 'title' => $title, 'link' => $link, 'author' => $item['AUTHOR'], 'description' => $item['DESCRIPTION'], 'guid' => $guid));
   }
 
   /*
@@ -933,7 +936,7 @@ function aggregator_parse_feed(&$data, $feed) {
 
 function aggregator_save_item($edit) {
   if ($edit['iid'] && $edit['title']) {
-    db_query("UPDATE {aggregator_item} SET title = '%s', link = '%s', author = '%s', description = '%s' WHERE iid = %d", $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['iid']);
+    db_query("UPDATE {aggregator_item} SET title = '%s', link = '%s', author = '%s', description = '%s', guid = '%s' WHERE iid = %d", $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['iid'], $edit['gid']);
   }
   else if ($edit['iid']) {
     db_query('DELETE FROM {aggregator_item} WHERE iid = %d', $edit['iid']);
@@ -941,7 +944,7 @@ function aggregator_save_item($edit) {
   }
   else if ($edit['title'] && $edit['link']) {
     $edit['iid'] = db_next_id('{aggregator_item}_iid');
-    db_query("INSERT INTO {aggregator_item} (iid, fid, title, link, author, description, timestamp) VALUES (%d, %d, '%s', '%s', '%s', '%s', %d)", $edit['iid'], $edit['fid'], $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['timestamp']);
+    db_query("INSERT INTO {aggregator_item} (iid, fid, title, link, author, description, timestamp, guid) VALUES (%d, %d, '%s', '%s', '%s', '%s', %d, '%s')", $edit['iid'], $edit['fid'], $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['timestamp'], $edit['guid']);
     // file the items in the categories indicated by the feed
     $categories = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = %d', $edit['fid']);
     while ($category = db_fetch_object($categories)) {
diff --git a/modules/aggregator/aggregator.module b/modules/aggregator/aggregator.module
index 1625ad4e859f..e9fe10d0259f 100644
--- a/modules/aggregator/aggregator.module
+++ b/modules/aggregator/aggregator.module
@@ -829,7 +829,7 @@ function aggregator_parse_feed(&$data, $feed) {
   $items = array_reverse($items);
 
   foreach ($items as $item) {
-    unset($title, $link, $author, $description);
+    unset($title, $link, $author, $description, $guid);
 
     // Prepare the item:
     foreach ($item as $key => $value) {
@@ -856,8 +856,8 @@ function aggregator_parse_feed(&$data, $feed) {
     if ($item['LINK']) {
       $link = $item['LINK'];
     }
-    elseif ($item['GUID'] && (strncmp($item['GUID'], 'http://', 7) == 0)) {
-      $link = $item['GUID'];
+    if ($item['GUID']) {
+      $guid = $item['GUID'];
     }
     else {
       $link = $feed['link'];
@@ -902,14 +902,17 @@ function aggregator_parse_feed(&$data, $feed) {
     ** pass along it's ID such that we can update it if needed.
     */
 
-    if ($link && $link != $feed['link'] && $link != $feed['url']) {
+    if ($guid) {
+      $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND guid = '%s'", $feed['fid'], $guid));
+    }
+    else if ($link && $link != $feed['link'] && $link != $feed['url']) {
       $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND link = '%s'", $feed['fid'], $link));
     }
     else {
       $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND title = '%s'", $feed['fid'], $title));
     }
 
-    aggregator_save_item(array('iid' => $entry->iid, 'fid' => $feed['fid'], 'timestamp' => $timestamp, 'title' => $title, 'link' => $link, 'author' => $item['AUTHOR'], 'description' => $item['DESCRIPTION']));
+    aggregator_save_item(array('iid' => $entry->iid, 'fid' => $feed['fid'], 'timestamp' => $timestamp, 'title' => $title, 'link' => $link, 'author' => $item['AUTHOR'], 'description' => $item['DESCRIPTION'], 'guid' => $guid));
   }
 
   /*
@@ -933,7 +936,7 @@ function aggregator_parse_feed(&$data, $feed) {
 
 function aggregator_save_item($edit) {
   if ($edit['iid'] && $edit['title']) {
-    db_query("UPDATE {aggregator_item} SET title = '%s', link = '%s', author = '%s', description = '%s' WHERE iid = %d", $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['iid']);
+    db_query("UPDATE {aggregator_item} SET title = '%s', link = '%s', author = '%s', description = '%s', guid = '%s' WHERE iid = %d", $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['iid'], $edit['gid']);
   }
   else if ($edit['iid']) {
     db_query('DELETE FROM {aggregator_item} WHERE iid = %d', $edit['iid']);
@@ -941,7 +944,7 @@ function aggregator_save_item($edit) {
   }
   else if ($edit['title'] && $edit['link']) {
     $edit['iid'] = db_next_id('{aggregator_item}_iid');
-    db_query("INSERT INTO {aggregator_item} (iid, fid, title, link, author, description, timestamp) VALUES (%d, %d, '%s', '%s', '%s', '%s', %d)", $edit['iid'], $edit['fid'], $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['timestamp']);
+    db_query("INSERT INTO {aggregator_item} (iid, fid, title, link, author, description, timestamp, guid) VALUES (%d, %d, '%s', '%s', '%s', '%s', %d, '%s')", $edit['iid'], $edit['fid'], $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['timestamp'], $edit['guid']);
     // file the items in the categories indicated by the feed
     $categories = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = %d', $edit['fid']);
     while ($category = db_fetch_object($categories)) {
-- 
GitLab