e9424fb1efd5737dcae01135eb6e58f70c4f3e9c
[cocanwiki.git] / scripts / recent_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: recent_rss.ml,v 1.3 2006/03/27 18:09:46 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 limit = 30
32
33 let run r (q : cgi) dbh hostid {hostname = hostname} _ =
34   let template = get_template dbh hostid "recent_rss.xml" in
35
36   template#set "hostname" hostname;
37
38   (* Get the changes. *)
39   let sth =
40     dbh#prepare_cached
41       "select p.id, p.url, p.url_deleted, p.title, p.last_modified_date,
42               p.logged_ip, u.name
43          from pages p left outer join users u on p.logged_user = u.id
44         where p.hostid = ?
45         order by p.last_modified_date desc
46         limit ?" in
47   sth#execute [Some hostid; Some limit];
48
49   let table =
50     sth#map
51       (function
52          | [Some version; Some url; _; Some 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                    None -> false, ""
58                  | Some ip -> true, ip
59                  | _ -> assert false in
60              let has_logged_user, logged_user =
61                match logged_user with
62                    None -> false, ""
63                  | Some name -> true, name
64                  | _ -> assert false in
65              [ "version", Template.VarString (Int32.to_string 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          | [Some version; None; Some url; Some 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                    None -> false, ""
80                  | Some ip -> true, ip
81                  | _ -> assert false in
82              let has_logged_user, logged_user =
83                match logged_user with
84                    None -> false, ""
85                  | Some name -> true, name
86                  | _ -> assert false in
87              [ "version", Template.VarString (Int32.to_string 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 "recent_changes" table;
99
100   q#template ~content_type:"application/rss+xml" template
101
102 let () =
103   register_script ~restrict:[CanEdit] run