X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;ds=sidebyside;f=scripts%2Flib%2Fwikilib.ml;h=557d7a95162ddab10b862d2ecf0c51a5bafa0832;hb=d303f75eed3a09bbe2516d9a2a9a4aa9b862ceb3;hp=20160093b2d3bfa5fa7857ee053581e1722735ec;hpb=fb1c781bdbe4a2b296651a051dda11d210c9ad33;p=cocanwiki.git diff --git a/scripts/lib/wikilib.ml b/scripts/lib/wikilib.ml index 2016009..557d7a9 100644 --- a/scripts/lib/wikilib.ml +++ b/scripts/lib/wikilib.ml @@ -1,7 +1,7 @@ (* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. - * $Id: wikilib.ml,v 1.4 2005/11/11 09:39:21 rich Exp $ + * $Id: wikilib.ml,v 1.5 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 @@ -40,7 +40,7 @@ type genurl_error_t = GenURL_OK of string let nontrivial_re = Pcre.regexp ~flags:[`CASELESS] "[a-z0-9]" -let generate_url_of_title (dbh : Dbi.connection) hostid title = +let generate_url_of_title dbh hostid title = (* Create a suitable URL from this title. *) let url = String.map (function @@ -63,16 +63,12 @@ let generate_url_of_title (dbh : Dbi.connection) hostid title = * then it probably means that another page exists with similar enough * content, so we should redirect to there instead. *) - let sth = dbh#prepare_cached "select 1 from pages - where hostid = ? and url = ?" in - sth#execute [`Int hostid; `String url]; - - try - sth#fetch1int (); - GenURL_Duplicate url - with - Not_found -> - GenURL_OK url + let rows = PGSQL(dbh) "select 1 from pages + where hostid = $hostid and url = $url" in + match rows with + | [Some 1l] -> GenURL_Duplicate url + | [] -> GenURL_OK url + | _ -> assert false ) (* Obscure a mailto: URL against spammers. *) @@ -119,8 +115,8 @@ let markup_link dbh hostid link = let url = Pcre.get_substring subs 1 in let tag name = function - `Null -> "" - | `String v -> " " ^ name ^ "=\"" ^ escape_html_tag v ^ "\"" + | None -> "" + | Some v -> " " ^ name ^ "=\"" ^ escape_html_tag v ^ "\"" in if Pcre.pmatch ~rex:image_re url then ( @@ -129,83 +125,91 @@ let markup_link dbh hostid link = let is_thumb = (Pcre.get_substring subs 1).[0] = 't' in let name = Pcre.get_substring subs 2 in - let sql = "select id, " ^ - (if is_thumb then "tn_width, tn_height" - else "width, height") ^ - ", alt, title, longdesc, class - from images - where hostid = ? and name = ?" in - let sth = dbh#prepare_cached sql in - sth#execute [`Int hostid; `String name]; + let rows = + PGSQL(dbh) + "select id, width, height, tn_width, tn_height, + alt, title, longdesc, class + from images + where hostid = $hostid and name = $name" in + + match is_thumb, rows with + (* [[image:...]] *) + | false, [imageid, width, height, _, _, alt, title, longdesc, clasz] + (* [[thumb:...]], but no thumbnail in the database - treat as image *) + | true, [imageid, width, height, None, None, + alt, title, longdesc, clasz] -> + let link = "/_image/" ^ escape_url name in + + "\""" + + (* [[thumb:...]] *) + | true, [imageid, _, _, Some tn_width, Some tn_height, + alt, title, longdesc, clasz] -> + let link = "/_image/" ^ escape_url name in + "" ^ + "\""" ^ + "" - try - let imageid, width, height, alt, title, longdesc, clasz = - match sth#fetch1 () with - [`Int imageid; `Int width; `Int height; `String alt; - (`Null | `String _) as title; - (`Null | `String _) as longdesc; - (`Null | `String _) as clasz] -> - imageid, width, height, alt, title, longdesc, clasz - | _ -> assert false in - - let link = "/_image/" ^ escape_url name in - - (if is_thumb then "" else "") ^ - "\""" ^ - (if is_thumb then "" else "") - with - Not_found -> - (* Image not found. *) - " + "" ^ escape_html name ^ "" + + (* image name is unique, so this shouldn't happen *) + | _, _ -> assert false + ) else if Pcre.pmatch ~rex:file_re url then ( (* It may be a file. *) let subs = Pcre.exec ~rex:file_re url in let name = Pcre.get_substring subs 1 in - let sth = dbh#prepare_cached "select title - from files - where hostid = ? and name = ?" in - sth#execute [`Int hostid; `String name]; - - try - let title = - match sth#fetch1 () with - [(`Null | `String _) as title] -> title - | _ -> assert false in - - "" ^ - escape_html name ^ - "" - with - Not_found -> - (* File not found. *) - " + "" ^ + escape_html name ^ + "" + | [] -> + (* File not found. *) + "" ^ escape_html name ^ "" + | _ -> assert false ) else ( (* Pcre changed behaviour between versions. Previously a non-capture * would return "". Now it throws 'Not_found'. @@ -231,37 +235,36 @@ let markup_link dbh hostid link = (* Look up the 'URL' against the titles in the database and * obtain the real URL. *) - let sth = dbh#prepare_cached "select url from pages - where hostid = ? and url is not null - and lower (title) = lower (?)" in - sth#execute [`Int hostid; `String url]; - - try - let url = sth#fetch1string () in - "/" ^ url, "internal", title - with - Not_found -> - (* It might be a template page ... These pages don't - * exist in the template, but can be synthesized on the - * fly by page.ml. + let rows = PGSQL(dbh) + "select url from pages + where hostid = $hostid and url is not null + and lower (title) = lower ($url)" in + + match rows with + | [Some url] -> + "/" ^ url, "internal", title + | [] -> + (* It might be a template page ... These pages don't + * exist in the template, but can be synthesized on the + * fly by page.ml. + *) + let is_template_page url = + [] <> PGSQL(dbh) + "select 1 from templates + where $url ~ url_regexp + order by ordering + limit 1" + in + + if is_template_page url then + "/" ^ url, "internal", title + else + (* No, it really doesn't exist, so make it a link to + * a new page. *) - let is_template_page url = - let sth = dbh#prepare_cached "select 1 from templates - where ? ~ url_regexp - order by ordering - limit 1" in - sth#execute [`String url]; - - try sth#fetch1int () = 1 with Not_found -> false - in - - if is_template_page url then - "/" ^ url, "internal", title - else - (* No, it really doesn't exist, so make it a link to - * a new page. - *) "/_bin/edit.cmo?title=" ^ escape_url url, "newpage", title + + | _ -> assert false ) in "\\s*$" let html_close_re = Pcre.regexp "^\\s*$" -let xhtml_of_content (dbh : Dbi.connection) hostid text = +let xhtml_of_content dbh hostid text = (* Split the text into lines. *) let lines = Pcre.split ~rex:split_lines_re text in