Skip to content

Commit

Permalink
Item5384: added basic support for renaming deleted attachments. It wa…
Browse files Browse the repository at this point in the history
…s getting on my tits.

git-svn-id: http://svn.foswiki.org/trunk@3776 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
CrawfordCurrie authored and CrawfordCurrie committed Apr 30, 2009
1 parent 8bdf9da commit 68902d9
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 39 deletions.
2 changes: 1 addition & 1 deletion PatternSkin/templates/attachagain.pattern.tmpl
Expand Up @@ -21,7 +21,7 @@



%TMPL:DEF{"movedelete"}%<div class="patternMoveAttachment"><a href="%SCRIPTURLPATH{"rename"}%/%WEB%/%TOPIC%?template=moveattachment&attachment=%ENCODE{%FILENAME%}%" rel='nofollow'><span class="foswikiLinkLabel">%MAKETEXT{"Move"}%</span></a> %MAKETEXT{"or"}% <a href="%SCRIPTURLPATH{"rename"}%/%WEB%/%TOPIC%?web=%TRASHWEB%&template=renameattachmentdelete&attachment=%ENCODE{%FILENAME%}%" rel='nofollow'><span class="foswikiLinkLabel">%MAKETEXT{"Delete"}%</span></a> =%FILENAME%= </div>%TMPL:END%
%TMPL:DEF{"movedelete"}%<div class="patternMoveAttachment"><a href="%SCRIPTURLPATH{"rename"}%/%WEB%/%TOPIC%?template=moveattachment&attachment=%ENCODE{%FILENAME%}%" rel='nofollow'><span class="foswikiLinkLabel">%MAKETEXT{"Move"}%</span></a> %MAKETEXT{"or"}% <a href="%SCRIPTURLPATH{"rename"}%/%WEB%/%TOPIC%?newweb=%TRASHWEB%;newtopic=TrashAttachment;template=renameattachmentdelete;attachment=%ENCODE{%FILENAME%}%" rel='nofollow'><span class="foswikiLinkLabel">%MAKETEXT{"Delete"}%</span></a> =%FILENAME%= </div>%TMPL:END%



Expand Down
11 changes: 6 additions & 5 deletions PatternSkin/templates/renameattachmentdelete.pattern.tmpl
Expand Up @@ -29,17 +29,18 @@



%TMPL:DEF{"totopictitle"}%---+++ %MAKETEXT{"Optionally enter a different topic name:" args="<nop>%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"}%<p><input name="newattachment" class="foswikiInputField" value="%NEW_FILENAME%" size="36" /><input type="hidden" name="attachment" value="%FILENAME%" /></p>%TMPL:P{"attachmenthelp"}%%TMPL:END%

%TMPL:DEF{"newtopic"}%<p><input name="newtopic" class="foswikiInputField" value="TrashAttachment" size="36" /><input type="hidden" name="nonwikiword" value="on" /></p>%TMPL:P{"namehelp"}%<input type="hidden" name="attachment" value="%FILENAME%" />%TMPL:END%
%TMPL:DEF{"newtopic"}%<p><input name="newtopic" class="foswikiInputField" value="%URLPARAM{"newtopic" default="TrashAttachment"}%" size="36" /><input type="hidden" name="nonwikiword" value="on" /></p>%TMPL:P{"namehelp"}%%TMPL:END%


%TMPL:DEF{"namehelp"}%<p>%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."}%</p>%TMPL:END%

%TMPL:DEF{"namehelp"}%<p>%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."}%</p>%TMPL:END%


%TMPL:DEF{"attachmenthelp"}%<p>%MAKETEXT{"The attachment may have to be renamed if an attachment called '%FILENAME%' already exists in the target topic."}%</p>%TMPL:END%

%TMPL:DEF{"toolbar"}%%TMPL:END%
%TMPL:DEF{"pagehelp"}%%TMPL:END%
Expand Down
1 change: 1 addition & 0 deletions PatternSkin/templates/renamebase.pattern.tmpl
Expand Up @@ -26,6 +26,7 @@
<div class="foswikiFormStep">%TMPL:P{"changefrom"}%</div><!-- /foswikiFormStep-->
<div class="foswikiFormStep">%TMPL:P{"toweb"}%</div><!-- /foswikiFormStep-->
<div class="foswikiFormStep foswikiLast">%TMPL:P{"totopic"}%</div><!-- /foswikiFormStep-->
<div class="foswikiFormStep foswikiLast">%TMPL:P{"toattachment"}%</div><!-- /foswikiFormStep-->
%TMPL:P{"searchresultsstep"}%</div><!-- /foswikiFormSteps-->%TMPL:END%


Expand Down
1 change: 1 addition & 0 deletions core/data/System/VarALLVARIABLES.txt
Expand Up @@ -5,3 +5,4 @@
* Syntax: =%<nop>ALLVARIABLES%=
* Expands to: a table showing all defined [[macros]] in the current context

*Deprecated 2009-04-29* in favour of [[VarSHOWPREFERENCE][SHOWPREFERENCE]]
104 changes: 73 additions & 31 deletions core/lib/Foswiki/UI/Rename.pm
Expand Up @@ -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;

Expand All @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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',
Expand All @@ -179,7 +198,7 @@ sub _renameTopicOrAttachment {
params => [
$newWeb,
$newTopic,
$attachment,
$newAttachment,
$session->i18n->maketext(
'Attachment already exists in new topic')
]
Expand Down Expand Up @@ -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;

}
Expand All @@ -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}
Expand Down Expand Up @@ -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} );
Expand All @@ -274,7 +298,7 @@ sub _renameTopicOrAttachment {
}
else {

#redirect to new topic
# redirect to new topic
$new_url = $session->getScriptUrl( 0, 'view', $newWeb, $newTopic );
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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') || '';
Expand All @@ -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');
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions core/templates/moveattachment.tmpl
Expand Up @@ -14,6 +14,7 @@

%TMPL:DEF{"namehelp"}%<p>%MAKETEXT{"The new topic chosen must already exist."}%</p>%TMPL:END%

%TMPL:DEF{"attachmenthelp"}%<p>%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."}%</p>%TMPL:END%

%TMPL:DEF{"newweb"}%<p><select class="foswikiSelect" name="newweb" onchange="if (document.rename.newweb.value == 'Trash') { document.rename.newtopic.value = 'TrashAttachment'; }"> %WEBLIST{"<option $marker value=$qname>$name</option>" webs="%TRASHWEB%,public" selection="%WEB%" separator=" "}% </select><input type="hidden" name="attachment" value="%FILENAME%" /></p>%TMPL:END%

Expand All @@ -23,6 +24,8 @@

%TMPL:DEF{"newtopic"}%<p><input name="newtopic" class="foswikiInputField" value="" size="36" /><input type="hidden" name="nonwikiword" value="on" /></p>%TMPL:P{"namehelp"}%%TMPL:END%

%TMPL:DEF{"newattachment"}%<p><input name="newattachment" class="foswikiInputField" value="%URLPARAM{"attachment"}%" size="36" /><input type="hidden" name="nonwikiword" value="on" /></p>%TMPL:P{"attachmenthelp"}%%TMPL:END%


%TMPL:DEF{"notes"}%
* %MAKETEXT{"The attachment and any history will be moved to the new topic."}%
Expand Down
2 changes: 2 additions & 0 deletions core/templates/rename.tmpl
Expand Up @@ -9,6 +9,8 @@
%TMPL:DEF{"newwebhelp"}%%TMPL:END%


%TMPL:DEF{"newattachment"}%<input name="newattachment" class="foswikiInputField" value="%NEW_FILENAME%" size="36" />%TMPL:END%

%TMPL:DEF{"newtopic"}%<input name="newtopic" class="foswikiInputField" value="%NEW_TOPIC%" size="36" /><br />
%TMPL:P{"nonwikiword"}%%TMPL:END%

Expand Down
8 changes: 7 additions & 1 deletion core/templates/renamebase.tmpl
Expand Up @@ -32,8 +32,14 @@
%TMPL:P{"totopictitle"}%
<p>%TMPL:P{"newtopic"}%%TMPL:P{"newtopichelp"}%</p>%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"}%
<p>%TMPL:P{"newattachment"}%%TMPL:P{"newattachmenthelp"}%</p>%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%
Expand Down
5 changes: 5 additions & 0 deletions core/templates/renameconfirm.tmpl
Expand Up @@ -13,9 +13,14 @@
%TMPL:DEF{"totopictitle"}%---+++ %MAKETEXT{"To topic:"}% <nop>%NEW_TOPIC%
<input type="hidden" name="newtopic" value="%NEW_TOPIC%" />%TMPL:P{"nonwikiwordallowed"}%%TMPL:END%

%TMPL:DEF{"toattachmenttitle"}%---+++ %MAKETEXT{"To attachment:"}% <nop>%NEW_TOPIC%
<input type="hidden" name="newattachment" value="%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%
Expand Down
4 changes: 4 additions & 0 deletions core/templates/renamedelete.tmpl
Expand Up @@ -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"}%<p><input name="newtopic" class="foswikiInputField" value="%NEW_TOPIC%" size="36" /> %TMPL:P{"namehelp"}%</p>%TMPL:P{"nonwikiword"}% %TMPL:END%

%TMPL:DEF{"newattachment"}%<p><input name="newattachment" class="foswikiInputField" value="%NEW_FILENAME%" size="36" /> %TMPL:P{"namehelp"}%</p>%TMPL:END%


%TMPL:DEF{"namehelp"}%<span>%MAKETEXT{"This name has been chosen so it doesn't conflict with any other topics already in the [_1] web." args="%NEW_WEB%"}%</span>%TMPL:END%

Expand Down
3 changes: 2 additions & 1 deletion core/templates/renamewebbase.tmpl
Expand Up @@ -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%

Expand Down

0 comments on commit 68902d9

Please sign in to comment.