Added optional branding <div> to sites.
[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.3 2004/11/03 13:36:45 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 : Dbi.connection) hostid filename =
81   let template = _get_template filename in
82
83   (* Get standard fields concerning this host from the database. *)
84   let sth =
85     dbh#prepare_cached
86       "select h.theme_css, p.name, p.url, h.search_box,
87               h.brand, h.brand_tagline, h.brand_description
88          from hosts h left outer join powered_by p on h.powered_by = p.id
89         where h.id = ?" in
90   sth#execute [`Int hostid];
91
92   let theme_css, powered_by_name, powered_by_url, search_box,
93       brand, brand_tagline, brand_description =
94     match sth#fetch1 () with
95       | [ a; b; c; d; e; f; g] -> a, b, c, d, e, f, g
96       | _ -> assert false in
97
98   let theme_css =
99     match theme_css with
100       | `Null -> "/_css/standard.css"
101       | `String file -> file
102       | _ -> assert false in
103
104   let powered_by_name, powered_by_url =
105     match powered_by_name, powered_by_url with
106       | `Null, `Null ->
107           let url = "http://sandbox.merjis.com/" in
108           let name = Cocanwiki_version.package ^ " " ^
109                      Cocanwiki_version.version in
110           name, url
111       | `String name, `String url -> name, url
112       | _ -> assert false in
113
114   let search_box = match search_box with `Bool b -> b | _ -> assert false in
115
116   let branding, brand,
117       has_brand_tagline, brand_tagline,
118       has_brand_description, brand_description =
119     match brand with
120       | `Null -> false, "", false, "", false, ""
121       | `String brand ->
122           let has_brand_tagline, brand_tagline =
123             match brand_tagline with
124               | `Null -> false, ""
125               | `String s -> true, s
126               | _ -> assert false in
127           let has_brand_description, brand_description =
128             match brand_description with
129               | `Null -> false, ""
130               | `String s -> true, s
131               | _ -> assert false in
132           true, brand,
133             has_brand_tagline, brand_tagline,
134             has_brand_description, brand_description
135       | _ -> assert false in
136
137   template#set "theme_css" theme_css;
138   template#set "powered_by_name" powered_by_name;
139   template#set "powered_by_url" powered_by_url;
140   template#conditional "search_box" search_box;
141   template#conditional "branding" branding;
142   template#set "brand" brand;
143   template#conditional "has_brand_tagline" has_brand_tagline;
144   template#set "brand_tagline" brand_tagline;
145   template#conditional "has_brand_description" has_brand_description;
146   template#set "brand_description" brand_description;
147
148   (* Site menu. *)
149   let sth = dbh#prepare_cached "select url, label, ordering from sitemenu
150                                  where hostid = ? order by ordering" in
151   sth#execute [`Int hostid];
152
153   let is_homepage =
154     match page with
155       | None -> false
156       | Some "index" -> true
157       | _ -> false in
158   template#conditional "is_homepage" is_homepage;
159
160   let table = sth#map (function [`String url; `String label; _] ->
161                          let is_linked =
162                            match page with
163                              | None -> true
164                              | Some page when page = url -> false
165                              | _ -> true in
166                          let id = id_of_url url in
167                          [ "url", Template.VarString url;
168                            "label", Template.VarString label;
169                            "is_linked", Template.VarConditional is_linked;
170                            "id", Template.VarString id ]
171                          | _ -> assert false) in
172
173   template#table "sitemenu" table;
174
175   (* Copyright year. *)
176   template#set "year" (string_of_int year);
177
178   template