publish.pl 2.3 KB

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