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 $
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.
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.
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.
21 * This module wraps around the Template library. It provides caching
22 * of templates and fills in standard fields on a host-specific basis.
33 open Cocanwiki_strings
35 (* This is used to generate the id fields from URLs on the site menu. *)
37 let buf = UTF8.Buf.create (String.length str) in
40 if iswebsafe c then UTF8.Buf.add_char buf c
41 else UTF8.Buf.add_char buf (UChar.of_char '_')
47 try Sys.getenv "COCANWIKI_TEMPLATES"
48 with Not_found -> "/usr/share/cocanwiki/templates" in
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)");
58 (* The webserver gets restarted regularly enough that this is reasonable. *)
59 let { tm_year = year } = gmtime (time ())
60 let year = year + 1900
62 (* Sniff for MSIE of a particular version. *)
63 let is_msie_version user_agent version =
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
69 Invalid_string -> false
71 (* Cache of precompiled templates, arranged by full path. *)
72 let cache = Hashtbl.create 32
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
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);
90 (* Template not seen before, so load it. *)
91 let template = Template.template path in
92 Hashtbl.replace cache path (template, mtime);
95 let get_template ?page r dbh hostid filename =
96 let template = _get_template filename in
99 (* Get standard fields concerning this host from the database. *)
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
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,
116 | _ -> assert false in
120 | None -> "/_css/standard.css"
121 | Some file -> file in
124 match has_host_css with
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.
134 try Table.get (Request.headers_in r) "User-Agent"
138 let has_ie6_fixes_css =
139 has_ie6_fixes_css = Some true && is_msie_version (Lazy.force ua) '6' in
141 let has_ie7_fixes_css =
142 has_ie7_fixes_css = Some true && is_msie_version (Lazy.force ua) '7' in
144 let powered_by_name, powered_by_url =
145 match powered_by_name, powered_by_url with
147 let url = "http://sandbox.merjis.com/" in
148 let name = Cocanwiki_version.package ^ " " ^
149 Cocanwiki_version.version in
151 | Some name, Some url -> name, url
152 | _ -> assert false in
155 has_brand_tagline, brand_tagline,
156 has_brand_description, brand_description =
158 | None -> false, "", false, "", false, ""
160 let has_brand_tagline, brand_tagline =
161 match brand_tagline with
163 | Some s -> true, s in
164 let has_brand_description, brand_description =
165 match brand_description with
167 | Some s -> true, s in
169 has_brand_tagline, brand_tagline,
170 has_brand_description, brand_description in
172 let has_pagebug, pagebug =
175 | Some pagebug -> true, pagebug in
177 let search_box = match search_box with Some b -> b | _ -> assert false in
179 let ie_imagetoolbar_no =
180 match ie_imagetoolbar_no with Some b -> b | _ -> assert false in
183 match global_noodp with Some b -> b | _ -> assert false in
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;
204 let rows = PGSQL(dbh)
205 "select url, label, ordering from sitemenu
206 where hostid = $hostid order by ordering" in
211 | Some "index" -> true
213 template#conditional "is_homepage" is_homepage;
216 (fun (url, label, _) ->
220 | Some page when page = url -> false
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 ]
229 template#table "sitemenu" table;
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" [];
253 (* Copyright year. *)
254 template#set "year" (string_of_int year);