Added a "name" field to the mailing list entries.
[cocanwiki.git] / scripts / 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.5 2004/09/09 12:21:22 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
30 let base =
31   let base =
32     try Sys.getenv "COCANWIKI_TEMPLATES"
33     with Not_found -> "/usr/share/cocanwiki/templates" in
34   let is_dir path =
35     try (Unix.stat path).Unix.st_kind = Unix.S_DIR
36     with Unix.Unix_error _ -> false in
37   if not (is_dir base) then
38     failwith ("environment variable $COCANWIKI_TEMPLATES " ^
39               "must be set to point to my 'templates' directory " ^
40               "(see README file for more details)");
41   base
42
43 (* The webserver gets restarted regularly enough that this is reasonable. *)
44 let { tm_year = year } = gmtime (time ())
45 let year = year + 1900
46
47 (* Cache of precompiled templates, arranged by full path. *)
48 let cache = Hashtbl.create 32
49
50 let _get_template filename =
51   let path = base // filename in
52   let stat = Unix.stat path in
53   let mtime = stat.st_mtime in
54
55   try
56     let template, old_mtime = Hashtbl.find cache path in
57     if old_mtime < mtime then (
58       (* The template has changed on disk since it was compiled.  Reload. *)
59       let template = Template.template path in
60       Hashtbl.replace cache path (template, mtime);
61       template
62     ) else
63       template
64   with
65       Not_found ->
66         (* Template not seen before, so load it. *)
67         let template = Template.template path in
68         Hashtbl.replace cache path (template, mtime);
69         template
70
71 let get_template (dbh : Dbi.connection) hostid filename =
72   let template = _get_template filename in
73
74   (* Get standard fields concerning this host from the database. *)
75   let sth = dbh#prepare_cached "select theme_css from hosts
76                                  where id = ?" in
77   sth#execute [`Int hostid];
78
79   let theme_css =
80     match sth#fetch1 () with
81         [ `Null ] -> "/_css/standard.css"
82       | [ `String file ] -> file
83       | _ -> assert false in
84
85   template#set "theme_css" theme_css;
86
87   (* Site menu. *)
88   let sth = dbh#prepare_cached "select url, label, ordering from sitemenu
89                                  where hostid = ? order by ordering" in
90   sth#execute [`Int hostid];
91
92   let table = sth#map (function [`String url; `String label; _] ->
93                          [ "url", Template.VarString url;
94                            "label", Template.VarString label ]
95                          | _ -> assert false) in
96
97   template#table "sitemenu" table;
98
99   (* Wiki version. *)
100   template#set "cocanwiki_package" Cocanwiki_version.package;
101   template#set "cocanwiki_version" Cocanwiki_version.version;
102
103   (* Copyright year. *)
104   template#set "year" (string_of_int year);
105
106   template