Commented a bug.
[cocanwiki.git] / scripts / history_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: history_rss.ml,v 1.2 2004/11/02 11:05:58 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 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open Cocanwiki
28 open Cocanwiki_template
29 open Cocanwiki_date
30
31 let run r (q : cgi) (dbh : Dbi.connection) hostid {hostname = hostname} _ =
32   let template = get_template dbh hostid "history_rss.xml" in
33
34   let page = q#param "page" in
35   let page = if page = "" then "index" else page in
36   template#set "page" page;
37
38   template#set "hostname" hostname;
39
40   let sth =
41     dbh#prepare_cached
42       "select p.id, p.url, p.url_deleted, p.title, p.last_modified_date,
43               p.logged_ip, u.name
44          from pages p left outer join users u on p.logged_user = u.id
45         where p.hostid = ? and (p.url = ? or p.url_deleted = ?)
46         order by p.last_modified_date desc" in
47   sth#execute [`Int hostid; `String page; `String page];
48
49   let table =
50     sth#map
51       (function
52          | [`Int version; `String url; _; `String title;
53             `Timestamp last_modified_date; logged_ip; logged_user ] ->
54              let date = printable_date_time last_modified_date in
55              let has_logged_ip, logged_ip =
56                match logged_ip with
57                    `Null -> false, ""
58                  | `String ip -> true, ip
59                  | _ -> assert false in
60              let has_logged_user, logged_user =
61                match logged_user with
62                    `Null -> false, ""
63                  | `String name -> true, name
64                  | _ -> assert false in
65              [ "version", Template.VarString (string_of_int version);
66                "url", Template.VarString url;
67                "title", Template.VarString title;
68                "last_modified_date", Template.VarString date;
69                "has_logged_ip", Template.VarConditional has_logged_ip;
70                "logged_ip", Template.VarString logged_ip;
71                "has_logged_user", Template.VarConditional has_logged_user;
72                "logged_user", Template.VarString logged_user;
73                "is_live", Template.VarConditional true ]
74          | [`Int version; `Null; `String url; `String title;
75             `Timestamp last_modified_date; logged_ip; logged_user ] ->
76              let date = printable_date_time last_modified_date in
77              let has_logged_ip, logged_ip =
78                match logged_ip with
79                    `Null -> false, ""
80                  | `String ip -> true, ip
81                  | _ -> assert false in
82              let has_logged_user, logged_user =
83                match logged_user with
84                    `Null -> false, ""
85                  | `String name -> true, name
86                  | _ -> assert false in
87              [ "version", Template.VarString (string_of_int version);
88                "url", Template.VarString url;
89                "title", Template.VarString title;
90                "last_modified_date", Template.VarString date;
91                "has_logged_ip", Template.VarConditional has_logged_ip;
92                "logged_ip", Template.VarString logged_ip;
93                "has_logged_user", Template.VarConditional has_logged_user;
94                "logged_user", Template.VarString logged_user;
95                "is_live", Template.VarConditional false ]
96          | _ -> assert false) in
97
98   template#table "history" table;
99
100   q#template ~content_type:"application/rss+xml" template
101
102 let () =
103   register_script ~restrict:[CanEdit] run