Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki.git] / IkiWiki / Plugin / comments.pm
blob8d46ed57983e6a0d0e7cac3c568a6cb368ec34a6
1 #!/usr/bin/perl
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;
8 use warnings;
9 use strict;
10 use IkiWiki 3.00;
11 use Encode;
12 use POSIX qw(strftime);
14 use constant PREVIEW => "Preview";
15 use constant POST_COMMENT => "Post comment";
16 use constant CANCEL => "Cancel";
18 my $postcomment;
19 my %commentstate;
21 sub import {
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");
40 sub getsetup () {
41 return
42 plugin => {
43 safe => 1,
44 rebuild => 1,
45 section => "web",
47 comments_pagespec => {
48 type => 'pagespec',
49 example => 'blog/* and !*/Discussion',
50 description => 'PageSpec of pages where comments are allowed',
51 link => 'ikiwiki/PageSpec',
52 safe => 1,
53 rebuild => 1,
55 comments_closed_pagespec => {
56 type => 'pagespec',
57 example => 'blog/controversial or blog/flamewar',
58 description => 'PageSpec of pages where posting new comments is not allowed',
59 link => 'ikiwiki/PageSpec',
60 safe => 1,
61 rebuild => 1,
63 comments_pagename => {
64 type => 'string',
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
68 rebuild => undef,
70 comments_allowdirectives => {
71 type => 'boolean',
72 example => 0,
73 description => 'Interpret directives in comments?',
74 safe => 1,
75 rebuild => 0,
77 comments_allowauthor => {
78 type => 'boolean',
79 example => 0,
80 description => 'Allow anonymous commenters to set an author name?',
81 safe => 1,
82 rebuild => 0,
84 comments_commit => {
85 type => 'boolean',
86 example => 1,
87 description => 'commit comments to the VCS',
88 # old uncommitted comments are likely to cause
89 # confusion if this is changed
90 safe => 0,
91 rebuild => 0,
95 sub checkconfig () {
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};
106 sub htmlize {
107 my %params = @_;
108 return $params{content};
111 sub htmlize_pending {
112 my %params = @_;
113 return sprintf(gettext("this comment needs %s"),
114 '<a href="'.
115 IkiWiki::cgiurl(do => "commentmoderation").'">'.
116 gettext("moderation").'</a>');
119 # FIXME: copied verbatim from meta
120 sub safeurl ($) {
121 my $url=shift;
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/;
126 else {
127 return 1;
131 sub preprocess {
132 my %params = @_;
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},
148 $content);
151 # no need to bother with htmlize if it's just HTML
152 $content = IkiWiki::htmlize($page, $params{destpage}, $format, $content)
153 if defined $format;
155 IkiWiki::run_hooks(sanitize => sub {
156 $content = shift->(
157 page => $page,
158 destpage => $params{destpage},
159 content => $content,
163 # set metadata, possibly overriding [[!meta]] directives from the
164 # comment itself
166 my $commentuser;
167 my $commentip;
168 my $commentauthor;
169 my $commentauthorurl;
170 my $commentopenid;
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;
182 else {
183 $commentauthorurl = IkiWiki::cgiurl(
184 do => 'goto',
185 page => IkiWiki::userpage($commentuser)
188 $commentauthor = $commentuser;
191 else {
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 if (! defined $pagestate{$page}{meta}{author}) {
204 $pagestate{$page}{meta}{author} = $commentauthor;
206 if (! defined $pagestate{$page}{meta}{authorurl}) {
207 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
210 if ($config{comments_allowauthor}) {
211 if (defined $params{claimedauthor}) {
212 $pagestate{$page}{meta}{author} = $params{claimedauthor};
215 if (defined $params{url}) {
216 my $url=$params{url};
218 eval q{use URI::Heuristic};
219 if (! $@) {
220 $url=URI::Heuristic::uf_uristr($url);
223 if (safeurl($url)) {
224 $pagestate{$page}{meta}{authorurl} = $url;
228 else {
229 $pagestate{$page}{meta}{author} = $commentauthor;
230 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
233 if (defined $params{subject}) {
234 # decode title the same way meta does
235 eval q{use HTML::Entities};
236 $pagestate{$page}{meta}{title} = decode_entities($params{subject});
239 if ($params{page} =~ m/\/\Q$config{comments_pagename}\E\d+_/) {
240 $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page})).
241 "#".page_to_id($params{page});
244 eval q{use Date::Parse};
245 if (! $@) {
246 my $time = str2time($params{date});
247 $IkiWiki::pagectime{$page} = $time if defined $time;
250 return $content;
253 sub preprocess_moderation {
254 my %params = @_;
256 $params{desc}=gettext("Comment Moderation")
257 unless defined $params{desc};
259 if (length $config{cgiurl}) {
260 return '<a href="'.
261 IkiWiki::cgiurl(do => 'commentmoderation').
262 '">'.$params{desc}.'</a>';
264 else {
265 return $params{desc};
269 sub sessioncgi ($$) {
270 my $cgi=shift;
271 my $session=shift;
273 my $do = $cgi->param('do');
274 if ($do eq 'comment') {
275 editcomment($cgi, $session);
277 elsif ($do eq 'commentmoderation') {
278 commentmoderation($cgi, $session);
280 elsif ($do eq 'commentsignin') {
281 IkiWiki::cgi_signin($cgi, $session);
282 exit;
286 # Mostly cargo-culted from IkiWiki::plugin::editpage
287 sub editcomment ($$) {
288 my $cgi=shift;
289 my $session=shift;
291 IkiWiki::decode_cgi_utf8($cgi);
293 eval q{use CGI::FormBuilder};
294 error($@) if $@;
296 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
297 my $form = CGI::FormBuilder->new(
298 fields => [qw{do sid page subject editcontent type author url}],
299 charset => 'utf-8',
300 method => 'POST',
301 required => [qw{editcontent}],
302 javascript => 0,
303 params => $cgi,
304 action => IkiWiki::cgiurl(),
305 header => 0,
306 table => 0,
307 template => { template('editcomment.tmpl') },
310 IkiWiki::decode_form_utf8($form);
311 IkiWiki::run_hooks(formbuilder_setup => sub {
312 shift->(title => "comment", form => $form, cgi => $cgi,
313 session => $session, buttons => \@buttons);
315 IkiWiki::decode_form_utf8($form);
317 my $type = $form->param('type');
318 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
319 $type = IkiWiki::possibly_foolish_untaint($type);
321 else {
322 $type = $config{default_pageext};
326 my @page_types;
327 if (exists $IkiWiki::hooks{htmlize}) {
328 foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
329 push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
332 @page_types=sort @page_types;
334 $form->field(name => 'do', type => 'hidden');
335 $form->field(name => 'sid', type => 'hidden', value => $session->id,
336 force => 1);
337 $form->field(name => 'page', type => 'hidden');
338 $form->field(name => 'subject', type => 'text', size => 72);
339 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
340 $form->field(name => "type", value => $type, force => 1,
341 type => 'select', options => \@page_types);
343 $form->tmpl_param(username => $session->param('name'));
345 if ($config{comments_allowauthor} and
346 ! defined $session->param('name')) {
347 $form->tmpl_param(allowauthor => 1);
348 $form->field(name => 'author', type => 'text', size => '40');
349 $form->field(name => 'url', type => 'text', size => '40');
351 else {
352 $form->tmpl_param(allowauthor => 0);
353 $form->field(name => 'author', type => 'hidden', value => '',
354 force => 1);
355 $form->field(name => 'url', type => 'hidden', value => '',
356 force => 1);
359 if (! defined $session->param('name')) {
360 # Make signinurl work and return here.
361 $form->tmpl_param(signinurl => IkiWiki::cgiurl(do => 'commentsignin'));
362 $session->param(postsignin => $ENV{QUERY_STRING});
363 IkiWiki::cgi_savesession($session);
366 # The untaint is OK (as in editpage) because we're about to pass
367 # it to file_pruned and wiki_file_regexp anyway.
368 my ($page) = $form->field('page')=~/$config{wiki_file_regexp}/;
369 $page = IkiWiki::possibly_foolish_untaint($page);
370 if (! defined $page || ! length $page ||
371 IkiWiki::file_pruned($page)) {
372 error(gettext("bad page name"));
375 $form->title(sprintf(gettext("commenting on %s"),
376 IkiWiki::pagetitle(IkiWiki::basename($page))));
378 $form->tmpl_param('helponformattinglink',
379 htmllink($page, $page, 'ikiwiki/formatting',
380 noimageinline => 1,
381 linktext => 'FormattingHelp'),
382 allowdirectives => $config{allow_directives});
384 if ($form->submitted eq CANCEL) {
385 # bounce back to the page they wanted to comment on, and exit.
386 IkiWiki::redirect($cgi, urlto($page));
387 exit;
390 if (not exists $pagesources{$page}) {
391 error(sprintf(gettext(
392 "page '%s' doesn't exist, so you can't comment"),
393 $page));
396 if (pagespec_match($page, $config{comments_closed_pagespec},
397 location => $page)) {
398 error(sprintf(gettext(
399 "comments on page '%s' are closed"),
400 $page));
403 # Set a flag to indicate that we're posting a comment,
404 # so that postcomment() can tell it should match.
405 $postcomment=1;
406 IkiWiki::check_canedit($page, $cgi, $session);
407 $postcomment=0;
409 my $content = "[[!comment format=$type\n";
411 if (defined $session->param('name')) {
412 my $username = $session->param('name');
413 $username =~ s/"/&quot;/g;
414 $content .= " username=\"$username\"\n";
416 if (defined $session->param('nickname')) {
417 my $nickname = $session->param('nickname');
418 $nickname =~ s/"/&quot;/g;
419 $content .= " nickname=\"$nickname\"\n";
421 elsif (defined $session->remote_addr()) {
422 my $ip = $session->remote_addr();
423 if ($ip =~ m/^([.0-9]+)$/) {
424 $content .= " ip=\"$1\"\n";
428 if ($config{comments_allowauthor}) {
429 my $author = $form->field('author');
430 if (defined $author && length $author) {
431 $author =~ s/"/&quot;/g;
432 $content .= " claimedauthor=\"$author\"\n";
434 my $url = $form->field('url');
435 if (defined $url && length $url) {
436 $url =~ s/"/&quot;/g;
437 $content .= " url=\"$url\"\n";
441 my $subject = $form->field('subject');
442 if (defined $subject && length $subject) {
443 $subject =~ s/"/&quot;/g;
445 else {
446 $subject = "comment ".(num_comments($page, $config{srcdir}) + 1);
448 $content .= " subject=\"$subject\"\n";
450 $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
452 my $editcontent = $form->field('editcontent');
453 $editcontent="" if ! defined $editcontent;
454 $editcontent =~ s/\r\n/\n/g;
455 $editcontent =~ s/\r/\n/g;
456 $editcontent =~ s/"/\\"/g;
457 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
459 my $location=unique_comment_location($page, $content, $config{srcdir});
461 # This is essentially a simplified version of editpage:
462 # - the user does not control the page that's created, only the parent
463 # - it's always a create operation, never an edit
464 # - this means that conflicts should never happen
465 # - this means that if they do, rocks fall and everyone dies
467 if ($form->submitted eq PREVIEW) {
468 my $preview=previewcomment($content, $location, $page, time);
469 IkiWiki::run_hooks(format => sub {
470 $preview = shift->(page => $page,
471 content => $preview);
473 $form->tmpl_param(page_preview => $preview);
475 else {
476 $form->tmpl_param(page_preview => "");
479 if ($form->submitted eq POST_COMMENT && $form->validate) {
480 IkiWiki::checksessionexpiry($cgi, $session);
482 $postcomment=1;
483 my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
484 subject => $form->field('subject'),
485 $config{comments_allowauthor} ? (
486 author => $form->field('author'),
487 url => $form->field('url'),
488 ) : (),
489 page => $location,
490 cgi => $cgi,
491 session => $session,
492 nonfatal => 1,
494 $postcomment=0;
496 if (! $ok) {
497 $location=unique_comment_location($page, $content, $config{srcdir}, "._comment_pending");
498 writefile("$location._comment_pending", $config{srcdir}, $content);
500 # Refresh so anything that deals with pending
501 # comments can be updated.
502 require IkiWiki::Render;
503 IkiWiki::refresh();
504 IkiWiki::saveindex();
506 IkiWiki::printheader($session);
507 print IkiWiki::cgitemplate($cgi, gettext(gettext("comment stored for moderation")),
508 "<p>".
509 gettext("Your comment will be posted after moderator review").
510 "</p>");
511 exit;
514 # FIXME: could probably do some sort of graceful retry
515 # on error? Would require significant unwinding though
516 my $file = "$location._comment";
517 writefile($file, $config{srcdir}, $content);
519 my $conflict;
521 if ($config{rcs} and $config{comments_commit}) {
522 my $message = gettext("Added a comment");
523 if (defined $form->field('subject') &&
524 length $form->field('subject')) {
525 $message = sprintf(
526 gettext("Added a comment: %s"),
527 $form->field('subject'));
530 IkiWiki::rcs_add($file);
531 IkiWiki::disable_commit_hook();
532 $conflict = IkiWiki::rcs_commit_staged(
533 message => $message,
534 session => $session,
536 IkiWiki::enable_commit_hook();
537 IkiWiki::rcs_update();
540 # Now we need a refresh
541 require IkiWiki::Render;
542 IkiWiki::refresh();
543 IkiWiki::saveindex();
545 # this should never happen, unless a committer deliberately
546 # breaks it or something
547 error($conflict) if defined $conflict;
549 # Jump to the new comment on the page.
550 # The trailing question mark tries to avoid broken
551 # caches and get the most recent version of the page.
552 IkiWiki::redirect($cgi, urlto($page).
553 "?updated#".page_to_id($location));
556 else {
557 IkiWiki::showform($form, \@buttons, $session, $cgi,
558 page => $page);
561 exit;
564 sub commentmoderation ($$) {
565 my $cgi=shift;
566 my $session=shift;
568 IkiWiki::needsignin($cgi, $session);
569 if (! IkiWiki::is_admin($session->param("name"))) {
570 error(gettext("you are not logged in as an admin"));
573 IkiWiki::decode_cgi_utf8($cgi);
575 if (defined $cgi->param('sid')) {
576 IkiWiki::checksessionexpiry($cgi, $session);
578 my $rejectalldefer=$cgi->param('rejectalldefer');
580 my %vars=$cgi->Vars;
581 my $added=0;
582 foreach my $id (keys %vars) {
583 if ($id =~ /(.*)\._comment(?:_pending)?$/) {
584 $id=decode_utf8($id);
585 my $action=$cgi->param($id);
586 next if $action eq 'Defer' && ! $rejectalldefer;
588 # Make sure that the id is of a legal
589 # pending comment.
590 my ($f) = $id =~ /$config{wiki_file_regexp}/;
591 if (! defined $f || ! length $f ||
592 IkiWiki::file_pruned($f)) {
593 error("illegal file");
596 my $page=IkiWiki::dirname($f);
597 my $file="$config{srcdir}/$f";
598 if (! -e $file) {
599 # old location
600 $file="$config{wikistatedir}/comments_pending/".$f;
603 if ($action eq 'Accept') {
604 my $content=eval { readfile($file) };
605 next if $@; # file vanished since form was displayed
606 my $dest=unique_comment_location($page, $content, $config{srcdir})."._comment";
607 writefile($dest, $config{srcdir}, $content);
608 if ($config{rcs} and $config{comments_commit}) {
609 IkiWiki::rcs_add($dest);
611 $added++;
614 require IkiWiki::Render;
615 IkiWiki::prune($file);
619 if ($added) {
620 my $conflict;
621 if ($config{rcs} and $config{comments_commit}) {
622 my $message = gettext("Comment moderation");
623 IkiWiki::disable_commit_hook();
624 $conflict=IkiWiki::rcs_commit_staged(
625 message => $message,
626 session => $session,
628 IkiWiki::enable_commit_hook();
629 IkiWiki::rcs_update();
632 # Now we need a refresh
633 require IkiWiki::Render;
634 IkiWiki::refresh();
635 IkiWiki::saveindex();
637 error($conflict) if defined $conflict;
641 my @comments=map {
642 my ($id, $dir, $ctime)=@{$_};
643 my $content=readfile("$dir/$id");
644 my $preview=previewcomment($content, $id,
645 $id, $ctime);
647 id => $id,
648 view => $preview,
650 } sort { $b->[2] <=> $a->[2] } comments_pending();
652 my $template=template("commentmoderation.tmpl");
653 $template->param(
654 sid => $session->id,
655 comments => \@comments,
656 cgiurl => IkiWiki::cgiurl(),
658 IkiWiki::printheader($session);
659 my $out=$template->output;
660 IkiWiki::run_hooks(format => sub {
661 $out = shift->(page => "", content => $out);
663 print IkiWiki::cgitemplate($cgi, gettext("comment moderation"), $out);
664 exit;
667 sub formbuilder_setup (@) {
668 my %params=@_;
670 my $form=$params{form};
671 if ($form->title eq "preferences" &&
672 IkiWiki::is_admin($params{session}->param("name"))) {
673 push @{$params{buttons}}, "Comment Moderation";
674 if ($form->submitted && $form->submitted eq "Comment Moderation") {
675 commentmoderation($params{cgi}, $params{session});
680 sub comments_pending () {
681 my @ret;
683 eval q{use File::Find};
684 error($@) if $@;
685 eval q{use Cwd};
686 error($@) if $@;
687 my $origdir=getcwd();
689 my $find_comments=sub {
690 my $dir=shift;
691 my $extension=shift;
692 return unless -d $dir;
694 chdir($dir) || die "chdir $dir: $!";
696 find({
697 no_chdir => 1,
698 wanted => sub {
699 my $file=decode_utf8($_);
700 $file=~s/^\.\///;
701 return if ! length $file || IkiWiki::file_pruned($file)
702 || -l $_ || -d _ || $file !~ /\Q$extension\E$/;
703 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
704 if (defined $f) {
705 my $ctime=(stat($_))[10];
706 push @ret, [$f, $dir, $ctime];
709 }, ".");
711 chdir($origdir) || die "chdir $origdir: $!";
714 $find_comments->($config{srcdir}, "._comment_pending");
715 # old location
716 $find_comments->("$config{wikistatedir}/comments_pending/",
717 "._comment");
719 return @ret;
722 sub previewcomment ($$$) {
723 my $content=shift;
724 my $location=shift;
725 my $page=shift;
726 my $time=shift;
728 # Previewing a comment should implicitly enable comment posting mode.
729 my $oldpostcomment=$postcomment;
730 $postcomment=1;
732 my $preview = IkiWiki::htmlize($location, $page, '_comment',
733 IkiWiki::linkify($location, $page,
734 IkiWiki::preprocess($location, $page,
735 IkiWiki::filter($location, $page, $content), 0, 1)));
737 my $template = template("comment.tmpl");
738 $template->param(content => $preview);
739 $template->param(ctime => displaytime($time, undef, 1));
740 $template->param(html5 => $config{html5});
742 IkiWiki::run_hooks(pagetemplate => sub {
743 shift->(page => $location,
744 destpage => $page,
745 template => $template);
748 $template->param(have_actions => 0);
750 $postcomment=$oldpostcomment;
752 return $template->output;
755 sub commentsshown ($) {
756 my $page=shift;
758 return pagespec_match($page, $config{comments_pagespec},
759 location => $page);
762 sub commentsopen ($) {
763 my $page = shift;
765 return length $config{cgiurl} > 0 &&
766 (! length $config{comments_closed_pagespec} ||
767 ! pagespec_match($page, $config{comments_closed_pagespec},
768 location => $page));
771 sub pagetemplate (@) {
772 my %params = @_;
774 my $page = $params{page};
775 my $template = $params{template};
776 my $shown = ($template->query(name => 'commentslink') ||
777 $template->query(name => 'commentsurl') ||
778 $template->query(name => 'atomcommentsurl') ||
779 $template->query(name => 'comments')) &&
780 commentsshown($page);
782 if ($template->query(name => 'comments')) {
783 my $comments = undef;
784 if ($shown) {
785 $comments = IkiWiki::preprocess_inline(
786 pages => "comment($page) and !comment($page/*)",
787 template => 'comment',
788 show => 0,
789 reverse => 'yes',
790 page => $page,
791 destpage => $params{destpage},
792 feedfile => 'comments',
793 emptyfeeds => 'no',
797 if (defined $comments && length $comments) {
798 $template->param(comments => $comments);
801 if ($shown && commentsopen($page)) {
802 $template->param(addcommenturl => addcommenturl($page));
806 if ($shown) {
807 if ($template->query(name => 'commentsurl')) {
808 $template->param(commentsurl =>
809 urlto($page).'#comments');
812 if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
813 # This will 404 until there are some comments, but I
814 # think that's probably OK...
815 $template->param(atomcommentsurl =>
816 urlto($page).'comments.atom');
819 if ($template->query(name => 'commentslink')) {
820 my $num=num_comments($page, $config{srcdir});
821 my $link;
822 if ($num > 0) {
823 $link = htmllink($page, $params{destpage}, $page,
824 linktext => sprintf(ngettext("%i comment", "%i comments", $num), $num),
825 anchor => "comments",
826 noimageinline => 1
829 elsif (commentsopen($page)) {
830 $link = "<a href=\"".addcommenturl($page)."\">".
831 #translators: Here "Comment" is a verb;
832 #translators: the user clicks on it to
833 #translators: post a comment.
834 gettext("Comment").
835 "</a>";
837 $template->param(commentslink => $link)
838 if defined $link;
842 # everything below this point is only relevant to the comments
843 # themselves
844 if (!exists $commentstate{$page}) {
845 return;
848 if ($template->query(name => 'commentid')) {
849 $template->param(commentid => page_to_id($page));
852 if ($template->query(name => 'commentuser')) {
853 $template->param(commentuser =>
854 $commentstate{$page}{commentuser});
857 if ($template->query(name => 'commentopenid')) {
858 $template->param(commentopenid =>
859 $commentstate{$page}{commentopenid});
862 if ($template->query(name => 'commentip')) {
863 $template->param(commentip =>
864 $commentstate{$page}{commentip});
867 if ($template->query(name => 'commentauthor')) {
868 $template->param(commentauthor =>
869 $commentstate{$page}{commentauthor});
872 if ($template->query(name => 'commentauthorurl')) {
873 $template->param(commentauthorurl =>
874 $commentstate{$page}{commentauthorurl});
877 if ($template->query(name => 'removeurl') &&
878 IkiWiki::Plugin::remove->can("check_canremove") &&
879 length $config{cgiurl}) {
880 $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
881 page => $page));
882 $template->param(have_actions => 1);
886 sub addcommenturl ($) {
887 my $page=shift;
889 return IkiWiki::cgiurl(do => 'comment', page => $page);
892 sub num_comments ($$) {
893 my $page=shift;
894 my $dir=shift;
896 my @comments=glob("$dir/$page/$config{comments_pagename}*._comment");
897 return int @comments;
900 sub unique_comment_location ($$$$) {
901 my $page=shift;
902 eval q{use Digest::MD5 'md5_hex'};
903 error($@) if $@;
904 my $content_md5=md5_hex(Encode::encode_utf8(shift));
905 my $dir=shift;
906 my $ext=shift || "._comment";
908 my $location;
909 my $i = num_comments($page, $dir);
910 do {
911 $i++;
912 $location = "$page/$config{comments_pagename}${i}_${content_md5}";
913 } while (-e "$dir/$location$ext");
915 return $location;
918 sub page_to_id ($) {
919 # Converts a comment page name into a unique, legal html id
920 # attribute value, that can be used as an anchor to link to the
921 # comment.
922 my $page=shift;
924 eval q{use Digest::MD5 'md5_hex'};
925 error($@) if $@;
927 return "comment-".md5_hex(Encode::encode_utf8(($page)));
930 package IkiWiki::PageSpec;
932 sub match_postcomment ($$;@) {
933 my $page = shift;
934 my $glob = shift;
936 if (! $postcomment) {
937 return IkiWiki::FailReason->new("not posting a comment");
939 return match_glob($page, $glob, @_);
942 sub match_comment ($$;@) {
943 my $page = shift;
944 my $glob = shift;
946 if (! $postcomment) {
947 # To see if it's a comment, check the source file type.
948 # Deal with comments that were just deleted.
949 my $source=exists $IkiWiki::pagesources{$page} ?
950 $IkiWiki::pagesources{$page} :
951 $IkiWiki::delpagesources{$page};
952 my $type=defined $source ? IkiWiki::pagetype($source) : undef;
953 if (! defined $type || $type ne "_comment") {
954 return IkiWiki::FailReason->new("$page is not a comment");
958 return match_glob($page, "$glob/*", internal => 1, @_);
961 sub match_comment_pending ($$;@) {
962 my $page = shift;
963 my $glob = shift;
965 my $source=exists $IkiWiki::pagesources{$page} ?
966 $IkiWiki::pagesources{$page} :
967 $IkiWiki::delpagesources{$page};
968 my $type=defined $source ? IkiWiki::pagetype($source) : undef;
969 if (! defined $type || $type ne "_comment_pending") {
970 return IkiWiki::FailReason->new("$page is not a pending comment");
973 return match_glob($page, "$glob/*", internal => 1, @_);