sync.pl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (C) 2005, 2006 Alex Schroeder <alex@emacswiki.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('sync.pl', 'Page Synchronization');
  18. our ($q, %Page, $OpenPageName, @MyRules, $FullUrl, $FullUrlPattern, $ScriptName);
  19. push(@MyRules, \&SyncRule);
  20. sub SyncRule {
  21. # [[copy:http://example.com/wiki]]
  22. if (m/\G\[\[(copy:$FullUrlPattern)\]\]/cg) {
  23. my ($text, $url) = ($1, $2);
  24. return $q->a({-href=>$2, class=>'outside copy'}, $text);
  25. }
  26. return;
  27. }
  28. *SyncOldSave = \&Save;
  29. *Save = \&SyncNewSave;
  30. sub SyncNewSave {
  31. my ($id) = @_;
  32. SyncOldSave(@_);
  33. # %Page is now set, but the reply was not yet sent back to the
  34. # browser
  35. $id = $OpenPageName; # TODO masks earlier declaration
  36. my $data = $Page{text};
  37. my $user = $Page{username};
  38. my $summary = $Page{summary};
  39. my $minor = $Page{minor};
  40. my @links = ();
  41. while ($data =~ m/\[\[copy:$FullUrlPattern\]\]/g) {
  42. push(@links, $1) unless $1 eq $ScriptName or $1 eq $FullUrl;
  43. }
  44. my $msg = GetParam('msg', '');
  45. foreach my $uri (@links) {
  46. next if $uri eq $ScriptName or $uri eq $FullUrl;
  47. require LWP::UserAgent;
  48. my $ua = LWP::UserAgent->new;
  49. my %params = ( title=>$id,
  50. text=>$data,
  51. raw=>1,
  52. username=>$user,
  53. pwd=>GetParam('pwd',''),
  54. summary=>$summary, );
  55. $params{recent_edit} = 'on' if $minor;
  56. my $response = $ua->post($uri, \%params);
  57. my $status = $response->code . ' ' . $response->message;
  58. warn "Result for $uri: $status";
  59. $msg .= ' ' if $msg;
  60. $msg .= $response->is_success
  61. ? Tss('Copy to %1 succeeded: %2.', $uri, $status)
  62. : Tss('Copy to %1 failed: %2.', $uri, $status);
  63. }
  64. SetParam('msg', $msg);
  65. }