From 68902d9902c8dad21fc8050c5990c3ef91debb08 Mon Sep 17 00:00:00 2001 From: CrawfordCurrie Date: Thu, 30 Apr 2009 08:59:54 +0000 Subject: [PATCH] Item5384: added basic support for renaming deleted attachments. It was getting on my tits. git-svn-id: http://svn.foswiki.org/trunk@3776 0b4bb1d4-4e5a-0410-9cc4-b2b747904278 --- .../templates/attachagain.pattern.tmpl | 2 +- .../renameattachmentdelete.pattern.tmpl | 11 +- PatternSkin/templates/renamebase.pattern.tmpl | 1 + core/data/System/VarALLVARIABLES.txt | 1 + core/lib/Foswiki/UI/Rename.pm | 104 ++++++++++++------ core/templates/moveattachment.tmpl | 3 + core/templates/rename.tmpl | 2 + core/templates/renamebase.tmpl | 8 +- core/templates/renameconfirm.tmpl | 5 + core/templates/renamedelete.tmpl | 4 + core/templates/renamewebbase.tmpl | 3 +- 11 files changed, 105 insertions(+), 39 deletions(-) diff --git a/PatternSkin/templates/attachagain.pattern.tmpl b/PatternSkin/templates/attachagain.pattern.tmpl index 0fcf362d89..e005b49748 100644 --- a/PatternSkin/templates/attachagain.pattern.tmpl +++ b/PatternSkin/templates/attachagain.pattern.tmpl @@ -21,7 +21,7 @@ -%TMPL:DEF{"movedelete"}%
%MAKETEXT{"Move"}% %MAKETEXT{"or"}% %MAKETEXT{"Delete"}% =%FILENAME%=
%TMPL:END% +%TMPL:DEF{"movedelete"}%
%MAKETEXT{"Move"}% %MAKETEXT{"or"}% %MAKETEXT{"Delete"}% =%FILENAME%=
%TMPL:END% diff --git a/PatternSkin/templates/renameattachmentdelete.pattern.tmpl b/PatternSkin/templates/renameattachmentdelete.pattern.tmpl index 923be0972c..bf2b861fce 100644 --- a/PatternSkin/templates/renameattachmentdelete.pattern.tmpl +++ b/PatternSkin/templates/renameattachmentdelete.pattern.tmpl @@ -29,17 +29,18 @@ -%TMPL:DEF{"totopictitle"}%---+++ %MAKETEXT{"Optionally enter a different topic name:" args="%URLPARAM{web}%"}%%TMPL:END% +%TMPL:DEF{"totopictitle"}%---+++ %MAKETEXT{"Optionally enter a different topic name:"}%%TMPL:END% +%TMPL:DEF{"toattachmenttitle"}%---+++ %MAKETEXT{"Optionally enter a different attachment name:"}%%TMPL:END% +%TMPL:DEF{"newattachment"}%

%TMPL:P{"attachmenthelp"}%%TMPL:END% -%TMPL:DEF{"newtopic"}%

%TMPL:P{"namehelp"}%%TMPL:END% +%TMPL:DEF{"newtopic"}%

%TMPL:P{"namehelp"}%%TMPL:END% +%TMPL:DEF{"namehelp"}%

%MAKETEXT{"[_1] is the default topic for deleted attachments." args="[[%TRASHWEB%.TrashAttachment][TrashAttachment]]"}% %MAKETEXT{"If you enter a different name, make sure the topic exists."}%

%TMPL:END% -%TMPL:DEF{"namehelp"}%

%MAKETEXT{"[_1] is the default topic for deleted attachments." args="[[%TRASHWEB%.TrashAttachment][TrashAttachment]]"}% %MAKETEXT{"If you enter a different name, make sure the topic does exist."}%

%TMPL:END% - - +%TMPL:DEF{"attachmenthelp"}%

%MAKETEXT{"The attachment may have to be renamed if an attachment called '%FILENAME%' already exists in the target topic."}%

%TMPL:END% %TMPL:DEF{"toolbar"}%%TMPL:END% %TMPL:DEF{"pagehelp"}%%TMPL:END% diff --git a/PatternSkin/templates/renamebase.pattern.tmpl b/PatternSkin/templates/renamebase.pattern.tmpl index 085f1f1030..2daed867b5 100644 --- a/PatternSkin/templates/renamebase.pattern.tmpl +++ b/PatternSkin/templates/renamebase.pattern.tmpl @@ -26,6 +26,7 @@
%TMPL:P{"changefrom"}%
%TMPL:P{"toweb"}%
%TMPL:P{"totopic"}%
+
%TMPL:P{"toattachment"}%
%TMPL:P{"searchresultsstep"}%%TMPL:END% diff --git a/core/data/System/VarALLVARIABLES.txt b/core/data/System/VarALLVARIABLES.txt index acb09a30b1..d61d852ec4 100644 --- a/core/data/System/VarALLVARIABLES.txt +++ b/core/data/System/VarALLVARIABLES.txt @@ -5,3 +5,4 @@ * Syntax: =%ALLVARIABLES%= * Expands to: a table showing all defined [[macros]] in the current context +*Deprecated 2009-04-29* in favour of [[VarSHOWPREFERENCE][SHOWPREFERENCE]] diff --git a/core/lib/Foswiki/UI/Rename.pm b/core/lib/Foswiki/UI/Rename.pm index 8a9cbabdb9..48f7748c52 100644 --- a/core/lib/Foswiki/UI/Rename.pm +++ b/core/lib/Foswiki/UI/Rename.pm @@ -41,6 +41,12 @@ parameters: =cut +# This function is entered twice during an interaction renaming session. The +# first time is when the parameters for the rename are being gathered +# referring topics etc) and in this case, it will terminate at either +# newWebScreen or newTopicOrAttachmentScreen. The second times is when the +# rename is proceeding, and/or all the appropriate parameters have been +# passed by the caller. In this case the rename proceeds. sub rename { my $session = shift; @@ -65,10 +71,9 @@ sub rename { sub _renameTopicOrAttachment { my ( $session, $oldWeb, $oldTopic ) = @_; - my $query = $session->{cgiQuery}; - my $newTopic = $query->param('newtopic') || ''; - - my $newWeb = $query->param('newweb') || ''; + my $query = $session->{cgiQuery}; + my $newTopic = $query->param('newtopic') || ''; + my $newWeb = $query->param('newweb') || ''; # Validate the new web name $newWeb = Foswiki::Sandbox::untaint( @@ -131,6 +136,8 @@ sub _renameTopicOrAttachment { } my $attachment = $query->param('attachment'); + my $newAttachment = $query->param('newattachment'); + my $old = Foswiki::Meta->load( $session, $oldWeb, $oldTopic ); if ($attachment) { @@ -163,14 +170,26 @@ sub _renameTopicOrAttachment { } ); - if ( $newWeb && $newTopic ) { + # Validate the new attachment name, if one was provided + if ($newAttachment) { + $newAttachment = Foswiki::Sandbox::untaint( + $newAttachment, + sub { + my ($att) = @_; + return Foswiki::Sandbox::sanitizeAttachmentName($att); + } + ); + } + + if ( $newWeb && $newTopic && $newAttachment ) { + Foswiki::UI::checkTopicExists( $session, $newWeb, $newTopic, 'rename' ); my $new = Foswiki::Meta->load( $session, $newWeb, $newTopic ); # does new attachment already exist? - if ( $new->hasAttachment($attachment) ) { + if ( $new->hasAttachment($newAttachment) ) { throw Foswiki::OopsException( 'attention', def => 'move_err', @@ -179,7 +198,7 @@ sub _renameTopicOrAttachment { params => [ $newWeb, $newTopic, - $attachment, + $newAttachment, $session->i18n->maketext( 'Attachment already exists in new topic') ] @@ -212,11 +231,14 @@ sub _renameTopicOrAttachment { ); # Has user selected new name yet? - if ( !$newTopic || $confirm ) { + if ( !$newTopic || ($attachment && !$newAttachment) || $confirm ) { + $newAttachment ||= $attachment; # Must be able to view the source to rename it Foswiki::UI::checkAccess( $session, 'VIEW', $old ); - _newTopicScreen( $session, $old, $new, $attachment, $confirm ); + + _newTopicOrAttachmentScreen( + $session, $old, $new, $attachment, $newAttachment, $confirm ); return undef; } @@ -232,7 +254,8 @@ sub _renameTopicOrAttachment { $newTopic ); } - _moveTopicOrAttachment( $session, $old, $new, $attachment, $refs ); + _moveTopicOrAttachment( + $session, $old, $new, $attachment, $newAttachment, $refs ); my $new_url; if ( $newWeb eq $Foswiki::cfg{TrashWebName} @@ -266,6 +289,7 @@ sub _renameTopicOrAttachment { $session->getScriptUrl( 0, 'view', $parentWeb, $parentTopic ); } else { + # No parent topic, redirect to home topic $new_url = $session->getScriptUrl( 0, 'view', $oldWeb, $Foswiki::cfg{HomeTopicName} ); @@ -274,7 +298,7 @@ sub _renameTopicOrAttachment { } else { - #redirect to new topic + # redirect to new topic $new_url = $session->getScriptUrl( 0, 'view', $newWeb, $newTopic ); } @@ -699,14 +723,15 @@ sub _releaseContents { # * =\@refs= - array of webg.topics that must have refs to this topic converted # Will throw Foswiki::OopsException on an error. sub _moveTopicOrAttachment { - my ( $session, $from, $to, $attachment, $refs ) = @_; + my ( $session, $from, $to, $attachment, $toattachment, $refs ) = @_; Foswiki::UI::checkAccess( $session, 'CHANGE', $from ); Foswiki::UI::checkAccess( $session, 'CHANGE', $to ); if ($attachment) { try { - $from->moveAttachment( $attachment, $to ); + $from->moveAttachment( + $attachment, $to, new_name => $toattachment ); } catch Error::Simple with { throw Foswiki::OopsException( @@ -958,10 +983,10 @@ sub _replaceInternalRefs { return $text; } -# Display screen so user can decide on new web and topic. -sub _newTopicScreen { - my ( $session, $from, $to, $attachment, $confirm, $doAllowNonWikiWord ) = - @_; +# Display screen so user can decide on new web, topic, attachment names. +sub _newTopicOrAttachmentScreen { + my ( $session, $from, $to, $attachment, $toattachment, + $confirm, $doAllowNonWikiWord ) = @_; my $query = $session->{cgiQuery}; my $tmplname = $query->param('template') || ''; @@ -974,7 +999,6 @@ sub _newTopicScreen { if ($attachment) { $tmpl = $session->templates->readTemplate( $tmplname || 'moveattachment' ); - $tmpl =~ s/%FILENAME%/$attachment/go; } elsif ($confirm) { $tmpl = $session->templates->readTemplate('renameconfirm'); @@ -988,23 +1012,41 @@ sub _newTopicScreen { $tmpl = $session->templates->readTemplate('rename'); } - if ( !$attachment && $to->web eq $Foswiki::cfg{TrashWebName} ) { + if ( $to->web eq $Foswiki::cfg{TrashWebName} ) { + # Deleting an attachment or a topic + if ( $attachment ) { + # Trashing an attachment; look for a non-conflicting name in the + # trash web + my $base = $toattachment || $attachment; + my $ext = ''; + if ($base =~ s/^(.*)(\..*?)$/$1_/) { + $ext = $2; + } + my $n = 1; + while ( $to->hasAttachment( $toattachment )) { + $toattachment = $base . $n . $ext; + $n++; + } - # Trashing a topic; look for a non-conflicting name in the - # trash web - my $renamedTopic = $from->web . $to->topic; - my $n = 1; - my $base = $to->topic; - while ( $session->topicExists( $to->web, $renamedTopic ) ) { - $renamedTopic = $base . $n; - $n++; + } else { + # Trashing a topic; look for a non-conflicting name in the + # trash web + my $renamedTopic = $from->web . $to->topic; + my $n = 1; + my $base = $to->topic; + while ( $session->topicExists( $to->web, $renamedTopic ) ) { + $renamedTopic = $base . $n; + $n++; + } + $to = Foswiki::Meta->new( $session, $to->web, $renamedTopic ); } - $to = Foswiki::Meta->new( $session, $to->web, $renamedTopic ); } - $tmpl =~ s/%NEW_WEB%/$to->web()/geo; - $tmpl =~ s/%NEW_TOPIC%/$to->topic()/geo; - $tmpl =~ s/%NONWIKIWORDFLAG%/$nonWikiWordFlag/go; + $tmpl =~ s/%FILENAME%/$attachment/g; + $tmpl =~ s/%NEW_FILENAME%/$toattachment/g; + $tmpl =~ s/%NEW_WEB%/$to->web()/ge; + $tmpl =~ s/%NEW_TOPIC%/$to->topic()/ge; + $tmpl =~ s/%NONWIKIWORDFLAG%/$nonWikiWordFlag/g; if ( !$attachment ) { my $refs; diff --git a/core/templates/moveattachment.tmpl b/core/templates/moveattachment.tmpl index 953cb251c3..9c80bcb93d 100644 --- a/core/templates/moveattachment.tmpl +++ b/core/templates/moveattachment.tmpl @@ -14,6 +14,7 @@ %TMPL:DEF{"namehelp"}%

%MAKETEXT{"The new topic chosen must already exist."}%

%TMPL:END% +%TMPL:DEF{"attachmenthelp"}%

%MAKETEXT{"The attachment may be renamed (by appending a number to the name) if an attachment of that name already exists in the target topic."}%

%TMPL:END% %TMPL:DEF{"newweb"}%

%TMPL:END% @@ -23,6 +24,8 @@ %TMPL:DEF{"newtopic"}%

%TMPL:P{"namehelp"}%%TMPL:END% +%TMPL:DEF{"newattachment"}%

%TMPL:P{"attachmenthelp"}%%TMPL:END% + %TMPL:DEF{"notes"}% * %MAKETEXT{"The attachment and any history will be moved to the new topic."}% diff --git a/core/templates/rename.tmpl b/core/templates/rename.tmpl index 366f1bc7df..68053c6882 100644 --- a/core/templates/rename.tmpl +++ b/core/templates/rename.tmpl @@ -9,6 +9,8 @@ %TMPL:DEF{"newwebhelp"}%%TMPL:END% +%TMPL:DEF{"newattachment"}%%TMPL:END% + %TMPL:DEF{"newtopic"}%
%TMPL:P{"nonwikiword"}%%TMPL:END% diff --git a/core/templates/renamebase.tmpl b/core/templates/renamebase.tmpl index 7b2648baa6..fa85e67ac5 100644 --- a/core/templates/renamebase.tmpl +++ b/core/templates/renamebase.tmpl @@ -32,8 +32,14 @@ %TMPL:P{"totopictitle"}%

%TMPL:P{"newtopic"}%%TMPL:P{"newtopichelp"}%

%TMPL:END% +%TMPL:DEF{"toattachmenttitle"}%---+++ %MAKETEXT{"To attachment:"}% %TMPL:END% -%TMPL:DEF{"renameformsteps"}%%TMPL:P{"changefrom"}%%TMPL:P{"toweb"}%%TMPL:P{"totopic"}%%TMPL:P{"searchresultsstep"}%%TMPL:END% +%TMPL:DEF{"toattachment"}% +%TMPL:P{"toattachmenttitle"}% +

%TMPL:P{"newattachment"}%%TMPL:P{"newattachmenthelp"}%

%TMPL:END% + + +%TMPL:DEF{"renameformsteps"}%%TMPL:P{"changefrom"}%%TMPL:P{"toweb"}%%TMPL:P{"totopic"}%%TMPL:P{"toattachment"}%%TMPL:P{"searchresultsstep"}%%TMPL:END% %TMPL:DEF{"checkuncheck"}%%TMPL:END% diff --git a/core/templates/renameconfirm.tmpl b/core/templates/renameconfirm.tmpl index 210f8506a7..c0797604ee 100644 --- a/core/templates/renameconfirm.tmpl +++ b/core/templates/renameconfirm.tmpl @@ -13,9 +13,14 @@ %TMPL:DEF{"totopictitle"}%---+++ %MAKETEXT{"To topic:"}% %NEW_TOPIC% %TMPL:P{"nonwikiwordallowed"}%%TMPL:END% +%TMPL:DEF{"toattachmenttitle"}%---+++ %MAKETEXT{"To attachment:"}% %NEW_TOPIC% +%TMPL:P{"nonwikiwordallowed"}%%TMPL:END% + %TMPL:DEF{"movewebhelp"}%%TMPL:END% +%TMPL:DEF{"newattachment"}%%TMPL:END% + %TMPL:DEF{"newtopic"}%%TMPL:END% %TMPL:DEF{"newweb"}%%TMPL:END% diff --git a/core/templates/renamedelete.tmpl b/core/templates/renamedelete.tmpl index 38443814e2..4673abb328 100644 --- a/core/templates/renamedelete.tmpl +++ b/core/templates/renamedelete.tmpl @@ -17,9 +17,13 @@ %TMPL:DEF{"totopictitle"}%---+++ %MAKETEXT{"Optionally change topic name:"}%%TMPL:END% +%TMPL:DEF{"toattachmenttitle"}%---+++ %MAKETEXT{"Optionally change attachment name:"}%%TMPL:END% + %TMPL:DEF{"newtopic"}%

%TMPL:P{"namehelp"}%

%TMPL:P{"nonwikiword"}% %TMPL:END% +%TMPL:DEF{"newattachment"}%

%TMPL:P{"namehelp"}%

%TMPL:END% + %TMPL:DEF{"namehelp"}%%MAKETEXT{"This name has been chosen so it doesn't conflict with any other topics already in the [_1] web." args="%NEW_WEB%"}%%TMPL:END% diff --git a/core/templates/renamewebbase.tmpl b/core/templates/renamewebbase.tmpl index 5b9f5d1607..0561af6123 100644 --- a/core/templates/renamewebbase.tmpl +++ b/core/templates/renamewebbase.tmpl @@ -9,8 +9,9 @@ %TMPL:DEF{"parentwebtitle"}%---+++ %MAKETEXT{"New parent web:"}%%TMPL:END% -%TMPL:DEF{"newtopic"}%%TMPL:END% +%TMPL:DEF{"newattachment"}%%TMPL:END% +%TMPL:DEF{"newtopic"}%%TMPL:END% %TMPL:DEF{"newweb"}%%TMPL:P{"newparentweb"}%.%TMPL:P{"newsubweb"}%%TMPL:END%