Allow username field to be prepopulated.
[cocanwiki.git] / scripts / 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.2 2004/10/09 12:24:47 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   let sth =
30     dbh#prepare_cached "set constraints hosts_hostname_cn deferred" in
31   sth#execute [];
32   let sth = dbh#prepare_cached "insert into hosts (canonical_hostname)
33                                   values (?)" in
34   sth#execute [`String canonical_hostname];
35
36   let hostid = sth#serial "hosts_id_seq" in
37
38   let sth = dbh#prepare_cached "insert into hostnames (hostid, name)
39                                   values (?, ?)" in
40   sth#execute [`Int hostid; `String canonical_hostname];
41   List.iter (fun name ->
42                sth#execute [`Int hostid; `String name]) hostnames;
43
44   (* Are we creating a blank site or copying a template? *)
45   if template = 0 then (
46     (* Blank site. *)
47     let sth = dbh#prepare_cached "insert into pages (hostid, url, title,
48                                   description) values (?, 'index', ?, ?)" in
49     sth#execute [`Int hostid; `String title; `String title];
50
51   ) else (
52     (* Copy from template. *)
53
54     (*dbh#set_debug true;*)
55
56     let sth = dbh#prepare_cached "select * from hosts where id = ?" in
57     sth#execute [`Int template];
58
59     let names = sth#names in
60     let row = sth#fetch1 () in
61     sth#finish ();
62
63     List.iter
64       (fun (name, field) ->
65          if name <> "id" && name <> "canonical_hostname" &&
66            name <> "is_template" then (
67              let sql = "update hosts set " ^ name ^ " = ? where id = ?" in
68              let sth = dbh#prepare_cached sql in
69              sth#execute [field; `Int hostid]
70            )
71       ) (List.combine names row);
72
73     (* Copy pages. *)
74     let sth =
75       dbh#prepare_cached
76         "insert into pages (hostid, url, title, description, redirect, css)
77          select ?, url, title, description, redirect, css
78            from pages
79           where hostid = ? and url is not null" in
80     sth#execute [`Int hostid; `Int template];
81
82     (* Copy page contents. *)
83     let sth =
84       dbh#prepare_cached
85         "insert into contents (pageid, ordering, sectionname, content,
86                                divname)
87          select (select id from pages where hostid = ? and url = p.url),
88                 c.ordering, c.sectionname, c.content, c.divname
89            from contents c, pages p
90           where c.pageid = p.id and p.hostid = ? and p.url is not null" in
91     sth#execute [`Int hostid; `Int template];
92
93     (* Copy files and images. *)
94     let sth =
95       dbh#prepare_cached
96         "insert into files (hostid, name, content, title, mime_type)
97          select ?, name, content, title, mime_type
98            from files
99           where hostid = ? and name is not null" in
100     sth#execute [`Int hostid; `Int template];
101
102     let sth =
103       dbh#prepare_cached
104         "insert into images (hostid, name, image, width, height, alt, title,
105                              longdesc, class, mime_type, thumbnail,
106                              tn_width, tn_height, tn_mime_type)
107          select ?, name, image, width, height, alt, title, longdesc, class,
108                 mime_type, thumbnail, tn_width, tn_height, tn_mime_type
109            from images
110           where hostid = ? and name is not null" in
111     sth#execute [`Int hostid; `Int template];
112
113     (* Copy sitemenu. *)
114     let sth =
115       dbh#prepare_cached
116         "insert into sitemenu (hostid, url, label, ordering)
117          select ?, url, label, ordering from sitemenu where hostid = ?" in
118     sth#execute [`Int hostid; `Int template];
119
120     (* Copy contacts. *)
121     let sth =
122       dbh#prepare_cached
123         "insert into contacts (hostid, name, subject)
124          select ?, name, subject from contacts where hostid = ?" in
125     sth#execute [`Int hostid; `Int template];
126
127     let sth =
128       dbh#prepare_cached
129         "insert into contact_emails (contactid, email)
130          select (select id from contacts
131                   where hostid = ? and name = c.name), ce.email
132            from contact_emails ce, contacts c
133           where ce.contactid = c.id and c.hostid = ?" in
134     sth#execute [`Int hostid; `Int template];
135
136     (* Set the title of the index page. *)
137     let sth = dbh#prepare_cached "update pages set title = ?
138                                    where hostid = ? and url = 'index'" in
139     sth#execute [`String title; `Int hostid]
140   );
141
142   (* Create the administrator user. *)
143   let email = match email with Some e -> `String e | None -> `Null in
144
145   let sth = dbh#prepare_cached "insert into users (hostid, name, password,
146                                 force_password_change, email, can_manage_users)
147                                 values (?, ?, ?, ?, ?, true)" in
148   sth#execute [`Int hostid; `String username; `String password;
149                `Bool force_password_change; email];
150
151   hostid