diff --git a/lib/TWiki/Contrib/UploadToTWikiAddOn/build.pl b/lib/TWiki/Contrib/UploadToTWikiAddOn/build.pl index 768492d..4d16320 100755 --- a/lib/TWiki/Contrib/UploadToTWikiAddOn/build.pl +++ b/lib/TWiki/Contrib/UploadToTWikiAddOn/build.pl @@ -12,12 +12,15 @@ BEGIN # name of web to upload to $build->{UPLOADTARGETWEB} = 'Extensions'; + # Full URL of pub directory $build->{UPLOADTARGETPUB} = 'http://foswiki.org/pub'; + # Full URL of bin directory $build->{UPLOADTARGETSCRIPT} = 'http://foswiki.org/bin'; + # Script extension $build->{UPLOADTARGETSUFFIX} = ''; # Build the target on the command line, or the default target -$build->build($build->{target}); +$build->build( $build->{target} ); diff --git a/tools/uploadtotwiki.pl b/tools/uploadtotwiki.pl index 1090af5..cf224af 100755 --- a/tools/uploadtotwiki.pl +++ b/tools/uploadtotwiki.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl -w # -# Quick & dirty utility to attach a local file to a TWiki topic +# Quick & dirty utility to attach a local file to a TWiki topic # via http. Add-on home is at # http://twiki.org/cgi-bin/view/Plugins/UploadToTWikiAddOn # @@ -24,80 +24,89 @@ use strict; -unless( $ARGV[2] ) { - print "Utility to attach files to any TWiki topic. Copyright (C) 2007, Peter\@Thoeny.org\n"; - print "Add-on home: http://twiki.org/cgi-bin/view/Plugins/UploadToTWikiAddOn\n"; +unless ( $ARGV[2] ) { + print +"Utility to attach files to any TWiki topic. Copyright (C) 2007, Peter\@Thoeny.org\n"; + print + "Add-on home: http://twiki.org/cgi-bin/view/Plugins/UploadToTWikiAddOn\n"; print "Usage:\n"; - print " ./uploadtotwiki.pl -l -c -h 1 \n"; + print +" ./uploadtotwiki.pl -l -c -h 1 \n"; print "Parameters:\n"; print " -l login - login name of TWiki account (optional)\n"; print " -p password - password of TWiki account (optional)\n"; - print " -c 'comment' - comment of attached file (default: 'Uploaded by " . $toolName . "')\n"; + print " -c 'comment' - comment of attached file (default: 'Uploaded by " + . $toolName . "')\n"; print " -h 1 - hide attachment, 0 or 1 (default: 0)\n"; - print " -i 1 - inline attachment, e.g create link in topic text (default: 0)\n"; - print " -d 60 - delay in seconds between uploads of multiple files (default: 30)\n"; + print +" -i 1 - inline attachment, e.g create link in topic text (default: 0)\n"; + print +" -d 60 - delay in seconds between uploads of multiple files (default: 30)\n"; print " file(s) - one or more local files to upload (required)\n"; - print " URL - view URL of TWiki topic, http or https protocol (required, must be last)\n"; + print +" URL - view URL of TWiki topic, http or https protocol (required, must be last)\n"; print "Example:\n"; - print " ./uploadtotwiki.pl -l MyWikiName *.gif http://twiki.org/cgi-bin/view/Sandbox/UploadTest\n"; + print +" ./uploadtotwiki.pl -l MyWikiName *.gif http://twiki.org/cgi-bin/view/Sandbox/UploadTest\n"; exit 1; } my $switch = ''; -my %opts = (); -my @files = (); -foreach my $item ( @ARGV ) { - if( $switch ) { - $opts{$switch} = $item; +my %opts = (); +my @files = (); +foreach my $item (@ARGV) { + if ($switch) { + $opts{$switch} = $item; $switch = ''; - } elsif( $item =~ /^-([a-zA-Z0-9]+)$/ ) { + } + elsif ( $item =~ /^-([a-zA-Z0-9]+)$/ ) { $switch = $1; - } else { - push( @files, $item); + } + else { + push( @files, $item ); } } -my $login = $opts{l} || ''; -my $password = $opts{p} || ''; -my $comment = $opts{c} || "Uploaded by $toolName"; -my $hide = $opts{h} || ''; -my $link = $opts{i} || ''; -my $delay = $opts{d} || '30'; -my $url = pop( @files ) || ''; +my $login = $opts{l} || ''; +my $password = $opts{p} || ''; +my $comment = $opts{c} || "Uploaded by $toolName"; +my $hide = $opts{h} || ''; +my $link = $opts{i} || ''; +my $delay = $opts{d} || '30'; +my $url = pop(@files) || ''; exit uploadFile( $comment, $hide, $link, $delay, $url, @files ); # ========================= -sub uploadFile -{ - my( $theComment, $theHide, $theLink, $theDelay, $theUrl, @theFiles ) = @_; +sub uploadFile { + my ( $theComment, $theHide, $theLink, $theDelay, $theUrl, @theFiles ) = @_; require LWP; - if ( $@ ) { + if ($@) { print STDERR "Error: LWP is not installed; cannot upload\n"; return 0; } my $ua = UploadToTWiki::UserAgent->new(); - $ua->agent( $toolName ); + $ua->agent($toolName); push @{ $ua->requests_redirectable }, 'POST'; my $uploadUrl = $theUrl; - unless( $uploadUrl =~ /^https?:/ ) { + unless ( $uploadUrl =~ /^https?:/ ) { print STDERR "Error: Only http and https protocols are supported\n"; return 0; } - unless( $uploadUrl =~ s|/view|/upload| ) { + unless ( $uploadUrl =~ s|/view|/upload| ) { print STDERR "Error: This is not the URL of a TWiki topic\n"; return 0; } - my $todo = scalar( @theFiles ); - unless( $todo ) { + my $todo = scalar(@theFiles); + unless ($todo) { print STDERR "Error: No files specified to upload\n"; return 0; } - foreach my $file ( @theFiles ) { - unless( -e $file ) { + foreach my $file (@theFiles) { + unless ( -e $file ) { print STDERR "Error: File $file does not exist\n"; return 0; } @@ -111,23 +120,25 @@ sub uploadFile $uploadUrl, [ 'filename' => $fileName, - 'filepath' => [ $file ], + 'filepath' => [$file], 'filecomment' => $theComment, 'hidefile' => $theHide, 'createlink' => $theLink ], - 'Content_Type' => 'form-data' ); + 'Content_Type' => 'form-data' + ); - if( $response->is_success ) { + if ( $response->is_success ) { print "... upload finished\n"; - } else { + } + else { print STDERR "Error: " . $response->status_line . "\n"; return 0; } $todo--; - if( $todo ) { + if ($todo) { print "Wait for $theDelay seconds (be nice to the server)\n"; - sleep( $theDelay ); + sleep($theDelay); } } return 1; @@ -135,37 +146,38 @@ sub uploadFile # ========================= { + package UploadToTWiki::UserAgent; use LWP::UserAgent; our @ISA = qw( LWP::UserAgent ); sub new { - my ($class, $id) = @_; + my ( $class, $id ) = @_; my $this = $class->SUPER::new(); $this->{domain} = $id; return $this; } sub get_basic_credentials { - my($this, $realm, $uri) = @_; + my ( $this, $realm, $uri ) = @_; my $host = $uri->host_port(); $host =~ s/\:[0-9]*$//; local $/ = "\n"; - unless( $login ) { - print( "Enter TWiki login name for $host: " ); + unless ($login) { + print("Enter TWiki login name for $host: "); $login = ; - chomp( $login ); + chomp($login); } - unless( $password ) { - print( "Enter password for $login at $host: " ); - system( 'stty -echo' ); + unless ($password) { + print("Enter password for $login at $host: "); + system('stty -echo'); $password = ; - system( 'stty echo' ); + system('stty echo'); print "\n"; - chomp( $password ); + chomp($password); } - return( $login, $password ); + return ( $login, $password ); } }