/_sitemap.rss for COCANWIKI.
[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.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 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_l
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 rows =
40     PGSQL(dbh) "nullable-results"
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 = $hostid
45         order by p.last_modified_date desc
46         limit $limit" in
47
48   let table =
49     List.map
50       (function
51          | (Some version, Some url, _, Some title,
52             Some last_modified_date, logged_ip, logged_user) ->
53              let date = printable_date_time last_modified_date in
54              let has_logged_ip, logged_ip =
55                match logged_ip with
56                    None -> false, ""
57                  | Some ip -> true, ip in
58              let has_logged_user, logged_user =
59                match logged_user with
60                    None -> false, ""
61                  | Some name -> true, name in
62              [ "version", Template.VarString (Int32.to_string version);
63                "url", Template.VarString url;
64                "title", Template.VarString title;
65                "last_modified_date", Template.VarString date;
66                "has_logged_ip", Template.VarConditional has_logged_ip;
67                "logged_ip", Template.VarString logged_ip;
68                "has_logged_user", Template.VarConditional has_logged_user;
69                "logged_user", Template.VarString logged_user;
70                "is_live", Template.VarConditional true ]
71          | (Some version, None, Some url, Some title,
72             Some last_modified_date, logged_ip, logged_user) ->
73              let date = printable_date_time last_modified_date in
74              let has_logged_ip, logged_ip =
75                match logged_ip with
76                    None -> false, ""
77                  | Some ip -> true, ip in
78              let has_logged_user, logged_user =
79                match logged_user with
80                    None -> false, ""
81                  | Some name -> true, name in
82              [ "version", Template.VarString (Int32.to_string version);
83                "url", Template.VarString url;
84                "title", Template.VarString title;
85                "last_modified_date", Template.VarString date;
86                "has_logged_ip", Template.VarConditional has_logged_ip;
87                "logged_ip", Template.VarString logged_ip;
88                "has_logged_user", Template.VarConditional has_logged_user;
89                "logged_user", Template.VarString logged_user;
90                "is_live", Template.VarConditional false ]
91          | _ -> assert false) rows in
92
93   template#table "recent_changes" table;
94
95   q#template ~content_type:"application/rss+xml" template
96
97 let () =
98   register_script ~restrict:[CanEdit] run