Note small bug in cocanwiki_links.
[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.6 2004/10/17 19:43:19 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 open Cocanwiki
31
32 (* This is used to generate the id fields from URLs on the site menu. *)
33 let id_of_url str =
34   let str = String.copy str in
35   for i = 0 to String.length str - 1 do
36     if not (isalnum str.[i]) then str.[i] <- '_'
37   done;
38   str
39
40 let base =
41   let base =
42     try Sys.getenv "COCANWIKI_TEMPLATES"
43     with Not_found -> "/usr/share/cocanwiki/templates" in
44   let is_dir path =
45     try (Unix.stat path).Unix.st_kind = Unix.S_DIR
46     with Unix.Unix_error _ -> false in
47   if not (is_dir base) then
48     failwith ("environment variable $COCANWIKI_TEMPLATES " ^
49               "must be set to point to my 'templates' directory " ^
50               "(see README file for more details)");
51   base
52
53 (* The webserver gets restarted regularly enough that this is reasonable. *)
54 let { tm_year = year } = gmtime (time ())
55 let year = year + 1900
56
57 (* Cache of precompiled templates, arranged by full path. *)
58 let cache = Hashtbl.create 32
59
60 let _get_template filename =
61   let path = base // filename in
62   let stat = Unix.stat path in
63   let mtime = stat.st_mtime in
64
65   try
66     let template, old_mtime = Hashtbl.find cache path in
67     if old_mtime < mtime then (
68       (* The template has changed on disk since it was compiled.  Reload. *)
69       let template = Template.template path in
70       Hashtbl.replace cache path (template, mtime);
71       template
72     ) else
73       template
74   with
75       Not_found ->
76         (* Template not seen before, so load it. *)
77         let template = Template.template path in
78         Hashtbl.replace cache path (template, mtime);
79         template
80
81 let get_template ?page (dbh : Dbi.connection) hostid filename =
82   let template = _get_template filename in
83
84   (* Get standard fields concerning this host from the database. *)
85   let sth = dbh#prepare_cached "select theme_css from hosts
86                                  where id = ?" in
87   sth#execute [`Int hostid];
88
89   let theme_css =
90     match sth#fetch1 () with
91         [ `Null ] -> "/_css/standard.css"
92       | [ `String file ] -> file
93       | _ -> assert false in
94
95   template#set "theme_css" theme_css;
96
97   (* Site menu. *)
98   let sth = dbh#prepare_cached "select url, label, ordering from sitemenu
99                                  where hostid = ? order by ordering" in
100   sth#execute [`Int hostid];
101
102   let is_homepage =
103     match page with
104       | None -> false
105       | Some "index" -> true
106       | _ -> false in
107   template#conditional "is_homepage" is_homepage;
108
109   let table = sth#map (function [`String url; `String label; _] ->
110                          let is_linked =
111                            match page with
112                              | None -> true
113                              | Some page when page = url -> false
114                              | _ -> true in
115                          let id = id_of_url url in
116                          [ "url", Template.VarString url;
117                            "label", Template.VarString label;
118                            "is_linked", Template.VarConditional is_linked;
119                            "id", Template.VarString id ]
120                          | _ -> assert false) in
121
122   template#table "sitemenu" table;
123
124   (* Wiki version. *)
125   template#set "cocanwiki_package" Cocanwiki_version.package;
126   template#set "cocanwiki_version" Cocanwiki_version.version;
127
128   (* Copyright year. *)
129   template#set "year" (string_of_int year);
130
131   template