+csv dep for PG'OCaml.
[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.8 2006/09/11 09:39:33 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     (* XXX - See tools/copy_host for a more reliable way to do this - XXX. *)
50
51     (* Use low-level PG'OCaml calls to make a duplicate of the
52      * old hosts row where id = template.
53      *
54      * But don't duplicate id (it's the new site number), or
55      * canonical_hostname (the hostname has changed) or
56      * is_template (new site is a copy of a template, not a template).
57      *)
58     let query = "select * from hosts where id = $1" in
59     PGOCaml.prepare dbh ~query ();
60     let types = Option.get (snd (PGOCaml.describe_statement dbh ())) in
61     let params = [Some (Int32.to_string template)] in
62     let fields = List.hd (PGOCaml.execute dbh ~params ()) in
63
64     List.iter (
65       fun (field, {PGOCaml.name = name}) ->
66         if name <> "id" && name <> "canonical_hostname" &&
67           name <> "is_template" then (
68             let query =
69               sprintf "update hosts set %s = $1 where id = $2" name in
70             PGOCaml.prepare dbh ~query ();
71             let params = [ field; Some (Int32.to_string hostid) ] in
72             ignore (PGOCaml.execute dbh ~params ())
73           )
74     ) (List.combine fields types);
75
76     (* Copy pages. *)
77     PGSQL(dbh)
78       "insert into pages (hostid, url, title, description, keywords,
79                           redirect, css, noodp)
80        select $hostid, url, title, description, keywords, redirect, css,
81               noodp
82          from pages
83         where hostid = $template and url is not null";
84
85     (* Copy page contents. *)
86     PGSQL(dbh)
87       "insert into contents (pageid, ordering, sectionname, content,
88                              divname, divclass, jsgo)
89        select (select id from pages where hostid = $hostid and url = p.url),
90               c.ordering, c.sectionname, c.content, c.divname, c.divclass,
91               c.jsgo
92          from contents c, pages p
93         where c.pageid = p.id and p.hostid = $template and p.url is not null";
94
95     (* Copy files and images. *)
96     PGSQL(dbh)
97       "insert into files (hostid, name, content, title, mime_type)
98        select $hostid, name, content, title, mime_type
99          from files
100         where hostid = $template and name is not null";
101
102     PGSQL(dbh)
103       "insert into images (hostid, name, image, width, height, alt, title,
104                            longdesc, class, mime_type, thumbnail,
105                            tn_width, tn_height, tn_mime_type)
106        select $hostid, name, image, width, height, alt, title, longdesc, class,
107               mime_type, thumbnail, tn_width, tn_height, tn_mime_type
108          from images
109         where hostid = $template and name is not null";
110
111     (* Copy sitemenu. *)
112     PGSQL(dbh)
113       "insert into sitemenu (hostid, url, label, ordering)
114        select $hostid, url, label, ordering from sitemenu
115         where hostid = $template";
116
117     (* Copy contacts. *)
118     PGSQL(dbh)
119       "insert into contacts (hostid, name, subject)
120        select $hostid, name, subject from contacts where hostid = $template";
121
122     PGSQL(dbh)
123       "insert into contact_emails (contactid, email)
124        select (select id from contacts
125                where hostid = $hostid and name = c.name), ce.email
126            from contact_emails ce, contacts c
127           where ce.contactid = c.id and c.hostid = $template";
128
129     (* XXX Macros. *)
130
131     (* Set the title of the index page. *)
132     PGSQL(dbh)
133       "update pages set title = $title
134         where hostid = $hostid and url = 'index'"
135   );
136
137   (* Create the administrator user. *)
138   PGSQL(dbh)
139     "insert into users (hostid, name, password,
140                         force_password_change, email, can_manage_users)
141      values ($hostid, $username, $password, $force_password_change,
142              $?email, true)";
143
144   hostid