wiki.pl: Port some fixes from upstream
[Orgmuse.git] / modules / publish.pl
blob74b5b2a8259788da2b9013ef5539650a4f81077f
1 # Copyright (C) 2006 Alex Schroeder <alex@emacswiki.org>
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.
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.
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
19 $ModulesDescription .= '<p><a href="http://git.savannah.gnu.org/cgit/oddmuse.git/tree/modules/publish.pl">publish.pl</a>, see <a href="http://www.oddmuse.org/cgi-bin/oddmuse/Publish_Page_Extension">Publish Page Extension</a></p>';
21 use vars qw($PublishTargetUrl);
23 $PublishTargetUrl = '';
25 $Action{publish} = \&DoPublish;
27 push(@MyAdminCode, \&PublishMenu);
29 sub PublishMenu {
30 my ($id, $menuref, $restref) = @_;
31 my $name = $id;
32 $name =~ s/_/ /g;
33 if ($id and $PublishTargetUrl) {
34 push(@$menuref, ScriptLink('action=publish;id=' . $id,
35 Ts('Publish %s', $name), 'publish'));
39 sub DoPublish {
40 my ($id) = @_;
41 ReportError(T('No target wiki was specified in the config file.'),
42 '500 INTERNAL SERVER ERROR')
43 unless $PublishTargetUrl;
44 ReportError(T('The target wiki was misconfigured.',
45 '500 INTERNAL SERVER ERROR'))
46 if $PublishTargetUrl eq $ScriptName or $PublishTargetUrl eq $FullUrl;
47 ReportError('LWP::UserAgent is not available',
48 '500 INTERNAL SERVER ERROR')
49 unless eval {require LWP::UserAgent};
50 my $ua = LWP::UserAgent->new;
51 OpenPage($id);
52 my %params = ( title=>$OpenPageName,
53 text=>$Page{text},
54 raw=>1,
55 username=>$Page{username},
56 summary=>$Page{summary},
57 pwd=>GetParam('pwd',''),
59 $params{recent_edit} = 'on' if $Page{minor};
60 my $response = $ua->post($PublishTargetUrl, \%params);
61 if ($response->code == 302 and $response->header('Location')) {
62 print $q->redirect($response->header('Location'));
63 } elsif ($response->code == 200) {
64 print $q->redirect($PublishTargetUrl . '?' . $id);
65 } else {
66 ReportError($response->content,
67 $response->code . ' ' . $response->message);