publish.pl 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (C) 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('publish.pl', 'Publish Page Extension');
  18. our ($q, %Action, %Page, $OpenPageName, @MyAdminCode, $ScriptName, $FullUrl);
  19. our ($PublishTargetUrl);
  20. $PublishTargetUrl = '';
  21. $Action{publish} = \&DoPublish;
  22. push(@MyAdminCode, \&PublishMenu);
  23. sub PublishMenu {
  24. my ($id, $menuref, $restref) = @_;
  25. my $name = $id;
  26. $name =~ s/_/ /g;
  27. if ($id and $PublishTargetUrl) {
  28. push(@$menuref, ScriptLink('action=publish;id=' . $id,
  29. Ts('Publish %s', $name), 'publish'));
  30. }
  31. }
  32. sub DoPublish {
  33. my ($id) = @_;
  34. ReportError(T('No target wiki was specified in the config file.'),
  35. '500 INTERNAL SERVER ERROR')
  36. unless $PublishTargetUrl;
  37. ReportError(T('The target wiki was misconfigured.',
  38. '500 INTERNAL SERVER ERROR'))
  39. if $PublishTargetUrl eq $ScriptName or $PublishTargetUrl eq $FullUrl;
  40. ReportError('LWP::UserAgent is not available',
  41. '500 INTERNAL SERVER ERROR')
  42. unless eval {require LWP::UserAgent};
  43. my $ua = LWP::UserAgent->new;
  44. OpenPage($id);
  45. my %params = ( title=>$OpenPageName,
  46. text=>$Page{text},
  47. raw=>1,
  48. username=>$Page{username},
  49. summary=>$Page{summary},
  50. pwd=>GetParam('pwd',''),
  51. );
  52. $params{recent_edit} = 'on' if $Page{minor};
  53. my $response = $ua->post($PublishTargetUrl, \%params);
  54. if ($response->code == 302 and $response->header('Location')) {
  55. print $q->redirect($response->header('Location'));
  56. } elsif ($response->code == 200) {
  57. print $q->redirect($PublishTargetUrl . '?' . $id);
  58. } else {
  59. ReportError($response->content,
  60. $response->code . ' ' . $response->message);
  61. }
  62. }