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 $
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.
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.
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.
27 let create_host dbh canonical_hostname hostnames template
28 title username password force_password_change email =
30 dbh#prepare_cached "set constraints hosts_hostname_cn deferred" in
32 let sth = dbh#prepare_cached "insert into hosts (canonical_hostname)
34 sth#execute [`String canonical_hostname];
36 let hostid = sth#serial "hosts_id_seq" in
38 let sth = dbh#prepare_cached "insert into hostnames (hostid, name)
40 sth#execute [`Int hostid; `String canonical_hostname];
41 List.iter (fun name ->
42 sth#execute [`Int hostid; `String name]) hostnames;
44 (* Are we creating a blank site or copying a template? *)
45 if template = 0 then (
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];
52 (* Copy from template. *)
54 (*dbh#set_debug true;*)
56 let sth = dbh#prepare_cached "select * from hosts where id = ?" in
57 sth#execute [`Int template];
59 let names = sth#names in
60 let row = sth#fetch1 () in
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]
71 ) (List.combine names row);
76 "insert into pages (hostid, url, title, description, redirect, css)
77 select ?, url, title, description, redirect, css
79 where hostid = ? and url is not null" in
80 sth#execute [`Int hostid; `Int template];
82 (* Copy page contents. *)
85 "insert into contents (pageid, ordering, sectionname, content,
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];
93 (* Copy files and images. *)
96 "insert into files (hostid, name, content, title, mime_type)
97 select ?, name, content, title, mime_type
99 where hostid = ? and name is not null" in
100 sth#execute [`Int hostid; `Int template];
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
110 where hostid = ? and name is not null" in
111 sth#execute [`Int hostid; `Int template];
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];
123 "insert into contacts (hostid, name, subject)
124 select ?, name, subject from contacts where hostid = ?" in
125 sth#execute [`Int hostid; `Int template];
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];
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]
142 (* Create the administrator user. *)
143 let email = match email with Some e -> `String e | None -> `Null in
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];