+sub check_canrename ($$$$$$) { #{{{
+ my $src=shift;
+ my $srcfile=shift;
+ my $dest=shift;
+ my $destfile=shift;
+ my $q=shift;
+ my $session=shift;
+
+ my $attachment=! defined pagetype($pagesources{$src});
+
+ # Must be a known source file.
+ if (! exists $pagesources{$src}) {
+ error(sprintf(gettext("%s does not exist"),
+ htmllink("", "", $src, noimageinline => 1)));
+ }
+
+ # Must exist on disk, and be a regular file.
+ if (! -e "$config{srcdir}/$srcfile") {
+ error(sprintf(gettext("%s is not in the srcdir, so it cannot be renamed"), $srcfile));
+ }
+ elsif (-l "$config{srcdir}/$srcfile" && ! -f _) {
+ error(sprintf(gettext("%s is not a file"), $srcfile));
+ }
+
+ # Must be editable.
+ IkiWiki::check_canedit($src, $q, $session);
+ if ($attachment) {
+ if (IkiWiki::Plugin::attachment->can("check_canattach")) {
+ IkiWiki::Plugin::attachment::check_canattach($session, $src, $srcfile);
+ }
+ else {
+ error("renaming of attachments is not allowed");
+ }
+ }
+
+ # Dest checks can be omitted by passing undef.
+ if (defined $dest) {
+ if ($srcfile eq $destfile) {
+ error(gettext("no change to the file name was specified"));
+ }
+
+ # Must be a legal filename, and not absolute.
+ if (IkiWiki::file_pruned($destfile, $config{srcdir}) ||
+ $destfile=~/^\//) {
+ error(sprintf(gettext("illegal name")));
+ }
+
+ # Must not be a known source file.
+ if ($src ne $dest && exists $pagesources{$dest}) {
+ error(sprintf(gettext("%s already exists"),
+ htmllink("", "", $dest, noimageinline => 1)));
+ }
+
+ # Must not exist on disk already.
+ if (-l "$config{srcdir}/$destfile" || -e _) {
+ error(sprintf(gettext("%s already exists on disk"), $destfile));
+ }
+
+ # Must be editable.
+ IkiWiki::check_canedit($dest, $q, $session);
+ if ($attachment) {
+ # Note that $srcfile is used here, not $destfile,
+ # because it wants the current file, to check it.
+ IkiWiki::Plugin::attachment::check_canattach($session, $dest, $srcfile);
+ }