From 73fe8c520c9864bf4091ec88d2dfc389473cc188 Mon Sep 17 00:00:00 2001 From: rich Date: Wed, 26 Jul 2006 16:26:42 +0000 Subject: [PATCH] Support for editing macros. --- schema/cocanwiki.sql | 3 +- scripts/create_macro.ml | 63 ++++++++++++++++++++++++++++++++++++++++ scripts/create_macro_form.ml | 36 +++++++++++++++++++++++ scripts/delete_macro.ml | 52 +++++++++++++++++++++++++++++++++ scripts/delete_macro_form.ml | 61 ++++++++++++++++++++++++++++++++++++++ scripts/edit_macro.ml | 47 ++++++++++++++++++++++++++++++ scripts/edit_macro_form.ml | 46 +++++++++++++++++++++++++++++ scripts/edit_macros.ml | 46 +++++++++++++++++++++++++++++ scripts/host_menu.ml | 4 ++- scripts/lib/cocanwiki.ml | 9 +++++- templates/create_macro_form.html | 33 +++++++++++++++++++++ templates/delete_macro_form.html | 29 ++++++++++++++++++ templates/edit_macro_form.html | 35 ++++++++++++++++++++++ templates/edit_macros.html | 37 +++++++++++++++++++++++ templates/host_menu.html | 4 +++ 15 files changed, 502 insertions(+), 3 deletions(-) create mode 100644 scripts/create_macro.ml create mode 100644 scripts/create_macro_form.ml create mode 100644 scripts/delete_macro.ml create mode 100644 scripts/delete_macro_form.ml create mode 100644 scripts/edit_macro.ml create mode 100644 scripts/edit_macro_form.ml create mode 100644 scripts/edit_macros.ml create mode 100644 templates/create_macro_form.html create mode 100644 templates/delete_macro_form.html create mode 100644 templates/edit_macro_form.html create mode 100644 templates/edit_macros.html diff --git a/schema/cocanwiki.sql b/schema/cocanwiki.sql index d9b4dae..99b0290 100644 --- a/schema/cocanwiki.sql +++ b/schema/cocanwiki.sql @@ -1298,7 +1298,8 @@ CREATE TABLE users ( force_password_change boolean DEFAULT false NOT NULL, can_import_mail boolean DEFAULT false NOT NULL, email_notify boolean DEFAULT true NOT NULL, - invite text + invite text, + can_edit_macros boolean DEFAULT false NOT NULL ); diff --git a/scripts/create_macro.ml b/scripts/create_macro.ml new file mode 100644 index 0000000..96fed62 --- /dev/null +++ b/scripts/create_macro.ml @@ -0,0 +1,63 @@ +(* COCANWIKI - a wiki written in Objective CAML. + * Written by Richard W.M. Jones . + * Copyright (C) 2004 Merjis Ltd. + * $Id: create_macro.ml,v 1.1 2006/07/26 16:26:43 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 + +open Cocanwiki +open Cocanwiki_ok +open Cocanwiki_strings + +let name_re = Pcre.regexp "^\\w+$" + +let run r (q : cgi) dbh hostid _ _ = + let name = trim (q#param "name") in + let content = q#param "content" in + + if name = "" then ( + error ~back_button:true ~title:"Name field missing" + dbh hostid q "You must name your macro."; + return () + ); + + if not (Pcre.pmatch ~rex:name_re name) then ( + error ~back_button:true ~title:"Name field invalid" + dbh hostid q "Name field can only contain letters and numbers."; + return () + ); + + (* XXX Check it doesn't already exist. *) + + (* Update the database. *) + PGSQL(dbh) "insert into macros (hostid, name, content) + values ($hostid, $name, $content)"; + + (* Finish off. *) + PGOCaml.commit dbh; + + let buttons = [ ok_button "/_bin/edit_macros.cmo" ] in + ok ~title:"Macro created" ~buttons dbh hostid q + "That macro was created." + +let () = + register_script ~restrict:[CanEditMacros] run diff --git a/scripts/create_macro_form.ml b/scripts/create_macro_form.ml new file mode 100644 index 0000000..9346c81 --- /dev/null +++ b/scripts/create_macro_form.ml @@ -0,0 +1,36 @@ +(* COCANWIKI - a wiki written in Objective CAML. + * Written by Richard W.M. Jones . + * Copyright (C) 2004 Merjis Ltd. + * $Id: create_macro_form.ml,v 1.1 2006/07/26 16:26:43 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 + +open Cocanwiki +open Cocanwiki_template + +let run r (q : cgi) dbh hostid _ _ = + let template = get_template dbh hostid "create_macro_form.html" in + + q#template template + +let () = + register_script ~restrict:[CanEditMacros] run diff --git a/scripts/delete_macro.ml b/scripts/delete_macro.ml new file mode 100644 index 0000000..fc4a6cd --- /dev/null +++ b/scripts/delete_macro.ml @@ -0,0 +1,52 @@ +(* COCANWIKI - a wiki written in Objective CAML. + * Written by Richard W.M. Jones . + * Copyright (C) 2004 Merjis Ltd. + * $Id: delete_macro.ml,v 1.1 2006/07/26 16:26:43 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 + +open Cocanwiki +open Cocanwiki_ok + +let run r (q : cgi) dbh hostid { hostname = hostname } _ = + (* Cancel? *) + if q#param_true "cancel" then + q#redirect ("http://" ^ hostname ^ "/_bin/edit_macros.cmo"); + + (* We can delete multiple macros from this script, which is + * quite unusual. + *) + let names = q#param_all "name" in + if names = [] then + q#redirect ("http://" ^ hostname ^ "/_bin/edit_macros.cmo"); + + (* Delete them. *) + PGSQL(dbh) "delete from macros where hostid = $hostid and name in $@names"; + + (* Finish off. *) + PGOCaml.commit dbh; + + ok ~title:"Macro deleted" ~buttons:[ok_button "/_bin/edit_macros.cmo"] + dbh hostid q "That macro was deleted." + +let () = + register_script ~restrict:[CanEditMacros] run diff --git a/scripts/delete_macro_form.ml b/scripts/delete_macro_form.ml new file mode 100644 index 0000000..609a23d --- /dev/null +++ b/scripts/delete_macro_form.ml @@ -0,0 +1,61 @@ +(* COCANWIKI - a wiki written in Objective CAML. + * Written by Richard W.M. Jones . + * Copyright (C) 2004 Merjis Ltd. + * $Id: delete_macro_form.ml,v 1.1 2006/07/26 16:26:43 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 + +open Cocanwiki +open Cocanwiki_template + +let run r (q : cgi) dbh hostid { hostname = hostname } _ = + let template = get_template dbh hostid "delete_macro_form.html" in + + (* We can delete multiple macros from this script, which is + * quite unusual. + *) + let names = q#param_all "name" in + if names = [] then + q#redirect ("http://" ^ hostname ^ "/_bin/edit_macros.cmo"); + + let rows = PGSQL(dbh) + "select name, content from macros + where hostid = $hostid and name in $@names + order by name" in + + let table = + List.map ( + fun (name, content) -> + [ "name", Template.VarString name; + "content", Template.VarString content ] + ) rows in + template#table "deletes" table; + + let table = + List.map (fun name -> + [ "name", Template.VarString name ]) names in + template#table "names" table; + + q#template template + +let () = + register_script ~restrict:[CanEditMacros] run diff --git a/scripts/edit_macro.ml b/scripts/edit_macro.ml new file mode 100644 index 0000000..71ec1de --- /dev/null +++ b/scripts/edit_macro.ml @@ -0,0 +1,47 @@ +(* COCANWIKI - a wiki written in Objective CAML. + * Written by Richard W.M. Jones . + * Copyright (C) 2004 Merjis Ltd. + * $Id: edit_macro.ml,v 1.1 2006/07/26 16:26:43 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 + +open Cocanwiki +open Cocanwiki_ok +open Cocanwiki_strings + +let run r (q : cgi) dbh hostid _ _ = + let name = q#param "name" in + let content = q#param "content" in + + (* Update the database. *) + PGSQL(dbh) "update macros set content = $content + where hostid = $hostid and name = $name"; + + (* Finish off. *) + PGOCaml.commit dbh; + + let buttons = [ ok_button "/_bin/edit_macros.cmo" ] in + ok ~title:"Macro edited" ~buttons + dbh hostid q "The macro was edited." + +let () = + register_script ~restrict:[CanEditMacros] run diff --git a/scripts/edit_macro_form.ml b/scripts/edit_macro_form.ml new file mode 100644 index 0000000..6a2f147 --- /dev/null +++ b/scripts/edit_macro_form.ml @@ -0,0 +1,46 @@ +(* COCANWIKI - a wiki written in Objective CAML. + * Written by Richard W.M. Jones . + * Copyright (C) 2004 Merjis Ltd. + * $Id: edit_macro_form.ml,v 1.1 2006/07/26 16:26:43 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 + +open Cocanwiki +open Cocanwiki_template + +let run r (q : cgi) dbh hostid _ _ = + let template = get_template dbh hostid "edit_macro_form.html" in + + let name = q#param "name" in + + (* Get details from the database. *) + let rows = PGSQL(dbh) "select content from macros + where hostid = $hostid and name = $name" in + let content = List.hd rows in + + template#set "name" name; + template#set "content" content; + + q#template template + +let () = + register_script ~restrict:[CanEditMacros] run diff --git a/scripts/edit_macros.ml b/scripts/edit_macros.ml new file mode 100644 index 0000000..0292323 --- /dev/null +++ b/scripts/edit_macros.ml @@ -0,0 +1,46 @@ +(* COCANWIKI - a wiki written in Objective CAML. + * Written by Richard W.M. Jones . + * Copyright (C) 2004 Merjis Ltd. + * $Id: edit_macros.ml,v 1.1 2006/07/26 16:26:43 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 + +open Cocanwiki +open Cocanwiki_template + +let run r (q : cgi) dbh hostid _ _ = + let template = get_template dbh hostid "edit_macros.html" in + + (* Get a list of the macros for this host. *) + let macros = + PGSQL(dbh) "select name from macros where hostid = $hostid order by 1" in + + let macros = List.map ( + fun name -> + [ "name", Template.VarString name ] + ) macros in + template#table "macros" macros; + + q#template template + +let () = + register_script ~restrict:[CanEditMacros] run diff --git a/scripts/host_menu.ml b/scripts/host_menu.ml index 0aa5ea9..e6daeef 100644 --- a/scripts/host_menu.ml +++ b/scripts/host_menu.ml @@ -1,7 +1,7 @@ (* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. - * $Id: host_menu.ml,v 1.9 2006/03/27 19:10:29 rich Exp $ + * $Id: host_menu.ml,v 1.10 2006/07/26 16:26:43 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 @@ -34,10 +34,12 @@ let run r (q : cgi) dbh hostid host user = let can_manage_users = can_manage_users host user in let can_manage_contacts = can_manage_contacts host user in let can_edit_global_css = can_edit_global_css host user in + let can_edit_macros = can_edit_macros host user in let can_manage_site = can_manage_site host user in template#conditional "can_manage_users" can_manage_users; template#conditional "can_manage_contacts" can_manage_contacts; template#conditional "can_edit_global_css" can_edit_global_css; + template#conditional "can_edit_macros" can_edit_macros; template#conditional "can_manage_site" can_manage_site; if can_manage_site then ( diff --git a/scripts/lib/cocanwiki.ml b/scripts/lib/cocanwiki.ml index 1ce26cf..8772306 100644 --- a/scripts/lib/cocanwiki.ml +++ b/scripts/lib/cocanwiki.ml @@ -1,7 +1,7 @@ (* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. - * $Id: cocanwiki.ml,v 1.11 2006/07/26 13:19:51 rich Exp $ + * $Id: cocanwiki.ml,v 1.12 2006/07/26 16:26:44 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 @@ -42,6 +42,7 @@ type host_t = { hostname : string; *) type permissions_t = CanView | CanEdit | CanManageUsers | CanManageContacts | CanManageSite | CanEditGlobalCSS | CanImportMail + | CanEditMacros (* User preferences and other settings (some cannot be changed by the user). *) type prefs_t = { @@ -67,6 +68,7 @@ let can_manage_contacts host = test_permission host CanManageContacts let can_manage_site host = test_permission host CanManageSite let can_edit_global_css host = test_permission host CanEditGlobalCSS let can_import_mail host = test_permission host CanImportMail +let can_edit_macros host = test_permission host CanEditMacros let get_uri_from_request r = try @@ -192,6 +194,7 @@ let register_script ?(restrict = []) ?(anonymous = true) run = "select u.id, u.name, u.can_edit, u.can_manage_users, u.can_manage_contacts, u.can_manage_site, u.can_edit_global_css, u.can_import_mail, + u.can_edit_macros, u.email, u.email_notify from usercookies uc, users u where uc.cookie = $cookie @@ -201,6 +204,7 @@ let register_script ?(restrict = []) ?(anonymous = true) run = | [userid, name, can_edit, can_manage_users, can_manage_contacts, can_manage_site, can_edit_global_css, can_import_mail, + can_edit_macros, email, email_notify] -> (* Every logged in user can view. *) let perms = [CanView] in @@ -222,6 +226,9 @@ let register_script ?(restrict = []) ?(anonymous = true) run = let perms = if can_import_mail then CanImportMail :: perms else perms in + let perms = + if can_edit_macros then CanEditMacros :: perms + else perms in (* Preferences. *) let prefs = { email = email; email_notify = email_notify; } in diff --git a/templates/create_macro_form.html b/templates/create_macro_form.html new file mode 100644 index 0000000..0af167e --- /dev/null +++ b/templates/create_macro_form.html @@ -0,0 +1,33 @@ + + + +Create a macro + + + + + +

Create a macro

+ +
+ + + + + + + + + + + + + + + +
Macro name:
Must be letters and numbers only
Content:
+
+ +::include(footer.html):: + + \ No newline at end of file diff --git a/templates/delete_macro_form.html b/templates/delete_macro_form.html new file mode 100644 index 0000000..bd571cb --- /dev/null +++ b/templates/delete_macro_form.html @@ -0,0 +1,29 @@ + + + +Delete macro + + + + + +

Delete macro

+ +

+Are you sure you want to delete the macro shown below? +

+ + + +::table(deletes)::::end:: +
Name Content
::name_html:: ::content_html::
+ +
+::table(names)::::end:: + + +
+ +::include(footer.html):: + + \ No newline at end of file diff --git a/templates/edit_macro_form.html b/templates/edit_macro_form.html new file mode 100644 index 0000000..1b35f86 --- /dev/null +++ b/templates/edit_macro_form.html @@ -0,0 +1,35 @@ + + + +Edit macro: ::name_html:: + + + + + +

Edit macro: ::name_html::

+ +
+ + + + + + + + + + + + + + + + +
Macro name: ::name_html::
Content: +
+
+ +::include(footer.html):: + + \ No newline at end of file diff --git a/templates/edit_macros.html b/templates/edit_macros.html new file mode 100644 index 0000000..b9e1cd5 --- /dev/null +++ b/templates/edit_macros.html @@ -0,0 +1,37 @@ + + + +Macros + + + + + +

Macros

+ + + + + + +::table(macros):: + + + + +::end:: +
Name  
::name_html:: + Edit | + Delete +
+ +

+Add a new macro ... + +

+Back to sitewide settings +

+ +::include(footer.html):: + + \ No newline at end of file diff --git a/templates/host_menu.html b/templates/host_menu.html index 36ac6bc..5b2c3c5 100644 --- a/templates/host_menu.html +++ b/templates/host_menu.html @@ -35,6 +35,10 @@
  • Edit global stylesheet
  • ::end:: +::if(can_edit_macros):: +
  • Edit macros
  • +::end:: + ::if(can_manage_site):: -- 1.8.3.1