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 $
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 =
29 PGSQL(dbh) "set constraints hosts_hostname_cn deferred";
31 "insert into hosts (canonical_hostname) values ($canonical_hostname)";
33 let hostid = PGOCaml.serial4 dbh "hosts_id_seq" in
36 PGSQL(dbh) "insert into hostnames (hostid, name) values ($hostid, $name)"
38 insert canonical_hostname;
39 List.iter insert hostnames;
41 (* Are we creating a blank site or copying a template? *)
42 if template = 0l then (
44 PGSQL(dbh) "insert into pages (hostid, url, title,
45 description) values ($hostid, 'index', $title, $title)"
47 (* Copy from template. *)
49 (* Use low-level PG'OCaml calls to make a duplicate of the
50 * old hosts row where id = template.
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).
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
63 fun (field, {PGOCaml.name = name}) ->
64 if name <> "id" && name <> "canonical_hostname" &&
65 name <> "is_template" then (
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 ())
72 ) (List.combine fields types);
76 "insert into pages (hostid, url, title, description, redirect, css)
77 select $hostid, url, title, description, redirect, css
79 where hostid = $template and url is not null";
81 (* Copy page contents. *)
83 "insert into contents (pageid, ordering, sectionname, content,
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";
90 (* Copy files and images. *)
92 "insert into files (hostid, name, content, title, mime_type)
93 select $hostid, name, content, title, mime_type
95 where hostid = $template and name is not null";
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
104 where hostid = $template and name is not null";
108 "insert into sitemenu (hostid, url, label, ordering)
109 select $hostid, url, label, ordering from sitemenu
110 where hostid = $template";
114 "insert into contacts (hostid, name, subject)
115 select $hostid, name, subject from contacts where hostid = $template";
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";
124 (* Set the title of the index page. *)
126 "update pages set title = $title
127 where hostid = $hostid and url = 'index'"
130 (* Create the administrator user. *)
132 "insert into users (hostid, name, password,
133 force_password_change, email, can_manage_users)
134 values ($hostid, $username, $password, $force_password_change,