When copying a template during site creation, force the hosts.is_template
[cocanwiki.git] / scripts / cocanwiki_links.ml
index 417fe19..9d856d2 100644 (file)
@@ -1,7 +1,7 @@
 (* COCANWIKI - a wiki written in Objective CAML.
  * Written by Richard W.M. Jones <rich@merjis.com>.
  * Copyright (C) 2004 Merjis Ltd.
- * $Id: cocanwiki_links.ml,v 1.1 2004/09/28 10:56:39 rich Exp $
+ * $Id: cocanwiki_links.ml,v 1.2 2004/09/28 11:28:39 rich Exp $
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -58,3 +58,41 @@ let get_links_from_section dbh hostid content =
 
   (* Return the list of links. *)
   links
+
+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)