2 # Copyright © 2006-2008 Joey Hess <joey@ikiwiki.info>
3 # Copyright © 2008 Simon McVittie <http://smcv.pseudorandom.co.uk/>
4 # Licensed under the GNU GPL, version 2, or any later version published by the
5 # Free Software Foundation
6 package IkiWiki
::Plugin
::comments
;
12 use POSIX
qw(strftime);
14 use constant PREVIEW
=> "Preview";
15 use constant POST_COMMENT
=> "Post comment";
16 use constant CANCEL
=> "Cancel";
22 hook
(type
=> "checkconfig", id
=> 'comments', call
=> \
&checkconfig
);
23 hook
(type
=> "getsetup", id
=> 'comments', call
=> \
&getsetup
);
24 hook
(type
=> "preprocess", id
=> 'comment', call
=> \
&preprocess
);
25 hook
(type
=> "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 ($config{comments_allowdirectives
}) {
147 $content = IkiWiki
::preprocess
($page, $params{destpage
},
151 # no need to bother with htmlize if it's just HTML
152 $content = IkiWiki
::htmlize
($page, $params{destpage
}, $format, $content)
155 IkiWiki
::run_hooks
(sanitize
=> sub {
158 destpage
=> $params{destpage
},
163 # set metadata, possibly overriding [[!meta]] directives from the
169 my $commentauthorurl;
171 if (defined $params{username
}) {
172 $commentuser = $params{username
};
174 my $oiduser = eval { IkiWiki
::openiduser
($commentuser) };
176 if (defined $oiduser) {
177 # looks like an OpenID
178 $commentauthorurl = $commentuser;
179 $commentauthor = (defined $params{nickname
} && length $params{nickname
}) ?
$params{nickname
} : $oiduser;
180 $commentopenid = $commentuser;
183 $commentauthorurl = IkiWiki
::cgiurl
(
185 page
=> IkiWiki
::userpage
($commentuser)
188 $commentauthor = $commentuser;
192 if (defined $params{ip
}) {
193 $commentip = $params{ip
};
195 $commentauthor = gettext
("Anonymous");
198 $commentstate{$page}{commentuser
} = $commentuser;
199 $commentstate{$page}{commentopenid
} = $commentopenid;
200 $commentstate{$page}{commentip
} = $commentip;
201 $commentstate{$page}{commentauthor
} = $commentauthor;
202 $commentstate{$page}{commentauthorurl
} = $commentauthorurl;
203 $commentstate{$page}{commentauthoravatar
} = $params{avatar
};
204 if (! defined $pagestate{$page}{meta
}{author
}) {
205 $pagestate{$page}{meta
}{author
} = $commentauthor;
207 if (! defined $pagestate{$page}{meta
}{authorurl
}) {
208 $pagestate{$page}{meta
}{authorurl
} = $commentauthorurl;
211 if ($config{comments_allowauthor
}) {
212 if (defined $params{claimedauthor
}) {
213 $pagestate{$page}{meta
}{author
} = $params{claimedauthor
};
216 if (defined $params{url
}) {
217 my $url=$params{url
};
219 eval q{use URI::Heuristic};
221 $url=URI
::Heuristic
::uf_uristr
($url);
225 $pagestate{$page}{meta
}{authorurl
} = $url;
230 $pagestate{$page}{meta
}{author
} = $commentauthor;
231 $pagestate{$page}{meta
}{authorurl
} = $commentauthorurl;
234 if (defined $params{subject
}) {
235 # decode title the same way meta does
236 eval q{use HTML::Entities};
237 $pagestate{$page}{meta
}{title
} = decode_entities
($params{subject
});
240 if ($params{page
} =~ m/\/\Q
$config{comments_pagename
}\E\d
+_
/) {
241 $pagestate{$page}{meta
}{permalink
} = urlto
(IkiWiki
::dirname
($params{page
})).
242 "#".page_to_id
($params{page
});
245 eval q{use Date::Parse};
247 my $time = str2time
($params{date
});
248 $IkiWiki::pagectime
{$page} = $time if defined $time;
254 sub preprocess_moderation
{
257 $params{desc
}=gettext
("Comment Moderation")
258 unless defined $params{desc
};
260 if (length $config{cgiurl
}) {
262 IkiWiki
::cgiurl
(do => 'commentmoderation').
263 '">'.$params{desc
}.'</a>';
266 return $params{desc
};
270 sub sessioncgi
($$) {
274 my $do = $cgi->param('do');
275 if ($do eq 'comment') {
276 editcomment
($cgi, $session);
278 elsif ($do eq 'commentmoderation') {
279 commentmoderation
($cgi, $session);
281 elsif ($do eq 'commentsignin') {
282 IkiWiki
::cgi_signin
($cgi, $session);
287 # Mostly cargo-culted from IkiWiki::plugin::editpage
288 sub editcomment
($$) {
292 IkiWiki
::decode_cgi_utf8
($cgi);
294 eval q{use CGI::FormBuilder};
297 my @buttons = (POST_COMMENT
, PREVIEW
, CANCEL
);
298 my $form = CGI
::FormBuilder
->new(
299 fields
=> [qw{do sid page subject editcontent type author url
}],
302 required
=> [qw{editcontent
}],
305 action
=> IkiWiki
::cgiurl
(),
308 template
=> { template
('editcomment.tmpl') },
311 IkiWiki
::decode_form_utf8
($form);
312 IkiWiki
::run_hooks
(formbuilder_setup
=> sub {
313 shift->(title
=> "comment", form
=> $form, cgi
=> $cgi,
314 session
=> $session, buttons
=> \
@buttons);
316 IkiWiki
::decode_form_utf8
($form);
318 my $type = $form->param('type');
319 if (defined $type && length $type && $IkiWiki::hooks
{htmlize
}{$type}) {
320 $type = IkiWiki
::possibly_foolish_untaint
($type);
323 $type = $config{default_pageext
};
328 if (exists $IkiWiki::hooks
{htmlize
}) {
329 foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks
{htmlize
}}) {
330 push @page_types, [$key, $IkiWiki::hooks
{htmlize
}{$key}{longname
} || $key];
333 @page_types=sort @page_types;
335 $form->field(name
=> 'do', type
=> 'hidden');
336 $form->field(name
=> 'sid', type
=> 'hidden', value
=> $session->id,
338 $form->field(name
=> 'page', type
=> 'hidden');
339 $form->field(name
=> 'subject', type
=> 'text', size
=> 72);
340 $form->field(name
=> 'editcontent', type
=> 'textarea', rows
=> 10);
341 $form->field(name
=> "type", value
=> $type, force
=> 1,
342 type
=> 'select', options
=> \
@page_types);
344 $form->tmpl_param(username
=> $session->param('name'));
346 if ($config{comments_allowauthor
} and
347 ! defined $session->param('name')) {
348 $form->tmpl_param(allowauthor
=> 1);
349 $form->field(name
=> 'author', type
=> 'text', size
=> '40');
350 $form->field(name
=> 'url', type
=> 'text', size
=> '40');
353 $form->tmpl_param(allowauthor
=> 0);
354 $form->field(name
=> 'author', type
=> 'hidden', value
=> '',
356 $form->field(name
=> 'url', type
=> 'hidden', value
=> '',
360 if (! defined $session->param('name')) {
361 # Make signinurl work and return here.
362 $form->tmpl_param(signinurl
=> IkiWiki
::cgiurl
(do => 'commentsignin'));
363 $session->param(postsignin
=> $ENV{QUERY_STRING
});
364 IkiWiki
::cgi_savesession
($session);
367 # The untaint is OK (as in editpage) because we're about to pass
368 # it to file_pruned and wiki_file_regexp anyway.
369 my ($page) = $form->field('page')=~/$config{wiki_file_regexp}/;
370 $page = IkiWiki
::possibly_foolish_untaint
($page);
371 if (! defined $page || ! length $page ||
372 IkiWiki
::file_pruned
($page)) {
373 error
(gettext
("bad page name"));
376 $form->title(sprintf(gettext
("commenting on %s"),
377 IkiWiki
::pagetitle
(IkiWiki
::basename
($page))));
379 $form->tmpl_param('helponformattinglink',
380 htmllink
($page, $page, 'ikiwiki/formatting',
382 linktext
=> 'FormattingHelp'),
383 allowdirectives
=> $config{allow_directives
});
385 if ($form->submitted eq CANCEL
) {
386 # bounce back to the page they wanted to comment on, and exit.
387 IkiWiki
::redirect
($cgi, urlto
($page));
391 if (not exists $pagesources{$page}) {
392 error
(sprintf(gettext
(
393 "page '%s' doesn't exist, so you can't comment"),
397 if (pagespec_match
($page, $config{comments_closed_pagespec
},
398 location
=> $page)) {
399 error
(sprintf(gettext
(
400 "comments on page '%s' are closed"),
404 # Set a flag to indicate that we're posting a comment,
405 # so that postcomment() can tell it should match.
407 IkiWiki
::check_canedit
($page, $cgi, $session);
410 my $content = "[[!comment format=$type\n";
412 if (defined $session->param('name')) {
413 my $username = $session->param('name');
414 $username =~ s/"/"/g;
415 $content .= " username=\"$username\"\n";
417 if (defined $session->param('nickname')) {
418 my $nickname = $session->param('nickname');
419 $nickname =~ s/"/"/g;
420 $content .= " nickname=\"$nickname\"\n";
422 elsif (defined $session->remote_addr()) {
423 my $ip = $session->remote_addr();
424 if ($ip =~ m/^([.0-9]+)$/) {
425 $content .= " ip=\"$1\"\n";
429 if ($config{comments_allowauthor
}) {
430 my $author = $form->field('author');
431 if (defined $author && length $author) {
432 $author =~ s/"/"/g;
433 $content .= " claimedauthor=\"$author\"\n";
435 my $url = $form->field('url');
436 if (defined $url && length $url) {
437 $url =~ s/"/"/g;
438 $content .= " url=\"$url\"\n";
442 my $avatar=getavatar
($session->param('name'));
443 if (defined $avatar && length $avatar) {
444 $avatar =~ s/"/"/g;
445 $content .= " avatar=\"$avatar\"\n";
448 my $subject = $form->field('subject');
449 if (defined $subject && length $subject) {
450 $subject =~ s/"/"/g;
453 $subject = "comment ".(num_comments
($page, $config{srcdir
}) + 1);
455 $content .= " subject=\"$subject\"\n";
457 $content .= " date=\"" . decode_utf8
(strftime
('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
459 my $editcontent = $form->field('editcontent');
460 $editcontent="" if ! defined $editcontent;
461 $editcontent =~ s/\r\n/\n/g;
462 $editcontent =~ s/\r/\n/g;
463 $editcontent =~ s/"/\\"/g;
464 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
466 my $location=unique_comment_location
($page, $content, $config{srcdir
});
468 # This is essentially a simplified version of editpage:
469 # - the user does not control the page that's created, only the parent
470 # - it's always a create operation, never an edit
471 # - this means that conflicts should never happen
472 # - this means that if they do, rocks fall and everyone dies
474 if ($form->submitted eq PREVIEW
) {
475 my $preview=previewcomment
($content, $location, $page, time);
476 IkiWiki
::run_hooks
(format
=> sub {
477 $preview = shift->(page
=> $page,
478 content
=> $preview);
480 $form->tmpl_param(page_preview
=> $preview);
483 $form->tmpl_param(page_preview
=> "");
486 if ($form->submitted eq POST_COMMENT
&& $form->validate) {
487 IkiWiki
::checksessionexpiry
($cgi, $session);
490 my $ok=IkiWiki
::check_content
(content
=> $form->field('editcontent'),
491 subject
=> $form->field('subject'),
492 $config{comments_allowauthor
} ?
(
493 author
=> $form->field('author'),
494 url
=> $form->field('url'),
504 $location=unique_comment_location
($page, $content, $config{srcdir
}, "._comment_pending");
505 writefile
("$location._comment_pending", $config{srcdir
}, $content);
507 # Refresh so anything that deals with pending
508 # comments can be updated.
509 require IkiWiki
::Render
;
511 IkiWiki
::saveindex
();
513 IkiWiki
::printheader
($session);
514 print IkiWiki
::cgitemplate
($cgi, gettext
(gettext
("comment stored for moderation")),
516 gettext
("Your comment will be posted after moderator review").
521 # FIXME: could probably do some sort of graceful retry
522 # on error? Would require significant unwinding though
523 my $file = "$location._comment";
524 writefile
($file, $config{srcdir
}, $content);
528 if ($config{rcs
} and $config{comments_commit
}) {
529 my $message = gettext
("Added a comment");
530 if (defined $form->field('subject') &&
531 length $form->field('subject')) {
533 gettext
("Added a comment: %s"),
534 $form->field('subject'));
537 IkiWiki
::rcs_add
($file);
538 IkiWiki
::disable_commit_hook
();
539 $conflict = IkiWiki
::rcs_commit_staged
(
543 IkiWiki
::enable_commit_hook
();
544 IkiWiki
::rcs_update
();
547 # Now we need a refresh
548 require IkiWiki
::Render
;
550 IkiWiki
::saveindex
();
552 # this should never happen, unless a committer deliberately
553 # breaks it or something
554 error
($conflict) if defined $conflict;
556 # Jump to the new comment on the page.
557 # The trailing question mark tries to avoid broken
558 # caches and get the most recent version of the page.
559 IkiWiki
::redirect
($cgi, urlto
($page).
560 "?updated#".page_to_id
($location));
564 IkiWiki
::showform
($form, \
@buttons, $session, $cgi,
575 eval q{use Libravatar::URL};
577 my $oiduser = eval { IkiWiki
::openiduser
($user) };
578 my $https=defined $config{url
} && $config{url
}=~/^https:/;
580 if (defined $oiduser) {
582 $avatar = libravatar_url
(openid
=> $user, https
=> $https);
585 if (! defined $avatar &&
586 (my $email = IkiWiki
::userinfo_get
($user, 'email'))) {
588 $avatar = libravatar_url
(email
=> $email, https
=> $https);
596 sub commentmoderation
($$) {
600 IkiWiki
::needsignin
($cgi, $session);
601 if (! IkiWiki
::is_admin
($session->param("name"))) {
602 error
(gettext
("you are not logged in as an admin"));
605 IkiWiki
::decode_cgi_utf8
($cgi);
607 if (defined $cgi->param('sid')) {
608 IkiWiki
::checksessionexpiry
($cgi, $session);
610 my $rejectalldefer=$cgi->param('rejectalldefer');
614 foreach my $id (keys %vars) {
615 if ($id =~ /(.*)\._comment(?:_pending)?$/) {
616 $id=decode_utf8
($id);
617 my $action=$cgi->param($id);
618 next if $action eq 'Defer' && ! $rejectalldefer;
620 # Make sure that the id is of a legal
622 my ($f) = $id =~ /$config{wiki_file_regexp}/;
623 if (! defined $f || ! length $f ||
624 IkiWiki
::file_pruned
($f)) {
625 error
("illegal file");
628 my $page=IkiWiki
::dirname
($f);
629 my $file="$config{srcdir}/$f";
632 $file="$config{wikistatedir}/comments_pending/".$f;
635 if ($action eq 'Accept') {
636 my $content=eval { readfile
($file) };
637 next if $@
; # file vanished since form was displayed
638 my $dest=unique_comment_location
($page, $content, $config{srcdir
})."._comment";
639 writefile
($dest, $config{srcdir
}, $content);
640 if ($config{rcs
} and $config{comments_commit
}) {
641 IkiWiki
::rcs_add
($dest);
646 require IkiWiki
::Render
;
647 IkiWiki
::prune
($file);
653 if ($config{rcs
} and $config{comments_commit
}) {
654 my $message = gettext
("Comment moderation");
655 IkiWiki
::disable_commit_hook
();
656 $conflict=IkiWiki
::rcs_commit_staged
(
660 IkiWiki
::enable_commit_hook
();
661 IkiWiki
::rcs_update
();
664 # Now we need a refresh
665 require IkiWiki
::Render
;
667 IkiWiki
::saveindex
();
669 error
($conflict) if defined $conflict;
674 my ($id, $dir, $ctime)=@
{$_};
675 my $content=readfile
("$dir/$id");
676 my $preview=previewcomment
($content, $id,
682 } sort { $b->[2] <=> $a->[2] } comments_pending
();
684 my $template=template
("commentmoderation.tmpl");
687 comments
=> \
@comments,
688 cgiurl
=> IkiWiki
::cgiurl
(),
690 IkiWiki
::printheader
($session);
691 my $out=$template->output;
692 IkiWiki
::run_hooks
(format
=> sub {
693 $out = shift->(page
=> "", content
=> $out);
695 print IkiWiki
::cgitemplate
($cgi, gettext
("comment moderation"), $out);
699 sub formbuilder_setup
(@
) {
702 my $form=$params{form
};
703 if ($form->title eq "preferences" &&
704 IkiWiki
::is_admin
($params{session
}->param("name"))) {
705 push @
{$params{buttons
}}, "Comment Moderation";
706 if ($form->submitted && $form->submitted eq "Comment Moderation") {
707 commentmoderation
($params{cgi
}, $params{session
});
712 sub comments_pending
() {
715 eval q{use File::Find};
719 my $origdir=getcwd
();
721 my $find_comments=sub {
724 return unless -d
$dir;
726 chdir($dir) || die "chdir $dir: $!";
731 my $file=decode_utf8
($_);
733 return if ! length $file || IkiWiki
::file_pruned
($file)
734 || -l
$_ || -d _
|| $file !~ /\Q$extension\E$/;
735 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
737 my $ctime=(stat($_))[10];
738 push @ret, [$f, $dir, $ctime];
743 chdir($origdir) || die "chdir $origdir: $!";
746 $find_comments->($config{srcdir
}, "._comment_pending");
748 $find_comments->("$config{wikistatedir}/comments_pending/",
754 sub previewcomment
($$$) {
760 # Previewing a comment should implicitly enable comment posting mode.
761 my $oldpostcomment=$postcomment;
764 my $preview = IkiWiki
::htmlize
($location, $page, '_comment',
765 IkiWiki
::linkify
($location, $page,
766 IkiWiki
::preprocess
($location, $page,
767 IkiWiki
::filter
($location, $page, $content), 0, 1)));
769 my $template = template
("comment.tmpl");
770 $template->param(content
=> $preview);
771 $template->param(ctime
=> displaytime
($time, undef, 1));
772 $template->param(html5
=> $config{html5
});
774 IkiWiki
::run_hooks
(pagetemplate
=> sub {
775 shift->(page
=> $location,
777 template
=> $template);
780 $template->param(have_actions
=> 0);
782 $postcomment=$oldpostcomment;
784 return $template->output;
787 sub commentsshown
($) {
790 return pagespec_match
($page, $config{comments_pagespec
},
794 sub commentsopen
($) {
797 return length $config{cgiurl
} > 0 &&
798 (! length $config{comments_closed_pagespec
} ||
799 ! pagespec_match
($page, $config{comments_closed_pagespec
},
803 sub pagetemplate
(@
) {
806 my $page = $params{page
};
807 my $template = $params{template
};
808 my $shown = ($template->query(name
=> 'commentslink') ||
809 $template->query(name
=> 'commentsurl') ||
810 $template->query(name
=> 'atomcommentsurl') ||
811 $template->query(name
=> 'comments')) &&
812 commentsshown
($page);
814 if ($template->query(name
=> 'comments')) {
815 my $comments = undef;
817 $comments = IkiWiki
::preprocess_inline
(
818 pages
=> "comment($page) and !comment($page/*)",
819 template
=> 'comment',
823 destpage
=> $params{destpage
},
824 feedfile
=> 'comments',
829 if (defined $comments && length $comments) {
830 $template->param(comments
=> $comments);
833 if ($shown && commentsopen
($page)) {
834 $template->param(addcommenturl
=> addcommenturl
($page));
839 if ($template->query(name
=> 'commentsurl')) {
840 $template->param(commentsurl
=>
841 urlto
($page).'#comments');
844 if ($template->query(name
=> 'atomcommentsurl') && $config{usedirs
}) {
845 # This will 404 until there are some comments, but I
846 # think that's probably OK...
847 $template->param(atomcommentsurl
=>
848 urlto
($page).'comments.atom');
851 if ($template->query(name
=> 'commentslink')) {
852 my $num=num_comments
($page, $config{srcdir
});
855 $link = htmllink
($page, $params{destpage
}, $page,
856 linktext
=> sprintf(ngettext
("%i comment", "%i comments", $num), $num),
857 anchor
=> "comments",
861 elsif (commentsopen
($page)) {
862 $link = "<a href=\"".addcommenturl
($page)."\">".
863 #translators: Here "Comment" is a verb;
864 #translators: the user clicks on it to
865 #translators: post a comment.
869 $template->param(commentslink
=> $link)
874 # everything below this point is only relevant to the comments
876 if (!exists $commentstate{$page}) {
880 if ($template->query(name
=> 'commentid')) {
881 $template->param(commentid
=> page_to_id
($page));
884 if ($template->query(name
=> 'commentuser')) {
885 $template->param(commentuser
=>
886 $commentstate{$page}{commentuser
});
889 if ($template->query(name
=> 'commentopenid')) {
890 $template->param(commentopenid
=>
891 $commentstate{$page}{commentopenid
});
894 if ($template->query(name
=> 'commentip')) {
895 $template->param(commentip
=>
896 $commentstate{$page}{commentip
});
899 if ($template->query(name
=> 'commentauthor')) {
900 $template->param(commentauthor
=>
901 $commentstate{$page}{commentauthor
});
904 if ($template->query(name
=> 'commentauthorurl')) {
905 $template->param(commentauthorurl
=>
906 $commentstate{$page}{commentauthorurl
});
909 if ($template->query(name
=> 'commentauthoravatar')) {
910 $template->param(commentauthoravatar
=>
911 $commentstate{$page}{commentauthoravatar
});
914 if ($template->query(name
=> 'removeurl') &&
915 IkiWiki
::Plugin
::remove
->can("check_canremove") &&
916 length $config{cgiurl
}) {
917 $template->param(removeurl
=> IkiWiki
::cgiurl
(do => 'remove',
919 $template->param(have_actions
=> 1);
923 sub addcommenturl
($) {
926 return IkiWiki
::cgiurl
(do => 'comment', page
=> $page);
929 sub num_comments
($$) {
933 my @comments=glob("$dir/$page/$config{comments_pagename}*._comment");
934 return int @comments;
937 sub unique_comment_location
($$$$) {
939 eval q{use Digest::MD5 'md5_hex'};
941 my $content_md5=md5_hex
(Encode
::encode_utf8
(shift));
943 my $ext=shift || "._comment";
946 my $i = num_comments
($page, $dir);
949 $location = "$page/$config{comments_pagename}${i}_${content_md5}";
950 } while (-e
"$dir/$location$ext");
956 # Converts a comment page name into a unique, legal html id
957 # attribute value, that can be used as an anchor to link to the
961 eval q{use Digest::MD5 'md5_hex'};
964 return "comment-".md5_hex
(Encode
::encode_utf8
(($page)));
967 package IkiWiki
::PageSpec
;
969 sub match_postcomment
($$;@
) {
973 if (! $postcomment) {
974 return IkiWiki
::FailReason
->new("not posting a comment");
976 return match_glob
($page, $glob, @_);
979 sub match_comment
($$;@
) {
983 if (! $postcomment) {
984 # To see if it's a comment, check the source file type.
985 # Deal with comments that were just deleted.
986 my $source=exists $IkiWiki::pagesources
{$page} ?
987 $IkiWiki::pagesources
{$page} :
988 $IkiWiki::delpagesources
{$page};
989 my $type=defined $source ? IkiWiki
::pagetype
($source) : undef;
990 if (! defined $type || $type ne "_comment") {
991 return IkiWiki
::FailReason
->new("$page is not a comment");
995 return match_glob
($page, "$glob/*", internal
=> 1, @_);
998 sub match_comment_pending
($$;@
) {
1002 my $source=exists $IkiWiki::pagesources
{$page} ?
1003 $IkiWiki::pagesources
{$page} :
1004 $IkiWiki::delpagesources
{$page};
1005 my $type=defined $source ? IkiWiki
::pagetype
($source) : undef;
1006 if (! defined $type || $type ne "_comment_pending") {
1007 return IkiWiki
::FailReason
->new("$page is not a pending comment");
1010 return match_glob
($page, "$glob/*", internal
=> 1, @_);