From f303d3e2bc355bdca4147d21441d6a13d4748d1a Mon Sep 17 00:00:00 2001 From: rich Date: Sat, 25 Sep 2004 11:45:59 +0000 Subject: [PATCH 1/1] Create new site by copying from an existing template site. --- cocanwiki.sql | 64 +++++++++++-------- scripts/admin/create_host.ml | 117 +++++++++++++++++++++++++++++++--- scripts/admin/create_host_form.ml | 17 ++++- templates/admin/create_host_form.html | 11 +++- 4 files changed, 171 insertions(+), 38 deletions(-) diff --git a/cocanwiki.sql b/cocanwiki.sql index 859eb4b..05c2a43 100644 --- a/cocanwiki.sql +++ b/cocanwiki.sql @@ -114,7 +114,8 @@ CREATE TABLE hosts ( create_account_anon boolean DEFAULT true NOT NULL, theme_css text, feedback_email text, - mailing_list boolean DEFAULT false NOT NULL + mailing_list boolean DEFAULT false NOT NULL, + is_template boolean DEFAULT false NOT NULL ); @@ -288,7 +289,8 @@ CREATE TABLE users ( can_manage_users boolean DEFAULT false NOT NULL, can_manage_contacts boolean DEFAULT false NOT NULL, can_manage_site boolean DEFAULT false NOT NULL, - can_edit_global_css boolean DEFAULT false NOT NULL + can_edit_global_css boolean DEFAULT false NOT NULL, + force_password_change boolean DEFAULT false NOT NULL ); @@ -596,7 +598,7 @@ CREATE UNIQUE INDEX sitemenu_url_uq ON sitemenu USING btree (hostid, url); -- --- TOC entry 62 (OID 542626) +-- TOC entry 63 (OID 542626) -- Name: contact_emails_uq; Type: INDEX; Schema: public; Owner: rich -- @@ -604,7 +606,7 @@ CREATE UNIQUE INDEX contact_emails_uq ON contact_emails USING btree (contactid, -- --- TOC entry 63 (OID 543505) +-- TOC entry 64 (OID 543505) -- Name: themes_theme_css_uq; Type: INDEX; Schema: public; Owner: rich -- @@ -612,7 +614,7 @@ CREATE UNIQUE INDEX themes_theme_css_uq ON themes USING btree (theme_css); -- --- TOC entry 64 (OID 543763) +-- TOC entry 65 (OID 543763) -- Name: page_emails_email_uq; Type: INDEX; Schema: public; Owner: rich -- @@ -620,7 +622,7 @@ CREATE UNIQUE INDEX page_emails_email_uq ON page_emails USING btree (hostid, url -- --- TOC entry 65 (OID 543795) +-- TOC entry 66 (OID 543795) -- Name: mailing_lists_email_uq; Type: INDEX; Schema: public; Owner: rich -- @@ -628,6 +630,14 @@ CREATE UNIQUE INDEX mailing_lists_email_uq ON mailing_lists USING btree (hostid, -- +-- TOC entry 61 (OID 543880) +-- Name: contacts_name_uq; Type: INDEX; Schema: public; Owner: rich +-- + +CREATE UNIQUE INDEX contacts_name_uq ON contacts USING btree (hostid, name); + + +-- -- TOC entry 45 (OID 536012) -- Name: pages_pkey; Type: CONSTRAINT; Schema: public; Owner: rich -- @@ -682,7 +692,7 @@ ALTER TABLE ONLY users -- --- TOC entry 61 (OID 542611) +-- TOC entry 62 (OID 542611) -- Name: contacts_pkey; Type: CONSTRAINT; Schema: public; Owner: rich -- @@ -691,7 +701,7 @@ ALTER TABLE ONLY contacts -- --- TOC entry 70 (OID 536029) +-- TOC entry 71 (OID 536029) -- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -700,7 +710,7 @@ ALTER TABLE ONLY contents -- --- TOC entry 73 (OID 536384) +-- TOC entry 74 (OID 536384) -- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -709,7 +719,7 @@ ALTER TABLE ONLY hostnames -- --- TOC entry 71 (OID 536394) +-- TOC entry 72 (OID 536394) -- Name: hosts_hostname_cn; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -718,7 +728,7 @@ ALTER TABLE ONLY hosts -- --- TOC entry 66 (OID 536404) +-- TOC entry 67 (OID 536404) -- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -727,7 +737,7 @@ ALTER TABLE ONLY pages -- --- TOC entry 74 (OID 536920) +-- TOC entry 75 (OID 536920) -- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -736,7 +746,7 @@ ALTER TABLE ONLY email_notify -- --- TOC entry 75 (OID 537160) +-- TOC entry 76 (OID 537160) -- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -745,7 +755,7 @@ ALTER TABLE ONLY images -- --- TOC entry 76 (OID 537175) +-- TOC entry 77 (OID 537175) -- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -754,7 +764,7 @@ ALTER TABLE ONLY files -- --- TOC entry 67 (OID 539155) +-- TOC entry 68 (OID 539155) -- Name: pages_redirect_cn; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -763,7 +773,7 @@ ALTER TABLE ONLY pages -- --- TOC entry 77 (OID 540827) +-- TOC entry 78 (OID 540827) -- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -772,7 +782,7 @@ ALTER TABLE ONLY users -- --- TOC entry 78 (OID 540837) +-- TOC entry 79 (OID 540837) -- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -781,7 +791,7 @@ ALTER TABLE ONLY usercookies -- --- TOC entry 68 (OID 540942) +-- TOC entry 69 (OID 540942) -- Name: $2; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -790,7 +800,7 @@ ALTER TABLE ONLY pages -- --- TOC entry 69 (OID 540947) +-- TOC entry 70 (OID 540947) -- Name: pages_user_cn; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -799,7 +809,7 @@ ALTER TABLE ONLY pages -- --- TOC entry 79 (OID 540966) +-- TOC entry 80 (OID 540966) -- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -808,7 +818,7 @@ ALTER TABLE ONLY sitemenu -- --- TOC entry 80 (OID 540972) +-- TOC entry 81 (OID 540972) -- Name: sitemenu_url_cn; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -817,7 +827,7 @@ ALTER TABLE ONLY sitemenu -- --- TOC entry 81 (OID 542613) +-- TOC entry 82 (OID 542613) -- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -826,7 +836,7 @@ ALTER TABLE ONLY contacts -- --- TOC entry 82 (OID 542622) +-- TOC entry 83 (OID 542622) -- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -835,7 +845,7 @@ ALTER TABLE ONLY contact_emails -- --- TOC entry 72 (OID 543506) +-- TOC entry 73 (OID 543506) -- Name: hosts_theme_css_cn; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -844,7 +854,7 @@ ALTER TABLE ONLY hosts -- --- TOC entry 83 (OID 543759) +-- TOC entry 84 (OID 543759) -- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -853,7 +863,7 @@ ALTER TABLE ONLY page_emails -- --- TOC entry 84 (OID 543764) +-- TOC entry 85 (OID 543764) -- Name: page_emails_url_cn; Type: FK CONSTRAINT; Schema: public; Owner: rich -- @@ -862,7 +872,7 @@ ALTER TABLE ONLY page_emails -- --- TOC entry 85 (OID 543791) +-- TOC entry 86 (OID 543791) -- Name: $1; Type: FK CONSTRAINT; Schema: public; Owner: rich -- diff --git a/scripts/admin/create_host.ml b/scripts/admin/create_host.ml index 239e9ca..9f9eab6 100644 --- a/scripts/admin/create_host.ml +++ b/scripts/admin/create_host.ml @@ -1,7 +1,7 @@ (* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. - * $Id: create_host.ml,v 1.3 2004/09/09 12:21:22 rich Exp $ + * $Id: create_host.ml,v 1.4 2004/09/25 11:45:59 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 @@ -62,25 +62,126 @@ let run r = let hostnames = List.map check_hostname hostnames in let hostnames = List.filter ((<>) "") hostnames in - (* Update the database. *) - let sth = dbh#prepare_cached - "set constraints \"hosts_hostname_cn\" deferred" in + let sth = + dbh#prepare_cached "set constraints hosts_hostname_cn deferred" in sth#execute []; let sth = dbh#prepare_cached "insert into hosts (canonical_hostname) - values (?)" in + values (?)" in sth#execute [`String canonical_hostname]; let hostid = sth#serial "hosts_id_seq" in let sth = dbh#prepare_cached "insert into hostnames (hostid, name) - values (?, ?)" in + values (?, ?)" in sth#execute [`Int hostid; `String canonical_hostname]; List.iter (fun name -> sth#execute [`Int hostid; `String name]) hostnames; - let sth = dbh#prepare_cached "insert into pages (hostid, url, title, + (* Are we creating a blank site or copying a template? *) + if not (q#param_true "template") then ( + (* Blank site. *) + + let sth = dbh#prepare_cached "insert into pages (hostid, url, title, description) values (?, 'index', ?, ?)" in - sth#execute [`Int hostid; `String title; `String title]; + sth#execute [`Int hostid; `String title; `String title]; + + ) else ( + (* Copy from template. *) + let template = int_of_string (q#param "template") in + + (*dbh#set_debug true;*) + + let sth = dbh#prepare_cached "select * from hosts where id = ?" in + sth#execute [`Int template]; + + let names = sth#names in + let row = sth#fetch1 () in + sth#finish (); + + List.iter + (fun (name, field) -> + if name <> "id" && name <> "canonical_hostname" then ( + let sql = "update hosts set " ^ name ^ " = ? where id = ?" in + let sth = dbh#prepare_cached sql in + sth#execute [field; `Int hostid] + ) + ) (List.combine names row); + + (* Copy pages. *) + let sth = + dbh#prepare_cached + "insert into pages (hostid, url, title, description, redirect, css) + select ?, url, title, description, redirect, css + from pages + where hostid = ? and url is not null" in + sth#execute [`Int hostid; `Int template]; + + (* Copy page contents. *) + let sth = + dbh#prepare_cached + "insert into contents (pageid, ordering, sectionname, content, + divname) + select (select id from pages where hostid = ? and url = p.url), + c.ordering, c.sectionname, c.content, c.divname + from contents c, pages p + where c.pageid = p.id and p.hostid = ? and p.url is not null" in + sth#execute [`Int hostid; `Int template]; + + (* Copy files and images. *) + let sth = + dbh#prepare_cached + "insert into files (hostid, name, content, title, mime_type) + select ?, name, content, title, mime_type + from files + where hostid = ? and name is not null" in + sth#execute [`Int hostid; `Int template]; + + let sth = + dbh#prepare_cached + "insert into images (hostid, name, image, width, height, alt, title, + longdesc, class, mime_type, thumbnail, + tn_width, tn_height, tn_mime_type) + select ?, name, image, width, height, alt, title, longdesc, class, + mime_type, thumbnail, tn_width, tn_height, tn_mime_type + from images + where hostid = ? and name is not null" in + sth#execute [`Int hostid; `Int template]; + + (* Copy sitemenu. *) + let sth = + dbh#prepare_cached + "insert into sitemenu (hostid, url, label, ordering) + select ?, url, label, ordering from sitemenu where hostid = ?" in + sth#execute [`Int hostid; `Int template]; + + (* Copy contacts. *) + let sth = + dbh#prepare_cached + "insert into contacts (hostid, name, subject) + select ?, name, subject from contacts where hostid = ?" in + sth#execute [`Int hostid; `Int template]; + + let sth = + dbh#prepare_cached + "insert into contact_emails (contactid, email) + select (select contactid from contacts + where hostid = ? and name = c.name), ce.email + from contact_emails ce, contacts c + where ce.contactid = c.id and c.hostid = ?" in + sth#execute [`Int hostid; `Int template]; + + (* Set the title of the index page. *) + let sth = dbh#prepare_cached "update pages set title = ? + where hostid = ? and url = 'index'" in + sth#execute [`String title; `Int hostid] + ); + + (* Create the administrator user. *) + let sth = dbh#prepare_cached "insert into users (hostid, name, password, + force_password_change, can_manage_users) + values (?, 'Administrator', '123456', true, + true)" in + sth#execute [`Int hostid]; (* Commit to the database. *) dbh#commit (); diff --git a/scripts/admin/create_host_form.ml b/scripts/admin/create_host_form.ml index 5ad6676..6b55927 100644 --- a/scripts/admin/create_host_form.ml +++ b/scripts/admin/create_host_form.ml @@ -1,7 +1,7 @@ (* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. - * $Id: create_host_form.ml,v 1.4 2004/09/09 12:21:22 rich Exp $ + * $Id: create_host_form.ml,v 1.5 2004/09/25 11:45:59 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 @@ -32,7 +32,20 @@ let template = Cocanwiki_template._get_template "admin/create_host_form.html" let run r = let q = new cgi r in - (* let dbh = Cocanwiki._get_dbh r in *) + let dbh = Cocanwiki._get_dbh r in + + (* Get the template hosts. *) + let sth = dbh#prepare_cached "select id, canonical_hostname from hosts + where is_template order by 2" in + sth#execute []; + + let table = sth#map (function [`Int id; `String canonical_hostname] -> + [ "id", Template.VarString (string_of_int id); + "canonical_hostname", + Template.VarString canonical_hostname ] + | _ -> assert false) in + template#table "templates" table; + q#template template let () = diff --git a/templates/admin/create_host_form.html b/templates/admin/create_host_form.html index f2d8699..ea75e75 100644 --- a/templates/admin/create_host_form.html +++ b/templates/admin/create_host_form.html @@ -25,10 +25,19 @@ + Copy from template: + + + + + - -- 1.8.3.1