Added the framework to allow us to populate standards parts of the
[cocanwiki.git] / scripts / cocanwiki_template.ml
1 (* COCANWIKI scripts.
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/09/08 15:46:52 rich Exp $
5  *
6  * This module wraps around the Template library.  It provides caching
7  * of templates and fills in standard fields on a host-specific basis.
8  *
9  *)
10
11 open Unix
12
13 open Cocanwiki_files
14
15 let base =
16   let base =
17     try Sys.getenv "COCANWIKI_TEMPLATES"
18     with Not_found -> "/usr/share/cocanwiki/templates" in
19   let is_dir path =
20     try (Unix.stat path).Unix.st_kind = Unix.S_DIR
21     with Unix.Unix_error _ -> false in
22   if not (is_dir base) then
23     failwith ("environment variable $COCANWIKI_TEMPLATES " ^
24               "must be set to point to my 'templates' directory " ^
25               "(see README file for more details)");
26   base
27
28 (* Cache of precompiled templates, arranged by full path. *)
29 let cache = Hashtbl.create 32
30
31 let _get_template filename =
32   let path = base // filename in
33   let stat = Unix.stat path in
34   let mtime = stat.st_mtime in
35
36   try
37     let template, old_mtime = Hashtbl.find cache path in
38     if old_mtime < mtime then (
39       (* The template has changed on disk since it was compiled.  Reload. *)
40       let template = Template.template path in
41       Hashtbl.replace cache path (template, mtime);
42       template
43     ) else
44       template
45   with
46       Not_found ->
47         (* Template not seen before, so load it. *)
48         let template = Template.template path in
49         Hashtbl.replace cache path (template, mtime);
50         template
51
52 let get_template (dbh : Dbi.connection) hostid filename =
53   let template = _get_template filename in
54
55   (* Get standard fields concerning this host from the database. *)
56   (* XXX *)
57
58   template