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::comments;
12 use constant PREVIEW => "Preview";
13 use constant POST_COMMENT => "Post comment";
14 use constant CANCEL => "Cancel";
17 hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
18 hook(type => "getsetup", id => 'comments', call => \&getsetup);
19 hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
20 hook(type => "htmlize", id => "_comment", call => \&htmlize);
21 hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
22 hook(type => "cgi", id => "comments", call => \&linkcgi);
23 IkiWiki::loadplugin("inline");
24 IkiWiki::loadplugin("mdwn");
28 eval q{use IkiWiki::Plugin::mdwn};
30 return IkiWiki::Plugin::mdwn::htmlize(@_)
33 sub getsetup () { #{{{
39 # Pages where comments are shown, but new comments are not
40 # allowed, will show "Comments are closed".
41 comments_shown_pagespec => {
45 description => 'PageSpec for pages where comments will be shown inline',
46 link => 'ikiwiki/PageSpec',
50 comments_open_pagespec => {
52 example => 'blog/* and created_after(close_old_comments)',
54 description => 'PageSpec for pages where new comments can be posted',
55 link => 'ikiwiki/PageSpec',
59 comments_pagename => {
61 example => 'comment_',
62 default => 'comment_',
63 description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
64 safe => 0, # manual page moving will required
67 comments_allowdirectives => {
71 description => 'Allow directives in newly posted comments?',
79 description => 'commit comments to the VCS',
80 # old uncommitted comments are likely to cause
81 # confusion if this is changed
87 sub checkconfig () { #{{{
88 $config{comments_commit} = 1 unless defined $config{comments_commit};
89 $config{comments_pagename} = 'comment_'
90 unless defined $config{comments_pagename};
93 # FIXME: logic taken from editpage, should be common code?
94 sub getcgiuser ($) { # {{{
96 my $user = $session->param('name');
97 $user = $ENV{REMOTE_ADDR} unless defined $user;
98 debug("getcgiuser() -> $user");
102 # This is exactly the same as recentchanges_link :-(
103 sub linkcgi ($) { #{{{
105 if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
107 my $page=decode_utf8($cgi->param("page"));
108 if (!defined $page) {
109 error("missing page parameter");
112 IkiWiki::loadindex();
114 my $link=bestlink("", $page);
115 if (! length $link) {
116 print "Content-type: text/html\n\n";
117 print IkiWiki::misctemplate(gettext(gettext("missing page")),
119 sprintf(gettext("The page %s does not exist."),
120 htmllink("", "", $page)).
124 IkiWiki::redirect($cgi, urlto($link, undef, 1));
131 # FIXME: basically the same logic as recentchanges
132 # returns (author URL, pretty-printed version)
133 sub linkuser ($) { # {{{
135 my $oiduser = eval { IkiWiki::openiduser($user) };
137 if (defined $oiduser) {
138 return ($user, $oiduser);
140 # FIXME: it'd be good to avoid having such a link for anonymous
143 return (IkiWiki::cgiurl(
145 page => (length $config{userdir}
146 ? "$config{userdir}/"
152 # Mostly cargo-culted from IkiWiki::plugin::editpage
153 sub sessioncgi ($$) { #{{{
157 my $do = $cgi->param('do');
158 return unless $do eq 'comment';
160 IkiWiki::decode_cgi_utf8($cgi);
162 eval q{use CGI::FormBuilder};
165 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
166 my $form = CGI::FormBuilder->new(
167 fields => [qw{do sid page subject body}],
170 required => [qw{body}],
173 action => $config{cgiurl},
176 template => scalar IkiWiki::template_params('comments_form.tmpl'),
177 # wtf does this do in editpage?
178 wikiname => $config{wikiname},
181 IkiWiki::decode_form_utf8($form);
182 IkiWiki::run_hooks(formbuilder_setup => sub {
183 shift->(title => "comment", form => $form, cgi => $cgi,
184 session => $session, buttons => \@buttons);
186 IkiWiki::decode_form_utf8($form);
188 $form->field(name => 'do', type => 'hidden');
189 $form->field(name => 'sid', type => 'hidden', value => $session->id,
191 $form->field(name => 'page', type => 'hidden');
192 $form->field(name => 'subject', type => 'text', size => 72);
193 $form->field(name => 'body', type => 'textarea', rows => 5,
196 # The untaint is OK (as in editpage) because we're about to pass
197 # it to file_pruned anyway
198 my $page = $form->field('page');
199 $page = IkiWiki::possibly_foolish_untaint($page);
200 if (!defined $page || !length $page ||
201 IkiWiki::file_pruned($page, $config{srcdir})) {
202 error(gettext("bad page name"));
205 my $allow_directives = $config{comments_allowdirectives};
206 my $commit_comments = $config{comments_commit};
207 my $comments_pagename = $config{comments_pagename};
209 # FIXME: is this right? Or should we be using the candidate subpage
210 # (whatever that might mean) as the base URL?
211 my $baseurl = urlto($page, undef, 1);
213 $form->title(sprintf(gettext("commenting on %s"),
214 IkiWiki::pagetitle($page)));
216 $form->tmpl_param('helponformattinglink',
217 htmllink($page, $page, 'ikiwiki/formatting',
219 linktext => 'FormattingHelp'),
220 allowdirectives => $allow_directives);
222 if ($form->submitted eq CANCEL) {
223 # bounce back to the page they wanted to comment on, and exit.
224 # CANCEL need not be considered in future
225 IkiWiki::redirect($cgi, urlto($page, undef, 1));
229 if (not exists $pagesources{$page}) {
230 error(sprintf(gettext(
231 "page '%s' doesn't exist, so you can't comment"),
235 if (not pagespec_match($page, $config{comments_open_pagespec},
236 location => $page)) {
237 error(sprintf(gettext(
238 "comments on page '%s' are closed"),
242 IkiWiki::check_canedit($page . "[postcomment]", $cgi, $session);
244 my ($authorurl, $author) = linkuser(getcgiuser($session));
246 my $body = $form->field('body') || '';
247 $body =~ s/\r\n/\n/g;
249 $body .= "\n" if $body !~ /\n$/;
251 unless ($allow_directives) {
252 # don't allow new-style directives at all
253 $body =~ s/(^|[^\\])\[\[!/$1[[!/g;
255 # don't allow [[ unless it begins an old-style
256 # wikilink, if prefix_directives is off
257 $body =~ s/(^|[^\\])\[\[(?![^\n\s\]+]\]\])/$1[[!/g
258 unless $config{prefix_directives};
261 # FIXME: check that the wiki is locked right now, because
262 # if it's not, there are mad race conditions!
264 # FIXME: rather a simplistic way to make the comments...
270 $location = "$page/${comments_pagename}${i}";
271 } while (-e "$config{srcdir}/$location._comment");
273 my $anchor = "${comments_pagename}${i}";
275 IkiWiki::run_hooks(sanitize => sub {
278 destpage => $location,
283 # In this template, the [[!meta]] directives should stay at the end,
284 # so that they will override anything the user specifies. (For
285 # instance, [[!meta author="I can fake the author"]]...)
286 my $content_tmpl = template('comments_comment.tmpl');
287 $content_tmpl->param(author => $author);
288 $content_tmpl->param(authorurl => $authorurl);
289 $content_tmpl->param(subject => $form->field('subject'));
290 $content_tmpl->param(body => $body);
291 $content_tmpl->param(anchor => "$anchor");
292 $content_tmpl->param(permalink => "$baseurl#$anchor");
293 $content_tmpl->param(date => IkiWiki::formattime(time, "%X %x"));
295 my $content = $content_tmpl->output;
297 # This is essentially a simplified version of editpage:
298 # - the user does not control the page that's created, only the parent
299 # - it's always a create operation, never an edit
300 # - this means that conflicts should never happen
301 # - this means that if they do, rocks fall and everyone dies
303 if ($form->submitted eq PREVIEW) {
304 my $preview = IkiWiki::htmlize($location, $page, 'mdwn',
305 IkiWiki::linkify($page, $page,
306 IkiWiki::preprocess($page, $page,
307 IkiWiki::filter($location,
310 IkiWiki::run_hooks(format => sub {
311 $preview = shift->(page => $page,
312 content => $preview);
315 my $template = template("comments_display.tmpl");
316 $template->param(content => $preview);
317 $template->param(title => $form->field('subject'));
318 $template->param(ctime => displaytime(time));
319 $template->param(author => $author);
320 $template->param(authorurl => $authorurl);
322 $form->tmpl_param(page_preview => $template->output);
325 $form->tmpl_param(page_preview => "");
328 if ($form->submitted eq POST_COMMENT && $form->validate) {
329 my $file = "$location._comment";
331 IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
333 # FIXME: could probably do some sort of graceful retry
334 # on error? Would require significant unwinding though
335 writefile($file, $config{srcdir}, $content);
339 if ($config{rcs} and $commit_comments) {
340 my $message = gettext("Added a comment");
341 if (defined $form->field('subject') &&
342 length $form->field('subject')) {
344 gettext("Added a comment: %s"),
345 $form->field('subject'));
348 IkiWiki::rcs_add($file);
349 IkiWiki::disable_commit_hook();
350 $conflict = IkiWiki::rcs_commit_staged($message,
351 $session->param('name'), $ENV{REMOTE_ADDR});
352 IkiWiki::enable_commit_hook();
353 IkiWiki::rcs_update();
356 # Now we need a refresh
357 require IkiWiki::Render;
359 IkiWiki::saveindex();
361 # this should never happen, unless a committer deliberately
362 # breaks it or something
363 error($conflict) if defined $conflict;
365 # Bounce back to where we were, but defeat broken caches
366 my $anticache = "?updated=$page/${comments_pagename}${i}";
367 IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
370 IkiWiki::showform ($form, \@buttons, $session, $cgi,
371 forcebaseurl => $baseurl);
377 sub pagetemplate (@) { #{{{
380 my $page = $params{page};
381 my $template = $params{template};
383 if ($template->query(name => 'comments')) {
384 my $comments = undef;
386 my $comments_pagename = $config{comments_pagename};
389 my $shown = pagespec_match($page,
390 $config{comments_shown_pagespec},
393 if (pagespec_match($page, "*/${comments_pagename}*",
394 location => $page)) {
399 if (length $config{cgiurl}) {
400 $open = pagespec_match($page,
401 $config{comments_open_pagespec},
406 eval q{use IkiWiki::Plugin::inline};
410 pages => "internal($page/${comments_pagename}*)",
411 template => 'comments_display',
415 destpage => $params{destpage},
417 $comments = IkiWiki::preprocess_inline(@args);
420 if (defined $comments && length $comments) {
421 $template->param(comments => $comments);
425 my $commenturl = IkiWiki::cgiurl(do => 'comment',
427 $template->param(commenturl => $commenturl);
432 package IkiWiki::PageSpec;
434 sub match_postcomment ($$;@) {
438 unless ($page =~ s/\[postcomment\]$//) {
439 return IkiWiki::FailReason->new("not posting a comment");
441 return match_glob($page, $glob);