--- /dev/null
+#!/usr/bin/perl
+package IkiWiki::Plugin::remove;
+
+use warnings;
+use strict;
+use IkiWiki 2.00;
+
+sub import { #{{{
+ hook(type => "formbuilder_setup", id => "remove", call => \&formbuilder_setup);
+ hook(type => "formbuilder", id => "remove", call => \&formbuilder);
+ hook(type => "sessioncgi", id => "remove", call => \&sessioncgi);
+
+} # }}}
+
+sub formbuilder_setup (@) { #{{{
+ my %params=@_;
+ my $form=$params{form};
+ my $q=$params{cgi};
+
+ if (defined $form->field("do") && $form->field("do") eq "edit") {
+ # Removal button for the page, and also for attachments.
+ push @{$params{buttons}}, "Remove";
+ $form->tmpl_param("field-remove" => '<input name="_submit" type="submit" value="Remove Attachments" />');
+ }
+} #}}}
+
+sub confirmation_form ($$) { #{{{
+ my $q=shift;
+ my $session=shift;
+
+ eval q{use CGI::FormBuilder};
+ error($@) if $@;
+ my @fields=qw(do page);
+ my $f = CGI::FormBuilder->new(
+ name => "remove",
+ header => 0,
+ charset => "utf-8",
+ method => 'POST',
+ javascript => 0,
+ params => $q,
+ action => $config{cgiurl},
+ stylesheet => IkiWiki::baseurl()."style.css",
+ fields => \@fields,
+ );
+
+ $f->field(name => "do", type => "hidden", value => "remove", force => 1);
+
+ return $f, ["Remove", "Cancel"];
+} #}}}
+
+sub removal_confirm ($$@) {
+ my $q=shift;
+ my $session=shift;
+ my $attachment=shift;
+ my @pages=@_;
+
+ # Save current form state to allow returning to it later
+ # without losing any edits.
+ # (But don't save what button was submitted, to avoid
+ # looping back to here.)
+ # Note: "_submit" is CGI::FormBuilder internals.
+ $q->param(-name => "_submit", -value => "");
+ $session->param(postremove => scalar $q->Vars);
+ IkiWiki::cgi_savesession($session);
+
+ my ($f, $buttons)=confirmation_form($q, $session);
+ $f->title(sprintf(gettext("confirm removal of %s"),
+ join(", ", map { IkiWiki::pagetitle($_) } @pages)));
+ $f->field(name => "page", type => "hidden", value => \@pages, force => 1);
+ if (defined $attachment) {
+ $f->field(name => "attachment", type => "hidden",
+ value => $attachment, force => 1);
+ }
+
+ IkiWiki::showform($f, $buttons, $session, $q);
+ exit 0;
+}
+
+sub postremove ($) {
+ my $session=shift;
+
+ # Load saved form state and return to edit form.
+ my $postremove=CGI->new($session->param("postremove"));
+ $session->clear("postremove");
+ IkiWiki::cgi_savesession($session);
+ IkiWiki::cgi($postremove, $session);
+}
+
+sub formbuilder (@) { #{{{
+ my %params=@_;
+ my $form=$params{form};
+
+ if (defined $form->field("do") && $form->field("do") eq "edit") {
+ my $q=$params{cgi};
+ my $session=$params{session};
+
+ if ($form->submitted eq "Remove") {
+ removal_confirm($q, $session, 0, $form->field("page"));
+ }
+ elsif ($form->submitted eq "Remove Attachments") {
+ removal_confirm($q, $session, 1, $q->param("attachment_select"));
+ }
+ }
+} #}}}
+
+sub sessioncgi ($$) { #{{{
+ my $q=shift;
+
+ if ($q->param("do") eq 'remove') {
+ my $session=shift;
+ my ($form, $buttons)=confirmation_form($q, $session);
+ IkiWiki::decode_form_utf8($form);
+
+ if ($form->submitted eq 'Cancel') {
+ postremove($session);
+ }
+ elsif ($form->submitted eq 'Remove' && $form->validate) {
+ my @pages=$q->param("page");
+
+ # Validate removal by checking that the page exists,
+ # and that the user is allowed to edit(/remove) it.
+ my @files;
+ foreach my $page (@pages) {
+ if (! exists $pagesources{$page}) {
+ error(sprintf(gettext("%s does not exist"),
+ htmllink("", "", $page, noimageinline => 1)));
+ }
+ IkiWiki::check_canedit($page, $q, $session);
+
+ my $file=$pagesources{$page};
+ if (! -e "$config{srcdir}/$file") {
+ error(sprintf(gettext("%s is not in the srcdir, so it cannot be deleted"), $file));
+ }
+ elsif (! -f "$config{srcdir}/$file") {
+ error(sprintf(gettext("%s is not a file"), $file));
+ }
+
+ # This untaint is safe because we've
+ # verified the file is a known source file,
+ # and is in the srcdir, and is a regular
+ # file.
+ push @files, IkiWiki::possibly_foolish_untaint($file);
+ }
+
+ # Do removal, and update the wiki.
+ require IkiWiki::Render;
+ if ($config{rcs}) {
+ IkiWiki::disable_commit_hook();
+ foreach my $file (@files) {
+ my $token=IkiWiki::rcs_prepedit($file);
+ IkiWiki::rcs_remove($file);
+ IkiWiki::rcs_commit($file, gettext("removed"),
+ $token, $session->param("name"), $ENV{REMOTE_ADDR});
+ }
+ IkiWiki::enable_commit_hook();
+ IkiWiki::rcs_update();
+ }
+ else {
+ foreach my $file (@files) {
+ IkiWiki::prune("$config{srcdir}/$file");
+ }
+ }
+ IkiWiki::refresh();
+ IkiWiki::saveindex();
+
+ if ($q->param("attachment")) {
+ # Attachments were deleted, so redirect
+ # back to the edit form.
+ postremove($session);
+ }
+ else {
+ # The page is gone, so redirect to parent
+ # of the page.
+ my $parent=IkiWiki::dirname($pages[0]);
+ if (! exists $pagesources{$parent}) {
+ $parent="index";
+ }
+ IkiWiki::redirect($q, $config{url}."/".htmlpage($parent));
+ }
+ }
+ else {
+ IkiWiki::showform($form, $buttons, $session, $q);
+ }
+
+ exit 0;
+ }
+}
+
+1
## RCS
-Two new optional functions are added to the RCS interface:
+Two new functions are added to the RCS interface:
-* `rcs_delete(file, message, rcstoken, user, ipaddr)`
-* `rcs_rename(old, new, message, rcstoken, user, ipaddr)`
-
-The page move/rename code will check if these are not available, and error
-out.
-
-Similar to `rcs_commit` both of these take a rcstoken, which is generated
-by an earlier `rcs_prepedit`.
+* `rcs_remove(file)`
+* `rcs_rename(old, new)`
## conflicts
* Alice clicks "delete" button for a page; Bob makes a modification;
Alice confirms deletion. Ideally in this case, Alice should get an error
message that there's a conflict.
+ Update: In my current code, alice's deletion will fail if the file was
+ moved or deleted in the meantime; if the file was modified since alice
+ clicked on the delete button, the modifications will be deleted too. I
+ think this is acceptable.
* Alice opens edit UI for a page; Bob makes a modification; Alice
clicks delete button and confirms deletion. Again here, Alice should get
a conflict error. Note that this means that the rcstoken should be
recorded when the edit UI is first opened, not when the delete button is
hit.
+ Update: Again here, there's no conflict, but the delete succeeds. Again,
+ basically acceptible.
* Alice and Bob both try to delete a page at the same time. It's fine for
the second one to get a message that it no longer exists. Or just to
silently fail to delete the deleted page..
+ Update: It will display an error to the second one that the page doesn't
+ exist.
* Alice deletes a page; Bob had edit window open for it, and saves
it afterwards. I think that Bob should win in this case; Alice can always
notice the page has been added back, and delete it again.
+ Update: Bob wins.
* Alice clicks "rename" button for a page; Bob makes a modification;
Alice confirms rename. This case seems easy, it should just rename the
modified page.