+}
+
+sub getsetup () {
+ return
+ plugin => {
+ safe => 1,
+ rebuild => 0,
+ section => "web",
+ },
+}
+
+sub allowed_dirs {
+ return grep { defined $_ } (
+ $config{srcdir},
+ $IkiWiki::Plugin::transient::transientdir,
+ );
+}
+
+sub check_canremove ($$$) {
+ my $page=shift;
+ my $q=shift;
+ my $session=shift;
+
+ # Must be a known source file.
+ if (! exists $pagesources{$page}) {
+ error(sprintf(gettext("%s does not exist"),
+ htmllink("", "", $page, noimageinline => 1)));
+ }
+
+ # Must exist in either the srcdir or a suitable underlay (e.g.
+ # transient underlay), and be a regular file.
+ my $file=$pagesources{$page};
+ my $dir;
+
+ foreach my $srcdir (allowed_dirs()) {
+ if (-e "$srcdir/$file") {
+ $dir = $srcdir;
+ last;
+ }
+ }
+
+ if (! defined $dir) {
+ error(sprintf(gettext("%s is not in the srcdir, so it cannot be deleted"), $file));
+ }
+ elsif (-l "$dir/$file" && ! -f _) {
+ error(sprintf(gettext("%s is not a file"), $file));
+ }
+
+ # If a user can't upload an attachment, don't let them delete it.
+ # This is sorta overkill, but better safe than sorry.
+ if (! defined pagetype($pagesources{$page})) {
+ if (IkiWiki::Plugin::attachment->can("check_canattach")) {
+ IkiWiki::Plugin::attachment::check_canattach($session, $page, "$dir/$file");
+ }
+ else {
+ error("removal of attachments is not allowed");
+ }
+ }
+
+ my $canremove;
+ IkiWiki::run_hooks(canremove => sub {
+ return if defined $canremove;
+ my $ret=shift->(page => $page, cgi => $q, session => $session);
+ if (defined $ret) {
+ if ($ret eq "") {
+ $canremove=1;
+ }
+ elsif (ref $ret eq 'CODE') {
+ $ret->();
+ $canremove=0;
+ }
+ elsif (defined $ret) {
+ error($ret);
+ $canremove=0;
+ }
+ }
+ });
+ return defined $canremove ? $canremove : 1;
+}