+
+let insert_link dbh hostid from_url to_url =
+ if from_url <> to_url then (
+ let sth = dbh#prepare_cached "select 1 from links
+ where hostid = ?
+ and from_url = ? and to_url = ?" in
+ sth#execute [`Int hostid; `String from_url; `String to_url];
+
+ let exists = try sth#fetch1int () = 1 with Not_found -> false in
+
+ if not exists then (
+ let sth =
+ dbh#prepare_cached "insert into links (hostid, from_url, to_url)
+ values (?, ?, ?)" in
+ sth#execute [`Int hostid; `String from_url; `String to_url]
+ )
+ )
+
+let update_links_for_page dbh hostid page =
+ (* Delete entries in the old links table. *)
+ let sth = dbh#prepare_cached "delete from links
+ where hostid = ? and from_url = ?" in
+ sth#execute [`Int hostid; `String page];
+
+ (* Get the sections from the page. *)
+ let sth = dbh#prepare_cached "select c.content from contents c, pages p
+ where c.pageid = p.id
+ and p.hostid = ?
+ and p.url = ?
+ and p.redirect is null" in
+ sth#execute [`Int hostid; `String page];
+
+ (* Get the links from each section. *)
+ sth#iter
+ (function [`String content] ->
+ let links = get_links_from_section dbh hostid content in
+ List.iter (insert_link dbh hostid page) links
+ | _ -> assert false)