(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: source.ml,v 1.7 2006/08/17 09:11:31 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 ExtList 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 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" dbh hostid 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 title = List.hd ( let model_id = model.id in PGSQL(dbh) "select title from pages where hostid = $hostid and id = $model_id" ) in (* Function to write out fields, with RFC822-like escaping. *) let write key value = ignore (print_string r key); ignore (print_string r ": "); ignore (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" (Int32.to_string model.id); write "Title" title; write "Description" model.description; (match model.redirect with | Some redirect -> write "Redirect" redirect | None -> write "Section-Count" (string_of_int (List.length model.contents_)) ); ignore (print_newline r); (* Now write out the sections. *) if model.redirect = None then List.iteri (fun i (sectionname, divname, divclass, jsgo, content) -> write "Section-Id" (string_of_int i); (match sectionname with None -> () | Some sectionname -> write "Section-Header" sectionname); (match divname with None -> () | Some divname -> write "Css-Id" divname); (match divclass with None -> () | Some divclass -> write "Css-Class" divclass); (match jsgo with None -> () | Some jsgo -> write "Javascript-Onclick" jsgo); write "Content" content; ignore (print_newline r)) model.contents_ let () = register_script ~restrict:[CanView] run