Fixed some problems found in testing. Now appears to be working fully.
[cocanwiki.git] / scripts / page_rss.ml
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: page_rss.ml,v 1.4 2006/03/28 16:24:08 rich Exp $
5  *
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.
10  *
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.
15  *
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.
20  *
21  *---
22  *
23  * This generates an RSS feed from a page.  Articles in the RSS feed are
24  * mapped to sections in the page.  At the moment it's fairly crude, but
25  * it's good enough for blogging at the moment.  We can think about
26  * cleaning it up later.
27  *)
28
29 open Apache
30 open Registry
31 open Cgi
32 open Printf
33
34 open Cocanwiki
35 open Cocanwiki_template
36
37 let run r (q : cgi) dbh hostid {hostname = hostname} _ =
38   let template = get_template dbh hostid "page_rss.xml" in
39
40   let page = q#param "page" in
41   let page = if page = "" then "index" else page in
42   template#set "page" page;
43
44   template#set "hostname" hostname;
45
46   (* Get the title and description of the page. *)
47   let rows = PGSQL(dbh)
48     "select id, title, description from pages
49       where hostid = $hostid and url = $page and redirect is null" in
50
51   let pageid, title, description =
52     match rows with
53     | [row] -> row
54     | _ -> assert false in
55
56   template#set "title" title;
57   template#set "description" description;
58
59   (* Get the sections in the live page. *)
60   let rows = PGSQL(dbh)
61     "select sectionname, content, ordering
62        from contents
63       where pageid = $pageid
64         and sectionname is not null
65       order by ordering" in
66
67   let sections =
68     List.map (fun (sectionname, content, _) -> sectionname, content) rows in
69
70   let sections =
71     List.map (fun (sectionname, content) ->
72                 let sectionname = match sectionname with
73                   | None -> "" | Some s -> s in
74                 let content = Wikilib.xhtml_of_content dbh hostid content in
75                 let linkname = linkname_of_sectionname sectionname in
76                 [ "sectionname", Template.VarString sectionname;
77                   "linkname", Template.VarString linkname;
78                   "content", Template.VarString content ]
79              ) sections in
80
81   template#table "sections" sections;
82
83   q#template ~content_type:"application/rss+xml" template
84
85 let () =
86   register_script ~restrict:[CanView] run