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) };
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 my $emailuser = IkiWiki::emailuser($commentuser);
209 if (defined $emailuser) {
210 $commentuser=$emailuser;
213 if (length $config{cgiurl}) {
214 $commentauthorurl = IkiWiki::cgiurl(
216 page => IkiWiki::userpage($commentuser)
220 $commentauthor = $commentuser;
224 if (defined $params{ip}) {
225 $commentip = $params{ip};
227 $commentauthor = gettext("Anonymous");
230 if ($config{comments_allowauthor}) {
231 if (defined $params{claimedauthor}) {
232 $commentauthor = $params{claimedauthor};
235 if (defined $params{url}) {
236 my $url=$params{url};
238 eval q{use URI::Heuristic};
240 $url=URI::Heuristic::uf_uristr($url);
244 $commentauthorurl = $url;
249 $commentstate{$page}{commentuser} = $commentuser;
250 $commentstate{$page}{commentopenid} = $commentopenid;
251 $commentstate{$page}{commentip} = $commentip;
252 $commentstate{$page}{commentauthor} = $commentauthor;
253 $commentstate{$page}{commentauthorurl} = $commentauthorurl;
254 $commentstate{$page}{commentauthoravatar} = $params{avatar};
255 if (! defined $pagestate{$page}{meta}{author}) {
256 $pagestate{$page}{meta}{author} = $commentauthor;
258 if (! defined $pagestate{$page}{meta}{authorurl}) {
259 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
262 if (defined $params{subject}) {
263 # decode title the same way meta does
264 eval q{use HTML::Entities};
265 $pagestate{$page}{meta}{title} = decode_entities($params{subject});
268 if ($params{page} =~ m/\/\Q$config{comments_pagename}\E\d+_/) {
269 $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page})).
270 "#".page_to_id($params{page});
273 eval q{use Date::Parse};
275 my $time = str2time($params{date});
276 $IkiWiki::pagectime{$page} = $time if defined $time;
282 sub preprocess_moderation {
285 $params{desc}=gettext("Comment Moderation")
286 unless defined $params{desc};
288 if (length $config{cgiurl}) {
290 IkiWiki::cgiurl(do => 'commentmoderation').
291 '">'.$params{desc}.'</a>';
294 return $params{desc};
298 sub sessioncgi ($$) {
302 my $do = $cgi->param('do');
303 if ($do eq 'comment') {
304 editcomment($cgi, $session);
306 elsif ($do eq 'commentmoderation') {
307 commentmoderation($cgi, $session);
309 elsif ($do eq 'commentsignin') {
310 IkiWiki::cgi_signin($cgi, $session);
315 # Mostly cargo-culted from IkiWiki::plugin::editpage
316 sub editcomment ($$) {
320 IkiWiki::decode_cgi_utf8($cgi);
322 eval q{use CGI::FormBuilder};
325 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
326 my $form = CGI::FormBuilder->new(
327 fields => [qw{do sid page subject editcontent type author
328 email url subscribe anonsubscribe}],
331 required => [qw{editcontent}],
334 action => IkiWiki::cgiurl(),
337 template => { template('editcomment.tmpl') },
340 IkiWiki::decode_form_utf8($form);
341 IkiWiki::run_hooks(formbuilder_setup => sub {
342 shift->(title => "comment", form => $form, cgi => $cgi,
343 session => $session, buttons => \@buttons);
345 IkiWiki::decode_form_utf8($form);
347 my $type = $form->param('type');
348 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
349 $type = IkiWiki::possibly_foolish_untaint($type);
352 $type = $config{default_pageext};
357 if (exists $IkiWiki::hooks{htmlize}) {
358 foreach my $key (grep { !/^_/ && isallowed($_) } keys %{$IkiWiki::hooks{htmlize}}) {
359 push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key]
360 unless $IkiWiki::hooks{htmlize}{$key}{nocreate};
363 @page_types=sort @page_types;
365 $form->field(name => 'do', type => 'hidden');
366 $form->field(name => 'sid', type => 'hidden', value => $session->id,
368 $form->field(name => 'page', type => 'hidden');
369 $form->field(name => 'subject', type => 'text', size => 72);
370 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
371 $form->field(name => "type", value => $type, force => 1,
372 type => 'select', options => \@page_types);
374 my $username=$session->param('name');
375 $form->tmpl_param(username => $username);
377 $form->field(name => "subscribe", type => 'hidden');
378 $form->field(name => "anonsubscribe", type => 'hidden');
379 if (IkiWiki::Plugin::notifyemail->can("subscribe")) {
380 if (defined $username) {
381 $form->field(name => "subscribe", type => "checkbox",
382 options => [gettext("email replies to me")]);
384 elsif (IkiWiki::Plugin::passwordauth->can("anonuser")) {
385 $form->field(name => "anonsubscribe", type => "checkbox",
386 options => [gettext("email replies to me")]);
390 if ($config{comments_allowauthor} and
391 ! defined $session->param('name')) {
392 $form->tmpl_param(allowauthor => 1);
393 $form->field(name => 'author', type => 'text', size => '40');
394 $form->field(name => 'email', type => 'text', size => '40');
395 $form->field(name => 'url', type => 'text', size => '40');
398 $form->tmpl_param(allowauthor => 0);
399 $form->field(name => 'author', type => 'hidden', value => '',
401 $form->field(name => 'email', type => 'hidden', value => '',
403 $form->field(name => 'url', type => 'hidden', value => '',
407 if (! defined $session->param('name')) {
408 # Make signinurl work and return here.
409 $form->tmpl_param(signinurl => IkiWiki::cgiurl(do => 'commentsignin'));
410 $session->param(postsignin => $ENV{QUERY_STRING});
411 IkiWiki::cgi_savesession($session);
414 # The untaint is OK (as in editpage) because we're about to pass
415 # it to file_pruned and wiki_file_regexp anyway.
416 my ($page) = $form->field('page')=~/$config{wiki_file_regexp}/;
417 $page = IkiWiki::possibly_foolish_untaint($page);
418 if (! defined $page || ! length $page ||
419 IkiWiki::file_pruned($page)) {
420 error(gettext("bad page name"));
423 $form->title(sprintf(gettext("commenting on %s"),
424 IkiWiki::pagetitle(IkiWiki::basename($page))));
426 $form->tmpl_param('helponformattinglink',
427 htmllink($page, $page, 'ikiwiki/formatting',
429 linktext => 'FormattingHelp'),
430 allowdirectives => $config{allow_directives});
432 if ($form->submitted eq CANCEL) {
433 # bounce back to the page they wanted to comment on, and exit.
434 IkiWiki::redirect($cgi, urlto($page));
438 if (not exists $pagesources{$page}) {
439 error(sprintf(gettext(
440 "page '%s' doesn't exist, so you can't comment"),
444 # There's no UI to get here, but someone might construct the URL,
445 # leading to a comment that exists in the repository but isn't
447 if (!pagespec_match($page, $config{comments_pagespec},
448 location => $page)) {
449 error(sprintf(gettext(
450 "comments on page '%s' are not allowed"),
454 if (pagespec_match($page, $config{comments_closed_pagespec},
455 location => $page)) {
456 error(sprintf(gettext(
457 "comments on page '%s' are closed"),
461 # Set a flag to indicate that we're posting a comment,
462 # so that postcomment() can tell it should match.
464 IkiWiki::check_canedit($page, $cgi, $session);
467 my $content = "[[!comment format=$type\n";
469 if (defined $session->param('name')) {
470 my $username = IkiWiki::cloak($session->param('name'));
471 $username =~ s/"/"/g;
472 $content .= " username=\"$username\"\n";
475 if (defined $session->param('nickname')) {
476 my $nickname = $session->param('nickname');
477 $nickname =~ s/"/"/g;
478 $content .= " nickname=\"$nickname\"\n";
481 if (!(defined $session->param('name') || defined $session->param('nickname')) &&
482 defined $session->remote_addr()) {
483 $content .= " ip=\"".IkiWiki::cloak($session->remote_addr())."\"\n";
486 if ($config{comments_allowauthor}) {
487 my $author = $form->field('author');
488 if (defined $author && length $author) {
489 $author =~ s/"/"/g;
490 $content .= " claimedauthor=\"$author\"\n";
492 my $url = $form->field('url');
493 if (defined $url && length $url) {
494 $url =~ s/"/"/g;
495 $content .= " url=\"$url\"\n";
499 my $avatar=getavatar($session->param('name'));
500 if (defined $avatar && length $avatar) {
501 $avatar =~ s/"/"/g;
502 $content .= " avatar=\"$avatar\"\n";
505 my $subject = $form->field('subject');
506 if (defined $subject && length $subject) {
507 $subject =~ s/"/"/g;
510 $subject = "comment ".(num_comments($page, $config{srcdir}) + 1);
512 $content .= " subject=\"$subject\"\n";
513 $content .= " date=\"" . commentdate() . "\"\n";
515 my $editcontent = $form->field('editcontent');
516 $editcontent="" if ! defined $editcontent;
517 $editcontent =~ s/\r\n/\n/g;
518 $editcontent =~ s/\r/\n/g;
519 $editcontent =~ s/"/\\"/g;
520 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
522 my $location=unique_comment_location($page, $content, $config{srcdir});
524 # This is essentially a simplified version of editpage:
525 # - the user does not control the page that's created, only the parent
526 # - it's always a create operation, never an edit
527 # - this means that conflicts should never happen
528 # - this means that if they do, rocks fall and everyone dies
530 if ($form->submitted eq PREVIEW) {
531 my $preview=previewcomment($content, $location, $page, time);
532 IkiWiki::run_hooks(format => sub {
533 $preview = shift->(page => $page,
534 content => $preview);
536 $form->tmpl_param(page_preview => $preview);
539 $form->tmpl_param(page_preview => "");
542 if ($form->submitted eq POST_COMMENT && $form->validate) {
543 IkiWiki::checksessionexpiry($cgi, $session);
545 if (IkiWiki::Plugin::notifyemail->can("subscribe")) {
546 my $subspec="comment($page)";
547 if (defined $username &&
548 length $form->field("subscribe")) {
549 IkiWiki::Plugin::notifyemail::subscribe(
550 $username, $subspec);
552 elsif (length $form->field("email") &&
553 length $form->field("anonsubscribe")) {
554 IkiWiki::Plugin::notifyemail::anonsubscribe(
555 $form->field("email"), $subspec);
560 my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
561 subject => $form->field('subject'),
562 $config{comments_allowauthor} ? (
563 author => $form->field('author'),
564 url => $form->field('url'),
574 $location=unique_comment_location($page, $content, $IkiWiki::Plugin::transient::transientdir, "._comment_pending");
575 writefile("$location._comment_pending", $IkiWiki::Plugin::transient::transientdir, $content);
577 # Refresh so anything that deals with pending
578 # comments can be updated.
579 require IkiWiki::Render;
581 IkiWiki::saveindex();
583 IkiWiki::printheader($session);
584 print IkiWiki::cgitemplate($cgi, gettext(gettext("comment stored for moderation")),
586 gettext("Your comment will be posted after moderator review").
591 # FIXME: could probably do some sort of graceful retry
592 # on error? Would require significant unwinding though
593 my $file = "$location._comment";
594 writefile($file, $config{srcdir}, $content);
598 if ($config{rcs} and $config{comments_commit}) {
599 my $message = gettext("Added a comment");
600 if (defined $form->field('subject') &&
601 length $form->field('subject')) {
603 gettext("Added a comment: %s"),
604 $form->field('subject'));
607 IkiWiki::rcs_add($file);
608 IkiWiki::disable_commit_hook();
609 $conflict = IkiWiki::rcs_commit_staged(
613 IkiWiki::enable_commit_hook();
614 IkiWiki::rcs_update();
617 # Now we need a refresh
618 require IkiWiki::Render;
620 IkiWiki::saveindex();
622 # this should never happen, unless a committer deliberately
623 # breaks it or something
624 error($conflict) if defined $conflict;
626 # Jump to the new comment on the page.
627 # The trailing question mark tries to avoid broken
628 # caches and get the most recent version of the page.
629 IkiWiki::redirect($cgi, urlto($page).
630 "?updated#".page_to_id($location));
634 IkiWiki::showform($form, \@buttons, $session, $cgi,
642 strftime_utf8('%Y-%m-%dT%H:%M:%SZ', gmtime);
647 return undef unless defined $user;
650 eval q{use Libravatar::URL};
652 my $oiduser = eval { IkiWiki::openiduser($user) };
653 my $https=defined $config{url} && $config{url}=~/^https:/;
655 if (defined $oiduser) {
657 $avatar = libravatar_url(openid => $user, https => $https);
660 if (! defined $avatar &&
661 (my $email = IkiWiki::userinfo_get($user, 'email'))) {
663 $avatar = libravatar_url(email => $email, https => $https);
671 sub commentmoderation ($$) {
675 IkiWiki::needsignin($cgi, $session);
676 if (! IkiWiki::is_admin($session->param("name"))) {
677 error(gettext("you are not logged in as an admin"));
680 IkiWiki::decode_cgi_utf8($cgi);
682 if (defined $cgi->param('sid')) {
683 IkiWiki::checksessionexpiry($cgi, $session);
685 my $rejectalldefer=$cgi->param('rejectalldefer');
689 foreach my $id (keys %vars) {
690 if ($id =~ /(.*)\._comment(?:_pending)?$/) {
691 $id=decode_utf8($id);
692 my $action=$cgi->param($id);
693 next if $action eq 'Defer' && ! $rejectalldefer;
695 # Make sure that the id is of a legal
697 my ($f) = $id =~ /$config{wiki_file_regexp}/;
698 if (! defined $f || ! length $f ||
699 IkiWiki::file_pruned($f)) {
700 error("illegal file");
703 my $page=IkiWiki::dirname($f);
704 my $filedir=$IkiWiki::Plugin::transient::transientdir;
705 my $file="$filedir/$f";
708 $file="$config{srcdir}/$f";
709 $filedir=$config{srcdir};
712 $file="$config{wikistatedir}/comments_pending/".$f;
713 $filedir="$config{wikistatedir}/comments_pending";
717 if ($action eq 'Accept') {
718 my $content=eval { readfile($file) };
719 next if $@; # file vanished since form was displayed
720 my $dest=unique_comment_location($page, $content, $config{srcdir})."._comment";
721 writefile($dest, $config{srcdir}, $content);
722 if ($config{rcs} and $config{comments_commit}) {
723 IkiWiki::rcs_add($dest);
728 require IkiWiki::Render;
729 IkiWiki::prune($file, $filedir);
735 if ($config{rcs} and $config{comments_commit}) {
736 my $message = gettext("Comment moderation");
737 IkiWiki::disable_commit_hook();
738 $conflict=IkiWiki::rcs_commit_staged(
742 IkiWiki::enable_commit_hook();
743 IkiWiki::rcs_update();
746 # Now we need a refresh
747 require IkiWiki::Render;
749 IkiWiki::saveindex();
751 error($conflict) if defined $conflict;
756 my ($id, $dir, $ctime)=@{$_};
757 my $content=readfile("$dir/$id");
758 my $preview=previewcomment($content, $id,
764 } sort { $b->[2] <=> $a->[2] } comments_pending();
766 my $template=template("commentmoderation.tmpl");
769 comments => \@comments,
770 cgiurl => IkiWiki::cgiurl(),
772 IkiWiki::printheader($session);
773 my $out=$template->output;
774 IkiWiki::run_hooks(format => sub {
775 $out = shift->(page => "", content => $out);
777 print IkiWiki::cgitemplate($cgi, gettext("comment moderation"), $out);
781 sub formbuilder_setup (@) {
784 my $form=$params{form};
785 if ($form->title eq "preferences" &&
786 IkiWiki::is_admin($params{session}->param("name"))) {
787 push @{$params{buttons}}, "Comment Moderation";
788 if ($form->submitted && $form->submitted eq "Comment Moderation") {
789 commentmoderation($params{cgi}, $params{session});
794 sub comments_pending () {
797 eval q{use File::Find};
801 my $origdir=getcwd();
803 my $find_comments=sub {
806 return unless -d $dir;
808 chdir($dir) || die "chdir $dir: $!";
813 my $file=decode_utf8($_);
815 return if ! length $file || IkiWiki::file_pruned($file)
816 || -l $_ || -d _ || $file !~ /\Q$extension\E$/;
817 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
819 my $ctime=(stat($_))[10];
820 push @ret, [$f, $dir, $ctime];
825 chdir($origdir) || die "chdir $origdir: $!";
828 $find_comments->($IkiWiki::Plugin::transient::transientdir, "._comment_pending");
830 $find_comments->($config{srcdir}, "._comment_pending");
832 $find_comments->("$config{wikistatedir}/comments_pending/",
838 sub previewcomment ($$$) {
844 # Previewing a comment should implicitly enable comment posting mode.
845 my $oldpostcomment=$postcomment;
848 my $preview = IkiWiki::htmlize($location, $page, '_comment',
849 IkiWiki::linkify($location, $page,
850 IkiWiki::preprocess($location, $page,
851 IkiWiki::filter($location, $page, $content), 0, 1)));
853 my $template = template("comment.tmpl");
854 $template->param(content => $preview);
855 $template->param(ctime => displaytime($time, undef, 1));
856 $template->param(html5 => $config{html5});
858 IkiWiki::run_hooks(pagetemplate => sub {
859 shift->(page => $location,
861 template => $template);
864 $template->param(have_actions => 0);
866 $postcomment=$oldpostcomment;
868 return $template->output;
871 sub commentsshown ($) {
874 return pagespec_match($page, $config{comments_pagespec},
878 sub commentsopen ($) {
881 return length $config{cgiurl} > 0 &&
882 (! length $config{comments_closed_pagespec} ||
883 ! pagespec_match($page, $config{comments_closed_pagespec},
887 sub pagetemplate (@) {
890 my $page = $params{page};
891 my $template = $params{template};
892 my $shown = ($template->query(name => 'commentslink') ||
893 $template->query(name => 'commentsurl') ||
894 $template->query(name => 'atomcommentsurl') ||
895 $template->query(name => 'comments')) &&
896 commentsshown($page);
898 if ($template->query(name => 'comments')) {
899 my $comments = undef;
901 $comments = IkiWiki::preprocess_inline(
902 pages => "comment($page) and !comment($page/*)",
903 template => 'comment',
907 destpage => $params{destpage},
908 feedfile => 'comments',
913 if (defined $comments && length $comments) {
914 $template->param(comments => $comments);
917 if ($shown && commentsopen($page)) {
918 $template->param(addcommenturl => addcommenturl($page));
923 my $absolute = $template->param('wants_absolute_urls');
925 if ($template->query(name => 'commentsurl')) {
926 $template->param(commentsurl =>
927 urlto($page, undef, $absolute).'#comments');
930 if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
931 # This will 404 until there are some comments, but I
932 # think that's probably OK...
933 $template->param(atomcommentsurl =>
934 urlto($page, undef, $absolute).'comments.atom');
937 if ($template->query(name => 'commentslink')) {
938 my $num=num_comments($page, $config{srcdir});
941 $link = htmllink($page, $params{destpage}, $page,
942 linktext => sprintf(ngettext("%i comment", "%i comments", $num), $num),
943 anchor => "comments",
947 elsif (commentsopen($page)) {
948 $link = "<a href=\"".addcommenturl($page)."\">".
949 #translators: Here "Comment" is a verb;
950 #translators: the user clicks on it to
951 #translators: post a comment.
955 $template->param(commentslink => $link)
960 # everything below this point is only relevant to the comments
962 if (!exists $commentstate{$page}) {
966 if ($template->query(name => 'commentid')) {
967 $template->param(commentid => page_to_id($page));
970 if ($template->query(name => 'commentuser')) {
971 $template->param(commentuser =>
972 $commentstate{$page}{commentuser});
975 if ($template->query(name => 'commentopenid')) {
976 $template->param(commentopenid =>
977 $commentstate{$page}{commentopenid});
980 if ($template->query(name => 'commentip')) {
981 $template->param(commentip =>
982 $commentstate{$page}{commentip});
985 if ($template->query(name => 'commentauthor')) {
986 $template->param(commentauthor =>
987 $commentstate{$page}{commentauthor});
990 if ($template->query(name => 'commentauthorurl')) {
991 $template->param(commentauthorurl =>
992 $commentstate{$page}{commentauthorurl});
995 if ($template->query(name => 'commentauthoravatar')) {
996 $template->param(commentauthoravatar =>
997 $commentstate{$page}{commentauthoravatar});
1000 if ($template->query(name => 'removeurl') &&
1001 IkiWiki::Plugin::remove->can("check_canremove") &&
1002 length $config{cgiurl}) {
1003 $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
1005 $template->param(have_actions => 1);
1009 sub addcommenturl ($) {
1012 return IkiWiki::cgiurl(do => 'comment', page => $page);
1015 sub num_comments ($$) {
1019 my @comments=glob("$dir/$page/$config{comments_pagename}*._comment");
1020 return int @comments;
1023 sub unique_comment_location ($$$;$) {
1025 eval q{use Digest::MD5 'md5_hex'};
1027 my $content_md5=md5_hex(Encode::encode_utf8(shift));
1029 my $ext=shift || "._comment";
1032 my $i = num_comments($page, $dir);
1035 $location = "$page/$config{comments_pagename}${i}_${content_md5}";
1036 } while (-e "$dir/$location$ext");
1041 sub page_to_id ($) {
1042 # Converts a comment page name into a unique, legal html id
1043 # attribute value, that can be used as an anchor to link to the
1047 eval q{use Digest::MD5 'md5_hex'};
1050 return "comment-".md5_hex(Encode::encode_utf8(($page)));
1053 package IkiWiki::PageSpec;
1055 sub match_postcomment ($$;@) {
1059 if (! $postcomment) {
1060 return IkiWiki::FailReason->new("not posting a comment");
1062 return match_glob($page, $glob, @_);
1065 sub match_comment ($$;@) {
1069 if (! $postcomment) {
1070 # To see if it's a comment, check the source file type.
1071 # Deal with comments that were just deleted.
1072 my $source=exists $IkiWiki::pagesources{$page} ?
1073 $IkiWiki::pagesources{$page} :
1074 $IkiWiki::delpagesources{$page};
1075 my $type=defined $source ? IkiWiki::pagetype($source) : undef;
1076 if (! defined $type || $type ne "_comment") {
1077 return IkiWiki::FailReason->new("$page is not a comment");
1081 return match_glob($page, "$glob/*", internal => 1, @_);
1084 sub match_comment_pending ($$;@) {
1088 my $source=exists $IkiWiki::pagesources{$page} ?
1089 $IkiWiki::pagesources{$page} :
1090 $IkiWiki::delpagesources{$page};
1091 my $type=defined $source ? IkiWiki::pagetype($source) : undef;
1092 if (! defined $type || $type ne "_comment_pending") {
1093 return IkiWiki::FailReason->new("$page is not a pending comment");
1096 return match_glob($page, "$glob/*", internal => 1, @_);