wikiput 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (C) 2004, 2005, 2006, 2008 Alex Schroeder <alex@gnu.org>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. require LWP;
  18. use Getopt::Std;
  19. our ($opt_v, $opt_w);
  20. # We make our own specialization of LWP::UserAgent that asks for
  21. # user/password if document is protected.
  22. {
  23. package RequestAgent;
  24. @ISA = qw(LWP::UserAgent);
  25. sub new {
  26. my $self = LWP::UserAgent::new(@_);
  27. $self;
  28. }
  29. sub get_basic_credentials {
  30. my($self, $realm, $uri) = @_;
  31. return split(':', $main::opt_w, 2);
  32. }
  33. }
  34. my $usage = qq{$0 [-f] [-m] [-s SUMMARY]
  35. \t[-u USERNAME] [-p PASSWORD] [-w USERNAME:PASSWORD]
  36. \t[-q QUESTION] [-a ANSWER] [-z SECRET]
  37. \tTARGET
  38. Post the data on stdin on the wikipage described by wikipage.
  39. TARGET is the URL for the wiki page.
  40. Options:
  41. -f Allow the posting of empty pages (default: no)
  42. -m Whether this is a minor edit (default: no)
  43. Options with arguments:
  44. -s The summary for RecentChanges (default: none)
  45. -u The username for RecentChanges (default: none)
  46. -p The password to use for locked pages (default: none)
  47. -w The username:password combo for basic authentication (default:none)
  48. -q The question number to answer (default: 0, ie. the first question)
  49. -a The answer to the question (default: none)
  50. -z Alternatively, the secret key (default: question)
  51. -v Verbose output for debugging (default: none)
  52. The defaults are chosen such that if the QuestionAsker extension is
  53. used and the secret key is unchanged, there is no need to provide
  54. either secret key or password.
  55. If the target wiki is protected by so-called "basic authentication" --
  56. that is, if you need to provide a username and password before you can
  57. even view the site -- then you can pass those along using the -w
  58. option. Separate username and password using a colon.
  59. };
  60. sub post {
  61. my ($uri, $id, $data, $minor, $summary, $username, $password,
  62. $question, $answer, $secret) = @_;
  63. my $ua = RequestAgent->new;
  64. my %params = (title=>$id, text=>$data, raw=>1,
  65. username=>$username, pwd=>$password,
  66. summary=>$summary, question_num=>$question,
  67. answer=>$answer, $secret=>1,
  68. recent_edit=>$minor);
  69. if ($opt_v) {
  70. foreach my $key (keys %params) {
  71. my $value = $params{$key} || '(none)';
  72. $value = substr($value,0,50) . '...'
  73. if $key eq 'text' and length($value) > 53;
  74. warn "$key: $value\n";
  75. }
  76. }
  77. my $response = $ua->post($uri, \%params);
  78. my $status = $response->code . ' ' . $response->message;
  79. warn "POST $id failed: $status.\n" unless $response->is_success;
  80. }
  81. sub main {
  82. # $opt_v, $opt_w are global
  83. our($opt_f, $opt_m, $opt_s, $opt_u, $opt_p, $opt_q, $opt_a, $opt_z);
  84. getopts('fms:u:p:q:a:z:vw:');
  85. my $target = shift @ARGV;
  86. die $usage if not $target or @ARGV; # not enough or too many
  87. die "Cannot determine page id from $target\n" unless $target =~ m!^(.*)[/?](.*?)$!;
  88. my ($uri, $id) = ($1, $2);
  89. warn "id $id" if $opt_v;
  90. undef $/;
  91. my $data = <STDIN>;
  92. die "No content to post or use -f to force it\n" if not $data and not $opt_f;
  93. warn length($data) . " bytes of data" if $opt_v;
  94. post ($uri, $id, $data, $opt_m ? 'on' : '', $opt_s, $opt_u, $opt_p,
  95. $opt_q, $opt_a, $opt_z||'question');
  96. }
  97. main();