About half way through switching cocanwiki to using the new PG interface.
[cocanwiki.git] / scripts / lib / cocanwiki_ext_calendar.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: cocanwiki_ext_calendar.ml,v 1.3 2006/03/27 16:43:44 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 ExtList
28
29 open Cocanwiki
30 open Cocanwiki_template
31 open Cocanwiki_strings
32 open Cocanwiki_date
33
34 let day_template = _get_template "calendar_day.html"
35 let month_template = _get_template "calendar_month.html"
36 let year_template = _get_template "calendar_year.html"
37 let year_1m_template = _get_template "calendar_year_1m.html"
38
39 (* Check a date is valid. *)
40 let check_date (y, m, d) =
41   try
42     let t = Date.make y m d in
43     Date.year t = y
44         && Date.int_of_month (Date.month t) = m
45         && Date.day_of_month t = d
46   with
47       Date.Out_of_bounds | Date.Undefined -> false
48
49 let rec range a b =
50   if a <= b then
51     a :: range (a+1) b
52   else
53     []
54
55 let extension dbh hostid url =
56   (* Validate a date in the form "yyyy[/mm[/dd]]".  Returns a (yyyy, mm, dd)
57    * tuple with missing fields set to 0.  If the string doesn't parse or the
58    * date isn't valid, then raises Not_found.
59    *)
60   let valid_date str =
61     if String.length str = 4 &&
62       isdigit str.[0] && isdigit str.[1] &&
63       isdigit str.[2] && isdigit str.[3] then (
64         let yyyy = int_of_string (String.sub str 0 4) in
65         (yyyy, 0, 0)
66       )
67     else if String.length str = 7 &&
68       isdigit str.[0] && isdigit str.[1] &&
69       isdigit str.[2] && isdigit str.[3] &&
70       str.[4] = '/' &&
71       isdigit str.[5] && isdigit str.[6] then (
72         let yyyy = int_of_string (String.sub str 0 4) in
73         let mm = int_of_string (String.sub str 5 2) in
74         if mm >= 1 && mm <= 12 then (yyyy, mm, 0) else raise Not_found
75       )
76     else if String.length str = 10 &&
77       isdigit str.[0] && isdigit str.[1] &&
78       isdigit str.[2] && isdigit str.[3] &&
79       str.[4] = '/' &&
80       isdigit str.[5] && isdigit str.[6] &&
81       str.[7] = '/' &&
82       isdigit str.[8] && isdigit str.[9] then (
83         let yyyy = int_of_string (String.sub str 0 4) in
84         let mm = int_of_string (String.sub str 5 2) in
85         let dd = int_of_string (String.sub str 8 2) in
86         let date = (yyyy, mm, dd) in
87         if check_date date then date else raise Not_found
88       )
89     else
90       raise Not_found
91   in
92
93   (* From the links table, find all links to this page, or sub-calendar pages.
94    * This query overselects.  We then filter the real pages in OCaml.
95    *)
96   let rows =
97     let patt = url ^ "%" in
98     PGSQL(dbh)
99       "select li.from_url, p.title, li.to_url
100          from links li, pages p
101         where li.hostid = $hostid and li.to_url like $patt
102           and li.hostid = p.hostid and li.from_url = p.url" in
103
104   let pages =
105     let results =
106       List.map (fun (from_url, title, to_url) ->
107                   from_url, title, to_url) rows in
108     List.filter_map
109       (fun (from_url, title, to_url) ->
110          try let date = valid_date to_url in Some (date, (title, from_url))
111          with Not_found -> None) results in
112   let pages = List.sort pages in
113
114   (* Validate the date in the URL itself. *)
115   let date =
116     try Some (valid_date url)
117     with Not_found -> None in
118
119   match date with
120     | None ->                           (* Not a valid date. *)
121         "<p>" ^ url ^ " is not an actual date.</p>"
122     | Some (yyyy, 0, 0) ->              (* Year view. *)
123         let template = year_template in
124
125         template#set "yyyy" (string_of_int yyyy);
126         template#set "prev_yyyy" (string_of_int (yyyy - 1));
127         template#set "next_yyyy" (string_of_int (yyyy + 1));
128
129         (* Return true if there are any events on a particular day. *)
130         let has_events date = List.exists (fun (d, _) -> date = d) pages in
131
132         let int_of_day_of_week = function
133           | Date.Sun -> 0 | Date.Mon -> 1 | Date.Tue -> 2
134           | Date.Wed -> 3 | Date.Thu -> 4 | Date.Fri -> 5
135           | Date.Sat -> 6
136         in
137
138         (* Generate each month template separately ...
139          * Wow, finally found a place I can use a for loop.
140          *)
141         for mm = 1 to 12 do
142           let str =
143             let template = year_1m_template in
144             template#set "yyyy" (string_of_int yyyy);
145             template#set "mm" (sprintf "%02d" mm);
146             template#set "month_name"
147               (!Printer.month_name (Date.month_of_int mm));
148             let dow = Date.day_of_week (Date.make yyyy mm 1) in
149             let max_dd = Date.days_in_month (Date.make yyyy mm 1) in
150             let dd = ref (1 - int_of_day_of_week dow) in
151             let rows = ref [] in
152             for r = 0 to 5 do           (* up to 5 rows ... *)
153               let cols = ref [] in
154               for c = 0 to 6 do         (* 7 columns, Sunday - Saturday *)
155                 let is_day = !dd >= 1 && !dd <= max_dd in
156                 let clasz =
157                   if is_day then (
158                     let is_weekend =
159                       match Date.day_of_week (Date.make yyyy mm !dd) with
160                           Date.Sat | Date.Sun -> true | _ -> false in
161                     let date = yyyy, mm, !dd in
162                     let events = has_events date in
163                     (if is_weekend then "cal_year_1m_weekend " else "") ^
164                     (if events then "cal_year_1m_events" else "")
165                   ) else
166                     "cal_year_1m_empty" in
167                 let col =
168                   [ "is_day", Template.VarConditional is_day;
169                     "dd", Template.VarString (sprintf "%02d" !dd);
170                     "class", Template.VarString clasz ] in
171                 cols := col :: !cols;
172                 incr dd
173               done;
174               rows := [ "cols", Template.VarTable (List.rev !cols) ] :: !rows;
175               cols := []
176             done;
177
178             template#table "rows" (List.rev !rows);
179
180             template#to_string in
181           template#set ("month" ^ string_of_int mm) str
182         done;
183
184         (* Annual events. *)
185         let events =
186           List.filter (function ((_, 0, 0), _) -> true | _ -> false) pages in
187
188         let table =
189           List.map (fun (_, (title, page)) ->
190                       [ "title", Template.VarString title;
191                         "page", Template.VarString page ]) events in
192         template#table "events" table;
193
194         template#to_string
195
196     | Some (yyyy, mm, 0) ->             (* Month view. *)
197         let template = month_template in
198
199         template#set "month_name" (!Printer.month_name (Date.month_of_int mm));
200         template#set "yyyy" (string_of_int yyyy);
201         template#set "mm" (sprintf "%02d" mm);
202
203         let prev_yyyy, prev_mm =
204           if mm = 1 then yyyy - 1, 12
205           else yyyy, mm - 1 in
206         let next_yyyy, next_mm =
207           if mm = 12 then yyyy + 1, 1
208           else yyyy, mm + 1 in
209         template#set "prev_yyyy" (string_of_int prev_yyyy);
210         template#set "prev_mm" (sprintf "%02d" prev_mm);
211         template#set "next_yyyy" (string_of_int next_yyyy);
212         template#set "next_mm" (sprintf "%02d" next_mm);
213
214         (* Get all monthly events and all daily events. *)
215         let monthly_events, daily_events =
216           List.partition (function ((_, _, 0), _) -> true | _ -> false)
217             pages in
218
219         (* Table of monthly events. *)
220         let table =
221           List.map (fun (_, (title, page)) ->
222                       [ "title", Template.VarString title;
223                         "page", Template.VarString page ]) monthly_events in
224         template#table "monthly_events" table;
225
226         (* How many days in this month? *)
227         let max_dd = Date.days_in_month (Date.make yyyy mm 1) in
228         let days = range 1 max_dd in
229
230         let table =
231           List.map (fun dd ->
232                       let events =
233                         List.filter (fun ((_, _, d), _) -> d = dd)
234                           daily_events in
235                       let table =
236                         List.map (fun (_, (title, page)) ->
237                                     [ "title", Template.VarString title;
238                                       "page", Template.VarString page ])
239                           events in
240                       let is_weekend =
241                         match Date.day_of_week (Date.make yyyy mm dd) with
242                             Date.Sat | Date.Sun -> true | _ -> false in
243                       [ "dd", Template.VarString (sprintf "%02d" dd);
244                         "is_weekend", Template.VarConditional is_weekend;
245                         "events", Template.VarTable table ])
246             days in
247         template#table "days" table;
248
249         template#to_string
250
251     | Some ((yyyy, mm, dd) as date) ->  (* Single day view. *)
252         let template = day_template in
253
254         (* XXX This will change once we start doing date and time events.
255          * For now it is very simple indeed.
256          *)
257         template#set "yyyy" (string_of_int yyyy);
258         template#set "mm" (sprintf "%02d" mm);
259         template#set "dd" (sprintf "%02d" dd);
260
261         template#set "month_name" (!Printer.month_name (Date.month_of_int mm));
262
263         let t = Date.make yyyy mm dd in
264         let dow = Date.day_of_week t in
265         template#set "short_weekday" (Printer.short_name_of_day dow);
266
267         let oneday = Date.Period.day 1 in
268         let prev_t = Date.rem t oneday in
269         let next_t = Date.add t oneday in
270         let prev_yyyy, prev_mm, prev_dd =
271           Date.year prev_t, Date.int_of_month (Date.month prev_t),
272           Date.day_of_month prev_t in
273         let next_yyyy, next_mm, next_dd =
274           Date.year next_t, Date.int_of_month (Date.month next_t),
275           Date.day_of_month next_t in
276
277         template#set "prev_yyyy" (string_of_int prev_yyyy);
278         template#set "prev_mm" (sprintf "%02d" prev_mm);
279         template#set "prev_dd" (sprintf "%02d" prev_dd);
280         template#set "next_yyyy" (string_of_int next_yyyy);
281         template#set "next_mm" (sprintf "%02d" next_mm);
282         template#set "next_dd" (sprintf "%02d" next_dd);
283
284         let table = List.map (fun (_, (title, page)) ->
285                                 [ "title", Template.VarString title;
286                                   "page", Template.VarString page ]) pages in
287         template#table "events" table;
288
289         template#to_string
290
291 (* Register the extension. *)
292 let () =
293   extensions := ("calendar", extension) :: !extensions