Completed all user scripts.
[cocanwiki.git] / scripts / recent.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.ml,v 1.8 2004/09/21 13:01:16 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 max_age = "3 months"
32
33 let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
34   let template = get_template dbh hostid "recent.html" in
35
36   let sth =
37     dbh#prepare_cached
38       "select p.id, p.url, p.url_deleted, p.title, p.last_modified_date,
39               p.logged_ip, u.name
40          from pages p left outer join users u on p.logged_user = u.id
41         where p.hostid = ?
42           and p.last_modified_date >= current_timestamp - interval ?
43         order by p.last_modified_date desc" in
44   sth#execute [`Int hostid; `String max_age];
45
46   let table =
47     sth#map
48       (function
49          | [`Int version; `String url; _; `String title;
50             `Timestamp last_modified_date; logged_ip; logged_user] ->
51              let date = printable_date_time last_modified_date in
52              let has_logged_ip, logged_ip =
53                match logged_ip with
54                    `Null -> false, ""
55                  | `String ip -> true, ip
56                  | _ -> assert false in
57              let has_logged_user, logged_user =
58                match logged_user with
59                    `Null -> false, ""
60                  | `String name -> true, name
61                  | _ -> assert false in
62              [ "version", Template.VarString (string_of_int 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          | [`Int version; `Null; `String url; `String title;
72             `Timestamp 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                    `Null -> false, ""
77                  | `String ip -> true, ip
78                  | _ -> assert false in
79              let has_logged_user, logged_user =
80                match logged_user with
81                    `Null -> false, ""
82                  | `String name -> true, name
83                  | _ -> assert false in
84              [ "version", Template.VarString (string_of_int version);
85                "url", Template.VarString url;
86                "title", Template.VarString title;
87                "last_modified_date", Template.VarString date;
88                "has_logged_ip", Template.VarConditional has_logged_ip;
89                "logged_ip", Template.VarString logged_ip;
90                "has_logged_user", Template.VarConditional has_logged_user;
91                "logged_user", Template.VarString logged_user;
92                "is_live", Template.VarConditional false ]
93          | _ -> assert false) in
94
95   template#table "recent_changes" table;
96
97   q#template template
98
99 let () =
100   register_script ~restrict:[CanEdit] run