(* 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/10/21 11:42:05 rich Exp $
+ * $Id: cocanwiki_links.ml,v 1.2 2006/03/27 16:43:44 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
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
+ let exists =
+ [] <> PGSQL(dbh) "select 1 from links
+ where hostid = $hostid
+ and from_url = $from_url
+ and to_url = $to_url" 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]
+ PGSQL(dbh) "insert into links (hostid, from_url, to_url)
+ values ($hostid, $from_url, $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];
+ PGSQL(dbh) "delete from links
+ where hostid = $hostid and from_url = $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];
+ let rows = PGSQL(dbh)
+ "select c.content from contents c, pages p
+ where c.pageid = p.id
+ and p.hostid = $hostid
+ and p.url = $page
+ and p.redirect is null" in
(* 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)
+ List.iter (
+ fun content ->
+ let links = get_links_from_section dbh hostid content in
+ List.iter (insert_link dbh hostid page) links
+ ) rows
(* Because of redirects, getting the list of pages which link to this
* page isn't a matter of just doing 'select from_url from links ...'.
* to page C, then querying what links to page C will list page A twice.
* This is a bug.
*)
-let what_links_here (dbh : Dbi.connection) hostid page =
+let what_links_here dbh hostid page =
(* Build up the complete list of URLs which redirect to the target
* page, within max_redirect redirections. This is sort of like
* Prim's algorithm.
let found = ref true in
let i = ref 1 in
while !found && !i <= max_redirect do
- let qs = Dbi.placeholders (List.length !urls) in
- let sql =
+ let new_urls =
+ let urls = !urls in
+ PGSQL(dbh)
"select url from pages
- where hostid = ?
+ where hostid = $hostid
and url is not null and redirect is not null
- and url not in " ^ qs ^ " and redirect in " ^ qs in
- let sth = dbh#prepare_cached sql in
- let args = List.map (fun s -> `String s) !urls in
- sth#execute (`Int hostid :: (args @ args));
- let new_urls = sth#map (function [`String s] -> s | _ -> assert false) in
+ and url not in $@urls and redirect in $@urls" in
+ let new_urls =
+ List.map (function | Some url -> url | None -> assert false) new_urls in
urls := !urls @ new_urls;
found := new_urls <> [];
incr i
(* Now find any pages which link to one of these target pages. For
* convenience we also select out the titles.
*)
- let qs = Dbi.placeholders (List.length urls) in
- let sth =
- dbh#prepare_cached
- ("select li.from_url, p.title, li.from_url = 'index'
- from links li, pages p
- where li.hostid = ? and li.to_url in " ^ qs ^ "
- and li.hostid = p.hostid and li.from_url = p.url
- order by 3 desc, 2, 1") in
- sth#execute (`Int hostid :: (List.map (fun s -> `String s) urls));
-
- sth#map (function
- | [`String url; `String title; _] -> url, title
- | _ -> assert false)
+ let rows =
+ PGSQL(dbh)
+ "select li.from_url, p.title, li.from_url = 'index'
+ from links li, pages p
+ where li.hostid = $hostid and li.to_url in $@urls
+ and li.hostid = p.hostid and li.from_url = p.url
+ order by 3 desc, 2, 1" in
+
+ List.map (fun (url, title, _) -> url, title) rows