]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/smcvpostcomment.pm
1255cfde1dcbf32cfda38090a5029e847ff9f6ea
[ikiwiki.git] / IkiWiki / Plugin / smcvpostcomment.pm
1 #!/usr/bin/perl
2 # Copyright © 2006-2008 Joey Hess <joey@ikiwiki.info>
3 # Copyright © 2008 Simon McVittie <http://smcv.pseudorandom.co.uk/>
4 # Licensed under the GNU GPL, version 2, or any later version published by the
5 # Free Software Foundation
6 package IkiWiki::Plugin::smcvpostcomment;
7
8 use warnings;
9 use strict;
10 use IkiWiki 2.00;
11 use IkiWiki::Plugin::mdwn;
12 use CGI 'escapeHTML';
13
14 use constant PLUGIN => "smcvpostcomment";
15 use constant PREVIEW => "Preview";
16 use constant POST_COMMENT => "Post comment";
17 use constant CANCEL => "Cancel";
18
19 sub import { #{{{
20         hook(type => "getsetup", id => PLUGIN,  call => \&getsetup);
21         hook(type => "preprocess", id => PLUGIN, call => \&preprocess);
22         hook(type => "sessioncgi", id => PLUGIN, call => \&sessioncgi);
23         hook(type => "htmlize", id => "_".PLUGIN,
24                 call => \&IkiWiki::Plugin::mdwn::htmlize);
25         IkiWiki::loadplugin("inline");
26 } # }}}
27
28 sub getsetup () { #{{{
29         return
30                 plugin => {
31                         safe => 1,
32                         rebuild => undef,
33                 },
34 } #}}}
35
36 # Somewhat based on IkiWiki::Plugin::inline blog posting support
37 sub preprocess (@) { #{{{
38         my %params=@_;
39
40         unless (length $config{cgiurl}) {
41                 error(sprintf (gettext("[[!%s plugin requires CGI enabled]]"),
42                         PLUGIN));
43         }
44
45         my $formtemplate = IkiWiki::template(PLUGIN . "_embed.tmpl",
46                 blind_cache => 1);
47         $formtemplate->param(cgiurl => $config{cgiurl});
48         $formtemplate->param(page => $params{page});
49
50         if ($params{preview}) {
51                 $formtemplate->param("disabled" =>
52                         gettext('not available during Preview'));
53         }
54
55         debug("page $params{page} => destpage $params{destpage}");
56
57         # I'm reasonably sure that this counts as abuse of [[!inline]]
58         return $formtemplate->output . "\n" .
59                 IkiWiki::preprocess_inline(
60                         pages => "internal($params{page}/_comment_*)",
61                         template => PLUGIN . "_display",
62                         show => 0,
63                         reverse => "yes",
64                         page => $params{page},
65                         destpage => $params{destpage},
66                         preview => $params{preview});
67 } # }}}
68
69 # FIXME: logic taken from editpage, should be common code?
70 sub getcgiuser ($) { # {{{
71         my $session = shift;
72         my $user = $session->param('name');
73         $user = $ENV{REMOTE_ADDR} unless defined $user;
74         debug("getcgiuser() -> $user");
75         return $user;
76 } # }}}
77
78 # FIXME: logic adapted from recentchanges, should be common code?
79 sub linkuser ($) { # {{{
80         my $user = shift;
81         my $oiduser = eval { IkiWiki::openiduser($user) };
82
83         if (defined $oiduser) {
84                 return ($user, $oiduser);
85         }
86         else {
87                 my $page = bestlink('', (length $config{userdir}
88                                 ? "$config{userdir}/"
89                                 : "").$user);
90                 return (urlto($page, undef, 1), $user);
91         }
92 } # }}}
93
94 # FIXME: taken from IkiWiki::Plugin::editpage, should be common?
95 sub checksessionexpiry ($$) { # {{{
96         my $session = shift;
97         my $sid = shift;
98
99         if (defined $session->param("name")) {
100                 if (! defined $sid || $sid ne $session->id) {
101                         error(gettext("Your login session has expired."));
102                 }
103         }
104 } # }}}
105
106 # Mostly cargo-culted from IkiWiki::plugin::editpage
107 sub sessioncgi ($$) { #{{{
108         my $cgi=shift;
109         my $session=shift;
110
111         my $do = $cgi->param('do');
112         return unless $do eq PLUGIN;
113
114         # These are theoretically configurable, but currently hard-coded
115         my $allow_wikilinks = 0;
116         my $allow_directives = 0;
117         my $commit_comments = 1;
118
119         IkiWiki::decode_cgi_utf8($cgi);
120
121         eval q{use CGI::FormBuilder};
122         error($@) if $@;
123
124         my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
125         my $form = CGI::FormBuilder->new(
126                 fields => [qw{do sid page subject body}],
127                 charset => 'utf-8',
128                 method => 'POST',
129                 required => [qw{body}],
130                 javascript => 0,
131                 params => $cgi,
132                 action => $config{cgiurl},
133                 header => 0,
134                 table => 0,
135                 template => scalar IkiWiki::template_params(PLUGIN . '_form.tmpl'),
136                 # wtf does this do in editpage?
137                 wikiname => $config{wikiname},
138         );
139
140         IkiWiki::decode_form_utf8($form);
141         IkiWiki::run_hooks(formbuilder_setup => sub {
142                         shift->(title => PLUGIN, form => $form, cgi => $cgi,
143                                 session => $session, buttons => \@buttons);
144                 });
145         IkiWiki::decode_form_utf8($form);
146
147         $form->field(name => 'do', type => 'hidden');
148         $form->field(name => 'sid', type => 'hidden', value => $session->id,
149                 force => 1);
150         $form->field(name => 'page', type => 'hidden');
151         $form->field(name => 'subject', type => 'text', size => 80);
152         $form->field(name => 'body', type => 'textarea', rows => 5,
153                 cols => 80);
154
155         # The untaint is OK (as in editpage) because we're about to pass
156         # it to file_pruned anyway
157         my $page = $form->field('page');
158         $page = IkiWiki::possibly_foolish_untaint($page);
159         if (!defined $page || !length $page ||
160                 IkiWiki::file_pruned($page, $config{srcdir})) {
161                 error(gettext("bad page name"));
162         }
163
164         # FIXME: is this right? Or should we be using the candidate subpage
165         # (whatever that might mean) as the base URL?
166         my $baseurl = urlto($page, undef, 1);
167
168         $form->title(sprintf(gettext("commenting on %s"),
169                         IkiWiki::pagetitle($page)));
170
171         $form->tmpl_param('helponformattinglink',
172                 htmllink($page, $page, 'ikiwiki/formatting',
173                         noimageinline => 1,
174                         linktext => 'FormattingHelp'));
175
176         if (not exists $pagesources{$page}) {
177                 error(sprintf(gettext(
178                         "page '%s' doesn't exist, so you can't comment"),
179                         $page));
180         }
181
182         if ($form->submitted eq CANCEL) {
183                 # bounce back to the page they wanted to comment on, and exit.
184                 # CANCEL need not be considered in future
185                 IkiWiki::redirect($cgi, urlto($page, undef, 1));
186                 exit;
187         }
188
189         my ($authorurl, $author) = linkuser(getcgiuser($session));
190
191         my $body = $form->field('body');
192         $body =~ s/\r\n/\n/g;
193         $body =~ s/\r/\n/g;
194         $body .= "\n" if $body !~ /\n$/;
195
196         $body =~ s/\[\[([^!])/&#91;&#91;$1/g unless $allow_wikilinks;
197         $body =~ s/\[\[!/&#91;&#91;!/g unless $allow_directives;
198
199         # In this template, the [[!meta]] directives should stay at the end,
200         # so that they will override anything the user specifies. (For
201         # instance, [[!meta author="I can fake the author"]]...)
202         my $content_tmpl = template(PLUGIN . '_comment.tmpl');
203         $content_tmpl->param(author => $author);
204         $content_tmpl->param(authorurl => $authorurl);
205         $content_tmpl->param(subject => $form->field('subject'));
206         $content_tmpl->param(body => $body);
207
208         my $content = $content_tmpl->output;
209
210         # This is essentially a simplified version of editpage:
211         # - the user does not control the page that's created, only the parent
212         # - it's always a create operation, never an edit
213         # - this means that conflicts should never happen
214         # - this means that if they do, rocks fall and everyone dies
215
216         if ($form->submitted eq PREVIEW) {
217                 my $fake = "$page/_" . PLUGIN . "hypothetical";
218                 my $preview = IkiWiki::htmlize($fake, $page, 'mdwn',
219                                 IkiWiki::linkify($page, $page,
220                                         IkiWiki::preprocess($page, $page,
221                                                 IkiWiki::filter($fake, $page,
222                                                         $content),
223                                                 0, 1)));
224                 IkiWiki::run_hooks(format => sub {
225                                 $preview = shift->(page => $page,
226                                         content => $preview);
227                         });
228
229                 my $template = template(PLUGIN . "_display.tmpl");
230                 $template->param(content => $preview);
231                 $template->param(title => $form->field('subject'));
232                 $template->param(ctime => displaytime(time));
233                 $template->param(author => $author);
234                 $template->param(authorurl => $authorurl);
235
236                 $form->tmpl_param(page_preview => $template->output);
237         }
238         else {
239                 $form->tmpl_param(page_preview => "");
240         }
241
242         if ($form->submitted eq POST_COMMENT && $form->validate) {
243                 # Let's get posting. We don't check_canedit here because
244                 # that somewhat defeats the point of this plugin.
245
246                 checksessionexpiry($session, $cgi->param('sid'));
247
248                 # FIXME: check that the wiki is locked right now, because
249                 # if it's not, there are mad race conditions!
250
251                 # FIXME: rather a simplistic way to make the comments...
252                 my $i = 0;
253                 my $file;
254                 do {
255                         $i++;
256                         $file = "$page/comment_${i}._" . PLUGIN;
257                 } while (-e "$config{srcdir}/$file");
258
259                 # FIXME: could probably do some sort of graceful retry
260                 # if I could be bothered
261                 writefile($file, $config{srcdir}, $content);
262
263                 my $conflict;
264
265                 if ($config{rcs} and $commit_comments) {
266                         my $message = gettext("Added a comment");
267                         if (defined $form->field('subject') &&
268                                 length $form->field('subject')) {
269                                 $message .= ": ".$form->field('subject');
270                         }
271
272                         IkiWiki::rcs_add($file);
273                         IkiWiki::disable_commit_hook();
274                         $conflict = IkiWiki::rcs_commit_staged($message,
275                                 $session->param('name'), $ENV{REMOTE_ADDR});
276                         IkiWiki::enable_commit_hook();
277                         IkiWiki::rcs_update();
278                 }
279
280                 # Now we need a refresh
281                 require IkiWiki::Render;
282                 IkiWiki::refresh();
283                 IkiWiki::saveindex();
284
285                 # this should never happen, unless a committer deliberately
286                 # breaks it or something
287                 error($conflict) if defined $conflict;
288
289                 # Bounce back to where we were, but defeat broken caches
290                 my $anticache = "?updated=$page/comment_$i";
291                 IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
292         }
293         else {
294                 IkiWiki::showform ($form, \@buttons, $session, $cgi,
295                         forcebaseurl => $baseurl);
296         }
297
298         exit;
299 } #}}}
300
301 1