(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: cocanwiki_template.ml,v 1.2 2004/10/23 10:13:20 rich Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * This module wraps around the Template library. It provides caching * of templates and fills in standard fields on a host-specific basis. * *) open Unix open Cocanwiki_files open Cocanwiki_strings (* This is used to generate the id fields from URLs on the site menu. *) let id_of_url str = let str = String.copy str in for i = 0 to String.length str - 1 do if not (isalnum str.[i]) then str.[i] <- '_' done; str let base = let base = try Sys.getenv "COCANWIKI_TEMPLATES" with Not_found -> "/usr/share/cocanwiki/templates" in let is_dir path = try (Unix.stat path).Unix.st_kind = Unix.S_DIR with Unix.Unix_error _ -> false in if not (is_dir base) then failwith ("environment variable $COCANWIKI_TEMPLATES " ^ "must be set to point to my 'templates' directory " ^ "(see README file for more details)"); base (* The webserver gets restarted regularly enough that this is reasonable. *) let { tm_year = year } = gmtime (time ()) let year = year + 1900 (* Cache of precompiled templates, arranged by full path. *) let cache = Hashtbl.create 32 let _get_template filename = let path = base // filename in let stat = Unix.stat path in let mtime = stat.st_mtime in try let template, old_mtime = Hashtbl.find cache path in if old_mtime < mtime then ( (* The template has changed on disk since it was compiled. Reload. *) let template = Template.template path in Hashtbl.replace cache path (template, mtime); template ) else template with Not_found -> (* Template not seen before, so load it. *) let template = Template.template path in Hashtbl.replace cache path (template, mtime); template let get_template ?page (dbh : Dbi.connection) hostid filename = let template = _get_template filename in (* Get standard fields concerning this host from the database. *) let sth = dbh#prepare_cached "select h.theme_css, p.name, p.url from hosts h left outer join powered_by p on h.powered_by = p.id where h.id = ?" in sth#execute [`Int hostid]; let theme_css, powered_by_name, powered_by_url = match sth#fetch1 () with | [ a; b; c] -> a, b, c | _ -> assert false in let theme_css = match theme_css with | `Null -> "/_css/standard.css" | `String file -> file | _ -> assert false in let powered_by_name, powered_by_url = match powered_by_name, powered_by_url with | `Null, `Null -> let url = "http://sandbox.merjis.com/" in let name = Cocanwiki_version.package ^ " " ^ Cocanwiki_version.version in name, url | `String name, `String url -> name, url | _ -> assert false in template#set "theme_css" theme_css; template#set "powered_by_name" powered_by_name; template#set "powered_by_url" powered_by_url; (* Site menu. *) let sth = dbh#prepare_cached "select url, label, ordering from sitemenu where hostid = ? order by ordering" in sth#execute [`Int hostid]; let is_homepage = match page with | None -> false | Some "index" -> true | _ -> false in template#conditional "is_homepage" is_homepage; let table = sth#map (function [`String url; `String label; _] -> let is_linked = match page with | None -> true | Some page when page = url -> false | _ -> true in let id = id_of_url url in [ "url", Template.VarString url; "label", Template.VarString label; "is_linked", Template.VarConditional is_linked; "id", Template.VarString id ] | _ -> assert false) in template#table "sitemenu" table; (* Wiki version. *) template#set "cocanwiki_package" Cocanwiki_version.package; template#set "cocanwiki_version" Cocanwiki_version.version; (* Copyright year. *) template#set "year" (string_of_int year); template