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 $commentauthorurl = IkiWiki::cgiurl(
211 page => IkiWiki::userpage($commentuser)
214 $commentauthor = $commentuser;
218 if (defined $params{ip}) {
219 $commentip = $params{ip};
221 $commentauthor = gettext("Anonymous");
224 $commentstate{$page}{commentuser} = $commentuser;
225 $commentstate{$page}{commentopenid} = $commentopenid;
226 $commentstate{$page}{commentip} = $commentip;
227 $commentstate{$page}{commentauthor} = $commentauthor;
228 $commentstate{$page}{commentauthorurl} = $commentauthorurl;
229 $commentstate{$page}{commentauthoravatar} = $params{avatar};
230 if (! defined $pagestate{$page}{meta}{author}) {
231 $pagestate{$page}{meta}{author} = $commentauthor;
233 if (! defined $pagestate{$page}{meta}{authorurl}) {
234 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
237 if ($config{comments_allowauthor}) {
238 if (defined $params{claimedauthor}) {
239 $pagestate{$page}{meta}{author} = $params{claimedauthor};
242 if (defined $params{url}) {
243 my $url=$params{url};
245 eval q{use URI::Heuristic};
247 $url=URI::Heuristic::uf_uristr($url);
251 $pagestate{$page}{meta}{authorurl} = $url;
256 $pagestate{$page}{meta}{author} = $commentauthor;
257 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
260 if (defined $params{subject}) {
261 # decode title the same way meta does
262 eval q{use HTML::Entities};
263 $pagestate{$page}{meta}{title} = decode_entities($params{subject});
266 if ($params{page} =~ m/\/\Q$config{comments_pagename}\E\d+_/) {
267 $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page})).
268 "#".page_to_id($params{page});
271 eval q{use Date::Parse};
273 my $time = str2time($params{date});
274 $IkiWiki::pagectime{$page} = $time if defined $time;
280 sub preprocess_moderation {
283 $params{desc}=gettext("Comment Moderation")
284 unless defined $params{desc};
286 if (length $config{cgiurl}) {
288 IkiWiki::cgiurl(do => 'commentmoderation').
289 '">'.$params{desc}.'</a>';
292 return $params{desc};
296 sub sessioncgi ($$) {
300 my $do = $cgi->param('do');
301 if ($do eq 'comment') {
302 editcomment($cgi, $session);
304 elsif ($do eq 'commentmoderation') {
305 commentmoderation($cgi, $session);
307 elsif ($do eq 'commentsignin') {
308 IkiWiki::cgi_signin($cgi, $session);
313 # Mostly cargo-culted from IkiWiki::plugin::editpage
314 sub editcomment ($$) {
318 IkiWiki::decode_cgi_utf8($cgi);
320 eval q{use CGI::FormBuilder};
323 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
324 my $form = CGI::FormBuilder->new(
325 fields => [qw{do sid page subject editcontent type author
326 email url subscribe anonsubscribe}],
329 required => [qw{editcontent}],
332 action => IkiWiki::cgiurl(),
335 template => { template('editcomment.tmpl') },
338 IkiWiki::decode_form_utf8($form);
339 IkiWiki::run_hooks(formbuilder_setup => sub {
340 shift->(title => "comment", form => $form, cgi => $cgi,
341 session => $session, buttons => \@buttons);
343 IkiWiki::decode_form_utf8($form);
345 my $type = $form->param('type');
346 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
347 $type = IkiWiki::possibly_foolish_untaint($type);
350 $type = $config{default_pageext};
355 if (exists $IkiWiki::hooks{htmlize}) {
356 foreach my $key (grep { !/^_/ && isallowed($_) } keys %{$IkiWiki::hooks{htmlize}}) {
357 push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
360 @page_types=sort @page_types;
362 $form->field(name => 'do', type => 'hidden');
363 $form->field(name => 'sid', type => 'hidden', value => $session->id,
365 $form->field(name => 'page', type => 'hidden');
366 $form->field(name => 'subject', type => 'text', size => 72);
367 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
368 $form->field(name => "type", value => $type, force => 1,
369 type => 'select', options => \@page_types);
371 my $username=$session->param('name');
372 $form->tmpl_param(username => $username);
374 $form->field(name => "subscribe", type => 'hidden');
375 $form->field(name => "anonsubscribe", type => 'hidden');
376 if (IkiWiki::Plugin::notifyemail->can("subscribe")) {
377 if (defined $username) {
378 $form->field(name => "subscribe", type => "checkbox",
379 options => [gettext("email replies to me")]);
381 elsif (IkiWiki::Plugin::passwordauth->can("anonuser")) {
382 $form->field(name => "anonsubscribe", type => "checkbox",
383 options => [gettext("email replies to me")]);
387 if ($config{comments_allowauthor} and
388 ! defined $session->param('name')) {
389 $form->tmpl_param(allowauthor => 1);
390 $form->field(name => 'author', type => 'text', size => '40');
391 $form->field(name => 'email', type => 'text', size => '40');
392 $form->field(name => 'url', type => 'text', size => '40');
395 $form->tmpl_param(allowauthor => 0);
396 $form->field(name => 'author', type => 'hidden', value => '',
398 $form->field(name => 'email', type => 'hidden', value => '',
400 $form->field(name => 'url', type => 'hidden', value => '',
404 if (! defined $session->param('name')) {
405 # Make signinurl work and return here.
406 $form->tmpl_param(signinurl => IkiWiki::cgiurl(do => 'commentsignin'));
407 $session->param(postsignin => $ENV{QUERY_STRING});
408 IkiWiki::cgi_savesession($session);
411 # The untaint is OK (as in editpage) because we're about to pass
412 # it to file_pruned and wiki_file_regexp anyway.
413 my ($page) = $form->field('page')=~/$config{wiki_file_regexp}/;
414 $page = IkiWiki::possibly_foolish_untaint($page);
415 if (! defined $page || ! length $page ||
416 IkiWiki::file_pruned($page)) {
417 error(gettext("bad page name"));
420 $form->title(sprintf(gettext("commenting on %s"),
421 IkiWiki::pagetitle(IkiWiki::basename($page))));
423 $form->tmpl_param('helponformattinglink',
424 htmllink($page, $page, 'ikiwiki/formatting',
426 linktext => 'FormattingHelp'),
427 allowdirectives => $config{allow_directives});
429 if ($form->submitted eq CANCEL) {
430 # bounce back to the page they wanted to comment on, and exit.
431 IkiWiki::redirect($cgi, urlto($page));
435 if (not exists $pagesources{$page}) {
436 error(sprintf(gettext(
437 "page '%s' doesn't exist, so you can't comment"),
441 # There's no UI to get here, but someone might construct the URL,
442 # leading to a comment that exists in the repository but isn't
444 if (!pagespec_match($page, $config{comments_pagespec},
445 location => $page)) {
446 error(sprintf(gettext(
447 "comments on page '%s' are not allowed"),
451 if (pagespec_match($page, $config{comments_closed_pagespec},
452 location => $page)) {
453 error(sprintf(gettext(
454 "comments on page '%s' are closed"),
458 # Set a flag to indicate that we're posting a comment,
459 # so that postcomment() can tell it should match.
461 IkiWiki::check_canedit($page, $cgi, $session);
464 my $content = "[[!comment format=$type\n";
466 if (defined $session->param('name')) {
467 my $username = $session->param('name');
468 $username =~ s/"/"/g;
469 $content .= " username=\"$username\"\n";
472 if (defined $session->param('nickname')) {
473 my $nickname = $session->param('nickname');
474 $nickname =~ s/"/"/g;
475 $content .= " nickname=\"$nickname\"\n";
478 if (!(defined $session->param('name') || defined $session->param('nickname')) &&
479 defined $session->remote_addr()) {
480 $content .= " ip=\"".$session->remote_addr()."\"\n";
483 if ($config{comments_allowauthor}) {
484 my $author = $form->field('author');
485 if (defined $author && length $author) {
486 $author =~ s/"/"/g;
487 $content .= " claimedauthor=\"$author\"\n";
489 my $url = $form->field('url');
490 if (defined $url && length $url) {
491 $url =~ s/"/"/g;
492 $content .= " url=\"$url\"\n";
496 my $avatar=getavatar($session->param('name'));
497 if (defined $avatar && length $avatar) {
498 $avatar =~ s/"/"/g;
499 $content .= " avatar=\"$avatar\"\n";
502 my $subject = $form->field('subject');
503 if (defined $subject && length $subject) {
504 $subject =~ s/"/"/g;
507 $subject = "comment ".(num_comments($page, $config{srcdir}) + 1);
509 $content .= " subject=\"$subject\"\n";
510 $content .= " " . commentdate() . "\n";
512 my $editcontent = $form->field('editcontent');
513 $editcontent="" if ! defined $editcontent;
514 $editcontent =~ s/\r\n/\n/g;
515 $editcontent =~ s/\r/\n/g;
516 $editcontent =~ s/"/\\"/g;
517 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
519 my $location=unique_comment_location($page, $content, $config{srcdir});
521 # This is essentially a simplified version of editpage:
522 # - the user does not control the page that's created, only the parent
523 # - it's always a create operation, never an edit
524 # - this means that conflicts should never happen
525 # - this means that if they do, rocks fall and everyone dies
527 if ($form->submitted eq PREVIEW) {
528 my $preview=previewcomment($content, $location, $page, time);
529 IkiWiki::run_hooks(format => sub {
530 $preview = shift->(page => $page,
531 content => $preview);
533 $form->tmpl_param(page_preview => $preview);
536 $form->tmpl_param(page_preview => "");
539 if ($form->submitted eq POST_COMMENT && $form->validate) {
540 IkiWiki::checksessionexpiry($cgi, $session);
542 if (IkiWiki::Plugin::notifyemail->can("subscribe")) {
543 my $subspec="comment($page)";
544 if (defined $username &&
545 length $form->field("subscribe")) {
546 IkiWiki::Plugin::notifyemail::subscribe(
547 $username, $subspec);
549 elsif (length $form->field("email") &&
550 length $form->field("anonsubscribe")) {
551 IkiWiki::Plugin::notifyemail::anonsubscribe(
552 $form->field("email"), $subspec);
557 my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
558 subject => $form->field('subject'),
559 $config{comments_allowauthor} ? (
560 author => $form->field('author'),
561 url => $form->field('url'),
571 $location=unique_comment_location($page, $content, $IkiWiki::Plugin::transient::transientdir, "._comment_pending");
572 writefile("$location._comment_pending", $IkiWiki::Plugin::transient::transientdir, $content);
574 # Refresh so anything that deals with pending
575 # comments can be updated.
576 require IkiWiki::Render;
578 IkiWiki::saveindex();
580 IkiWiki::printheader($session);
581 print IkiWiki::cgitemplate($cgi, gettext(gettext("comment stored for moderation")),
583 gettext("Your comment will be posted after moderator review").
588 # FIXME: could probably do some sort of graceful retry
589 # on error? Would require significant unwinding though
590 my $file = "$location._comment";
591 writefile($file, $config{srcdir}, $content);
595 if ($config{rcs} and $config{comments_commit}) {
596 my $message = gettext("Added a comment");
597 if (defined $form->field('subject') &&
598 length $form->field('subject')) {
600 gettext("Added a comment: %s"),
601 $form->field('subject'));
604 IkiWiki::rcs_add($file);
605 IkiWiki::disable_commit_hook();
606 $conflict = IkiWiki::rcs_commit_staged(
610 IkiWiki::enable_commit_hook();
611 IkiWiki::rcs_update();
614 # Now we need a refresh
615 require IkiWiki::Render;
617 IkiWiki::saveindex();
619 # this should never happen, unless a committer deliberately
620 # breaks it or something
621 error($conflict) if defined $conflict;
623 # Jump to the new comment on the page.
624 # The trailing question mark tries to avoid broken
625 # caches and get the most recent version of the page.
626 IkiWiki::redirect($cgi, urlto($page).
627 "?updated#".page_to_id($location));
631 IkiWiki::showform($form, \@buttons, $session, $cgi,
639 "date=\"" . strftime_utf8('%Y-%m-%dT%H:%M:%SZ', gmtime) . "\"";
644 return undef unless defined $user;
647 eval q{use Libravatar::URL};
649 my $oiduser = eval { IkiWiki::openiduser($user) };
650 my $https=defined $config{url} && $config{url}=~/^https:/;
652 if (defined $oiduser) {
654 $avatar = libravatar_url(openid => $user, https => $https);
657 if (! defined $avatar &&
658 (my $email = IkiWiki::userinfo_get($user, 'email'))) {
660 $avatar = libravatar_url(email => $email, https => $https);
668 sub commentmoderation ($$) {
672 IkiWiki::needsignin($cgi, $session);
673 if (! IkiWiki::is_admin($session->param("name"))) {
674 error(gettext("you are not logged in as an admin"));
677 IkiWiki::decode_cgi_utf8($cgi);
679 if (defined $cgi->param('sid')) {
680 IkiWiki::checksessionexpiry($cgi, $session);
682 my $rejectalldefer=$cgi->param('rejectalldefer');
686 foreach my $id (keys %vars) {
687 if ($id =~ /(.*)\._comment(?:_pending)?$/) {
688 $id=decode_utf8($id);
689 my $action=$cgi->param($id);
690 next if $action eq 'Defer' && ! $rejectalldefer;
692 # Make sure that the id is of a legal
694 my ($f) = $id =~ /$config{wiki_file_regexp}/;
695 if (! defined $f || ! length $f ||
696 IkiWiki::file_pruned($f)) {
697 error("illegal file");
700 my $page=IkiWiki::dirname($f);
701 my $filedir=$IkiWiki::Plugin::transient::transientdir;
702 my $file="$filedir/$f";
705 $file="$config{srcdir}/$f";
706 $filedir=$config{srcdir};
709 $file="$config{wikistatedir}/comments_pending/".$f;
710 $filedir="$config{wikistatedir}/comments_pending";
714 if ($action eq 'Accept') {
715 my $content=eval { readfile($file) };
716 next if $@; # file vanished since form was displayed
717 my $dest=unique_comment_location($page, $content, $config{srcdir})."._comment";
718 writefile($dest, $config{srcdir}, $content);
719 if ($config{rcs} and $config{comments_commit}) {
720 IkiWiki::rcs_add($dest);
725 require IkiWiki::Render;
726 IkiWiki::prune($file, $filedir);
732 if ($config{rcs} and $config{comments_commit}) {
733 my $message = gettext("Comment moderation");
734 IkiWiki::disable_commit_hook();
735 $conflict=IkiWiki::rcs_commit_staged(
739 IkiWiki::enable_commit_hook();
740 IkiWiki::rcs_update();
743 # Now we need a refresh
744 require IkiWiki::Render;
746 IkiWiki::saveindex();
748 error($conflict) if defined $conflict;
753 my ($id, $dir, $ctime)=@{$_};
754 my $content=readfile("$dir/$id");
755 my $preview=previewcomment($content, $id,
761 } sort { $b->[2] <=> $a->[2] } comments_pending();
763 my $template=template("commentmoderation.tmpl");
766 comments => \@comments,
767 cgiurl => IkiWiki::cgiurl(),
769 IkiWiki::printheader($session);
770 my $out=$template->output;
771 IkiWiki::run_hooks(format => sub {
772 $out = shift->(page => "", content => $out);
774 print IkiWiki::cgitemplate($cgi, gettext("comment moderation"), $out);
778 sub formbuilder_setup (@) {
781 my $form=$params{form};
782 if ($form->title eq "preferences" &&
783 IkiWiki::is_admin($params{session}->param("name"))) {
784 push @{$params{buttons}}, "Comment Moderation";
785 if ($form->submitted && $form->submitted eq "Comment Moderation") {
786 commentmoderation($params{cgi}, $params{session});
791 sub comments_pending () {
794 eval q{use File::Find};
798 my $origdir=getcwd();
800 my $find_comments=sub {
803 return unless -d $dir;
805 chdir($dir) || die "chdir $dir: $!";
810 my $file=decode_utf8($_);
812 return if ! length $file || IkiWiki::file_pruned($file)
813 || -l $_ || -d _ || $file !~ /\Q$extension\E$/;
814 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
816 my $ctime=(stat($_))[10];
817 push @ret, [$f, $dir, $ctime];
822 chdir($origdir) || die "chdir $origdir: $!";
825 $find_comments->($IkiWiki::Plugin::transient::transientdir, "._comment_pending");
827 $find_comments->($config{srcdir}, "._comment_pending");
829 $find_comments->("$config{wikistatedir}/comments_pending/",
835 sub previewcomment ($$$) {
841 # Previewing a comment should implicitly enable comment posting mode.
842 my $oldpostcomment=$postcomment;
845 my $preview = IkiWiki::htmlize($location, $page, '_comment',
846 IkiWiki::linkify($location, $page,
847 IkiWiki::preprocess($location, $page,
848 IkiWiki::filter($location, $page, $content), 0, 1)));
850 my $template = template("comment.tmpl");
851 $template->param(content => $preview);
852 $template->param(ctime => displaytime($time, undef, 1));
853 $template->param(html5 => $config{html5});
855 IkiWiki::run_hooks(pagetemplate => sub {
856 shift->(page => $location,
858 template => $template);
861 $template->param(have_actions => 0);
863 $postcomment=$oldpostcomment;
865 return $template->output;
868 sub commentsshown ($) {
871 return pagespec_match($page, $config{comments_pagespec},
875 sub commentsopen ($) {
878 return length $config{cgiurl} > 0 &&
879 (! length $config{comments_closed_pagespec} ||
880 ! pagespec_match($page, $config{comments_closed_pagespec},
884 sub pagetemplate (@) {
887 my $page = $params{page};
888 my $template = $params{template};
889 my $shown = ($template->query(name => 'commentslink') ||
890 $template->query(name => 'commentsurl') ||
891 $template->query(name => 'atomcommentsurl') ||
892 $template->query(name => 'comments')) &&
893 commentsshown($page);
895 if ($template->query(name => 'comments')) {
896 my $comments = undef;
898 $comments = IkiWiki::preprocess_inline(
899 pages => "comment($page) and !comment($page/*)",
900 template => 'comment',
904 destpage => $params{destpage},
905 feedfile => 'comments',
910 if (defined $comments && length $comments) {
911 $template->param(comments => $comments);
914 if ($shown && commentsopen($page)) {
915 $template->param(addcommenturl => addcommenturl($page));
920 if ($template->query(name => 'commentsurl')) {
921 $template->param(commentsurl =>
922 urlto($page).'#comments');
925 if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
926 # This will 404 until there are some comments, but I
927 # think that's probably OK...
928 $template->param(atomcommentsurl =>
929 urlto($page).'comments.atom');
932 if ($template->query(name => 'commentslink')) {
933 my $num=num_comments($page, $config{srcdir});
936 $link = htmllink($page, $params{destpage}, $page,
937 linktext => sprintf(ngettext("%i comment", "%i comments", $num), $num),
938 anchor => "comments",
942 elsif (commentsopen($page)) {
943 $link = "<a href=\"".addcommenturl($page)."\">".
944 #translators: Here "Comment" is a verb;
945 #translators: the user clicks on it to
946 #translators: post a comment.
950 $template->param(commentslink => $link)
955 # everything below this point is only relevant to the comments
957 if (!exists $commentstate{$page}) {
961 if ($template->query(name => 'commentid')) {
962 $template->param(commentid => page_to_id($page));
965 if ($template->query(name => 'commentuser')) {
966 $template->param(commentuser =>
967 $commentstate{$page}{commentuser});
970 if ($template->query(name => 'commentopenid')) {
971 $template->param(commentopenid =>
972 $commentstate{$page}{commentopenid});
975 if ($template->query(name => 'commentip')) {
976 $template->param(commentip =>
977 $commentstate{$page}{commentip});
980 if ($template->query(name => 'commentauthor')) {
981 $template->param(commentauthor =>
982 $commentstate{$page}{commentauthor});
985 if ($template->query(name => 'commentauthorurl')) {
986 $template->param(commentauthorurl =>
987 $commentstate{$page}{commentauthorurl});
990 if ($template->query(name => 'commentauthoravatar')) {
991 $template->param(commentauthoravatar =>
992 $commentstate{$page}{commentauthoravatar});
995 if ($template->query(name => 'removeurl') &&
996 IkiWiki::Plugin::remove->can("check_canremove") &&
997 length $config{cgiurl}) {
998 $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
1000 $template->param(have_actions => 1);
1004 sub addcommenturl ($) {
1007 return IkiWiki::cgiurl(do => 'comment', page => $page);
1010 sub num_comments ($$) {
1014 my @comments=glob("$dir/$page/$config{comments_pagename}*._comment");
1015 return int @comments;
1018 sub unique_comment_location ($$$;$) {
1020 eval q{use Digest::MD5 'md5_hex'};
1022 my $content_md5=md5_hex(Encode::encode_utf8(shift));
1024 my $ext=shift || "._comment";
1027 my $i = num_comments($page, $dir);
1030 $location = "$page/$config{comments_pagename}${i}_${content_md5}";
1031 } while (-e "$dir/$location$ext");
1036 sub page_to_id ($) {
1037 # Converts a comment page name into a unique, legal html id
1038 # attribute value, that can be used as an anchor to link to the
1042 eval q{use Digest::MD5 'md5_hex'};
1045 return "comment-".md5_hex(Encode::encode_utf8(($page)));
1048 package IkiWiki::PageSpec;
1050 sub match_postcomment ($$;@) {
1054 if (! $postcomment) {
1055 return IkiWiki::FailReason->new("not posting a comment");
1057 return match_glob($page, $glob, @_);
1060 sub match_comment ($$;@) {
1064 if (! $postcomment) {
1065 # To see if it's a comment, check the source file type.
1066 # Deal with comments that were just deleted.
1067 my $source=exists $IkiWiki::pagesources{$page} ?
1068 $IkiWiki::pagesources{$page} :
1069 $IkiWiki::delpagesources{$page};
1070 my $type=defined $source ? IkiWiki::pagetype($source) : undef;
1071 if (! defined $type || $type ne "_comment") {
1072 return IkiWiki::FailReason->new("$page is not a comment");
1076 return match_glob($page, "$glob/*", internal => 1, @_);
1079 sub match_comment_pending ($$;@) {
1083 my $source=exists $IkiWiki::pagesources{$page} ?
1084 $IkiWiki::pagesources{$page} :
1085 $IkiWiki::delpagesources{$page};
1086 my $type=defined $source ? IkiWiki::pagetype($source) : undef;
1087 if (! defined $type || $type ne "_comment_pending") {
1088 return IkiWiki::FailReason->new("$page is not a pending comment");
1091 return match_glob($page, "$glob/*", internal => 1, @_);