X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=scripts%2Fsearch.ml;h=49014c5c01cf764276f6e0064af92d0f070cb175;hb=3bbcd6cd55f445a3b8b54269237655570c89354b;hp=2d449b88424a807ce190603fee5be979c1f13b4c;hpb=bfa88724ee152ba00c2b2fca881dd78a6599820a;p=cocanwiki.git diff --git a/scripts/search.ml b/scripts/search.ml index 2d449b8..49014c5 100644 --- a/scripts/search.ml +++ b/scripts/search.ml @@ -1,7 +1,7 @@ (* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. - * $Id: search.ml,v 1.9 2006/03/27 18:09:46 rich Exp $ + * $Id: search.ml,v 1.13 2006/12/06 09:46:57 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 @@ -33,7 +33,7 @@ open Cocanwiki_date let split_words = Pcre.regexp "\\W+" let run r (q : cgi) dbh hostid host user = - let template = get_template dbh hostid "search.html" in + let template = get_template r dbh hostid "search.html" in template#set "canonical_hostname" host.canonical_hostname; @@ -62,83 +62,97 @@ let run r (q : cgi) dbh hostid host user = let keywords = Pcre.split ~rex:split_words query in let keywords = List.filter (fun s -> not (string_is_whitespace s)) keywords in - let keywords = List.map String.lowercase keywords in + let keywords = List.map lowercase keywords in (* Turn the keywords into a tsearch2 ts_query string. *) let tsquery = String.concat "&" keywords in (* Search the titles first. *) - let sth = - dbh#prepare_cached - ("select id, url, url_deleted, title, last_modified_date, - (lower (title) = lower (?)) as exact - from pages - where hostid = ? " ^ - (if not old_versions then "and url is not null " else "") ^ " - and redirect is null - and title_description_fti @@ to_tsquery (?, ?) - order by exact desc, last_modified_date desc, title") in - sth#execute [Some query; - Some hostid; Some "default"; Some tsquery]; + let rows = + if not old_versions then + PGSQL(dbh) + "select id, url, url_deleted, title, last_modified_date, + (lower (title) = lower ($query)) as exact + from pages + where hostid = $hostid + and url is not null + and redirect is null + and title_description_fti @@ to_tsquery ('default', $tsquery) + order by exact desc, last_modified_date desc, title" + else + PGSQL(dbh) + "select id, url, url_deleted, title, last_modified_date, + (lower (title) = lower ($query)) as exact + from pages + where hostid = $hostid + and redirect is null + and title_description_fti @@ to_tsquery ('default', $tsquery) + order by exact desc, last_modified_date desc, title" in let titles = - sth#map (function - | [_; Some url; None; Some title; - `Timestamp last_modified; _] -> - url, title, None, last_modified - | [Some version; None; Some url; Some title; - `Timestamp last_modified; _] -> - url, title, Some version, last_modified - | _ -> assert false) in + List.map (function + | (_, Some url, None, title, last_modified, _) -> + url, title, None, last_modified + | (version, None, Some url, title, last_modified, _) -> + url, title, Some version, last_modified + | _ -> assert false) rows in let have_titles = titles <> [] in template#conditional "have_titles" have_titles; (* Search the contents. *) - let sth = - dbh#prepare_cached - ("select c.id, p.id, p.url, p.url_deleted, p.title, - p.last_modified_date - from contents c, pages p - where c.pageid = p.id - and p.hostid = ? " ^ - (if not old_versions then "and url is not null " else "") ^ " - and p.redirect is null - and c.content_fti @@ to_tsquery (?, ?) - order by p.last_modified_date desc, p.title - limit 50") in - sth#execute [Some hostid; Some "default"; Some tsquery]; + let rows = + if not old_versions then + PGSQL(dbh) + "select c.id, p.id, p.url, p.url_deleted, p.title, + p.last_modified_date + from contents c, pages p + where c.pageid = p.id + and p.hostid = $hostid + and url is not null + and p.redirect is null + and c.content_fti @@ to_tsquery ('default', $tsquery) + order by p.last_modified_date desc, p.title + limit 50" + else + PGSQL(dbh) + "select c.id, p.id, p.url, p.url_deleted, p.title, + p.last_modified_date + from contents c, pages p + where c.pageid = p.id + and p.hostid = $hostid + and p.redirect is null + and c.content_fti @@ to_tsquery ('default', $tsquery) + order by p.last_modified_date desc, p.title + limit 50" in let contents = - sth#map (function - | [Some contentid; _; Some url; None; - Some title; `Timestamp last_modified] -> + List.map (function + | (contentid, _, Some url, None, title, last_modified) -> contentid, url, title, None, last_modified - | [Some contentid; Some version; None; Some url; - Some title; `Timestamp last_modified] -> + | (contentid, version, None, Some url, title, + last_modified) -> contentid, url, title, Some version, last_modified - | _ -> assert false) in + | _ -> assert false) rows in let have_contents = contents <> [] in template#conditional "have_contents" have_contents; - (* Pull out the actual text which matched so we can generate a summary.*) + (* Pull out the actual text which matched so we can generate a summary. + * XXX tsearch2 can actually do better than this by emboldening + * the text which maps. + *) let content_map = if contents = [] then [] else ( - let qs = Dbi.placeholders (List.length contents) in - let sth = - dbh#prepare_cached - ("select id, sectionname, content from contents - where id in " ^ qs) in - sth#execute - (List.map (fun (contentid, _,_,_,_) -> Some contentid) contents); - sth#map (function - | [ Some id; None; Some content ] -> - id, (None, content) - | [ Some id; Some sectionname; Some content ] -> - id, (Some sectionname, content) - | _ -> assert false) + let rows = + let contentids = + List.map (fun (contentid, _,_,_,_) -> contentid) contents in + PGSQL(dbh) + "select id, sectionname, content from contents + where id in $@contentids" in + List.map (fun (id, sectionname, content) -> + id, (sectionname, content)) rows ) in (* Generate the final tables. *) @@ -146,7 +160,7 @@ let run r (q : cgi) dbh hostid host user = List.map (fun (url, title, version, last_modified) -> let have_version, version = match version with - None -> false, 0 + None -> false, 0l | Some version -> true, version in let last_modified = printable_date last_modified in [ "url", Template.VarString url; @@ -162,7 +176,7 @@ let run r (q : cgi) dbh hostid host user = (fun (contentid, url, title, version, last_modified) -> let have_version, version = match version with - None -> false, 0 + None -> false, 0l | Some version -> true, version in let sectionname, content = List.assoc contentid content_map in let have_sectionname, sectionname = @@ -172,7 +186,7 @@ let run r (q : cgi) dbh hostid host user = let content = truncate 160 (Wikilib.text_of_xhtml - (Wikilib.xhtml_of_content dbh hostid content)) in + (Wikilib.xhtml_of_content r dbh hostid content)) in let linkname = linkname_of_sectionname sectionname in let last_modified = printable_date last_modified in [ "url", Template.VarString url;