1 (* COCANWIKI - a wiki written in Objective CAML.
2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: source.ml,v 1.2 2004/11/02 18:47:54 rich Exp $
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
31 let rex = Pcre.regexp "\r?\n"
32 let itempl = Pcre.subst "\r\n\t"
34 (* This is a very simple script which just returns the source of a page
35 * in a format which is easily machine-parsable.
37 let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
38 let url = q#param "page" in
39 let url = if url = "" then "index" else url in
42 try load_page dbh hostid ~url ()
45 error ~title:"Page not found"
46 q "That page was not found";
49 (* XXX CSS - eventually both title and CSS fields should be returned in
50 * the Cocanwiki_pages.model_t structure.
54 let sth = dbh#prepare_cached "select title from pages
55 where hostid = ? and id = ?" in
56 sth#execute [`Int hostid; `Int model.id];
57 let title = sth#fetch1string () in
59 (* Function to write out fields, with RFC822-like escaping. *)
63 print_string r (Pcre.replace ~rex ~itempl value);
64 ignore (print_newline r);
68 q#header ~content_type:"text/plain" ();
70 (* Write out the standard fields. *)
71 write "Version" (string_of_int model.id);
73 write "Description" model.description;
74 if model.redirect <> "" then
75 write "Redirect" model.redirect
77 write "Section-Count" (string_of_int (List.length model.contents));
78 ignore (print_newline r);
80 (* Now write out the sections. *)
81 if model.redirect = "" then
83 (fun (sectionname, divname, content) ->
84 write "Section-Header" sectionname;
85 write "Css-Id" divname;
86 write "Content" content;
87 ignore (print_newline r)) model.contents
90 register_script ~restrict:[CanView] run