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
96 $config{comments_commit} = 1
97 unless defined $config{comments_commit};
98 $config{comments_pagespec} = ''
99 unless defined $config{comments_pagespec};
100 $config{comments_closed_pagespec} = ''
101 unless defined $config{comments_closed_pagespec};
102 $config{comments_pagename} = 'comment_'
103 unless defined $config{comments_pagename};
108 return $params{content};
111 sub htmlize_pending {
113 return sprintf(gettext("this comment needs %s"),
115 IkiWiki::cgiurl(do => "commentmoderation").'">'.
116 gettext("moderation").'</a>');
119 # FIXME: copied verbatim from meta
122 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
123 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
124 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
133 my $page = $params{page};
135 my $format = $params{format};
136 if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
137 error(sprintf(gettext("unsupported page format %s"), $format));
140 my $content = $params{content};
141 if (! defined $content) {
142 error(gettext("comment must have content"));
144 $content =~ s/\\"/"/g;
146 if (defined wantarray) {
147 if ($config{comments_allowdirectives}) {
148 $content = IkiWiki::preprocess($page, $params{destpage},
152 # no need to bother with htmlize if it's just HTML
153 $content = IkiWiki::htmlize($page, $params{destpage}, $format, $content)
156 IkiWiki::run_hooks(sanitize => sub {
159 destpage => $params{destpage},
165 IkiWiki::preprocess($page, $params{destpage}, $content, 1);
168 # set metadata, possibly overriding [[!meta]] directives from the
174 my $commentauthorurl;
176 if (defined $params{username}) {
177 $commentuser = $params{username};
179 my $oiduser = eval { IkiWiki::openiduser($commentuser) };
181 if (defined $oiduser) {
182 # looks like an OpenID
183 $commentauthorurl = $commentuser;
184 $commentauthor = (defined $params{nickname} && length $params{nickname}) ? $params{nickname} : $oiduser;
185 $commentopenid = $commentuser;
188 $commentauthorurl = IkiWiki::cgiurl(
190 page => IkiWiki::userpage($commentuser)
193 $commentauthor = $commentuser;
197 if (defined $params{ip}) {
198 $commentip = $params{ip};
200 $commentauthor = gettext("Anonymous");
203 $commentstate{$page}{commentuser} = $commentuser;
204 $commentstate{$page}{commentopenid} = $commentopenid;
205 $commentstate{$page}{commentip} = $commentip;
206 $commentstate{$page}{commentauthor} = $commentauthor;
207 $commentstate{$page}{commentauthorurl} = $commentauthorurl;
208 $commentstate{$page}{commentauthoravatar} = $params{avatar};
209 if (! defined $pagestate{$page}{meta}{author}) {
210 $pagestate{$page}{meta}{author} = $commentauthor;
212 if (! defined $pagestate{$page}{meta}{authorurl}) {
213 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
216 if ($config{comments_allowauthor}) {
217 if (defined $params{claimedauthor}) {
218 $pagestate{$page}{meta}{author} = $params{claimedauthor};
221 if (defined $params{url}) {
222 my $url=$params{url};
224 eval q{use URI::Heuristic};
226 $url=URI::Heuristic::uf_uristr($url);
230 $pagestate{$page}{meta}{authorurl} = $url;
235 $pagestate{$page}{meta}{author} = $commentauthor;
236 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
239 if (defined $params{subject}) {
240 # decode title the same way meta does
241 eval q{use HTML::Entities};
242 $pagestate{$page}{meta}{title} = decode_entities($params{subject});
245 if ($params{page} =~ m/\/\Q$config{comments_pagename}\E\d+_/) {
246 $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page})).
247 "#".page_to_id($params{page});
250 eval q{use Date::Parse};
252 my $time = str2time($params{date});
253 $IkiWiki::pagectime{$page} = $time if defined $time;
259 sub preprocess_moderation {
262 $params{desc}=gettext("Comment Moderation")
263 unless defined $params{desc};
265 if (length $config{cgiurl}) {
267 IkiWiki::cgiurl(do => 'commentmoderation').
268 '">'.$params{desc}.'</a>';
271 return $params{desc};
275 sub sessioncgi ($$) {
279 my $do = $cgi->param('do');
280 if ($do eq 'comment') {
281 editcomment($cgi, $session);
283 elsif ($do eq 'commentmoderation') {
284 commentmoderation($cgi, $session);
286 elsif ($do eq 'commentsignin') {
287 IkiWiki::cgi_signin($cgi, $session);
292 # Mostly cargo-culted from IkiWiki::plugin::editpage
293 sub editcomment ($$) {
297 IkiWiki::decode_cgi_utf8($cgi);
299 eval q{use CGI::FormBuilder};
302 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
303 my $form = CGI::FormBuilder->new(
304 fields => [qw{do sid page subject editcontent type author
305 email url subscribe anonsubscribe}],
308 required => [qw{editcontent}],
311 action => IkiWiki::cgiurl(),
314 template => { template('editcomment.tmpl') },
317 IkiWiki::decode_form_utf8($form);
318 IkiWiki::run_hooks(formbuilder_setup => sub {
319 shift->(title => "comment", form => $form, cgi => $cgi,
320 session => $session, buttons => \@buttons);
322 IkiWiki::decode_form_utf8($form);
324 my $type = $form->param('type');
325 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
326 $type = IkiWiki::possibly_foolish_untaint($type);
329 $type = $config{default_pageext};
334 if (exists $IkiWiki::hooks{htmlize}) {
335 foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
336 push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
339 @page_types=sort @page_types;
341 $form->field(name => 'do', type => 'hidden');
342 $form->field(name => 'sid', type => 'hidden', value => $session->id,
344 $form->field(name => 'page', type => 'hidden');
345 $form->field(name => 'subject', type => 'text', size => 72);
346 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
347 $form->field(name => "type", value => $type, force => 1,
348 type => 'select', options => \@page_types);
350 my $username=$session->param('name');
351 $form->tmpl_param(username => $username);
353 $form->field(name => "subscribe", type => 'hidden');
354 $form->field(name => "anonsubscribe", type => 'hidden');
355 if (IkiWiki::Plugin::notifyemail->can("subscribe")) {
356 if (defined $username) {
357 $form->field(name => "subscribe", type => "checkbox",
358 options => [gettext("email replies to me")]);
360 elsif (IkiWiki::Plugin::passwordauth->can("anonuser")) {
361 $form->field(name => "anonsubscribe", type => "checkbox",
362 options => [gettext("email replies to me")]);
366 if ($config{comments_allowauthor} and
367 ! defined $session->param('name')) {
368 $form->tmpl_param(allowauthor => 1);
369 $form->field(name => 'author', type => 'text', size => '40');
370 $form->field(name => 'email', type => 'text', size => '40');
371 $form->field(name => 'url', type => 'text', size => '40');
374 $form->tmpl_param(allowauthor => 0);
375 $form->field(name => 'author', type => 'hidden', value => '',
377 $form->field(name => 'email', type => 'hidden', value => '',
379 $form->field(name => 'url', type => 'hidden', value => '',
383 if (! defined $session->param('name')) {
384 # Make signinurl work and return here.
385 $form->tmpl_param(signinurl => IkiWiki::cgiurl(do => 'commentsignin'));
386 $session->param(postsignin => $ENV{QUERY_STRING});
387 IkiWiki::cgi_savesession($session);
390 # The untaint is OK (as in editpage) because we're about to pass
391 # it to file_pruned and wiki_file_regexp anyway.
392 my ($page) = $form->field('page')=~/$config{wiki_file_regexp}/;
393 $page = IkiWiki::possibly_foolish_untaint($page);
394 if (! defined $page || ! length $page ||
395 IkiWiki::file_pruned($page)) {
396 error(gettext("bad page name"));
399 $form->title(sprintf(gettext("commenting on %s"),
400 IkiWiki::pagetitle(IkiWiki::basename($page))));
402 $form->tmpl_param('helponformattinglink',
403 htmllink($page, $page, 'ikiwiki/formatting',
405 linktext => 'FormattingHelp'),
406 allowdirectives => $config{allow_directives});
408 if ($form->submitted eq CANCEL) {
409 # bounce back to the page they wanted to comment on, and exit.
410 IkiWiki::redirect($cgi, urlto($page));
414 if (not exists $pagesources{$page}) {
415 error(sprintf(gettext(
416 "page '%s' doesn't exist, so you can't comment"),
420 if (pagespec_match($page, $config{comments_closed_pagespec},
421 location => $page)) {
422 error(sprintf(gettext(
423 "comments on page '%s' are closed"),
427 # Set a flag to indicate that we're posting a comment,
428 # so that postcomment() can tell it should match.
430 IkiWiki::check_canedit($page, $cgi, $session);
433 my $content = "[[!comment format=$type\n";
435 if (defined $session->param('name')) {
436 my $username = $session->param('name');
437 $username =~ s/"/"/g;
438 $content .= " username=\"$username\"\n";
440 if (defined $session->param('nickname')) {
441 my $nickname = $session->param('nickname');
442 $nickname =~ s/"/"/g;
443 $content .= " nickname=\"$nickname\"\n";
445 elsif (defined $session->remote_addr()) {
446 my $ip = $session->remote_addr();
447 if ($ip =~ m/^([.0-9]+)$/) {
448 $content .= " ip=\"$1\"\n";
452 if ($config{comments_allowauthor}) {
453 my $author = $form->field('author');
454 if (defined $author && length $author) {
455 $author =~ s/"/"/g;
456 $content .= " claimedauthor=\"$author\"\n";
458 my $url = $form->field('url');
459 if (defined $url && length $url) {
460 $url =~ s/"/"/g;
461 $content .= " url=\"$url\"\n";
465 my $avatar=getavatar($session->param('name'));
466 if (defined $avatar && length $avatar) {
467 $avatar =~ s/"/"/g;
468 $content .= " avatar=\"$avatar\"\n";
471 my $subject = $form->field('subject');
472 if (defined $subject && length $subject) {
473 $subject =~ s/"/"/g;
476 $subject = "comment ".(num_comments($page, $config{srcdir}) + 1);
478 $content .= " subject=\"$subject\"\n";
480 $content .= " date=\"" . strftime_utf8('%Y-%m-%dT%H:%M:%SZ', gmtime) . "\"\n";
482 my $editcontent = $form->field('editcontent');
483 $editcontent="" if ! defined $editcontent;
484 $editcontent =~ s/\r\n/\n/g;
485 $editcontent =~ s/\r/\n/g;
486 $editcontent =~ s/"/\\"/g;
487 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
489 my $location=unique_comment_location($page, $content, $config{srcdir});
491 # This is essentially a simplified version of editpage:
492 # - the user does not control the page that's created, only the parent
493 # - it's always a create operation, never an edit
494 # - this means that conflicts should never happen
495 # - this means that if they do, rocks fall and everyone dies
497 if ($form->submitted eq PREVIEW) {
498 my $preview=previewcomment($content, $location, $page, time);
499 IkiWiki::run_hooks(format => sub {
500 $preview = shift->(page => $page,
501 content => $preview);
503 $form->tmpl_param(page_preview => $preview);
506 $form->tmpl_param(page_preview => "");
509 if ($form->submitted eq POST_COMMENT && $form->validate) {
510 IkiWiki::checksessionexpiry($cgi, $session);
512 if (IkiWiki::Plugin::notifyemail->can("subscribe")) {
513 my $subspec="comment($page)";
514 if (defined $username &&
515 length $form->field("subscribe")) {
516 IkiWiki::Plugin::notifyemail::subscribe(
517 $username, $subspec);
519 elsif (length $form->field("email") &&
520 length $form->field("anonsubscribe")) {
521 IkiWiki::Plugin::notifyemail::anonsubscribe(
522 $form->field("email"), $subspec);
527 my $ok=IkiWiki::check_content(
528 content => scalar $form->field('editcontent'),
529 subject => scalar $form->field('subject'),
530 $config{comments_allowauthor} ? (
531 author => scalar $form->field('author'),
532 url => scalar $form->field('url'),
542 $location=unique_comment_location($page, $content, $config{srcdir}, "._comment_pending");
543 writefile("$location._comment_pending", $config{srcdir}, $content);
545 # Refresh so anything that deals with pending
546 # comments can be updated.
547 require IkiWiki::Render;
549 IkiWiki::saveindex();
551 IkiWiki::printheader($session);
552 print IkiWiki::cgitemplate($cgi, gettext(gettext("comment stored for moderation")),
554 gettext("Your comment will be posted after moderator review").
559 # FIXME: could probably do some sort of graceful retry
560 # on error? Would require significant unwinding though
561 my $file = "$location._comment";
562 writefile($file, $config{srcdir}, $content);
566 if ($config{rcs} and $config{comments_commit}) {
567 my $message = gettext("Added a comment");
568 if (defined $form->field('subject') &&
569 length $form->field('subject')) {
571 gettext("Added a comment: %s"),
572 scalar $form->field('subject'));
575 IkiWiki::rcs_add($file);
576 IkiWiki::disable_commit_hook();
577 $conflict = IkiWiki::rcs_commit_staged(
581 IkiWiki::enable_commit_hook();
582 IkiWiki::rcs_update();
585 # Now we need a refresh
586 require IkiWiki::Render;
588 IkiWiki::saveindex();
590 # this should never happen, unless a committer deliberately
591 # breaks it or something
592 error($conflict) if defined $conflict;
594 # Jump to the new comment on the page.
595 # The trailing question mark tries to avoid broken
596 # caches and get the most recent version of the page.
597 IkiWiki::redirect($cgi, urlto($page).
598 "?updated#".page_to_id($location));
602 IkiWiki::showform($form, \@buttons, $session, $cgi,
611 return undef unless defined $user;
614 eval q{use Libravatar::URL};
616 my $oiduser = eval { IkiWiki::openiduser($user) };
617 my $https=defined $config{url} && $config{url}=~/^https:/;
619 if (defined $oiduser) {
621 $avatar = libravatar_url(openid => $user, https => $https);
624 if (! defined $avatar &&
625 (my $email = IkiWiki::userinfo_get($user, 'email'))) {
627 $avatar = libravatar_url(email => $email, https => $https);
635 sub commentmoderation ($$) {
639 IkiWiki::needsignin($cgi, $session);
640 if (! IkiWiki::is_admin($session->param("name"))) {
641 error(gettext("you are not logged in as an admin"));
644 IkiWiki::decode_cgi_utf8($cgi);
646 if (defined $cgi->param('sid')) {
647 IkiWiki::checksessionexpiry($cgi, $session);
649 my $rejectalldefer=$cgi->param('rejectalldefer');
653 foreach my $id (keys %vars) {
654 if ($id =~ /(.*)\._comment(?:_pending)?$/) {
655 $id=decode_utf8($id);
656 my $action=$cgi->param($id);
657 next if $action eq 'Defer' && ! $rejectalldefer;
659 # Make sure that the id is of a legal
661 my ($f) = $id =~ /$config{wiki_file_regexp}/;
662 if (! defined $f || ! length $f ||
663 IkiWiki::file_pruned($f)) {
664 error("illegal file");
667 my $page=IkiWiki::dirname($f);
668 my $file="$config{srcdir}/$f";
669 my $filedir=$config{srcdir};
672 $file="$config{wikistatedir}/comments_pending/".$f;
673 $filedir="$config{wikistatedir}/comments_pending";
676 if ($action eq 'Accept') {
677 my $content=eval { readfile($file) };
678 next if $@; # file vanished since form was displayed
679 my $dest=unique_comment_location($page, $content, $config{srcdir})."._comment";
680 writefile($dest, $config{srcdir}, $content);
681 if ($config{rcs} and $config{comments_commit}) {
682 IkiWiki::rcs_add($dest);
687 require IkiWiki::Render;
688 IkiWiki::prune($file, $filedir);
694 if ($config{rcs} and $config{comments_commit}) {
695 my $message = gettext("Comment moderation");
696 IkiWiki::disable_commit_hook();
697 $conflict=IkiWiki::rcs_commit_staged(
701 IkiWiki::enable_commit_hook();
702 IkiWiki::rcs_update();
705 # Now we need a refresh
706 require IkiWiki::Render;
708 IkiWiki::saveindex();
710 error($conflict) if defined $conflict;
715 my ($id, $dir, $ctime)=@{$_};
716 my $content=readfile("$dir/$id");
717 my $preview=previewcomment($content, $id,
723 } sort { $b->[2] <=> $a->[2] } comments_pending();
725 my $template=template("commentmoderation.tmpl");
728 comments => \@comments,
729 cgiurl => IkiWiki::cgiurl(),
731 IkiWiki::printheader($session);
732 my $out=$template->output;
733 IkiWiki::run_hooks(format => sub {
734 $out = shift->(page => "", content => $out);
736 print IkiWiki::cgitemplate($cgi, gettext("comment moderation"), $out);
740 sub formbuilder_setup (@) {
743 my $form=$params{form};
744 if ($form->title eq "preferences" &&
745 IkiWiki::is_admin($params{session}->param("name"))) {
746 push @{$params{buttons}}, "Comment Moderation";
747 if ($form->submitted && $form->submitted eq "Comment Moderation") {
748 commentmoderation($params{cgi}, $params{session});
753 sub comments_pending () {
756 eval q{use File::Find};
760 my $origdir=getcwd();
762 my $find_comments=sub {
765 return unless -d $dir;
767 chdir($dir) || die "chdir $dir: $!";
772 my $file=decode_utf8($_);
774 return if ! length $file || IkiWiki::file_pruned($file)
775 || -l $_ || -d _ || $file !~ /\Q$extension\E$/;
776 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
778 my $ctime=(stat($_))[10];
779 push @ret, [$f, $dir, $ctime];
784 chdir($origdir) || die "chdir $origdir: $!";
787 $find_comments->($config{srcdir}, "._comment_pending");
789 $find_comments->("$config{wikistatedir}/comments_pending/",
795 sub previewcomment ($$$) {
801 # Previewing a comment should implicitly enable comment posting mode.
802 my $oldpostcomment=$postcomment;
805 my $preview = IkiWiki::htmlize($location, $page, '_comment',
806 IkiWiki::linkify($location, $page,
807 IkiWiki::preprocess($location, $page,
808 IkiWiki::filter($location, $page, $content), 0, 1)));
810 my $template = template("comment.tmpl");
811 $template->param(content => $preview);
812 $template->param(ctime => displaytime($time, undef, 1));
813 $template->param(html5 => $config{html5});
815 IkiWiki::run_hooks(pagetemplate => sub {
816 shift->(page => $location,
818 template => $template);
821 $template->param(have_actions => 0);
823 $postcomment=$oldpostcomment;
825 return $template->output;
828 sub commentsshown ($) {
831 return pagespec_match($page, $config{comments_pagespec},
835 sub commentsopen ($) {
838 return length $config{cgiurl} > 0 &&
839 (! length $config{comments_closed_pagespec} ||
840 ! pagespec_match($page, $config{comments_closed_pagespec},
844 sub pagetemplate (@) {
847 my $page = $params{page};
848 my $template = $params{template};
849 my $shown = ($template->query(name => 'commentslink') ||
850 $template->query(name => 'commentsurl') ||
851 $template->query(name => 'atomcommentsurl') ||
852 $template->query(name => 'comments')) &&
853 commentsshown($page);
855 if ($template->query(name => 'comments')) {
856 my $comments = undef;
858 $comments = IkiWiki::preprocess_inline(
859 pages => "comment($page) and !comment($page/*)",
860 template => 'comment',
864 destpage => $params{destpage},
865 feedfile => 'comments',
870 if (defined $comments && length $comments) {
871 $template->param(comments => $comments);
874 if ($shown && commentsopen($page)) {
875 $template->param(addcommenturl => addcommenturl($page));
880 if ($template->query(name => 'commentsurl')) {
881 $template->param(commentsurl =>
882 urlto($page).'#comments');
885 if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
886 # This will 404 until there are some comments, but I
887 # think that's probably OK...
888 $template->param(atomcommentsurl =>
889 urlto($page).'comments.atom');
892 if ($template->query(name => 'commentslink')) {
893 my $num=num_comments($page, $config{srcdir});
896 $link = htmllink($page, $params{destpage}, $page,
897 linktext => sprintf(ngettext("%i comment", "%i comments", $num), $num),
898 anchor => "comments",
902 elsif (commentsopen($page)) {
903 $link = "<a href=\"".addcommenturl($page)."\">".
904 #translators: Here "Comment" is a verb;
905 #translators: the user clicks on it to
906 #translators: post a comment.
910 $template->param(commentslink => $link)
915 # everything below this point is only relevant to the comments
917 if (!exists $commentstate{$page}) {
921 if ($template->query(name => 'commentid')) {
922 $template->param(commentid => page_to_id($page));
925 if ($template->query(name => 'commentuser')) {
926 $template->param(commentuser =>
927 $commentstate{$page}{commentuser});
930 if ($template->query(name => 'commentopenid')) {
931 $template->param(commentopenid =>
932 $commentstate{$page}{commentopenid});
935 if ($template->query(name => 'commentip')) {
936 $template->param(commentip =>
937 $commentstate{$page}{commentip});
940 if ($template->query(name => 'commentauthor')) {
941 $template->param(commentauthor =>
942 $commentstate{$page}{commentauthor});
945 if ($template->query(name => 'commentauthorurl')) {
946 $template->param(commentauthorurl =>
947 $commentstate{$page}{commentauthorurl});
950 if ($template->query(name => 'commentauthoravatar')) {
951 $template->param(commentauthoravatar =>
952 $commentstate{$page}{commentauthoravatar});
955 if ($template->query(name => 'removeurl') &&
956 IkiWiki::Plugin::remove->can("check_canremove") &&
957 length $config{cgiurl}) {
958 $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
960 $template->param(have_actions => 1);
964 sub addcommenturl ($) {
967 return IkiWiki::cgiurl(do => 'comment', page => $page);
970 sub num_comments ($$) {
974 my @comments=glob("$dir/$page/$config{comments_pagename}*._comment");
975 return int @comments;
978 sub unique_comment_location ($$$$) {
980 eval q{use Digest::MD5 'md5_hex'};
982 my $content_md5=md5_hex(Encode::encode_utf8(shift));
984 my $ext=shift || "._comment";
987 my $i = num_comments($page, $dir);
990 $location = "$page/$config{comments_pagename}${i}_${content_md5}";
991 } while (-e "$dir/$location$ext");
997 # Converts a comment page name into a unique, legal html id
998 # attribute value, that can be used as an anchor to link to the
1002 eval q{use Digest::MD5 'md5_hex'};
1005 return "comment-".md5_hex(Encode::encode_utf8(($page)));
1008 package IkiWiki::PageSpec;
1010 sub match_postcomment ($$;@) {
1014 if (! $postcomment) {
1015 return IkiWiki::FailReason->new("not posting a comment");
1017 return match_glob($page, $glob, @_);
1020 sub match_comment ($$;@) {
1024 if (! $postcomment) {
1025 # To see if it's a comment, check the source file type.
1026 # Deal with comments that were just deleted.
1027 my $source=exists $IkiWiki::pagesources{$page} ?
1028 $IkiWiki::pagesources{$page} :
1029 $IkiWiki::delpagesources{$page};
1030 my $type=defined $source ? IkiWiki::pagetype($source) : undef;
1031 if (! defined $type || $type ne "_comment") {
1032 return IkiWiki::FailReason->new("$page is not a comment");
1036 return match_glob($page, "$glob/*", internal => 1, @_);
1039 sub match_comment_pending ($$;@) {
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_pending") {
1048 return IkiWiki::FailReason->new("$page is not a pending comment");
1051 return match_glob($page, "$glob/*", internal => 1, @_);