#! perl -w package TWiki::Contrib::AppraisalMakerContrib; use strict; use Data::Dumper; use LWP; my %settings = ( templatetopic => "Plugins.PluginAppraisalTemplate", web => 'Plugins', templatecontent => '', destinations => ['TestAppraisal'], ); { package TWiki::Auth::UserAgent; our @ISA = qw(LWP::UserAgent); my ( $knownUser, $knownPass ); sub get_basic_credentials { my($self, $realm, $uri) = @_; unless ( $knownUser ) { print "Logon to ",$uri->host_port,"\n"; print "Enter $realm: "; $knownUser = ; chomp($knownUser); return (undef, undef) unless length $knownUser; print "Password on ",$uri->host_port,": "; system("stty -echo"); $knownPass = ; system("stty echo"); print "\n"; # because we disabled echo chomp($knownPass); } return ($knownUser, $knownPass); } } sub run_gettemplate { my ($shell, $config, $topic) = @_; my $userAgent = TWiki::Auth::UserAgent->new(); $userAgent->agent( "Appraisal Maker" ); # Get the old form data and attach it to the update print "Downloading template topic\n"; my $response = $userAgent->get( "http://twiki.org/cgi-bin/view/".$settings{templatetopic}."?raw=debug&skin=text" ); unless ($response->is_success) { print "Failed to GET template topic ", $response->request->uri, " -- ", $response->status_line, "\nAborting\n"; return; } $settings{templatecontent} = $response->content(); } sub run_setdestinations { my ($shell, $config) = (shift, shift); my @places = @_; map {s/\?//} @places; $settings{destinations} = \@places; print "Destinations set to ".join(" ",@{$settings{destinations}})."\n"; } sub run_makeappraisal { my ($shell, $config, $appraisal) = @_; my $web = $settings{web}; my @topics; if ($appraisal) { @topics = ($appraisal); } else { @topics = @{$settings{destinations}}; } for my $topic (@topics) { # if (TWiki::Store::RemoteStore::topicExists($web, $topic)) { # print "$web.$topic already exists - skipping"; # next; # } unless ($topic =~ /Appraisal$/) { $topic = $topic . "Appraisal"; } my ($text, $meta, $saveCmd, $doUnlock, $dontNotify) = ($settings{templatecontent}, "", "", 1, 0); TWiki::Store::RemoteStore::saveTopic($web, $topic, $text, $meta, $saveCmd, $doUnlock, $dontNotify); print "Uploaded $topic\n"; } print "\nUploaded ".($#topics+1)." topics\n"; } sub run_settings { print Dumper(\%settings); } sub run_topicExists { my ($shell, $config, $topic) = @_; my $web = $settings{web}; print TWiki::Store::RemoteStore::topicExists($web, $topic); } { package TWiki::Store::RemoteStore; use Data::Dumper; my $userAgent = TWiki::Auth::UserAgent->new(); $userAgent->agent( "TWiki::Store::RemoteStore" ); my $server = "http://twiki.org"; my $viewUrl = $server."/cgi-bin/view"; =pod ---+++ topicExists( $web, $topic ) ==> $flag | Description: | Test if topic exists | | Parameter: =$web= | Web name, optional, e.g. ="Main"= | | Parameter: =$topic= | Topic name, required, e.g. ="TokyoOffice"=, or ="Main.TokyoOffice"= | | Return: =$flag= | ="1"= if topic exists, ="0"= if not | =cut sub topicExists { my ($web, $topic) = @_; my $response = $userAgent->get($viewUrl."/".$web."/".$topic); print Dumper($response); if ($response->is_redirect && $response->headers->header('Location') =~ /viewauth([\.\w]*)\/$web\/$topic/) { return 0; } else { return 1; } } =pod ---++ sub saveTopic ( $web, $topic, $text, $meta, $saveCmd, $doUnlock, $dontNotify, $dontLogSave, $forceDate ) NB. $dontLogSave, $forceDate ignored. =cut sub saveTopic { my( $web, $topic, $text, $meta, $saveCmd, $doUnlock, $dontNotify, ) = @_; my %newform; $newform{'text'} = $text; if ($meta ne "") { die "Setting of META not supported"; } if ($saveCmd ne "") { die "Setting of saveCmd not supported"; } print "Uploading TO $web.$topic\n"; my $response = $userAgent->post( $server."/cgi-bin/save/$web/$topic", \%newform ); unless ($response->is_redirect && $response->headers->header('Location') =~ /view([\.\w]*)\/$web\/$topic/) { print "Update of topic failed ", $response->request->uri, " -- ", $response->status_line, "\nAborting"; return; } } sub saveAttachment { } } 1;