Contact form now identified by name, not ID field.
[cocanwiki.git] / scripts / cocanwiki_server_settings.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_server_settings.ml,v 1.1 2004/09/23 15:16:21 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
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 (* Server-wide settings.
28  *
29  * These are stored in a single row in the database in the table
30  * 'server_settings'.  You must restart the server if you change
31  * this row.
32  *
33  * It's not possible to read these at server start-up time because the
34  * Apache server is still running as 'root' and will not normally be
35  * allowed to access the database.  We thus read them at the earliest
36  * opportunity, in a request context, and cache the results.
37  *)
38 let server_settings_version, server_settings_stats_page =
39   let settings = ref None in
40   let get_settings (dbh : Dbi.connection) =
41     let sth = dbh#prepare "select version, stats_page from server_settings" in
42     sth#execute [];
43     let s =
44       match sth#fetch1 () with
45         | [ `Int version; (`String _ | `Null) as stats_page ] ->
46             let stats_page =
47               match stats_page with `String s -> Some s | `Null -> None in
48             version, stats_page
49         | _ -> assert false in
50     sth#finish ();
51     settings := Some s;
52     s
53   in
54
55   let server_settings_version dbh =
56     let (version, _) =
57       match !settings with
58           None -> get_settings dbh
59         | Some settings -> settings in
60     version
61   in
62
63   let server_settings_stats_page dbh =
64     let (_, stats_page) =
65       match !settings with
66           None -> get_settings dbh
67         | Some settings -> settings in
68     stats_page
69   in
70
71   server_settings_version, server_settings_stats_page