About half way through switching cocanwiki to using the new PG interface.
[cocanwiki.git] / scripts / lib / 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.2 2006/03/27 16:43:44 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     server_settings_crash_email =
40   let default = 1l, None, None in
41   let settings = ref None in
42   let get_settings dbh =
43     let rows = PGSQL(dbh)
44       "select version, stats_page, crash_email from server_settings" in
45     let s =
46       match rows with
47       | [row] -> row
48       | [] -> default
49       | _ -> assert false in
50     settings := Some s;
51     s
52   in
53
54   let server_settings_version dbh =
55     let (version, _, _) =
56       match !settings with
57       | None -> get_settings dbh
58       | Some settings -> settings in
59     version
60   in
61
62   let server_settings_stats_page dbh =
63     let (_, stats_page, _) =
64       match !settings with
65       | None -> get_settings dbh
66       | Some settings -> settings in
67     stats_page
68   in
69
70   let server_settings_crash_email dbh =
71     let (_, _, crash_email) =
72       match !settings with
73       | None -> get_settings dbh
74       | Some settings -> settings in
75     crash_email
76   in
77
78   server_settings_version, server_settings_stats_page,
79   server_settings_crash_email