#!/usr/bin/perl -wTI. # # Copyright (C) 2001 Sven Dowideit, svenud@ozemail.com.au # ## XXX 3/25/2001: TODO: Should integrate checks for locked topics being edited # See http://twiki.org/cgi-bin/view/Codev/RenameTopic # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details, published at # http://www.gnu.ai.mit.edu/copyleft/gpl.html #usage example: #
# SGK 22-mar-01: modify to allow rename to new directory (including trash) # with support for multiple levels of directory use CGI; use wiki; use File::Path; $query= new CGI; &main(); sub main { my $thePathInfo = $query->path_info(); my $theRemoteUser = $query->remote_user(); my $theTopic = $query->param( 'topic' ); my $newTopic = $query->param( 'newtopic' ); my $newWeb = $query->param( 'newdirectory' ); # sgk my $theUrl = $query->url; ( $topic, $dirWebName, $scriptUrlPath, $userName, $dataDir ) = &wiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, $theUrl ); my $theWeb = $query->param( 'web' ); my $foo = $userName; # remove warning? $webName = $theWeb || $dirWebName; $webName =~ s/$wiki::securityFilter//go; $webName =~ /(.*)/; $webName = $1; # untaint variable $wiki::webName = $webName; # do some of the "multilevel web" support $wiki::webPath = $wiki::webName; $wiki::webPath =~ s|\.|\/|g; # SGK: 21-mar-01: fixup the directory name, the user might given a/b or a.b $newWebName = $newWeb || $webName; $newWebName =~ s/$wiki::securityFilter//go; $newWebName =~ s|\.|\/|g; $newWebName =~ /(.*)/; $newWebName = $1; # untaint variable # do some of the "multilevel web" support $newWebDotted = $newWebName; $newWebDotted =~ s|\/|\.|g; if( ! &wiki::webExists( $webName ) ) { print "Content-type: text/html\n\n"; $tmpl= &wiki::readTemplate( "noweb" ); $tmpl = &wiki::handleCommonTags( $tmpl, $topic ); print $tmpl; return; } # SGK: 21-mar-01: give error if new directory does not exist if( ! &wiki::webExists( $newWebName ) ) { print "Content-type: text/html\n\n"; $tmpl= &wiki::readTemplate( "noweb" ); $wiki::webName = $newWebName; $wiki::webPath = $newWebPath; $tmpl = &wiki::handleCommonTags( $tmpl, $topic ); print $tmpl; return; } # BWW: 13-may-01: print a template for the user to specify the new name if( $newTopic =~ /^$/ ) { print "Content-type: text/html\n\n"; $tmpl = &wiki::readTemplate( "rename" ); $tmpl = &wiki::handleCommonTags( $tmpl, $topic ); print $tmpl; return; } my $fileName = "$dataDir/$webName/$topic.txt"; my $newName = "$dataDir/$newWebName/$newTopic.txt"; my $lockName = "$dataDir/$webName/$topic.lock"; my $newLockName = "$dataDir/$newWebName/$newTopic.lock"; if (( -e $fileName ) && !( -e $newName)) #need to make sure newName ia a wikiName { my $search = $newTopic; my $scope = "text"; my $caseSensitive = ""; my @result = findReferingPages($webName, $topic); my $resultVal = ""; foreach $item (@result) { my $resultText = ""; $resultVal .= "$item "; #open each file, replace $topic with $newTopic if ( &wiki::topicExists($webName, $item) ) { my $scantext = &wiki::readTopic($item); foreach( split( /\n/, $scantext ) ) { s/\Q$topic\E/$newTopic/; $resultText .= "$_\n"; } } else { $resultVal .= " (?? $item does not exist ??)" } &wiki::saveTopic($item, $resultText); } $fileName =~ /(.*)/; $fileName = $1; $newName =~ /(.*)/; $newName = $1; rename($fileName, $newName); &wiki::writeLog("rename", "$fileName", "$newName"); # rename the log file too. rename("$fileName,v", "$newName,v"); &wiki::writeLog("rename", "$fileName,v", "$newName,v"); if ( -e $lockName ) { # sanitize names just in case $lockName =~ /(.*)/; $lockName = $1; $newLockName =~ /(.*)/; $newLockName = $1; rename($lockName, $newLockName); &wiki::writeLog("rename", $lockName, $newLockName); } # SGK 22-Mar-01: now deal with the attachment directory my $pubDir = &wiki::getPubDir(); my $oldAttachDir = "$pubDir/$webName/$topic"; my $newAttachLoc = "$pubDir/$newWebName"; my $attmsg = ""; my $tvl = ( -d $oldAttachDir ); if (-d $oldAttachDir) { if ($webName ne $newWebName) { umask (0); mkpath($newAttachLoc,0777); } # TODO: should verify the new attachment topic directory does not exit my $newAttachDir = "$newAttachLoc/$newTopic"; $newAttachDir =~ /(.*)/; rename($oldAttachDir, $1); # use untainted variable &wiki::writeLog("rename attachment", $oldAttachDir, $newAttachDir); $attachmsg = "Renamed attachment directory $oldAttachDir to $newAttachDir\n"; } #redirect to new pagename... my ( $newUrl ) = &wiki::getViewUrl($newWebName, $newTopic ); ## print $query->redirect( $newUrl ); ## print "Redirecting to $new_url
"; # should be a template! print "Content-type: text/html\n\n"; print ""; print "
"; print ""; print "New name is $newWebDotted.$newTopic -- View New Topic
CAUTION: since the new topic directory $newWebDotted is different, the referring pages were NOT properly fixed to do cross links. Please edit those pages manually.\n"; } print ""; print ""; } else { print "Content-type: text/html\n\n"; print ""; print "
"; print ""; print "You need to search in web $theWeb for the topic $topic");
print ("
The for file $fileName is not present");
} elsif ( !(-e $newName )) {
print ("
Sorry, the topic $newTopic exists already");
print ("
(file $newName)"); }
print "";
print "";
}
}
sub findReferingPages #($webName, $topic);
{
my ($webName, $topic) = @_;
my ( $result ) = "";
my $cmd = "%GREP% %SWITCHES% -l '$topic' *.txt";
my $tempVal = "-i";
$cmd =~ s/%SWITCHES%/$tempVal/go;
$tempVal = $wiki::fgrepCmd;
$cmd =~ s/%GREP%/$tempVal/go;
my @topicList = "";
# do grep search
my $webDir = "$dataDir/$webName";
$webDir =~ /(.*)/;
chdir( "$1" ); # untaint hack
$cmd =~ /(.*)/;
$cmd = $1; # untaint variable (NOTE: Needs a better check!)
$tempVal = `$cmd`;
@result = split( /\n/, $tempVal );
# cut .txt extension
@result = map { /(.*)\.txt$/; $_ = $1; } @result;
return @result;
}