(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: source.ml,v 1.1 2004/10/27 21:14:05 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 * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) open Apache open Registry open Cgi open Printf open Cocanwiki open Cocanwiki_pages open Cocanwiki_ok let rex = Pcre.regexp "\r?\n" let itempl = Pcre.subst "\r\n\t" (* This is a very simple script which just returns the source of a page * in a format which is easily machine-parsable. *) let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ = let url = q#param "page" in let url = if url = "" then "index" else url in let model = try load_page dbh hostid ~url () with Not_found -> error ~title:"Page not found" q "That page was not found"; return () in (* XXX CSS - eventually both title and CSS fields should be returned in * the Cocanwiki_pages.model_t structure. *) (* Get the title. *) let sth = dbh#prepare_cached "select title from pages where hostid = ? and id = ?" in sth#execute [`Int hostid; `Int model.id]; let title = sth#fetch1string () in (* Function to write out fields, with RFC822-like escaping. *) let write key value = print_string r key; print_string r ": "; print_string r (Pcre.replace ~rex ~itempl value); ignore (print_newline r); in (* HTTP header. *) q#header ~content_type:"text/plain" (); (* Write out the standard fields. *) write "Version" (string_of_int model.id); write "Title" title; write "Description" model.description; if model.redirect <> "" then write "Redirect" model.redirect else write "Section-Count" (string_of_int (List.length model.contents)); ignore (print_newline r); (* Now write out the sections. *) if model.redirect = "" then List.iter (fun (sectionname, divname, content) -> write "Section-Header" sectionname; write "Css-Id" divname; write "Content" content; ignore (print_newline r)) model.contents let () = register_script run