| 1 | package Pod::Simple::Wiki::Twiki::Upload; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Data::Dumper; | ||
| 7 | use File::Find; | ||
| 8 | use File::Spec; | ||
| 9 | use IO::File; | ||
| 10 | require Exporter; | ||
| 11 | |||
| 12 | our $VERSION = 0.3; | ||
| 13 | our @ISA = qw(Exporter); | ||
| 14 | our @EXPORT = qw(make_file_list); | ||
| 15 | |||
| 16 | |||
| 17 | sub make_file_list | ||
| 18 | { | ||
| 19 | my %bin_files = find_bin_pods(); | ||
| 20 | my %files; | ||
| 21 | for my $bin (keys %bin_files) { | ||
| 22 | my $x = $bin; | ||
| 23 | $x =~ s=.*/(.+)=$1=; | ||
| 24 | $files{"$x"} = $bin_files{$bin}; | ||
| 25 | } | ||
| 26 | for my $f (@{rscan_dir('lib', qr/\.(pm|pod)$/)}) { | ||
| 27 | my $base = $f; | ||
| 28 | $base =~ s/\.pm$//; | ||
| 29 | my $mod = $base; | ||
| 30 | $mod =~ s=/=::=g; | ||
| 31 | $mod =~ s/^lib:://; | ||
| 32 | if (contains_pod($f)) { | ||
| 33 | $files{$mod} = $f; | ||
| 34 | } | ||
| 35 | elsif (-e "$base.pod") { | ||
| 36 | $files{$mod} = "$base.pod"; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | return %files; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub twiki_upload | ||
| 43 | { | ||
| 44 | my ($twikiroot, $twikiweb) = @_; | ||
| 45 | $twikiweb ||= 'Main'; | ||
| 46 | |||
| 47 | my %files = make_file_list; | ||
| 48 | return unless %files; | ||
| 49 | |||
| 50 | my $user = $ENV{TWIKI_USER} || prompt("What is your twiki user id?", scalar(getpwuid($<))); | ||
| 51 | chomp($user); | ||
| 52 | $user =~ s/ $//; | ||
| 53 | system("stty -echo"); | ||
| 54 | my $pass = $ENV{TWIKI_PASS} || prompt("What is your twiki password?", ''); | ||
| 55 | chomp($pass); | ||
| 56 | system("stty echo"); | ||
| 57 | print "\n"; | ||
| 58 | |||
| 59 | require IO::Scalar or die; | ||
| 60 | import IO::Scalar; | ||
| 61 | |||
| 62 | require Pod::Simple::Wiki or die; | ||
| 63 | import Pod::Simple::Wiki; | ||
| 64 | |||
| 65 | require WWW::TWikiClient or die; | ||
| 66 | import WWW::TWikiClient; | ||
| 67 | |||
| 68 | { | ||
| 69 | package Private::Module::WWW::TWikiClient; | ||
| 70 | use strict; | ||
| 71 | use warnings; | ||
| 72 | our @ISA = qw(WWW::TWikiClient); | ||
| 73 | sub _skin_regex_authentication_failed { | ||
| 74 | return qr/Please enter your username and pas|Unrecognized user and/; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub get_old_stuff { | ||
| 78 | my ($self, $topic) = @_; | ||
| 79 | $self->get("$twikiroot/edit/$twikiweb/$topic"); | ||
| 80 | die unless $self->{form}; | ||
| 81 | die unless $self->{form}{action}; | ||
| 82 | die unless $self->{form}{inputs}; | ||
| 83 | die unless $self->{form}{inputs}[0]; | ||
| 84 | die unless $self->{form}{inputs}[0]{name} eq 'text'; | ||
| 85 | die unless $self->{form}{inputs}[0]{type} eq 'textarea'; | ||
| 86 | return $self->{form}{inputs}[0]{value}; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub save_new_stuff { | ||
| 90 | my ($self, $stuff) = @_; | ||
| 91 | $self->submit_form( | ||
| 92 | form_name => 'main', | ||
| 93 | fields => { | ||
| 94 | text => $stuff, | ||
| 95 | action_save => 'Save', | ||
| 96 | }, | ||
| 97 | ); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | my $twiki = new Private::Module::WWW::TWikiClient ( | ||
| 102 | verbose => 1, | ||
| 103 | auth_user => $user, | ||
| 104 | auth_passwd => $pass, | ||
| 105 | bin_url => $twikiroot, | ||
| 106 | ); | ||
| 107 | die "could get twiki link" unless $twiki; | ||
| 108 | |||
| 109 | $twiki->get("$twikiroot/login/TWiki/LoginName"); | ||
| 110 | $twiki->submit_form( | ||
| 111 | form_name => 'loginform', | ||
| 112 | fields => { | ||
| 113 | username => $user, | ||
| 114 | password => $pass, | ||
| 115 | }, | ||
| 116 | ); | ||
| 117 | |||
| 118 | my $newcontent = qr{\A\s+-- Main\.\S+ -\s+\d+ (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) 20\d\d\s+\Z}s; | ||
| 119 | |||
| 120 | $| = 1; | ||
| 121 | for my $name (sort keys %files) { | ||
| 122 | my $file = $files{$name}; | ||
| 123 | print "working on $name (from $file)... "; | ||
| 124 | my $parser = Pod::Simple::Wiki->new('twiki'); | ||
| 125 | my $newtext; | ||
| 126 | my $outfh = new IO::Scalar \$newtext; | ||
| 127 | |||
| 128 | open my $input, "<", $file or die "open $file: $!"; | ||
| 129 | |||
| 130 | $parser->output_fh($outfh); | ||
| 131 | $parser->parse_file($input); | ||
| 132 | |||
| 133 | my $n = $name; | ||
| 134 | #$n =~ s/:://g; | ||
| 135 | #$n = ucfirst($n); | ||
| 136 | $twiki->current_default_web('Perl'); | ||
| 137 | $twiki->current_topic("$n"); | ||
| 138 | |||
| 139 | my $old_content = $twiki->get_old_stuff($n); | ||
| 140 | |||
| 141 | |||
| 142 | my $new; | ||
| 143 | pos($newtext) = 0; | ||
| 144 | while (pos($newtext) < length($newtext)) { | ||
| 145 | if ($newtext =~ m{\G((?:.(?!<verbatim>))+)}gcs) { | ||
| 146 | my $t = $1; | ||
| 147 | $t =~ s/\b([\w+:]+)\b/[[Perl.$1][$1]]/g; | ||
| 148 | $new .= $t; | ||
| 149 | } elsif ($newtext =~ m{(<verbatim>.*?</verbatim>)}gcs) { | ||
| 150 | $new .= $1; | ||
| 151 | } else { | ||
| 152 | die; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | $newtext = $new; | ||
| 156 | |||
| 157 | my $header = "This is auto-generated content from =$file=, do not edit"; | ||
| 158 | my $new_content = $header."\n\n".$newtext; | ||
| 159 | if ($old_content eq $new_content) { | ||
| 160 | print "already up-to-date.\n"; | ||
| 161 | } elsif ($old_content =~ /$newcontent/ or $old_content =~ /\Q$header\E/) { | ||
| 162 | # my $res = $twiki->save_topic($header."\n\n".$newtext); | ||
| 163 | $twiki->save_new_stuff($new_content); | ||
| 164 | print "uploaded\n"; | ||
| 165 | } else { | ||
| 166 | print "skipping: content doesn't match starting cookie\n"; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | sub contains_pod { | ||
| 173 | my ($file) = @_; | ||
| 174 | return '' unless -T $file; # Only look at text files | ||
| 175 | |||
| 176 | my $fh = IO::File->new( $file ) or die "Can't open $file: $!"; | ||
| 177 | while (my $line = <$fh>) { | ||
| 178 | return 1 if $line =~ /^\=(?:head|pod|item)/; | ||
| 179 | } | ||
| 180 | |||
| 181 | return ''; | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | sub find_bin_pods { | ||
| 186 | my %files; | ||
| 187 | for my $spec ("blib/script") { | ||
| 188 | my $dir = localize_dir_path($spec); | ||
| 189 | next unless -e $dir; | ||
| 190 | for my $file ( @{ rscan_dir( $dir ) } ) { | ||
| 191 | next if $file =~ /\.bat$/; | ||
| 192 | if ( contains_pod( $file ) ) { | ||
| 193 | $files{$file} = $file; | ||
| 194 | } | ||
| 195 | elsif (my $pm_file = find_client_lib( $file ) ) { | ||
| 196 | $files{$file} = $pm_file; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | } | ||
| 200 | return %files; | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | sub find_client_lib { | ||
| 205 | my ($file) = @_; | ||
| 206 | return '' unless -T $file; # Only look at text files | ||
| 207 | |||
| 208 | my $fh = IO::File->new( $file ) or die "Can't open $file: $!"; | ||
| 209 | while (my $line = <$fh>) { | ||
| 210 | next if $line !~ /^use\s+(?:aliased\s+(['"]))((?:[\w:]+)?Client::\w+)\1;$/; | ||
| 211 | # We have a client class. | ||
| 212 | return join( '/', 'lib', split /::/, $2 ) . '.pm' | ||
| 213 | } | ||
| 214 | return; | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | sub localize_dir_path { | ||
| 219 | my ($path) = @_; | ||
| 220 | return File::Spec->catdir( split m{/}, $path ); | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | sub rscan_dir { | ||
| 225 | my ($dir, $pattern) = @_; | ||
| 226 | my @result; | ||
| 227 | local $_; # find() can overwrite $_, so protect ourselves | ||
| 228 | my $subr = !$pattern ? sub {push @result, $File::Find::name} : | ||
| 229 | !ref($pattern) || (ref $pattern eq 'Regexp') ? sub {push @result, $File::Find::name if /$pattern/} : | ||
| 230 | ref($pattern) eq 'CODE' ? sub {push @result, $File::Find::name if $pattern->()} : die "Unknown pattern type"; | ||
| 231 | |||
| 232 | File::Find::find({wanted => $subr, no_chdir => 1}, $dir); | ||
| 233 | return \@result; | ||
| 234 | } | ||
| 235 | |||
| 236 | |||
| 237 | # NOTE this is a blocking operation if(-t STDIN) | ||
| 238 | sub _is_interactive { | ||
| 239 | return -t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT)) ; # Pipe? | ||
| 240 | } | ||
| 241 | |||
| 242 | sub _is_unattended { | ||
| 243 | return $ENV{PERL_MM_USE_DEFAULT} || ( ! _is_interactive() && eof STDIN ); | ||
| 244 | } | ||
| 245 | |||
| 246 | sub _readline { | ||
| 247 | return undef if _is_unattended(); | ||
| 248 | |||
| 249 | my $answer = <STDIN>; | ||
| 250 | chomp $answer if defined $answer; | ||
| 251 | return $answer; | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | sub prompt { | ||
| 256 | my ($mess) = @_; | ||
| 257 | if (not defined $mess) { | ||
| 258 | die "prompt() called without a prompt message"; | ||
| 259 | } | ||
| 260 | |||
| 261 | # use a list to distinguish a default of undef() from no default | ||
| 262 | my @def; | ||
| 263 | @def = (shift) if @_; | ||
| 264 | # use dispdef for output | ||
| 265 | my @dispdef = scalar(@def) ? ('[', (defined($def[0]) ? $def[0] . ' ' : ''), ']') : (' ', ''); | ||
| 266 | |||
| 267 | local $|=1; | ||
| 268 | print "$mess ", @dispdef; | ||
| 269 | |||
| 270 | if ( _is_unattended() && !@def ) { | ||
| 271 | die "ERROR: This runseems to be unattended, but there is no default value for this question. Aborting."; | ||
| 272 | } | ||
| 273 | |||
| 274 | my $ans = _readline(); | ||
| 275 | |||
| 276 | # Ctrl-D or unattendeda User hit return | ||
| 277 | if ( !defined($ans) or !length($ans) ) { | ||
| 278 | print "$dispdef[1]\n"; | ||
| 279 | $ans = scalar(@def) ? $def[0] : ''; | ||
| 280 | } | ||
| 281 | |||
| 282 | return $ans; | ||
| 283 | } | ||
| 284 | |||
| 285 | |||
| 286 | 1; | ||
| 287 | |||
| 288 | __END__ |