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 POSIX qw(strftime);
14 use constant PREVIEW => "Preview";
15 use constant POST_COMMENT => "Post comment";
16 use constant CANCEL => "Cancel";
22 hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
23 hook(type => "getsetup", id => 'comments', call => \&getsetup);
24 hook(type => "preprocess", id => '_comment', call => \&preprocess);
25 hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
26 hook(type => "htmlize", id => "_comment", call => \&htmlize);
27 hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
28 hook(type => "formbuilder_setup", id => "comments", call => \&formbuilder_setup);
29 IkiWiki::loadplugin("inline");
38 comments_pagespec => {
40 example => 'blog/* and !*/Discussion',
41 description => 'PageSpec of pages where comments are allowed',
42 link => 'ikiwiki/PageSpec',
46 comments_closed_pagespec => {
48 example => 'blog/controversial or blog/flamewar',
49 description => 'PageSpec of pages where posting new comments is not allowed',
50 link => 'ikiwiki/PageSpec',
54 comments_pagename => {
56 default => 'comment_',
57 description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
58 safe => 0, # manual page moving required
61 comments_allowdirectives => {
64 description => 'Interpret directives in comments?',
68 comments_allowauthor => {
71 description => 'Allow anonymous commenters to set an author name?',
78 description => 'commit comments to the VCS',
79 # old uncommitted comments are likely to cause
80 # confusion if this is changed
87 $config{comments_commit} = 1
88 unless defined $config{comments_commit};
89 $config{comments_pagespec} = ''
90 unless defined $config{comments_pagespec};
91 $config{comments_closed_pagespec} = ''
92 unless defined $config{comments_closed_pagespec};
93 $config{comments_pagename} = 'comment_'
94 unless defined $config{comments_pagename};
99 return $params{content};
102 # FIXME: copied verbatim from meta
105 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
106 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
107 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
116 my $page = $params{page};
118 my $format = $params{format};
119 if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
120 error(sprintf(gettext("unsupported page format %s"), $format));
123 my $content = $params{content};
124 if (! defined $content) {
125 error(gettext("comment must have content"));
127 $content =~ s/\\"/"/g;
129 $content = IkiWiki::filter($page, $params{destpage}, $content);
131 if ($config{comments_allowdirectives}) {
132 $content = IkiWiki::preprocess($page, $params{destpage},
136 # no need to bother with htmlize if it's just HTML
137 $content = IkiWiki::htmlize($page, $params{destpage}, $format, $content)
140 IkiWiki::run_hooks(sanitize => sub {
143 destpage => $params{destpage},
148 # set metadata, possibly overriding [[!meta]] directives from the
154 my $commentauthorurl;
156 if (defined $params{username}) {
157 $commentuser = $params{username};
159 my $oiduser = eval { IkiWiki::openiduser($commentuser) };
161 if (defined $oiduser) {
162 # looks like an OpenID
163 $commentauthorurl = $commentuser;
164 $commentauthor = $oiduser;
165 $commentopenid = $commentuser;
168 $commentauthorurl = IkiWiki::cgiurl(
170 page => (length $config{userdir}
171 ? "$config{userdir}/$commentuser"
174 $commentauthor = $commentuser;
178 if (defined $params{ip}) {
179 $commentip = $params{ip};
181 $commentauthor = gettext("Anonymous");
184 $commentstate{$page}{commentuser} = $commentuser;
185 $commentstate{$page}{commentopenid} = $commentopenid;
186 $commentstate{$page}{commentip} = $commentip;
187 $commentstate{$page}{commentauthor} = $commentauthor;
188 $commentstate{$page}{commentauthorurl} = $commentauthorurl;
189 if (! defined $pagestate{$page}{meta}{author}) {
190 $pagestate{$page}{meta}{author} = $commentauthor;
192 if (! defined $pagestate{$page}{meta}{authorurl}) {
193 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
196 if ($config{comments_allowauthor}) {
197 if (defined $params{claimedauthor}) {
198 $pagestate{$page}{meta}{author} = $params{claimedauthor};
201 if (defined $params{url}) {
202 my $url=$params{url};
204 eval q{use URI::Heuristic};
206 $url=URI::Heuristic::uf_uristr($url);
210 $pagestate{$page}{meta}{authorurl} = $url;
215 $pagestate{$page}{meta}{author} = $commentauthor;
216 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
219 if (defined $params{subject}) {
220 $pagestate{$page}{meta}{title} = $params{subject};
223 if ($params{page} =~ m/\/(\Q$config{comments_pagename}\E\d+)$/) {
224 $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page}), undef, 1).
228 eval q{use Date::Parse};
230 my $time = str2time($params{date});
231 $IkiWiki::pagectime{$page} = $time if defined $time;
237 sub sessioncgi ($$) {
241 my $do = $cgi->param('do');
242 if ($do eq 'comment') {
243 editcomment($cgi, $session);
245 elsif ($do eq 'commentmoderation') {
246 commentmoderation($cgi, $session);
250 # Mostly cargo-culted from IkiWiki::plugin::editpage
251 sub editcomment ($$) {
255 IkiWiki::decode_cgi_utf8($cgi);
257 eval q{use CGI::FormBuilder};
260 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
261 my $form = CGI::FormBuilder->new(
262 fields => [qw{do sid page subject editcontent type author url}],
265 required => [qw{editcontent}],
268 action => $config{cgiurl},
271 template => scalar IkiWiki::template_params('editcomment.tmpl'),
274 IkiWiki::decode_form_utf8($form);
275 IkiWiki::run_hooks(formbuilder_setup => sub {
276 shift->(title => "comment", form => $form, cgi => $cgi,
277 session => $session, buttons => \@buttons);
279 IkiWiki::decode_form_utf8($form);
281 my $type = $form->param('type');
282 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
283 $type = IkiWiki::possibly_foolish_untaint($type);
286 $type = $config{default_pageext};
289 if (exists $IkiWiki::hooks{htmlize}) {
290 @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
293 $form->field(name => 'do', type => 'hidden');
294 $form->field(name => 'sid', type => 'hidden', value => $session->id,
296 $form->field(name => 'page', type => 'hidden');
297 $form->field(name => 'subject', type => 'text', size => 72);
298 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
299 $form->field(name => "type", value => $type, force => 1,
300 type => 'select', options => \@page_types);
302 $form->tmpl_param(username => $session->param('name'));
304 if ($config{comments_allowauthor} and
305 ! defined $session->param('name')) {
306 $form->tmpl_param(allowauthor => 1);
307 $form->field(name => 'author', type => 'text', size => '40');
308 $form->field(name => 'url', type => 'text', size => '40');
311 $form->tmpl_param(allowauthor => 0);
312 $form->field(name => 'author', type => 'hidden', value => '',
314 $form->field(name => 'url', type => 'hidden', value => '',
318 # The untaint is OK (as in editpage) because we're about to pass
319 # it to file_pruned anyway
320 my $page = $form->field('page');
321 $page = IkiWiki::possibly_foolish_untaint($page);
322 if (! defined $page || ! length $page ||
323 IkiWiki::file_pruned($page, $config{srcdir})) {
324 error(gettext("bad page name"));
327 my $baseurl = urlto($page, undef, 1);
329 $form->title(sprintf(gettext("commenting on %s"),
330 IkiWiki::pagetitle($page)));
332 $form->tmpl_param('helponformattinglink',
333 htmllink($page, $page, 'ikiwiki/formatting',
335 linktext => 'FormattingHelp'),
336 allowdirectives => $config{allow_directives});
338 if ($form->submitted eq CANCEL) {
339 # bounce back to the page they wanted to comment on, and exit.
340 # CANCEL need not be considered in future
341 IkiWiki::redirect($cgi, urlto($page, undef, 1));
345 if (not exists $pagesources{$page}) {
346 error(sprintf(gettext(
347 "page '%s' doesn't exist, so you can't comment"),
351 if (pagespec_match($page, $config{comments_closed_pagespec},
352 location => $page)) {
353 error(sprintf(gettext(
354 "comments on page '%s' are closed"),
358 # Set a flag to indicate that we're posting a comment,
359 # so that postcomment() can tell it should match.
361 IkiWiki::check_canedit($page, $cgi, $session);
364 my $location=unique_comment_location($page, $config{srcdir});
366 my $content = "[[!_comment format=$type\n";
368 # FIXME: handling of double quotes probably wrong?
369 if (defined $session->param('name')) {
370 my $username = $session->param('name');
371 $username =~ s/"/"/g;
372 $content .= " username=\"$username\"\n";
374 elsif (defined $ENV{REMOTE_ADDR}) {
375 my $ip = $ENV{REMOTE_ADDR};
376 if ($ip =~ m/^([.0-9]+)$/) {
377 $content .= " ip=\"$1\"\n";
381 if ($config{comments_allowauthor}) {
382 my $author = $form->field('author');
383 if (defined $author && length $author) {
384 $author =~ s/"/"/g;
385 $content .= " claimedauthor=\"$author\"\n";
387 my $url = $form->field('url');
388 if (defined $url && length $url) {
389 $url =~ s/"/"/g;
390 $content .= " url=\"$url\"\n";
394 my $subject = $form->field('subject');
395 if (defined $subject && length $subject) {
396 $subject =~ s/"/"/g;
397 $content .= " subject=\"$subject\"\n";
400 $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
402 my $editcontent = $form->field('editcontent') || '';
403 $editcontent =~ s/\r\n/\n/g;
404 $editcontent =~ s/\r/\n/g;
405 $editcontent =~ s/"/\\"/g;
406 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
408 # This is essentially a simplified version of editpage:
409 # - the user does not control the page that's created, only the parent
410 # - it's always a create operation, never an edit
411 # - this means that conflicts should never happen
412 # - this means that if they do, rocks fall and everyone dies
414 if ($form->submitted eq PREVIEW) {
415 my $preview=previewcomment($content, $location, $page, time);
416 IkiWiki::run_hooks(format => sub {
417 $preview = shift->(page => $page,
418 content => $preview);
420 $form->tmpl_param(page_preview => $preview);
423 $form->tmpl_param(page_preview => "");
426 if ($form->submitted eq POST_COMMENT && $form->validate) {
427 IkiWiki::checksessionexpiry($cgi, $session);
430 my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
431 subject => $form->field('subject'),
432 $config{comments_allowauthor} ? (
433 author => $form->field('author'),
434 url => $form->field('url'),
444 my $penddir=$config{wikistatedir}."/comments_pending";
445 $location=unique_comment_location($page, $penddir);
446 writefile("$location._comment", $penddir, $content);
447 IkiWiki::printheader($session);
448 print IkiWiki::misctemplate(gettext(gettext("comment stored for moderation")),
450 gettext("Your comment will be posted after moderator review").
455 # FIXME: could probably do some sort of graceful retry
456 # on error? Would require significant unwinding though
457 my $file = "$location._comment";
458 writefile($file, $config{srcdir}, $content);
462 if ($config{rcs} and $config{comments_commit}) {
463 my $message = gettext("Added a comment");
464 if (defined $form->field('subject') &&
465 length $form->field('subject')) {
467 gettext("Added a comment: %s"),
468 $form->field('subject'));
471 IkiWiki::rcs_add($file);
472 IkiWiki::disable_commit_hook();
473 $conflict = IkiWiki::rcs_commit_staged($message,
474 $session->param('name'), $ENV{REMOTE_ADDR});
475 IkiWiki::enable_commit_hook();
476 IkiWiki::rcs_update();
479 # Now we need a refresh
480 require IkiWiki::Render;
482 IkiWiki::saveindex();
484 # this should never happen, unless a committer deliberately
485 # breaks it or something
486 error($conflict) if defined $conflict;
488 # Jump to the new comment on the page.
489 # The trailing question mark tries to avoid broken
490 # caches and get the most recent version of the page.
491 IkiWiki::redirect($cgi, urlto($page, undef, 1)."?updated#$location");
495 IkiWiki::showform ($form, \@buttons, $session, $cgi,
496 forcebaseurl => $baseurl);
502 sub commentmoderation ($$) {
506 IkiWiki::needsignin($cgi, $session);
507 if (! IkiWiki::is_admin($session->param("name"))) {
508 error(gettext("you are not logged in as an admin"));
511 IkiWiki::decode_cgi_utf8($cgi);
513 if (defined $cgi->param('sid')) {
514 IkiWiki::checksessionexpiry($cgi, $session);
516 my $rejectalldefer=$cgi->param('rejectalldefer');
520 foreach my $id (keys %vars) {
521 if ($id =~ /(.*)\Q._comment\E$/) {
522 my $action=$cgi->param($id);
523 next if $action eq 'Defer' && ! $rejectalldefer;
525 # Make sure that the id is of a legal
526 # pending comment before untainting.
527 my ($f)= $id =~ /$config{wiki_file_regexp}/;
528 if (! defined $f || ! length $f ||
529 IkiWiki::file_pruned($f, $config{srcdir})) {
530 error("illegal file");
533 my $page=IkiWiki::possibly_foolish_untaint(IkiWiki::dirname($1));
534 my $file="$config{wikistatedir}/comments_pending/".
535 IkiWiki::possibly_foolish_untaint($id);
537 if ($action eq 'Accept') {
538 my $content=eval { readfile($file) };
539 next if $@; # file vanished since form was displayed
540 my $dest=unique_comment_location($page, $config{srcdir})."._comment";
541 writefile($dest, $config{srcdir}, $content);
542 if ($config{rcs} and $config{comments_commit}) {
543 IkiWiki::rcs_add($dest);
548 # This removes empty subdirs, so the
549 # .ikiwiki/comments_pending dir will
550 # go away when all are moderated.
551 require IkiWiki::Render;
552 IkiWiki::prune($file);
558 if ($config{rcs} and $config{comments_commit}) {
559 my $message = gettext("Comment moderation");
560 IkiWiki::disable_commit_hook();
561 $conflict=IkiWiki::rcs_commit_staged($message,
562 $session->param('name'), $ENV{REMOTE_ADDR});
563 IkiWiki::enable_commit_hook();
564 IkiWiki::rcs_update();
567 # Now we need a refresh
568 require IkiWiki::Render;
570 IkiWiki::saveindex();
572 error($conflict) if defined $conflict;
577 my ($id, $ctime)=@{$_};
578 my $file="$config{wikistatedir}/comments_pending/$id";
579 my $content=readfile($file);
580 my $preview=previewcomment($content, $id,
581 IkiWiki::dirname($_), $ctime);
586 } sort { $b->[1] <=> $a->[1] } comments_pending();
588 my $template=template("commentmoderation.tmpl");
591 comments => \@comments,
593 IkiWiki::printheader($session);
594 my $out=$template->output;
595 IkiWiki::run_hooks(format => sub {
596 $out = shift->(page => "", content => $out);
598 print IkiWiki::misctemplate(gettext("comment moderation"), $out);
602 sub formbuilder_setup (@) {
605 my $form=$params{form};
606 if ($form->title eq "preferences") {
607 push @{$params{buttons}}, "Comment Moderation";
608 if ($form->submitted && $form->submitted eq "Comment Moderation") {
609 commentmoderation($params{cgi}, $params{session});
614 sub comments_pending () {
615 my $dir="$config{wikistatedir}/comments_pending/";
616 return unless -d $dir;
619 eval q{use File::Find};
625 if (IkiWiki::file_pruned($_, $dir)) {
626 $File::Find::prune=1;
628 elsif (! -l $_ && ! -d _) {
629 $File::Find::prune=0;
630 my ($f)=/$config{wiki_file_regexp}/; # untaint
631 if (defined $f && $f =~ /\Q._comment\E$/) {
632 my $ctime=(stat($f))[10];
633 $f=~s/^\Q$dir\E\/?//;
634 push @ret, [$f, $ctime];
643 sub previewcomment ($$$) {
649 my $preview = IkiWiki::htmlize($location, $page, '_comment',
650 IkiWiki::linkify($location, $page,
651 IkiWiki::preprocess($location, $page,
652 IkiWiki::filter($location, $page, $content), 0, 1)));
654 my $template = template("comment.tmpl");
655 $template->param(content => $preview);
656 $template->param(ctime => displaytime($time));
658 IkiWiki::run_hooks(pagetemplate => sub {
659 shift->(page => $location,
661 template => $template);
664 $template->param(have_actions => 0);
666 return $template->output;
669 sub commentsshown ($) {
672 return ! pagespec_match($page, "*/$config{comments_pagename}*",
673 location => $page) &&
674 pagespec_match($page, $config{comments_pagespec},
678 sub commentsopen ($) {
681 return length $config{cgiurl} > 0 &&
682 (! length $config{comments_closed_pagespec} ||
683 ! pagespec_match($page, $config{comments_closed_pagespec},
687 sub pagetemplate (@) {
690 my $page = $params{page};
691 my $template = $params{template};
692 my $shown = ($template->query(name => 'commentslink') ||
693 $template->query(name => 'commentsurl') ||
694 $template->query(name => 'atomcommentsurl') ||
695 $template->query(name => 'comments')) &&
696 commentsshown($page);
698 if ($template->query(name => 'comments')) {
699 my $comments = undef;
701 $comments = IkiWiki::preprocess_inline(
702 pages => "internal($page/$config{comments_pagename}*)",
703 template => 'comment',
707 destpage => $params{destpage},
708 feedfile => 'comments',
713 if (defined $comments && length $comments) {
714 $template->param(comments => $comments);
717 if ($shown && commentsopen($page)) {
718 my $addcommenturl = IkiWiki::cgiurl(do => 'comment',
720 $template->param(addcommenturl => $addcommenturl);
724 if ($template->query(name => 'commentsurl')) {
726 $template->param(commentsurl =>
727 urlto($page, undef, 1).'#comments');
731 if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
733 # This will 404 until there are some comments, but I
734 # think that's probably OK...
735 $template->param(atomcommentsurl =>
736 urlto($page, undef, 1).'comments.atom');
740 if ($template->query(name => 'commentslink')) {
741 # XXX Would be nice to say how many comments there are in
742 # the link. But, to update the number, blog pages
743 # would have to update whenever comments of any inlines
744 # page are added, which is not currently done.
746 $template->param(commentslink =>
747 htmllink($page, $params{destpage}, $page,
748 linktext => gettext("Comments"),
749 anchor => "comments",
750 noimageinline => 1));
754 # everything below this point is only relevant to the comments
756 if (!exists $commentstate{$page}) {
760 if ($template->query(name => 'commentuser')) {
761 $template->param(commentuser =>
762 $commentstate{$page}{commentuser});
765 if ($template->query(name => 'commentopenid')) {
766 $template->param(commentopenid =>
767 $commentstate{$page}{commentopenid});
770 if ($template->query(name => 'commentip')) {
771 $template->param(commentip =>
772 $commentstate{$page}{commentip});
775 if ($template->query(name => 'commentauthor')) {
776 $template->param(commentauthor =>
777 $commentstate{$page}{commentauthor});
780 if ($template->query(name => 'commentauthorurl')) {
781 $template->param(commentauthorurl =>
782 $commentstate{$page}{commentauthorurl});
785 if ($template->query(name => 'removeurl') &&
786 IkiWiki::Plugin::remove->can("check_canremove") &&
787 length $config{cgiurl}) {
788 $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
790 $template->param(have_actions => 1);
794 sub unique_comment_location ($) {
802 $location = "$page/$config{comments_pagename}$i";
803 } while (-e "$dir/$location._comment");
808 package IkiWiki::PageSpec;
810 sub match_postcomment ($$;@) {
814 if (! $postcomment) {
815 return IkiWiki::FailReason->new("not posting a comment");
817 return match_glob($page, $glob);