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.7 2006/03/28 18:40:50 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.
29 open Cocanwiki_strings
31 (* This is used to generate the id fields from URLs on the site menu. *)
33 let str = String.copy str in
34 for i = 0 to String.length str - 1 do
35 if not (isalnum str.[i]) then str.[i] <- '_'
41 try Sys.getenv "COCANWIKI_TEMPLATES"
42 with Not_found -> "/usr/share/cocanwiki/templates" in
44 try (Unix.stat path).Unix.st_kind = Unix.S_DIR
45 with Unix.Unix_error _ -> false in
46 if not (is_dir base) then
47 failwith ("environment variable $COCANWIKI_TEMPLATES " ^
48 "must be set to point to my 'templates' directory " ^
49 "(see README file for more details)");
52 (* The webserver gets restarted regularly enough that this is reasonable. *)
53 let { tm_year = year } = gmtime (time ())
54 let year = year + 1900
56 (* Cache of precompiled templates, arranged by full path. *)
57 let cache = Hashtbl.create 32
59 let _get_template filename =
60 let path = base // filename in
61 let stat = Unix.stat path in
62 let mtime = stat.st_mtime in
65 let template, old_mtime = Hashtbl.find cache path in
66 if old_mtime < mtime then (
67 (* The template has changed on disk since it was compiled. Reload. *)
68 let template = Template.template path in
69 Hashtbl.replace cache path (template, mtime);
75 (* Template not seen before, so load it. *)
76 let template = Template.template path in
77 Hashtbl.replace cache path (template, mtime);
80 let get_template ?page dbh hostid filename =
81 let template = _get_template filename in
84 (* Get standard fields concerning this host from the database. *)
86 PGSQL(dbh) "nullable-results"
87 "select h.theme_css, p.name, p.url, h.search_box,
88 h.brand, h.brand_tagline, h.brand_description,
90 from hosts h left outer join powered_by p on h.powered_by = p.id
91 where h.id = $hostid" in
93 let theme_css, powered_by_name, powered_by_url, search_box,
94 brand, brand_tagline, brand_description, pagebug =
97 | _ -> assert false in
101 | None -> "/_css/standard.css"
102 | Some file -> file in
104 let powered_by_name, powered_by_url =
105 match powered_by_name, powered_by_url with
107 let url = "http://sandbox.merjis.com/" in
108 let name = Cocanwiki_version.package ^ " " ^
109 Cocanwiki_version.version in
111 | Some name, Some url -> name, url
112 | _ -> assert false in
115 has_brand_tagline, brand_tagline,
116 has_brand_description, brand_description =
118 | None -> false, "", false, "", false, ""
120 let has_brand_tagline, brand_tagline =
121 match brand_tagline with
123 | Some s -> true, s in
124 let has_brand_description, brand_description =
125 match brand_description with
127 | Some s -> true, s in
129 has_brand_tagline, brand_tagline,
130 has_brand_description, brand_description in
132 let has_pagebug, pagebug =
135 | Some pagebug -> true, pagebug in
137 let search_box = match search_box with Some b -> b | _ -> assert false in
139 template#set "theme_css" theme_css;
140 template#set "powered_by_name" powered_by_name;
141 template#set "powered_by_url" powered_by_url;
142 template#conditional "search_box" search_box;
143 template#conditional "branding" branding;
144 template#set "brand" brand;
145 template#conditional "has_brand_tagline" has_brand_tagline;
146 template#set "brand_tagline" brand_tagline;
147 template#conditional "has_brand_description" has_brand_description;
148 template#set "brand_description" brand_description;
149 template#conditional "has_pagebug" has_pagebug;
150 template#set "pagebug" pagebug;
153 let rows = PGSQL(dbh)
154 "select url, label, ordering from sitemenu
155 where hostid = $hostid order by ordering" in
160 | Some "index" -> true
162 template#conditional "is_homepage" is_homepage;
165 (fun (url, label, _) ->
169 | Some page when page = url -> false
171 let id = id_of_url url in
172 [ "url", Template.VarString url;
173 "label", Template.VarString label;
174 "is_linked", Template.VarConditional is_linked;
175 "id", Template.VarString id ]
178 template#table "sitemenu" table;
180 else (* if we have no hostid *) (
181 template#set "theme_css" "/_css/standard.css";
182 template#set "powered_by_name" (Cocanwiki_version.package ^ " " ^
183 Cocanwiki_version.version);
184 template#set "powered_by_url" "http://sandbox.merjis.com/";
185 template#conditional "search_box" false;
186 template#conditional "branding" false;
187 template#set "brand" "";
188 template#conditional "has_brand_tagline" false;
189 template#set "brand_tagline" "";
190 template#conditional "has_brand_description" false;
191 template#set "brand_description" "";
192 template#conditional "has_pagebug" false;
193 template#set "pagebug" "";
194 template#conditional "is_homepage" false;
195 template#table "sitemenu" [];
197 (* Copyright year. *)
198 template#set "year" (string_of_int year);