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;
13 use constant PREVIEW => "Preview";
14 use constant POST_COMMENT => "Post comment";
15 use constant CANCEL => "Cancel";
21 hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
22 hook(type => "getsetup", id => 'comments', call => \&getsetup);
23 hook(type => "preprocess", id => 'comment', call => \&preprocess,
25 hook(type => "preprocess", id => 'commentmoderation', call => \&preprocess_moderation);
26 # here for backwards compatability with old comments
27 hook(type => "preprocess", id => '_comment', call => \&preprocess);
28 hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
29 hook(type => "htmlize", id => "_comment", call => \&htmlize);
30 hook(type => "htmlize", id => "_comment_pending",
31 call => \&htmlize_pending);
32 hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
33 hook(type => "formbuilder_setup", id => "comments",
34 call => \&formbuilder_setup);
35 # Load goto to fix up user page links for logged-in commenters
36 IkiWiki::loadplugin("goto");
37 IkiWiki::loadplugin("inline");
47 comments_pagespec => {
49 example => 'blog/* and !*/Discussion',
50 description => 'PageSpec of pages where comments are allowed',
51 link => 'ikiwiki/PageSpec',
55 comments_closed_pagespec => {
57 example => 'blog/controversial or blog/flamewar',
58 description => 'PageSpec of pages where posting new comments is not allowed',
59 link => 'ikiwiki/PageSpec',
63 comments_pagename => {
65 default => 'comment_',
66 description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
67 safe => 0, # manual page moving required
70 comments_allowdirectives => {
73 description => 'Interpret directives in comments?',
77 comments_allowauthor => {
80 description => 'Allow anonymous commenters to set an author name?',
87 description => 'commit comments to the VCS',
88 # old uncommitted comments are likely to cause
89 # confusion if this is changed
93 comments_allowformats => {
96 example => 'mdwn txt',
97 description => 'Restrict formats for comments to (no restriction if empty)',
105 $config{comments_commit} = 1
106 unless defined $config{comments_commit};
107 if (! $config{comments_commit}) {
108 $config{only_committed_changes}=0;
110 $config{comments_pagespec} = ''
111 unless defined $config{comments_pagespec};
112 $config{comments_closed_pagespec} = ''
113 unless defined $config{comments_closed_pagespec};
114 $config{comments_pagename} = 'comment_'
115 unless defined $config{comments_pagename};
116 $config{comments_allowformats} = ''
117 unless defined $config{comments_allowformats};
122 return $params{content};
125 sub htmlize_pending {
127 return sprintf(gettext("this comment needs %s"),
129 IkiWiki::cgiurl(do => "commentmoderation").'">'.
130 gettext("moderation").'</a>');
133 # FIXME: copied verbatim from meta
136 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
137 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
138 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
147 return ! $config{comments_allowformats} || $config{comments_allowformats} =~ /\b$format\b/;
152 my $page = $params{page};
154 my $format = $params{format};
155 if (defined $format && (! exists $IkiWiki::hooks{htmlize}{$format} ||
156 ! isallowed($format))) {
157 error(sprintf(gettext("unsupported page format %s"), $format));
160 my $content = $params{content};
161 if (! defined $content) {
162 error(gettext("comment must have content"));
164 $content =~ s/\\"/"/g;
166 if (defined wantarray) {
167 if ($config{comments_allowdirectives}) {
168 $content = IkiWiki::preprocess($page, $params{destpage},
172 # no need to bother with htmlize if it's just HTML
173 $content = IkiWiki::htmlize($page, $params{destpage}, $format, $content)
176 IkiWiki::run_hooks(sanitize => sub {
179 destpage => $params{destpage},
185 IkiWiki::preprocess($page, $params{destpage}, $content, 1);
188 # set metadata, possibly overriding [[!meta]] directives from the
194 my $commentauthorurl;
196 if (defined $params{username}) {
197 $commentuser = $params{username};
199 my $oiduser = eval { IkiWiki::openiduser($commentuser) };
201 if (defined $oiduser) {
202 # looks like an OpenID
203 $commentauthorurl = $commentuser;
204 $commentauthor = (defined $params{nickname} && length $params{nickname}) ? $params{nickname} : $oiduser;
205 $commentopenid = $commentuser;
208 $commentauthorurl = IkiWiki::cgiurl(
210 page => IkiWiki::userpage($commentuser)
213 $commentauthor = $commentuser;
217 if (defined $params{ip}) {
218 $commentip = $params{ip};
220 $commentauthor = gettext("Anonymous");
223 $commentstate{$page}{commentuser} = $commentuser;
224 $commentstate{$page}{commentopenid} = $commentopenid;
225 $commentstate{$page}{commentip} = $commentip;
226 $commentstate{$page}{commentauthor} = $commentauthor;
227 $commentstate{$page}{commentauthorurl} = $commentauthorurl;
228 $commentstate{$page}{commentauthoravatar} = $params{avatar};
229 if (! defined $pagestate{$page}{meta}{author}) {
230 $pagestate{$page}{meta}{author} = $commentauthor;
232 if (! defined $pagestate{$page}{meta}{authorurl}) {
233 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
236 if ($config{comments_allowauthor}) {
237 if (defined $params{claimedauthor}) {
238 $pagestate{$page}{meta}{author} = $params{claimedauthor};
241 if (defined $params{url}) {
242 my $url=$params{url};
244 eval q{use URI::Heuristic};
246 $url=URI::Heuristic::uf_uristr($url);
250 $pagestate{$page}{meta}{authorurl} = $url;
255 $pagestate{$page}{meta}{author} = $commentauthor;
256 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
259 if (defined $params{subject}) {
260 # decode title the same way meta does
261 eval q{use HTML::Entities};
262 $pagestate{$page}{meta}{title} = decode_entities($params{subject});
265 if ($params{page} =~ m/\/\Q$config{comments_pagename}\E\d+_/) {
266 $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page})).
267 "#".page_to_id($params{page});
270 eval q{use Date::Parse};
272 my $time = str2time($params{date});
273 $IkiWiki::pagectime{$page} = $time if defined $time;
279 sub preprocess_moderation {
282 $params{desc}=gettext("Comment Moderation")
283 unless defined $params{desc};
285 if (length $config{cgiurl}) {
287 IkiWiki::cgiurl(do => 'commentmoderation').
288 '">'.$params{desc}.'</a>';
291 return $params{desc};
295 sub sessioncgi ($$) {
299 my $do = $cgi->param('do');
300 if ($do eq 'comment') {
301 editcomment($cgi, $session);
303 elsif ($do eq 'commentmoderation') {
304 commentmoderation($cgi, $session);
306 elsif ($do eq 'commentsignin') {
307 IkiWiki::cgi_signin($cgi, $session);
312 # Mostly cargo-culted from IkiWiki::plugin::editpage
313 sub editcomment ($$) {
317 IkiWiki::decode_cgi_utf8($cgi);
319 eval q{use CGI::FormBuilder};
322 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
323 my $form = CGI::FormBuilder->new(
324 fields => [qw{do sid page subject editcontent type author
325 email url subscribe anonsubscribe}],
328 required => [qw{editcontent}],
331 action => IkiWiki::cgiurl(),
334 template => { template('editcomment.tmpl') },
337 IkiWiki::decode_form_utf8($form);
338 IkiWiki::run_hooks(formbuilder_setup => sub {
339 shift->(title => "comment", form => $form, cgi => $cgi,
340 session => $session, buttons => \@buttons);
342 IkiWiki::decode_form_utf8($form);
344 my $type = $form->param('type');
345 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
346 $type = IkiWiki::possibly_foolish_untaint($type);
349 $type = $config{default_pageext};
354 if (exists $IkiWiki::hooks{htmlize}) {
355 foreach my $key (grep { !/^_/ && isallowed($_) } keys %{$IkiWiki::hooks{htmlize}}) {
356 push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
359 @page_types=sort @page_types;
361 $form->field(name => 'do', type => 'hidden');
362 $form->field(name => 'sid', type => 'hidden', value => $session->id,
364 $form->field(name => 'page', type => 'hidden');
365 $form->field(name => 'subject', type => 'text', size => 72);
366 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
367 $form->field(name => "type", value => $type, force => 1,
368 type => 'select', options => \@page_types);
370 my $username=$session->param('name');
371 $form->tmpl_param(username => $username);
373 $form->field(name => "subscribe", type => 'hidden');
374 $form->field(name => "anonsubscribe", type => 'hidden');
375 if (IkiWiki::Plugin::notifyemail->can("subscribe")) {
376 if (defined $username) {
377 $form->field(name => "subscribe", type => "checkbox",
378 options => [gettext("email replies to me")]);
380 elsif (IkiWiki::Plugin::passwordauth->can("anonuser")) {
381 $form->field(name => "anonsubscribe", type => "checkbox",
382 options => [gettext("email replies to me")]);
386 if ($config{comments_allowauthor} and
387 ! defined $session->param('name')) {
388 $form->tmpl_param(allowauthor => 1);
389 $form->field(name => 'author', type => 'text', size => '40');
390 $form->field(name => 'email', type => 'text', size => '40');
391 $form->field(name => 'url', type => 'text', size => '40');
394 $form->tmpl_param(allowauthor => 0);
395 $form->field(name => 'author', type => 'hidden', value => '',
397 $form->field(name => 'email', type => 'hidden', value => '',
399 $form->field(name => 'url', type => 'hidden', value => '',
403 if (! defined $session->param('name')) {
404 # Make signinurl work and return here.
405 $form->tmpl_param(signinurl => IkiWiki::cgiurl(do => 'commentsignin'));
406 $session->param(postsignin => $ENV{QUERY_STRING});
407 IkiWiki::cgi_savesession($session);
410 # The untaint is OK (as in editpage) because we're about to pass
411 # it to file_pruned and wiki_file_regexp anyway.
412 my ($page) = $form->field('page')=~/$config{wiki_file_regexp}/;
413 $page = IkiWiki::possibly_foolish_untaint($page);
414 if (! defined $page || ! length $page ||
415 IkiWiki::file_pruned($page)) {
416 error(gettext("bad page name"));
419 $form->title(sprintf(gettext("commenting on %s"),
420 IkiWiki::pagetitle(IkiWiki::basename($page))));
422 $form->tmpl_param('helponformattinglink',
423 htmllink($page, $page, 'ikiwiki/formatting',
425 linktext => 'FormattingHelp'),
426 allowdirectives => $config{allow_directives});
428 if ($form->submitted eq CANCEL) {
429 # bounce back to the page they wanted to comment on, and exit.
430 IkiWiki::redirect($cgi, urlto($page));
434 if (not exists $pagesources{$page}) {
435 error(sprintf(gettext(
436 "page '%s' doesn't exist, so you can't comment"),
440 if (pagespec_match($page, $config{comments_closed_pagespec},
441 location => $page)) {
442 error(sprintf(gettext(
443 "comments on page '%s' are closed"),
447 # Set a flag to indicate that we're posting a comment,
448 # so that postcomment() can tell it should match.
450 IkiWiki::check_canedit($page, $cgi, $session);
453 my $content = "[[!comment format=$type\n";
455 if (defined $session->param('name')) {
456 my $username = $session->param('name');
457 $username =~ s/"/"/g;
458 $content .= " username=\"$username\"\n";
460 if (defined $session->param('nickname')) {
461 my $nickname = $session->param('nickname');
462 $nickname =~ s/"/"/g;
463 $content .= " nickname=\"$nickname\"\n";
465 elsif (defined $session->remote_addr()) {
466 $content .= " ip=\"".$session->remote_addr()."\"\n";
469 if ($config{comments_allowauthor}) {
470 my $author = $form->field('author');
471 if (defined $author && length $author) {
472 $author =~ s/"/"/g;
473 $content .= " claimedauthor=\"$author\"\n";
475 my $url = $form->field('url');
476 if (defined $url && length $url) {
477 $url =~ s/"/"/g;
478 $content .= " url=\"$url\"\n";
482 my $avatar=getavatar($session->param('name'));
483 if (defined $avatar && length $avatar) {
484 $avatar =~ s/"/"/g;
485 $content .= " avatar=\"$avatar\"\n";
488 my $subject = $form->field('subject');
489 if (defined $subject && length $subject) {
490 $subject =~ s/"/"/g;
493 $subject = "comment ".(num_comments($page, $config{srcdir}) + 1);
495 $content .= " subject=\"$subject\"\n";
497 $content .= " date=\"" . strftime_utf8('%Y-%m-%dT%H:%M:%SZ', gmtime) . "\"\n";
499 my $editcontent = $form->field('editcontent');
500 $editcontent="" if ! defined $editcontent;
501 $editcontent =~ s/\r\n/\n/g;
502 $editcontent =~ s/\r/\n/g;
503 $editcontent =~ s/"/\\"/g;
504 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
506 my $location=unique_comment_location($page, $content, $config{srcdir});
508 # This is essentially a simplified version of editpage:
509 # - the user does not control the page that's created, only the parent
510 # - it's always a create operation, never an edit
511 # - this means that conflicts should never happen
512 # - this means that if they do, rocks fall and everyone dies
514 if ($form->submitted eq PREVIEW) {
515 my $preview=previewcomment($content, $location, $page, time);
516 IkiWiki::run_hooks(format => sub {
517 $preview = shift->(page => $page,
518 content => $preview);
520 $form->tmpl_param(page_preview => $preview);
523 $form->tmpl_param(page_preview => "");
526 if ($form->submitted eq POST_COMMENT && $form->validate) {
527 IkiWiki::checksessionexpiry($cgi, $session);
529 if (IkiWiki::Plugin::notifyemail->can("subscribe")) {
530 my $subspec="comment($page)";
531 if (defined $username &&
532 length $form->field("subscribe")) {
533 IkiWiki::Plugin::notifyemail::subscribe(
534 $username, $subspec);
536 elsif (length $form->field("email") &&
537 length $form->field("anonsubscribe")) {
538 IkiWiki::Plugin::notifyemail::anonsubscribe(
539 $form->field("email"), $subspec);
544 my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
545 subject => $form->field('subject'),
546 $config{comments_allowauthor} ? (
547 author => $form->field('author'),
548 url => $form->field('url'),
558 $location=unique_comment_location($page, $content, $config{srcdir}, "._comment_pending");
559 writefile("$location._comment_pending", $config{srcdir}, $content);
561 # Refresh so anything that deals with pending
562 # comments can be updated.
563 require IkiWiki::Render;
565 IkiWiki::saveindex();
567 IkiWiki::printheader($session);
568 print IkiWiki::cgitemplate($cgi, gettext(gettext("comment stored for moderation")),
570 gettext("Your comment will be posted after moderator review").
575 # FIXME: could probably do some sort of graceful retry
576 # on error? Would require significant unwinding though
577 my $file = "$location._comment";
578 writefile($file, $config{srcdir}, $content);
582 if ($config{rcs} and $config{comments_commit}) {
583 my $message = gettext("Added a comment");
584 if (defined $form->field('subject') &&
585 length $form->field('subject')) {
587 gettext("Added a comment: %s"),
588 $form->field('subject'));
591 IkiWiki::rcs_add($file);
592 IkiWiki::disable_commit_hook();
593 $conflict = IkiWiki::rcs_commit_staged(
597 IkiWiki::enable_commit_hook();
598 IkiWiki::rcs_update();
601 # Now we need a refresh
602 require IkiWiki::Render;
604 IkiWiki::saveindex();
606 # this should never happen, unless a committer deliberately
607 # breaks it or something
608 error($conflict) if defined $conflict;
610 # Jump to the new comment on the page.
611 # The trailing question mark tries to avoid broken
612 # caches and get the most recent version of the page.
613 IkiWiki::redirect($cgi, urlto($page).
614 "?updated#".page_to_id($location));
618 IkiWiki::showform($form, \@buttons, $session, $cgi,
627 return undef unless defined $user;
630 eval q{use Libravatar::URL};
632 my $oiduser = eval { IkiWiki::openiduser($user) };
633 my $https=defined $config{url} && $config{url}=~/^https:/;
635 if (defined $oiduser) {
637 $avatar = libravatar_url(openid => $user, https => $https);
640 if (! defined $avatar &&
641 (my $email = IkiWiki::userinfo_get($user, 'email'))) {
643 $avatar = libravatar_url(email => $email, https => $https);
651 sub commentmoderation ($$) {
655 IkiWiki::needsignin($cgi, $session);
656 if (! IkiWiki::is_admin($session->param("name"))) {
657 error(gettext("you are not logged in as an admin"));
660 IkiWiki::decode_cgi_utf8($cgi);
662 if (defined $cgi->param('sid')) {
663 IkiWiki::checksessionexpiry($cgi, $session);
665 my $rejectalldefer=$cgi->param('rejectalldefer');
669 foreach my $id (keys %vars) {
670 if ($id =~ /(.*)\._comment(?:_pending)?$/) {
671 $id=decode_utf8($id);
672 my $action=$cgi->param($id);
673 next if $action eq 'Defer' && ! $rejectalldefer;
675 # Make sure that the id is of a legal
677 my ($f) = $id =~ /$config{wiki_file_regexp}/;
678 if (! defined $f || ! length $f ||
679 IkiWiki::file_pruned($f)) {
680 error("illegal file");
683 my $page=IkiWiki::dirname($f);
684 my $file="$config{srcdir}/$f";
685 my $filedir=$config{srcdir};
688 $file="$config{wikistatedir}/comments_pending/".$f;
689 $filedir="$config{wikistatedir}/comments_pending";
692 if ($action eq 'Accept') {
693 my $content=eval { readfile($file) };
694 next if $@; # file vanished since form was displayed
695 my $dest=unique_comment_location($page, $content, $config{srcdir})."._comment";
696 writefile($dest, $config{srcdir}, $content);
697 if ($config{rcs} and $config{comments_commit}) {
698 IkiWiki::rcs_add($dest);
703 require IkiWiki::Render;
704 IkiWiki::prune($file, $filedir);
710 if ($config{rcs} and $config{comments_commit}) {
711 my $message = gettext("Comment moderation");
712 IkiWiki::disable_commit_hook();
713 $conflict=IkiWiki::rcs_commit_staged(
717 IkiWiki::enable_commit_hook();
718 IkiWiki::rcs_update();
721 # Now we need a refresh
722 require IkiWiki::Render;
724 IkiWiki::saveindex();
726 error($conflict) if defined $conflict;
731 my ($id, $dir, $ctime)=@{$_};
732 my $content=readfile("$dir/$id");
733 my $preview=previewcomment($content, $id,
739 } sort { $b->[2] <=> $a->[2] } comments_pending();
741 my $template=template("commentmoderation.tmpl");
744 comments => \@comments,
745 cgiurl => IkiWiki::cgiurl(),
747 IkiWiki::printheader($session);
748 my $out=$template->output;
749 IkiWiki::run_hooks(format => sub {
750 $out = shift->(page => "", content => $out);
752 print IkiWiki::cgitemplate($cgi, gettext("comment moderation"), $out);
756 sub formbuilder_setup (@) {
759 my $form=$params{form};
760 if ($form->title eq "preferences" &&
761 IkiWiki::is_admin($params{session}->param("name"))) {
762 push @{$params{buttons}}, "Comment Moderation";
763 if ($form->submitted && $form->submitted eq "Comment Moderation") {
764 commentmoderation($params{cgi}, $params{session});
769 sub comments_pending () {
772 eval q{use File::Find};
776 my $origdir=getcwd();
778 my $find_comments=sub {
781 return unless -d $dir;
783 chdir($dir) || die "chdir $dir: $!";
788 my $file=decode_utf8($_);
790 return if ! length $file || IkiWiki::file_pruned($file)
791 || -l $_ || -d _ || $file !~ /\Q$extension\E$/;
792 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
794 my $ctime=(stat($_))[10];
795 push @ret, [$f, $dir, $ctime];
800 chdir($origdir) || die "chdir $origdir: $!";
803 $find_comments->($config{srcdir}, "._comment_pending");
805 $find_comments->("$config{wikistatedir}/comments_pending/",
811 sub previewcomment ($$$) {
817 # Previewing a comment should implicitly enable comment posting mode.
818 my $oldpostcomment=$postcomment;
821 my $preview = IkiWiki::htmlize($location, $page, '_comment',
822 IkiWiki::linkify($location, $page,
823 IkiWiki::preprocess($location, $page,
824 IkiWiki::filter($location, $page, $content), 0, 1)));
826 my $template = template("comment.tmpl");
827 $template->param(content => $preview);
828 $template->param(ctime => displaytime($time, undef, 1));
829 $template->param(html5 => $config{html5});
831 IkiWiki::run_hooks(pagetemplate => sub {
832 shift->(page => $location,
834 template => $template);
837 $template->param(have_actions => 0);
839 $postcomment=$oldpostcomment;
841 return $template->output;
844 sub commentsshown ($) {
847 return pagespec_match($page, $config{comments_pagespec},
851 sub commentsopen ($) {
854 return length $config{cgiurl} > 0 &&
855 (! length $config{comments_closed_pagespec} ||
856 ! pagespec_match($page, $config{comments_closed_pagespec},
860 sub pagetemplate (@) {
863 my $page = $params{page};
864 my $template = $params{template};
865 my $shown = ($template->query(name => 'commentslink') ||
866 $template->query(name => 'commentsurl') ||
867 $template->query(name => 'atomcommentsurl') ||
868 $template->query(name => 'comments')) &&
869 commentsshown($page);
871 if ($template->query(name => 'comments')) {
872 my $comments = undef;
874 $comments = IkiWiki::preprocess_inline(
875 pages => "comment($page) and !comment($page/*)",
876 template => 'comment',
880 destpage => $params{destpage},
881 feedfile => 'comments',
886 if (defined $comments && length $comments) {
887 $template->param(comments => $comments);
890 if ($shown && commentsopen($page)) {
891 $template->param(addcommenturl => addcommenturl($page));
896 if ($template->query(name => 'commentsurl')) {
897 $template->param(commentsurl =>
898 urlto($page).'#comments');
901 if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
902 # This will 404 until there are some comments, but I
903 # think that's probably OK...
904 $template->param(atomcommentsurl =>
905 urlto($page).'comments.atom');
908 if ($template->query(name => 'commentslink')) {
909 my $num=num_comments($page, $config{srcdir});
912 $link = htmllink($page, $params{destpage}, $page,
913 linktext => sprintf(ngettext("%i comment", "%i comments", $num), $num),
914 anchor => "comments",
918 elsif (commentsopen($page)) {
919 $link = "<a href=\"".addcommenturl($page)."\">".
920 #translators: Here "Comment" is a verb;
921 #translators: the user clicks on it to
922 #translators: post a comment.
926 $template->param(commentslink => $link)
931 # everything below this point is only relevant to the comments
933 if (!exists $commentstate{$page}) {
937 if ($template->query(name => 'commentid')) {
938 $template->param(commentid => page_to_id($page));
941 if ($template->query(name => 'commentuser')) {
942 $template->param(commentuser =>
943 $commentstate{$page}{commentuser});
946 if ($template->query(name => 'commentopenid')) {
947 $template->param(commentopenid =>
948 $commentstate{$page}{commentopenid});
951 if ($template->query(name => 'commentip')) {
952 $template->param(commentip =>
953 $commentstate{$page}{commentip});
956 if ($template->query(name => 'commentauthor')) {
957 $template->param(commentauthor =>
958 $commentstate{$page}{commentauthor});
961 if ($template->query(name => 'commentauthorurl')) {
962 $template->param(commentauthorurl =>
963 $commentstate{$page}{commentauthorurl});
966 if ($template->query(name => 'commentauthoravatar')) {
967 $template->param(commentauthoravatar =>
968 $commentstate{$page}{commentauthoravatar});
971 if ($template->query(name => 'removeurl') &&
972 IkiWiki::Plugin::remove->can("check_canremove") &&
973 length $config{cgiurl}) {
974 $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
976 $template->param(have_actions => 1);
980 sub addcommenturl ($) {
983 return IkiWiki::cgiurl(do => 'comment', page => $page);
986 sub num_comments ($$) {
990 my @comments=glob("$dir/$page/$config{comments_pagename}*._comment");
991 return int @comments;
994 sub unique_comment_location ($$$$) {
996 eval q{use Digest::MD5 'md5_hex'};
998 my $content_md5=md5_hex(Encode::encode_utf8(shift));
1000 my $ext=shift || "._comment";
1003 my $i = num_comments($page, $dir);
1006 $location = "$page/$config{comments_pagename}${i}_${content_md5}";
1007 } while (-e "$dir/$location$ext");
1012 sub page_to_id ($) {
1013 # Converts a comment page name into a unique, legal html id
1014 # attribute value, that can be used as an anchor to link to the
1018 eval q{use Digest::MD5 'md5_hex'};
1021 return "comment-".md5_hex(Encode::encode_utf8(($page)));
1024 package IkiWiki::PageSpec;
1026 sub match_postcomment ($$;@) {
1030 if (! $postcomment) {
1031 return IkiWiki::FailReason->new("not posting a comment");
1033 return match_glob($page, $glob, @_);
1036 sub match_comment ($$;@) {
1040 if (! $postcomment) {
1041 # To see if it's a comment, check the source file type.
1042 # Deal with comments that were just deleted.
1043 my $source=exists $IkiWiki::pagesources{$page} ?
1044 $IkiWiki::pagesources{$page} :
1045 $IkiWiki::delpagesources{$page};
1046 my $type=defined $source ? IkiWiki::pagetype($source) : undef;
1047 if (! defined $type || $type ne "_comment") {
1048 return IkiWiki::FailReason->new("$page is not a comment");
1052 return match_glob($page, "$glob/*", internal => 1, @_);
1055 sub match_comment_pending ($$;@) {
1059 my $source=exists $IkiWiki::pagesources{$page} ?
1060 $IkiWiki::pagesources{$page} :
1061 $IkiWiki::delpagesources{$page};
1062 my $type=defined $source ? IkiWiki::pagetype($source) : undef;
1063 if (! defined $type || $type ne "_comment_pending") {
1064 return IkiWiki::FailReason->new("$page is not a pending comment");
1067 return match_glob($page, "$glob/*", internal => 1, @_);