Support for NOODP flag, sitewide and per-page.
[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.10 2006/08/14 11:36:50 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 Cocanwiki_files
29 open Cocanwiki_strings
30
31 (* This is used to generate the id fields from URLs on the site menu. *)
32 let id_of_url str =
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] <- '_'
36   done;
37   str
38
39 let base =
40   let base =
41     try Sys.getenv "COCANWIKI_TEMPLATES"
42     with Not_found -> "/usr/share/cocanwiki/templates" in
43   let is_dir path =
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)");
50   base
51
52 (* The webserver gets restarted regularly enough that this is reasonable. *)
53 let { tm_year = year } = gmtime (time ())
54 let year = year + 1900
55
56 (* Cache of precompiled templates, arranged by full path. *)
57 let cache = Hashtbl.create 32
58
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
63
64   try
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);
70       template
71     ) else
72       template
73   with
74       Not_found ->
75         (* Template not seen before, so load it. *)
76         let template = Template.template path in
77         Hashtbl.replace cache path (template, mtime);
78         template
79
80 let get_template ?page dbh hostid filename =
81   let template = _get_template filename in
82
83   if hostid > 0l then (
84     (* Get standard fields concerning this host from the database. *)
85     let rows =
86       PGSQL(dbh) "nullable-results"
87         "select h.theme_css, h.css is not null,
88                 p.name, p.url, h.search_box,
89                 h.brand, h.brand_tagline, h.brand_description,
90                 h.pagebug, h.ie_imagetoolbar_no, h.global_noodp
91            from hosts h left outer join powered_by p on h.powered_by = p.id
92           where h.id = $hostid" in
93
94     let theme_css, has_host_css, powered_by_name, powered_by_url, search_box,
95       brand, brand_tagline, brand_description, pagebug, ie_imagetoolbar_no,
96       global_noodp =
97       match rows with
98       | [ row ] -> row
99       | _ -> assert false in
100
101     let theme_css =
102       match theme_css with
103       | None -> "/_css/standard.css"
104       | Some file -> file in
105
106     let has_host_css =
107       match has_host_css with
108       | Some true -> true
109       | _ -> false in
110
111     let powered_by_name, powered_by_url =
112       match powered_by_name, powered_by_url with
113       | None, None ->
114           let url = "http://sandbox.merjis.com/" in
115           let name = Cocanwiki_version.package ^ " " ^
116                      Cocanwiki_version.version in
117           name, url
118       | Some name, Some url -> name, url
119       | _ -> assert false in
120
121     let branding, brand,
122       has_brand_tagline, brand_tagline,
123       has_brand_description, brand_description =
124       match brand with
125       | None -> false, "", false, "", false, ""
126       | Some brand ->
127           let has_brand_tagline, brand_tagline =
128             match brand_tagline with
129             | None -> false, ""
130             | Some s -> true, s in
131           let has_brand_description, brand_description =
132             match brand_description with
133             | None -> false, ""
134             | Some s -> true, s in
135           true, brand,
136           has_brand_tagline, brand_tagline,
137           has_brand_description, brand_description in
138
139     let has_pagebug, pagebug =
140       match pagebug with
141       | None -> false, ""
142       | Some pagebug -> true, pagebug in
143
144     let search_box = match search_box with Some b -> b | _ -> assert false in
145
146     let ie_imagetoolbar_no =
147       match ie_imagetoolbar_no with Some b -> b | _ -> assert false in
148
149     let global_noodp =
150       match global_noodp with Some b -> b | _ -> assert false in
151
152     template#set "theme_css" theme_css;
153     template#conditional "has_host_css" has_host_css;
154     template#set "powered_by_name" powered_by_name;
155     template#set "powered_by_url" powered_by_url;
156     template#conditional "search_box" search_box;
157     template#conditional "branding" branding;
158     template#set "brand" brand;
159     template#conditional "has_brand_tagline" has_brand_tagline;
160     template#set "brand_tagline" brand_tagline;
161     template#conditional "has_brand_description" has_brand_description;
162     template#set "brand_description" brand_description;
163     template#conditional "has_pagebug" has_pagebug;
164     template#set "pagebug" pagebug;
165     template#conditional "ie_imagetoolbar_no" ie_imagetoolbar_no;
166     template#conditional "noodp" global_noodp;
167
168     (* Site menu. *)
169     let rows = PGSQL(dbh)
170       "select url, label, ordering from sitemenu
171         where hostid = $hostid order by ordering" in
172
173     let is_homepage =
174       match page with
175       | None -> false
176       | Some "index" -> true
177       | _ -> false in
178     template#conditional "is_homepage" is_homepage;
179
180     let table = List.map
181       (fun (url, label, _) ->
182          let is_linked =
183            match page with
184            | None -> true
185            | Some page when page = url -> false
186            | _ -> true in
187          let id = id_of_url url in
188          [ "url", Template.VarString url;
189            "label", Template.VarString label;
190            "is_linked", Template.VarConditional is_linked;
191            "id", Template.VarString id ]
192       ) rows in
193
194     template#table "sitemenu" table;
195   )
196   else (* if we have no hostid *) (
197     template#set "theme_css" "/_css/standard.css";
198     template#conditional "has_host_css" false;
199     template#set "powered_by_name" (Cocanwiki_version.package ^ " " ^
200                                     Cocanwiki_version.version);
201     template#set "powered_by_url" "http://sandbox.merjis.com/";
202     template#conditional "search_box" false;
203     template#conditional "branding" false;
204     template#set "brand" "";
205     template#conditional "has_brand_tagline" false;
206     template#set "brand_tagline" "";
207     template#conditional "has_brand_description" false;
208     template#set "brand_description" "";
209     template#conditional "has_pagebug" false;
210     template#set "pagebug" "";
211     template#conditional "ie_imagetoolbar_no" false;
212     template#conditional "noodp" false;
213     template#conditional "is_homepage" false;
214     template#table "sitemenu" [];
215   );
216   (* Copyright year. *)
217   template#set "year" (string_of_int year);
218
219   template