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 $
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 (* XXX - See tools/copy_host for a more reliable way to do this - XXX. *)
51 (* Use low-level PG'OCaml calls to make a duplicate of the
52 * old hosts row where id = template.
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).
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
65 fun (field, {PGOCaml.name = name}) ->
66 if name <> "id" && name <> "canonical_hostname" &&
67 name <> "is_template" then (
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 ())
74 ) (List.combine fields types);
78 "insert into pages (hostid, url, title, description, keywords,
80 select $hostid, url, title, description, keywords, redirect, css,
83 where hostid = $template and url is not null";
85 (* Copy page contents. *)
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,
92 from contents c, pages p
93 where c.pageid = p.id and p.hostid = $template and p.url is not null";
95 (* Copy files and images. *)
97 "insert into files (hostid, name, content, title, mime_type)
98 select $hostid, name, content, title, mime_type
100 where hostid = $template and name is not null";
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
109 where hostid = $template and name is not null";
113 "insert into sitemenu (hostid, url, label, ordering)
114 select $hostid, url, label, ordering from sitemenu
115 where hostid = $template";
119 "insert into contacts (hostid, name, subject)
120 select $hostid, name, subject from contacts where hostid = $template";
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";
131 (* Set the title of the index page. *)
133 "update pages set title = $title
134 where hostid = $hostid and url = 'index'"
137 (* Create the administrator user. *)
139 "insert into users (hostid, name, password,
140 force_password_change, email, can_manage_users)
141 values ($hostid, $username, $password, $force_password_change,