Serial columns now 64 bits.
[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.2 2004/11/02 11:05:59 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 : Dbi.connection) 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 sth = dbh#prepare_cached "select id, title, description
48                                   from pages
49                                  where hostid = ? and url = ?
50                                    and redirect is null" in
51   sth#execute [`Int hostid; `String page];
52
53   let pageid, title, description =
54     match sth#fetch1 () with
55         [ `Int id; `String title; `String description ] ->
56           id, title, description
57       | _ -> assert false in
58
59   template#set "title" title;
60   template#set "description" description;
61
62   (* Get the sections in the live page. *)
63   let sth = dbh#prepare_cached "select sectionname, content, ordering
64                                   from contents
65                                  where pageid = ?
66                                    and sectionname is not null
67                                  order by ordering" in
68   sth#execute [`Int pageid];
69
70   let sections =
71     sth#map (function [`String sectionname; `String content; _] ->
72                sectionname, content
73                | _ -> assert false) in
74
75   let sections =
76     List.map (fun (sectionname, content) ->
77                 let content = Wikilib.xhtml_of_content dbh hostid content in
78                 let linkname = linkname_of_sectionname sectionname in
79                 [ "sectionname", Template.VarString sectionname;
80                   "linkname", Template.VarString linkname;
81                   "content", Template.VarString content ]
82              ) sections in
83
84   template#table "sections" sections;
85
86   q#template ~content_type:"application/rss+xml" template
87
88 let () =
89   register_script ~restrict:[CanView] run