+csv dep for PG'OCaml.
[cocanwiki.git] / scripts / lib / cocanwiki_template.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_template.ml,v 1.12 2006/12/06 09:46:57 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  * This module wraps around the Template library.  It provides caching
22  * of templates and fills in standard fields on a host-specific basis.
23  *
24  *)
25
26 open Unix
27
28 open ExtString
29
30 open Apache
31
32 open Cocanwiki_files
33 open Cocanwiki_strings
34
35 (* This is used to generate the id fields from URLs on the site menu. *)
36 let id_of_url str =
37   let buf = UTF8.Buf.create (String.length str) in
38   UTF8.iter (
39     fun c ->
40       if iswebsafe c then UTF8.Buf.add_char buf c
41       else UTF8.Buf.add_char buf (UChar.of_char '_')
42   ) str;
43   UTF8.Buf.contents buf
44
45 let base =
46   let base =
47     try Sys.getenv "COCANWIKI_TEMPLATES"
48     with Not_found -> "/usr/share/cocanwiki/templates" in
49   let is_dir path =
50     try (Unix.stat path).Unix.st_kind = Unix.S_DIR
51     with Unix.Unix_error _ -> false in
52   if not (is_dir base) then
53     failwith ("environment variable $COCANWIKI_TEMPLATES " ^
54               "must be set to point to my 'templates' directory " ^
55               "(see README file for more details)");
56   base
57
58 (* The webserver gets restarted regularly enough that this is reasonable. *)
59 let { tm_year = year } = gmtime (time ())
60 let year = year + 1900
61
62 (* Sniff for MSIE of a particular version. *)
63 let is_msie_version user_agent version =
64   try
65     let index = String.find user_agent "MSIE " in
66     let index = index + 5 in
67     String.length user_agent > index && user_agent.[index] = version
68   with
69     Invalid_string -> false
70
71 (* Cache of precompiled templates, arranged by full path. *)
72 let cache = Hashtbl.create 32
73
74 let _get_template filename =
75   let path = base // filename in
76   let stat = Unix.stat path in
77   let mtime = stat.st_mtime in
78
79   try
80     let template, old_mtime = Hashtbl.find cache path in
81     if old_mtime < mtime then (
82       (* The template has changed on disk since it was compiled.  Reload. *)
83       let template = Template.template path in
84       Hashtbl.replace cache path (template, mtime);
85       template
86     ) else
87       template
88   with
89       Not_found ->
90         (* Template not seen before, so load it. *)
91         let template = Template.template path in
92         Hashtbl.replace cache path (template, mtime);
93         template
94
95 let get_template ?page r dbh hostid filename =
96   let template = _get_template filename in
97
98   if hostid > 0l then (
99     (* Get standard fields concerning this host from the database. *)
100     let rows =
101       PGSQL(dbh) "nullable-results"
102         "select h.theme_css, h.css is not null,
103                 h.ie6_fixes_css is not null, h.ie7_fixes_css is not null,
104                 p.name, p.url, h.search_box,
105                 h.brand, h.brand_tagline, h.brand_description,
106                 h.pagebug, h.ie_imagetoolbar_no, h.global_noodp
107            from hosts h left outer join powered_by p on h.powered_by = p.id
108           where h.id = $hostid" in
109
110     let theme_css, has_host_css, has_ie6_fixes_css, has_ie7_fixes_css,
111       powered_by_name, powered_by_url, search_box,
112       brand, brand_tagline, brand_description, pagebug, ie_imagetoolbar_no,
113       global_noodp =
114       match rows with
115       | [ row ] -> row
116       | _ -> assert false in
117
118     let theme_css =
119       match theme_css with
120       | None -> "/_css/standard.css"
121       | Some file -> file in
122
123     let has_host_css =
124       match has_host_css with
125       | Some true -> true
126       | _ -> false in
127
128     (* Send the IE6/7 fixes header only to browsers claiming to be MSIE.
129      * A browser such as Opera which claims to be MSIE will see the header
130      * but ignore it because it is surrounded by comment code to defend
131      * against browsers which aren't really MSIE.
132      *)
133     let ua = lazy (
134       try Table.get (Request.headers_in r) "User-Agent"
135       with Not_found -> ""
136     ) in
137
138     let has_ie6_fixes_css =
139       has_ie6_fixes_css = Some true && is_msie_version (Lazy.force ua) '6' in
140
141     let has_ie7_fixes_css =
142       has_ie7_fixes_css = Some true && is_msie_version (Lazy.force ua) '7' in
143
144     let powered_by_name, powered_by_url =
145       match powered_by_name, powered_by_url with
146       | None, None ->
147           let url = "http://sandbox.merjis.com/" in
148           let name = Cocanwiki_version.package ^ " " ^
149                      Cocanwiki_version.version in
150           name, url
151       | Some name, Some url -> name, url
152       | _ -> assert false in
153
154     let branding, brand,
155       has_brand_tagline, brand_tagline,
156       has_brand_description, brand_description =
157       match brand with
158       | None -> false, "", false, "", false, ""
159       | Some brand ->
160           let has_brand_tagline, brand_tagline =
161             match brand_tagline with
162             | None -> false, ""
163             | Some s -> true, s in
164           let has_brand_description, brand_description =
165             match brand_description with
166             | None -> false, ""
167             | Some s -> true, s in
168           true, brand,
169           has_brand_tagline, brand_tagline,
170           has_brand_description, brand_description in
171
172     let has_pagebug, pagebug =
173       match pagebug with
174       | None -> false, ""
175       | Some pagebug -> true, pagebug in
176
177     let search_box = match search_box with Some b -> b | _ -> assert false in
178
179     let ie_imagetoolbar_no =
180       match ie_imagetoolbar_no with Some b -> b | _ -> assert false in
181
182     let global_noodp =
183       match global_noodp with Some b -> b | _ -> assert false in
184
185     template#set "theme_css" theme_css;
186     template#conditional "has_host_css" has_host_css;
187     template#conditional "has_ie6_fixes_css" has_ie6_fixes_css;
188     template#conditional "has_ie7_fixes_css" has_ie7_fixes_css;
189     template#set "powered_by_name" powered_by_name;
190     template#set "powered_by_url" powered_by_url;
191     template#conditional "search_box" search_box;
192     template#conditional "branding" branding;
193     template#set "brand" brand;
194     template#conditional "has_brand_tagline" has_brand_tagline;
195     template#set "brand_tagline" brand_tagline;
196     template#conditional "has_brand_description" has_brand_description;
197     template#set "brand_description" brand_description;
198     template#conditional "has_pagebug" has_pagebug;
199     template#set "pagebug" pagebug;
200     template#conditional "ie_imagetoolbar_no" ie_imagetoolbar_no;
201     template#conditional "noodp" global_noodp;
202
203     (* Site menu. *)
204     let rows = PGSQL(dbh)
205       "select url, label, ordering from sitemenu
206         where hostid = $hostid order by ordering" in
207
208     let is_homepage =
209       match page with
210       | None -> false
211       | Some "index" -> true
212       | _ -> false in
213     template#conditional "is_homepage" is_homepage;
214
215     let table = List.map
216       (fun (url, label, _) ->
217          let is_linked =
218            match page with
219            | None -> true
220            | Some page when page = url -> false
221            | _ -> true in
222          let id = id_of_url url in
223          [ "url", Template.VarString url;
224            "label", Template.VarString label;
225            "is_linked", Template.VarConditional is_linked;
226            "id", Template.VarString id ]
227       ) rows in
228
229     template#table "sitemenu" table;
230   )
231   else (* if we have no hostid *) (
232     template#set "theme_css" "/_css/standard.css";
233     template#conditional "has_host_css" false;
234     template#conditional "has_ie6_fixes_css" false;
235     template#conditional "has_ie7_fixes_css" false;
236     template#set "powered_by_name" (Cocanwiki_version.package ^ " " ^
237                                     Cocanwiki_version.version);
238     template#set "powered_by_url" "http://sandbox.merjis.com/";
239     template#conditional "search_box" false;
240     template#conditional "branding" false;
241     template#set "brand" "";
242     template#conditional "has_brand_tagline" false;
243     template#set "brand_tagline" "";
244     template#conditional "has_brand_description" false;
245     template#set "brand_description" "";
246     template#conditional "has_pagebug" false;
247     template#set "pagebug" "";
248     template#conditional "ie_imagetoolbar_no" false;
249     template#conditional "noodp" false;
250     template#conditional "is_homepage" false;
251     template#table "sitemenu" [];
252   );
253   (* Copyright year. *)
254   template#set "year" (string_of_int year);
255
256   template