changeset 83:f171a9fb7b7b

urllog: Split urllog_add function to urllog_exists for checking whether given URL already exists in the database. Use urllog_exists where appropriate.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 11 Sep 2011 19:39:30 +0300
parents 1bbc79f41a1c
children fa1e95c2a0bc
files urllog.tcl
diffstat 1 files changed, 28 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/urllog.tcl	Sun Sep 11 19:37:40 2011 +0300
+++ b/urllog.tcl	Sun Sep 11 19:39:30 2011 +0300
@@ -286,33 +286,37 @@
 }
 
 #-------------------------------------------------------------------------
-proc urllog_addurl {urlStr urlNick urlHost urlChan urlTitle} {
+proc urllog_exists {urlStr urlNick urlHost urlChan} {
   global urldb urlmsg_alreadyknown urllog_shorturl
 
-   ### Let's check if we already know the URL
-   set tmpURL [urllog_escape $urlStr]
-  urldb eval "SELECT id AS urlID, utime AS utime, url AS uurl, user AS uuser, host AS uhost, chan AS uchan FROM urls WHERE url='$tmpURL'" {
+  set usql "SELECT id AS uid, utime AS utime, url AS uurl, user AS uuser, host AS uhost, chan AS uchan, title AS utitle FROM urls WHERE url='[urllog_escape $urlStr]'"
+  urldb eval $usql {
     urllog_log "URL said by $urlNick ($urlStr) already known"
     if {$urllog_shorturl != 0} {
-      set qstr "[urllog_get_short $urlID] "
+      set qstr "[urllog_get_short $uid] "
     } else {
       set qstr ""
     }
     append qstr "($uuser/$uchan@[urllog_ctime $utime])"
-    if {[string length $urlTitle] > 0} {
-      set qstr "$urlmsg_alreadyknown - '$urlTitle' $qstr"
+    if {[string length $utitle] > 0} {
+      set qstr "$urlmsg_alreadyknown - '$utitle' $qstr"
     } else {
       set qstr "$urlmsg_alreadyknown $qstr"
     }
     urllog_verb_msg $urlNick $urlChan $qstr
     return 0
   }
+  return 1
+}
 
 
-  ### OK, the URL was not already known - thus we add it
-  set sql "INSERT INTO urls (utime,url,user,host,chan) VALUES ([unixtime], '[urllog_escape $urlStr]', '[urllog_escape $urlNick]', '[urllog_escape $urlHost]', '[urllog_escape $urlChan]')"
-  if {[catch {urldb eval $sql} uerrmsg]} {
-    urllog_log "$uerrmsg on SQL:\n$sql"
+#-------------------------------------------------------------------------
+proc urllog_addurl {urlStr urlNick urlHost urlChan urlTitle} {
+  global urldb urllog_shorturl
+
+  set usql "INSERT INTO urls (utime,url,user,host,chan,title) VALUES ([unixtime], '[urllog_escape $urlStr]', '[urllog_escape $urlNick]', '[urllog_escape $urlHost]', '[urllog_escape $urlChan]', '[urllog_escape $urlTitle]')"
+  if {[catch {urldb eval $usql} uerrmsg]} {
+    urllog_log "$uerrmsg on SQL:\n$usql"
     return 0
   }
   set uid [urldb last_insert_rowid]
@@ -429,8 +433,15 @@
 
   ### Do we perform additional optional checks?
   if {$urllog_check == 0 || [string range $urlStr 0 4] != "http:"} {
-    # No optional checks, just add the URL
-    urllog_addurl $urlStr $urlNick $urlHost $urlChan ""
+    # No optional checks, just add the URL, if it does not exist already
+    if {[urllog_exists $urlStr $urlNick $urlHost $urlChan]} {
+      urllog_addurl $urlStr $urlNick $urlHost $urlChan ""
+    }
+    return 1
+  }
+  
+  ### Does the URL already exist?
+  if {![urllog_exists $urlStr $urlNick $urlHost $urlChan]} {
     return 1
   }
 
@@ -503,7 +514,10 @@
       }
     }
 
-    urllog_addurl $urlStr $urlNick $urlHost $urlChan $urlTitle
+    # Check if the URL already exists, just in case we had some redirects
+    if {[urllog_exists $urlStr $urlNick $urlHost $urlChan]} {
+      urllog_addurl $urlStr $urlNick $urlHost $urlChan $urlTitle
+    }
     return 1
   } else {
     urllog_verb_msg $urlNick $urlChan "$urlmsg_errorgettingdoc ([::http::code $utoken])"