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");
38 IkiWiki::loadplugin("transient");
48 comments_pagespec => {
50 example => 'blog/* and !*/Discussion',
51 description => 'PageSpec of pages where comments are allowed',
52 link => 'ikiwiki/PageSpec',
56 comments_closed_pagespec => {
58 example => 'blog/controversial or blog/flamewar',
59 description => 'PageSpec of pages where posting new comments is not allowed',
60 link => 'ikiwiki/PageSpec',
64 comments_pagename => {
66 default => 'comment_',
67 description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
68 safe => 0, # manual page moving required
71 comments_allowdirectives => {
74 description => 'Interpret directives in comments?',
78 comments_allowauthor => {
81 description => 'Allow anonymous commenters to set an author name?',
88 description => 'commit comments to the VCS',
89 # old uncommitted comments are likely to cause
90 # confusion if this is changed
94 comments_allowformats => {
97 example => 'mdwn txt',
98 description => 'Restrict formats for comments to (no restriction if empty)',
106 $config{comments_commit} = 1
107 unless defined $config{comments_commit};
108 if (! $config{comments_commit}) {
109 $config{only_committed_changes}=0;
111 $config{comments_pagespec} = ''
112 unless defined $config{comments_pagespec};
113 $config{comments_closed_pagespec} = ''
114 unless defined $config{comments_closed_pagespec};
115 $config{comments_pagename} = 'comment_'
116 unless defined $config{comments_pagename};
117 $config{comments_allowformats} = ''
118 unless defined $config{comments_allowformats};
123 return $params{content};
126 sub htmlize_pending {
128 return sprintf(gettext("this comment needs %s"),
130 IkiWiki::cgiurl(do => "commentmoderation").'">'.
131 gettext("moderation").'</a>');
134 # FIXME: copied verbatim from meta
137 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
138 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
139 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
148 return ! $config{comments_allowformats} || $config{comments_allowformats} =~ /\b$format\b/;
153 my $page = $params{page};
155 my $format = $params{format};
156 if (defined $format && (! exists $IkiWiki::hooks{htmlize}{$format} ||
157 ! isallowed($format))) {
158 error(sprintf(gettext("unsupported page format %s"), $format));
161 my $content = $params{content};
162 if (! defined $content) {
163 error(gettext("comment must have content"));
165 $content =~ s/\\"/"/g;
167 if (defined wantarray) {
168 if ($config{comments_allowdirectives}) {
169 $content = IkiWiki::preprocess($page, $params{destpage},
173 # no need to bother with htmlize if it's just HTML
174 $content = IkiWiki::htmlize($page, $params{destpage}, $format, $content)
177 IkiWiki::run_hooks(sanitize => sub {
180 destpage => $params{destpage},
186 IkiWiki::preprocess($page, $params{destpage}, $content, 1);
189 # set metadata, possibly overriding [[!meta]] directives from the
195 my $commentauthorurl;
197 if (defined $params{username}) {
198 $commentuser = $params{username};
200 my $oiduser = eval { IkiWiki::openiduser($commentuser) };
202 if (defined $oiduser) {
203 # looks like an OpenID
204 $commentauthorurl = $commentuser;
205 $commentauthor = (defined $params{nickname} && length $params{nickname}) ? $params{nickname} : $oiduser;
206 $commentopenid = $commentuser;
209 if (length $config{cgiurl}) {
210 $commentauthorurl = IkiWiki::cgiurl(
212 page => IkiWiki::userpage($commentuser)
216 $commentauthor = $commentuser;
220 if (defined $params{ip}) {
221 $commentip = $params{ip};
223 $commentauthor = gettext("Anonymous");
226 if ($config{comments_allowauthor}) {
227 if (defined $params{claimedauthor}) {
228 $commentauthor = $params{claimedauthor};
231 if (defined $params{url}) {
232 my $url=$params{url};
234 eval q{use URI::Heuristic};
236 $url=URI::Heuristic::uf_uristr($url);
240 $commentauthorurl = $url;
245 $commentstate{$page}{commentuser} = $commentuser;
246 $commentstate{$page}{commentopenid} = $commentopenid;
247 $commentstate{$page}{commentip} = $commentip;
248 $commentstate{$page}{commentauthor} = $commentauthor;
249 $commentstate{$page}{commentauthorurl} = $commentauthorurl;
250 $commentstate{$page}{commentauthoravatar} = $params{avatar};
251 if (! defined $pagestate{$page}{meta}{author}) {
252 $pagestate{$page}{meta}{author} = $commentauthor;
254 if (! defined $pagestate{$page}{meta}{authorurl}) {
255 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
258 if (defined $params{subject}) {
259 # decode title the same way meta does
260 eval q{use HTML::Entities};
261 $pagestate{$page}{meta}{title} = decode_entities($params{subject});
264 if ($params{page} =~ m/\/\Q$config{comments_pagename}\E\d+_/) {
265 $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page})).
266 "#".page_to_id($params{page});
269 eval q{use Date::Parse};
271 my $time = str2time($params{date});
272 $IkiWiki::pagectime{$page} = $time if defined $time;
278 sub preprocess_moderation {
281 $params{desc}=gettext("Comment Moderation")
282 unless defined $params{desc};
284 if (length $config{cgiurl}) {
286 IkiWiki::cgiurl(do => 'commentmoderation').
287 '">'.$params{desc}.'</a>';
290 return $params{desc};
294 sub sessioncgi ($$) {
298 my $do = $cgi->param('do');
299 if ($do eq 'comment') {
300 editcomment($cgi, $session);
302 elsif ($do eq 'commentmoderation') {
303 commentmoderation($cgi, $session);
305 elsif ($do eq 'commentsignin') {
306 IkiWiki::cgi_signin($cgi, $session);
311 # Mostly cargo-culted from IkiWiki::plugin::editpage
312 sub editcomment ($$) {
316 IkiWiki::decode_cgi_utf8($cgi);
318 eval q{use CGI::FormBuilder};
321 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
322 my $form = CGI::FormBuilder->new(
323 fields => [qw{do sid page subject editcontent type author
324 email url subscribe anonsubscribe}],
327 required => [qw{editcontent}],
330 action => IkiWiki::cgiurl(),
333 template => { template('editcomment.tmpl') },
336 IkiWiki::decode_form_utf8($form);
337 IkiWiki::run_hooks(formbuilder_setup => sub {
338 shift->(title => "comment", form => $form, cgi => $cgi,
339 session => $session, buttons => \@buttons);
341 IkiWiki::decode_form_utf8($form);
343 my $type = $form->param('type');
344 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
345 $type = IkiWiki::possibly_foolish_untaint($type);
348 $type = $config{default_pageext};
353 if (exists $IkiWiki::hooks{htmlize}) {
354 foreach my $key (grep { !/^_/ && isallowed($_) } keys %{$IkiWiki::hooks{htmlize}}) {
355 push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
358 @page_types=sort @page_types;
360 $form->field(name => 'do', type => 'hidden');
361 $form->field(name => 'sid', type => 'hidden', value => $session->id,
363 $form->field(name => 'page', type => 'hidden');
364 $form->field(name => 'subject', type => 'text', size => 72);
365 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
366 $form->field(name => "type", value => $type, force => 1,
367 type => 'select', options => \@page_types);
369 my $username=$session->param('name');
370 $form->tmpl_param(username => $username);
372 $form->field(name => "subscribe", type => 'hidden');
373 $form->field(name => "anonsubscribe", type => 'hidden');
374 if (IkiWiki::Plugin::notifyemail->can("subscribe")) {
375 if (defined $username) {
376 $form->field(name => "subscribe", type => "checkbox",
377 options => [gettext("email replies to me")]);
379 elsif (IkiWiki::Plugin::passwordauth->can("anonuser")) {
380 $form->field(name => "anonsubscribe", type => "checkbox",
381 options => [gettext("email replies to me")]);
385 if ($config{comments_allowauthor} and
386 ! defined $session->param('name')) {
387 $form->tmpl_param(allowauthor => 1);
388 $form->field(name => 'author', type => 'text', size => '40');
389 $form->field(name => 'email', type => 'text', size => '40');
390 $form->field(name => 'url', type => 'text', size => '40');
393 $form->tmpl_param(allowauthor => 0);
394 $form->field(name => 'author', type => 'hidden', value => '',
396 $form->field(name => 'email', type => 'hidden', value => '',
398 $form->field(name => 'url', type => 'hidden', value => '',
402 if (! defined $session->param('name')) {
403 # Make signinurl work and return here.
404 $form->tmpl_param(signinurl => IkiWiki::cgiurl(do => 'commentsignin'));
405 $session->param(postsignin => $ENV{QUERY_STRING});
406 IkiWiki::cgi_savesession($session);
409 # The untaint is OK (as in editpage) because we're about to pass
410 # it to file_pruned and wiki_file_regexp anyway.
411 my ($page) = $form->field('page')=~/$config{wiki_file_regexp}/;
412 $page = IkiWiki::possibly_foolish_untaint($page);
413 if (! defined $page || ! length $page ||
414 IkiWiki::file_pruned($page)) {
415 error(gettext("bad page name"));
418 $form->title(sprintf(gettext("commenting on %s"),
419 IkiWiki::pagetitle(IkiWiki::basename($page))));
421 $form->tmpl_param('helponformattinglink',
422 htmllink($page, $page, 'ikiwiki/formatting',
424 linktext => 'FormattingHelp'),
425 allowdirectives => $config{allow_directives});
427 if ($form->submitted eq CANCEL) {
428 # bounce back to the page they wanted to comment on, and exit.
429 IkiWiki::redirect($cgi, urlto($page));
433 if (not exists $pagesources{$page}) {
434 error(sprintf(gettext(
435 "page '%s' doesn't exist, so you can't comment"),
439 # There's no UI to get here, but someone might construct the URL,
440 # leading to a comment that exists in the repository but isn't
442 if (!pagespec_match($page, $config{comments_pagespec},
443 location => $page)) {
444 error(sprintf(gettext(
445 "comments on page '%s' are not allowed"),
449 if (pagespec_match($page, $config{comments_closed_pagespec},
450 location => $page)) {
451 error(sprintf(gettext(
452 "comments on page '%s' are closed"),
456 # Set a flag to indicate that we're posting a comment,
457 # so that postcomment() can tell it should match.
459 IkiWiki::check_canedit($page, $cgi, $session);
462 my $content = "[[!comment format=$type\n";
464 if (defined $session->param('name')) {
465 my $username = $session->param('name');
466 $username =~ s/"/"/g;
467 $content .= " username=\"$username\"\n";
470 if (defined $session->param('nickname')) {
471 my $nickname = $session->param('nickname');
472 $nickname =~ s/"/"/g;
473 $content .= " nickname=\"$nickname\"\n";
476 if (!(defined $session->param('name') || defined $session->param('nickname')) &&
477 defined $session->remote_addr()) {
478 $content .= " ip=\"".$session->remote_addr()."\"\n";
481 if ($config{comments_allowauthor}) {
482 my $author = $form->field('author');
483 if (defined $author && length $author) {
484 $author =~ s/"/"/g;
485 $content .= " claimedauthor=\"$author\"\n";
487 my $url = $form->field('url');
488 if (defined $url && length $url) {
489 $url =~ s/"/"/g;
490 $content .= " url=\"$url\"\n";
494 my $avatar=getavatar($session->param('name'));
495 if (defined $avatar && length $avatar) {
496 $avatar =~ s/"/"/g;
497 $content .= " avatar=\"$avatar\"\n";
500 my $subject = $form->field('subject');
501 if (defined $subject && length $subject) {
502 $subject =~ s/"/"/g;
505 $subject = "comment ".(num_comments($page, $config{srcdir}) + 1);
507 $content .= " subject=\"$subject\"\n";
508 $content .= " date=\"" . commentdate() . "\"\n";
510 my $editcontent = $form->field('editcontent');
511 $editcontent="" if ! defined $editcontent;
512 $editcontent =~ s/\r\n/\n/g;
513 $editcontent =~ s/\r/\n/g;
514 $editcontent =~ s/"/\\"/g;
515 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
517 my $location=unique_comment_location($page, $content, $config{srcdir});
519 # This is essentially a simplified version of editpage:
520 # - the user does not control the page that's created, only the parent
521 # - it's always a create operation, never an edit
522 # - this means that conflicts should never happen
523 # - this means that if they do, rocks fall and everyone dies
525 if ($form->submitted eq PREVIEW) {
526 my $preview=previewcomment($content, $location, $page, time);
527 IkiWiki::run_hooks(format => sub {
528 $preview = shift->(page => $page,
529 content => $preview);
531 $form->tmpl_param(page_preview => $preview);
534 $form->tmpl_param(page_preview => "");
537 if ($form->submitted eq POST_COMMENT && $form->validate) {
538 IkiWiki::checksessionexpiry($cgi, $session);
540 if (IkiWiki::Plugin::notifyemail->can("subscribe")) {
541 my $subspec="comment($page)";
542 if (defined $username &&
543 length $form->field("subscribe")) {
544 IkiWiki::Plugin::notifyemail::subscribe(
545 $username, $subspec);
547 elsif (length $form->field("email") &&
548 length $form->field("anonsubscribe")) {
549 IkiWiki::Plugin::notifyemail::anonsubscribe(
550 $form->field("email"), $subspec);
555 my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
556 subject => $form->field('subject'),
557 $config{comments_allowauthor} ? (
558 author => $form->field('author'),
559 url => $form->field('url'),
569 $location=unique_comment_location($page, $content, $IkiWiki::Plugin::transient::transientdir, "._comment_pending");
570 writefile("$location._comment_pending", $IkiWiki::Plugin::transient::transientdir, $content);
572 # Refresh so anything that deals with pending
573 # comments can be updated.
574 require IkiWiki::Render;
576 IkiWiki::saveindex();
578 IkiWiki::printheader($session);
579 print IkiWiki::cgitemplate($cgi, gettext(gettext("comment stored for moderation")),
581 gettext("Your comment will be posted after moderator review").
586 # FIXME: could probably do some sort of graceful retry
587 # on error? Would require significant unwinding though
588 my $file = "$location._comment";
589 writefile($file, $config{srcdir}, $content);
593 if ($config{rcs} and $config{comments_commit}) {
594 my $message = gettext("Added a comment");
595 if (defined $form->field('subject') &&
596 length $form->field('subject')) {
598 gettext("Added a comment: %s"),
599 $form->field('subject'));
602 IkiWiki::rcs_add($file);
603 IkiWiki::disable_commit_hook();
604 $conflict = IkiWiki::rcs_commit_staged(
608 IkiWiki::enable_commit_hook();
609 IkiWiki::rcs_update();
612 # Now we need a refresh
613 require IkiWiki::Render;
615 IkiWiki::saveindex();
617 # this should never happen, unless a committer deliberately
618 # breaks it or something
619 error($conflict) if defined $conflict;
621 # Jump to the new comment on the page.
622 # The trailing question mark tries to avoid broken
623 # caches and get the most recent version of the page.
624 IkiWiki::redirect($cgi, urlto($page).
625 "?updated#".page_to_id($location));
629 IkiWiki::showform($form, \@buttons, $session, $cgi,
637 strftime_utf8('%Y-%m-%dT%H:%M:%SZ', gmtime);
642 return undef unless defined $user;
645 eval q{use Libravatar::URL};
647 my $oiduser = eval { IkiWiki::openiduser($user) };
648 my $https=defined $config{url} && $config{url}=~/^https:/;
650 if (defined $oiduser) {
652 $avatar = libravatar_url(openid => $user, https => $https);
655 if (! defined $avatar &&
656 (my $email = IkiWiki::userinfo_get($user, 'email'))) {
658 $avatar = libravatar_url(email => $email, https => $https);
666 sub commentmoderation ($$) {
670 IkiWiki::needsignin($cgi, $session);
671 if (! IkiWiki::is_admin($session->param("name"))) {
672 error(gettext("you are not logged in as an admin"));
675 IkiWiki::decode_cgi_utf8($cgi);
677 if (defined $cgi->param('sid')) {
678 IkiWiki::checksessionexpiry($cgi, $session);
680 my $rejectalldefer=$cgi->param('rejectalldefer');
684 foreach my $id (keys %vars) {
685 if ($id =~ /(.*)\._comment(?:_pending)?$/) {
686 $id=decode_utf8($id);
687 my $action=$cgi->param($id);
688 next if $action eq 'Defer' && ! $rejectalldefer;
690 # Make sure that the id is of a legal
692 my ($f) = $id =~ /$config{wiki_file_regexp}/;
693 if (! defined $f || ! length $f ||
694 IkiWiki::file_pruned($f)) {
695 error("illegal file");
698 my $page=IkiWiki::dirname($f);
699 my $filedir=$IkiWiki::Plugin::transient::transientdir;
700 my $file="$filedir/$f";
703 $file="$config{srcdir}/$f";
704 $filedir=$config{srcdir};
707 $file="$config{wikistatedir}/comments_pending/".$f;
708 $filedir="$config{wikistatedir}/comments_pending";
712 if ($action eq 'Accept') {
713 my $content=eval { readfile($file) };
714 next if $@; # file vanished since form was displayed
715 my $dest=unique_comment_location($page, $content, $config{srcdir})."._comment";
716 writefile($dest, $config{srcdir}, $content);
717 if ($config{rcs} and $config{comments_commit}) {
718 IkiWiki::rcs_add($dest);
723 require IkiWiki::Render;
724 IkiWiki::prune($file, $filedir);
730 if ($config{rcs} and $config{comments_commit}) {
731 my $message = gettext("Comment moderation");
732 IkiWiki::disable_commit_hook();
733 $conflict=IkiWiki::rcs_commit_staged(
737 IkiWiki::enable_commit_hook();
738 IkiWiki::rcs_update();
741 # Now we need a refresh
742 require IkiWiki::Render;
744 IkiWiki::saveindex();
746 error($conflict) if defined $conflict;
751 my ($id, $dir, $ctime)=@{$_};
752 my $content=readfile("$dir/$id");
753 my $preview=previewcomment($content, $id,
759 } sort { $b->[2] <=> $a->[2] } comments_pending();
761 my $template=template("commentmoderation.tmpl");
764 comments => \@comments,
765 cgiurl => IkiWiki::cgiurl(),
767 IkiWiki::printheader($session);
768 my $out=$template->output;
769 IkiWiki::run_hooks(format => sub {
770 $out = shift->(page => "", content => $out);
772 print IkiWiki::cgitemplate($cgi, gettext("comment moderation"), $out);
776 sub formbuilder_setup (@) {
779 my $form=$params{form};
780 if ($form->title eq "preferences" &&
781 IkiWiki::is_admin($params{session}->param("name"))) {
782 push @{$params{buttons}}, "Comment Moderation";
783 if ($form->submitted && $form->submitted eq "Comment Moderation") {
784 commentmoderation($params{cgi}, $params{session});
789 sub comments_pending () {
792 eval q{use File::Find};
796 my $origdir=getcwd();
798 my $find_comments=sub {
801 return unless -d $dir;
803 chdir($dir) || die "chdir $dir: $!";
808 my $file=decode_utf8($_);
810 return if ! length $file || IkiWiki::file_pruned($file)
811 || -l $_ || -d _ || $file !~ /\Q$extension\E$/;
812 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
814 my $ctime=(stat($_))[10];
815 push @ret, [$f, $dir, $ctime];
820 chdir($origdir) || die "chdir $origdir: $!";
823 $find_comments->($IkiWiki::Plugin::transient::transientdir, "._comment_pending");
825 $find_comments->($config{srcdir}, "._comment_pending");
827 $find_comments->("$config{wikistatedir}/comments_pending/",
833 sub previewcomment ($$$) {
839 # Previewing a comment should implicitly enable comment posting mode.
840 my $oldpostcomment=$postcomment;
843 my $preview = IkiWiki::htmlize($location, $page, '_comment',
844 IkiWiki::linkify($location, $page,
845 IkiWiki::preprocess($location, $page,
846 IkiWiki::filter($location, $page, $content), 0, 1)));
848 my $template = template("comment.tmpl");
849 $template->param(content => $preview);
850 $template->param(ctime => displaytime($time, undef, 1));
851 $template->param(html5 => $config{html5});
853 IkiWiki::run_hooks(pagetemplate => sub {
854 shift->(page => $location,
856 template => $template);
859 $template->param(have_actions => 0);
861 $postcomment=$oldpostcomment;
863 return $template->output;
866 sub commentsshown ($) {
869 return pagespec_match($page, $config{comments_pagespec},
873 sub commentsopen ($) {
876 return length $config{cgiurl} > 0 &&
877 (! length $config{comments_closed_pagespec} ||
878 ! pagespec_match($page, $config{comments_closed_pagespec},
882 sub pagetemplate (@) {
885 my $page = $params{page};
886 my $template = $params{template};
887 my $shown = ($template->query(name => 'commentslink') ||
888 $template->query(name => 'commentsurl') ||
889 $template->query(name => 'atomcommentsurl') ||
890 $template->query(name => 'comments')) &&
891 commentsshown($page);
893 if ($template->query(name => 'comments')) {
894 my $comments = undef;
896 $comments = IkiWiki::preprocess_inline(
897 pages => "comment($page) and !comment($page/*)",
898 template => 'comment',
902 destpage => $params{destpage},
903 feedfile => 'comments',
908 if (defined $comments && length $comments) {
909 $template->param(comments => $comments);
912 if ($shown && commentsopen($page)) {
913 $template->param(addcommenturl => addcommenturl($page));
918 if ($template->query(name => 'commentsurl')) {
919 $template->param(commentsurl =>
920 urlto($page).'#comments');
923 if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
924 # This will 404 until there are some comments, but I
925 # think that's probably OK...
926 $template->param(atomcommentsurl =>
927 urlto($page).'comments.atom');
930 if ($template->query(name => 'commentslink')) {
931 my $num=num_comments($page, $config{srcdir});
934 $link = htmllink($page, $params{destpage}, $page,
935 linktext => sprintf(ngettext("%i comment", "%i comments", $num), $num),
936 anchor => "comments",
940 elsif (commentsopen($page)) {
941 $link = "<a href=\"".addcommenturl($page)."\">".
942 #translators: Here "Comment" is a verb;
943 #translators: the user clicks on it to
944 #translators: post a comment.
948 $template->param(commentslink => $link)
953 # everything below this point is only relevant to the comments
955 if (!exists $commentstate{$page}) {
959 if ($template->query(name => 'commentid')) {
960 $template->param(commentid => page_to_id($page));
963 if ($template->query(name => 'commentuser')) {
964 $template->param(commentuser =>
965 $commentstate{$page}{commentuser});
968 if ($template->query(name => 'commentopenid')) {
969 $template->param(commentopenid =>
970 $commentstate{$page}{commentopenid});
973 if ($template->query(name => 'commentip')) {
974 $template->param(commentip =>
975 $commentstate{$page}{commentip});
978 if ($template->query(name => 'commentauthor')) {
979 $template->param(commentauthor =>
980 $commentstate{$page}{commentauthor});
983 if ($template->query(name => 'commentauthorurl')) {
984 $template->param(commentauthorurl =>
985 $commentstate{$page}{commentauthorurl});
988 if ($template->query(name => 'commentauthoravatar')) {
989 $template->param(commentauthoravatar =>
990 $commentstate{$page}{commentauthoravatar});
993 if ($template->query(name => 'removeurl') &&
994 IkiWiki::Plugin::remove->can("check_canremove") &&
995 length $config{cgiurl}) {
996 $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
998 $template->param(have_actions => 1);
1002 sub addcommenturl ($) {
1005 return IkiWiki::cgiurl(do => 'comment', page => $page);
1008 sub num_comments ($$) {
1012 my @comments=glob("$dir/$page/$config{comments_pagename}*._comment");
1013 return int @comments;
1016 sub unique_comment_location ($$$;$) {
1018 eval q{use Digest::MD5 'md5_hex'};
1020 my $content_md5=md5_hex(Encode::encode_utf8(shift));
1022 my $ext=shift || "._comment";
1025 my $i = num_comments($page, $dir);
1028 $location = "$page/$config{comments_pagename}${i}_${content_md5}";
1029 } while (-e "$dir/$location$ext");
1034 sub page_to_id ($) {
1035 # Converts a comment page name into a unique, legal html id
1036 # attribute value, that can be used as an anchor to link to the
1040 eval q{use Digest::MD5 'md5_hex'};
1043 return "comment-".md5_hex(Encode::encode_utf8(($page)));
1046 package IkiWiki::PageSpec;
1048 sub match_postcomment ($$;@) {
1052 if (! $postcomment) {
1053 return IkiWiki::FailReason->new("not posting a comment");
1055 return match_glob($page, $glob, @_);
1058 sub match_comment ($$;@) {
1062 if (! $postcomment) {
1063 # To see if it's a comment, check the source file type.
1064 # Deal with comments that were just deleted.
1065 my $source=exists $IkiWiki::pagesources{$page} ?
1066 $IkiWiki::pagesources{$page} :
1067 $IkiWiki::delpagesources{$page};
1068 my $type=defined $source ? IkiWiki::pagetype($source) : undef;
1069 if (! defined $type || $type ne "_comment") {
1070 return IkiWiki::FailReason->new("$page is not a comment");
1074 return match_glob($page, "$glob/*", internal => 1, @_);
1077 sub match_comment_pending ($$;@) {
1081 my $source=exists $IkiWiki::pagesources{$page} ?
1082 $IkiWiki::pagesources{$page} :
1083 $IkiWiki::delpagesources{$page};
1084 my $type=defined $source ? IkiWiki::pagetype($source) : undef;
1085 if (! defined $type || $type ne "_comment_pending") {
1086 return IkiWiki::FailReason->new("$page is not a pending comment");
1089 return match_glob($page, "$glob/*", internal => 1, @_);