(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: cocanwiki_create_host.ml,v 1.8 2006/09/11 09:39:33 rich Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) open Apache open Registry open Cgi open Printf let create_host dbh canonical_hostname hostnames template title username password force_password_change email = PGSQL(dbh) "set constraints hosts_hostname_cn deferred"; PGSQL(dbh) "insert into hosts (canonical_hostname) values ($canonical_hostname)"; let hostid = PGOCaml.serial4 dbh "hosts_id_seq" in let insert name = PGSQL(dbh) "insert into hostnames (hostid, name) values ($hostid, $name)" in insert canonical_hostname; List.iter insert hostnames; (* Are we creating a blank site or copying a template? *) if template = 0l then ( (* Blank site. *) PGSQL(dbh) "insert into pages (hostid, url, title, description) values ($hostid, 'index', $title, $title)" ) else ( (* Copy from template. *) (* XXX - See tools/copy_host for a more reliable way to do this - XXX. *) (* Use low-level PG'OCaml calls to make a duplicate of the * old hosts row where id = template. * * But don't duplicate id (it's the new site number), or * canonical_hostname (the hostname has changed) or * is_template (new site is a copy of a template, not a template). *) let query = "select * from hosts where id = $1" in PGOCaml.prepare dbh ~query (); let types = Option.get (snd (PGOCaml.describe_statement dbh ())) in let params = [Some (Int32.to_string template)] in let fields = List.hd (PGOCaml.execute dbh ~params ()) in List.iter ( fun (field, {PGOCaml.name = name}) -> if name <> "id" && name <> "canonical_hostname" && name <> "is_template" then ( let query = sprintf "update hosts set %s = $1 where id = $2" name in PGOCaml.prepare dbh ~query (); let params = [ field; Some (Int32.to_string hostid) ] in ignore (PGOCaml.execute dbh ~params ()) ) ) (List.combine fields types); (* Copy pages. *) PGSQL(dbh) "insert into pages (hostid, url, title, description, keywords, redirect, css, noodp) select $hostid, url, title, description, keywords, redirect, css, noodp from pages where hostid = $template and url is not null"; (* Copy page contents. *) PGSQL(dbh) "insert into contents (pageid, ordering, sectionname, content, divname, divclass, jsgo) select (select id from pages where hostid = $hostid and url = p.url), c.ordering, c.sectionname, c.content, c.divname, c.divclass, c.jsgo from contents c, pages p where c.pageid = p.id and p.hostid = $template and p.url is not null"; (* Copy files and images. *) PGSQL(dbh) "insert into files (hostid, name, content, title, mime_type) select $hostid, name, content, title, mime_type from files where hostid = $template and name is not null"; PGSQL(dbh) "insert into images (hostid, name, image, width, height, alt, title, longdesc, class, mime_type, thumbnail, tn_width, tn_height, tn_mime_type) select $hostid, name, image, width, height, alt, title, longdesc, class, mime_type, thumbnail, tn_width, tn_height, tn_mime_type from images where hostid = $template and name is not null"; (* Copy sitemenu. *) PGSQL(dbh) "insert into sitemenu (hostid, url, label, ordering) select $hostid, url, label, ordering from sitemenu where hostid = $template"; (* Copy contacts. *) PGSQL(dbh) "insert into contacts (hostid, name, subject) select $hostid, name, subject from contacts where hostid = $template"; PGSQL(dbh) "insert into contact_emails (contactid, email) select (select id from contacts where hostid = $hostid and name = c.name), ce.email from contact_emails ce, contacts c where ce.contactid = c.id and c.hostid = $template"; (* XXX Macros. *) (* Set the title of the index page. *) PGSQL(dbh) "update pages set title = $title where hostid = $hostid and url = 'index'" ); (* Create the administrator user. *) PGSQL(dbh) "insert into users (hostid, name, password, force_password_change, email, can_manage_users) values ($hostid, $username, $password, $force_password_change, $?email, true)"; hostid