About half way through switching cocanwiki to using the new PG interface.
[cocanwiki.git] / scripts / lib / cocanwiki_create_host.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_create_host.ml,v 1.3 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 let create_host dbh canonical_hostname hostnames template
28     title username password force_password_change email =
29   PGSQL(dbh) "set constraints hosts_hostname_cn deferred";
30   PGSQL(dbh)
31     "insert into hosts (canonical_hostname) values ($canonical_hostname)";
32
33   let hostid = PGOCaml.serial4 dbh "hosts_id_seq" in
34
35   let insert name =
36     PGSQL(dbh) "insert into hostnames (hostid, name) values ($hostid, $name)"
37   in
38   insert canonical_hostname;
39   List.iter insert hostnames;
40
41   (* Are we creating a blank site or copying a template? *)
42   if template = 0l then (
43     (* Blank site. *)
44     PGSQL(dbh) "insert into pages (hostid, url, title,
45                   description) values ($hostid, 'index', $title, $title)"
46   ) else (
47     (* Copy from template. *)
48
49     (* Use low-level PG'OCaml calls to make a duplicate of the
50      * old hosts row where id = template.
51      *
52      * But don't duplicate id (it's the new site number), or
53      * canonical_hostname (the hostname has changed) or
54      * is_template (new site is a copy of a template, not a template).
55      *)
56     let query = "select * from hosts where id = $1" in
57     PGOCaml.prepare dbh ~query ();
58     let types = Option.get (snd (PGOCaml.describe_statement dbh ())) in
59     let params = [Some (Int32.to_string template)] in
60     let fields = List.hd (PGOCaml.execute dbh ~params ()) in
61
62     List.iter (
63       fun (field, {PGOCaml.name = name}) ->
64         if name <> "id" && name <> "canonical_hostname" &&
65           name <> "is_template" then (
66             let query =
67               sprintf "update hosts set %s = $1 where id = $2" name in
68             PGOCaml.prepare dbh ~query ();
69             let params = [ field; Some (Int32.to_string hostid) ] in
70             ignore (PGOCaml.execute dbh ~params ())
71           )
72     ) (List.combine fields types);
73
74     (* Copy pages. *)
75     PGSQL(dbh)
76       "insert into pages (hostid, url, title, description, redirect, css)
77        select $hostid, url, title, description, redirect, css
78          from pages
79         where hostid = $template and url is not null";
80
81     (* Copy page contents. *)
82     PGSQL(dbh)
83       "insert into contents (pageid, ordering, sectionname, content,
84                              divname)
85        select (select id from pages where hostid = $hostid and url = p.url),
86               c.ordering, c.sectionname, c.content, c.divname
87          from contents c, pages p
88         where c.pageid = p.id and p.hostid = $template and p.url is not null";
89
90     (* Copy files and images. *)
91     PGSQL(dbh)
92       "insert into files (hostid, name, content, title, mime_type)
93        select $hostid, name, content, title, mime_type
94          from files
95         where hostid = $template and name is not null";
96
97     PGSQL(dbh)
98       "insert into images (hostid, name, image, width, height, alt, title,
99                            longdesc, class, mime_type, thumbnail,
100                            tn_width, tn_height, tn_mime_type)
101        select $hostid, name, image, width, height, alt, title, longdesc, class,
102               mime_type, thumbnail, tn_width, tn_height, tn_mime_type
103          from images
104         where hostid = $template and name is not null";
105
106     (* Copy sitemenu. *)
107     PGSQL(dbh)
108       "insert into sitemenu (hostid, url, label, ordering)
109        select $hostid, url, label, ordering from sitemenu
110         where hostid = $template";
111
112     (* Copy contacts. *)
113     PGSQL(dbh)
114       "insert into contacts (hostid, name, subject)
115        select $hostid, name, subject from contacts where hostid = $template";
116
117     PGSQL(dbh)
118       "insert into contact_emails (contactid, email)
119        select (select id from contacts
120                where hostid = $hostid and name = c.name), ce.email
121            from contact_emails ce, contacts c
122           where ce.contactid = c.id and c.hostid = $template";
123
124     (* Set the title of the index page. *)
125     PGSQL(dbh)
126       "update pages set title = $title
127         where hostid = $hostid and url = 'index'"
128   );
129
130   (* Create the administrator user. *)
131   PGSQL(dbh)
132     "insert into users (hostid, name, password,
133                         force_password_change, email, can_manage_users)
134      values ($hostid, $username, $password, $force_password_change,
135              $?email, true)";
136
137   hostid