]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/comments.pm
Merge branch 'master'
[ikiwiki.git] / IkiWiki / Plugin / comments.pm
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;
7
8 use warnings;
9 use strict;
10 use IkiWiki 3.00;
11 use Encode;
12 use POSIX qw(strftime);
13
14 use constant PREVIEW => "Preview";
15 use constant POST_COMMENT => "Post comment";
16 use constant CANCEL => "Cancel";
17
18 my $postcomment;
19 my %commentstate;
20
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 => "sessioncgi", id => 'comment', call => \&sessioncgi);
26         hook(type => "htmlize", id => "_comment", call => \&htmlize);
27         hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
28         hook(type => "formbuilder_setup", id => "comments", call => \&formbuilder_setup);
29         # Load goto to fix up user page links for logged-in commenters
30         IkiWiki::loadplugin("goto");
31         IkiWiki::loadplugin("inline");
32 }
33
34 sub getsetup () {
35         return
36                 plugin => {
37                         safe => 1,
38                         rebuild => 1,
39                 },
40                 comments_pagespec => {
41                         type => 'pagespec',
42                         example => 'blog/* and !*/Discussion',
43                         description => 'PageSpec of pages where comments are allowed',
44                         link => 'ikiwiki/PageSpec',
45                         safe => 1,
46                         rebuild => 1,
47                 },
48                 comments_closed_pagespec => {
49                         type => 'pagespec',
50                         example => 'blog/controversial or blog/flamewar',
51                         description => 'PageSpec of pages where posting new comments is not allowed',
52                         link => 'ikiwiki/PageSpec',
53                         safe => 1,
54                         rebuild => 1,
55                 },
56                 comments_pagename => {
57                         type => 'string',
58                         default => 'comment_',
59                         description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
60                         safe => 0, # manual page moving required
61                         rebuild => undef,
62                 },
63                 comments_allowdirectives => {
64                         type => 'boolean',
65                         example => 0,
66                         description => 'Interpret directives in comments?',
67                         safe => 1,
68                         rebuild => 0,
69                 },
70                 comments_allowauthor => {
71                         type => 'boolean',
72                         example => 0,
73                         description => 'Allow anonymous commenters to set an author name?',
74                         safe => 1,
75                         rebuild => 0,
76                 },
77                 comments_commit => {
78                         type => 'boolean',
79                         example => 1,
80                         description => 'commit comments to the VCS',
81                         # old uncommitted comments are likely to cause
82                         # confusion if this is changed
83                         safe => 0,
84                         rebuild => 0,
85                 },
86 }
87
88 sub checkconfig () {
89         $config{comments_commit} = 1
90                 unless defined $config{comments_commit};
91         $config{comments_pagespec} = ''
92                 unless defined $config{comments_pagespec};
93         $config{comments_closed_pagespec} = ''
94                 unless defined $config{comments_closed_pagespec};
95         $config{comments_pagename} = 'comment_'
96                 unless defined $config{comments_pagename};
97 }
98
99 sub htmlize {
100         my %params = @_;
101         return $params{content};
102 }
103
104 # FIXME: copied verbatim from meta
105 sub safeurl ($) {
106         my $url=shift;
107         if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
108             defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
109                 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
110         }
111         else {
112                 return 1;
113         }
114 }
115
116 sub preprocess {
117         my %params = @_;
118         my $page = $params{page};
119
120         my $format = $params{format};
121         if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
122                 error(sprintf(gettext("unsupported page format %s"), $format));
123         }
124
125         my $content = $params{content};
126         if (! defined $content) {
127                 error(gettext("comment must have content"));
128         }
129         $content =~ s/\\"/"/g;
130
131         $content = IkiWiki::filter($page, $params{destpage}, $content);
132
133         if ($config{comments_allowdirectives}) {
134                 $content = IkiWiki::preprocess($page, $params{destpage},
135                         $content);
136         }
137
138         # no need to bother with htmlize if it's just HTML
139         $content = IkiWiki::htmlize($page, $params{destpage}, $format, $content)
140                 if defined $format;
141
142         IkiWiki::run_hooks(sanitize => sub {
143                 $content = shift->(
144                         page => $page,
145                         destpage => $params{destpage},
146                         content => $content,
147                 );
148         });
149
150         # set metadata, possibly overriding [[!meta]] directives from the
151         # comment itself
152
153         my $commentuser;
154         my $commentip;
155         my $commentauthor;
156         my $commentauthorurl;
157         my $commentopenid;
158         if (defined $params{username}) {
159                 $commentuser = $params{username};
160
161                 my $oiduser = eval { IkiWiki::openiduser($commentuser) };
162
163                 if (defined $oiduser) {
164                         # looks like an OpenID
165                         $commentauthorurl = $commentuser;
166                         $commentauthor = $oiduser;
167                         $commentopenid = $commentuser;
168                 }
169                 else {
170                         $commentauthorurl = IkiWiki::cgiurl(
171                                 do => 'goto',
172                                 page => (length $config{userdir}
173                                         ? "$config{userdir}/$commentuser"
174                                         : "$commentuser"));
175
176                         $commentauthor = $commentuser;
177                 }
178         }
179         else {
180                 if (defined $params{ip}) {
181                         $commentip = $params{ip};
182                 }
183                 $commentauthor = gettext("Anonymous");
184         }
185
186         $commentstate{$page}{commentuser} = $commentuser;
187         $commentstate{$page}{commentopenid} = $commentopenid;
188         $commentstate{$page}{commentip} = $commentip;
189         $commentstate{$page}{commentauthor} = $commentauthor;
190         $commentstate{$page}{commentauthorurl} = $commentauthorurl;
191         if (! defined $pagestate{$page}{meta}{author}) {
192                 $pagestate{$page}{meta}{author} = $commentauthor;
193         }
194         if (! defined $pagestate{$page}{meta}{authorurl}) {
195                 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
196         }
197
198         if ($config{comments_allowauthor}) {
199                 if (defined $params{claimedauthor}) {
200                         $pagestate{$page}{meta}{author} = $params{claimedauthor};
201                 }
202
203                 if (defined $params{url}) {
204                         my $url=$params{url};
205
206                         eval q{use URI::Heuristic}; 
207                         if (! $@) {
208                                 $url=URI::Heuristic::uf_uristr($url);
209                         }
210
211                         if (safeurl($url)) {
212                                 $pagestate{$page}{meta}{authorurl} = $url;
213                         }
214                 }
215         }
216         else {
217                 $pagestate{$page}{meta}{author} = $commentauthor;
218                 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
219         }
220
221         if (defined $params{subject}) {
222                 $pagestate{$page}{meta}{title} = $params{subject};
223         }
224
225         if ($params{page} =~ m/\/(\Q$config{comments_pagename}\E\d+)$/) {
226                 $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page}), undef, 1).
227                         "#".page_to_id($params{page});
228         }
229
230         eval q{use Date::Parse};
231         if (! $@) {
232                 my $time = str2time($params{date});
233                 $IkiWiki::pagectime{$page} = $time if defined $time;
234         }
235
236         return $content;
237 }
238
239 sub sessioncgi ($$) {
240         my $cgi=shift;
241         my $session=shift;
242
243         my $do = $cgi->param('do');
244         if ($do eq 'comment') {
245                 editcomment($cgi, $session);
246         }
247         elsif ($do eq 'commentmoderation') {
248                 commentmoderation($cgi, $session);
249         }
250 }
251
252 # Mostly cargo-culted from IkiWiki::plugin::editpage
253 sub editcomment ($$) {
254         my $cgi=shift;
255         my $session=shift;
256
257         IkiWiki::decode_cgi_utf8($cgi);
258
259         eval q{use CGI::FormBuilder};
260         error($@) if $@;
261
262         my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
263         my $form = CGI::FormBuilder->new(
264                 fields => [qw{do sid page subject editcontent type author url}],
265                 charset => 'utf-8',
266                 method => 'POST',
267                 required => [qw{editcontent}],
268                 javascript => 0,
269                 params => $cgi,
270                 action => $config{cgiurl},
271                 header => 0,
272                 table => 0,
273                 template => scalar IkiWiki::template_params('editcomment.tmpl'),
274         );
275
276         IkiWiki::decode_form_utf8($form);
277         IkiWiki::run_hooks(formbuilder_setup => sub {
278                         shift->(title => "comment", form => $form, cgi => $cgi,
279                                 session => $session, buttons => \@buttons);
280                 });
281         IkiWiki::decode_form_utf8($form);
282
283         my $type = $form->param('type');
284         if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
285                 $type = IkiWiki::possibly_foolish_untaint($type);
286         }
287         else {
288                 $type = $config{default_pageext};
289         }
290         my @page_types;
291         if (exists $IkiWiki::hooks{htmlize}) {
292                 @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
293         }
294
295         $form->field(name => 'do', type => 'hidden');
296         $form->field(name => 'sid', type => 'hidden', value => $session->id,
297                 force => 1);
298         $form->field(name => 'page', type => 'hidden');
299         $form->field(name => 'subject', type => 'text', size => 72);
300         $form->field(name => 'editcontent', type => 'textarea', rows => 10);
301         $form->field(name => "type", value => $type, force => 1,
302                 type => 'select', options => \@page_types);
303
304         $form->tmpl_param(username => $session->param('name'));
305
306         if ($config{comments_allowauthor} and
307             ! defined $session->param('name')) {
308                 $form->tmpl_param(allowauthor => 1);
309                 $form->field(name => 'author', type => 'text', size => '40');
310                 $form->field(name => 'url', type => 'text', size => '40');
311         }
312         else {
313                 $form->tmpl_param(allowauthor => 0);
314                 $form->field(name => 'author', type => 'hidden', value => '',
315                         force => 1);
316                 $form->field(name => 'url', type => 'hidden', value => '',
317                         force => 1);
318         }
319
320         # The untaint is OK (as in editpage) because we're about to pass
321         # it to file_pruned anyway
322         my $page = $form->field('page');
323         $page = IkiWiki::possibly_foolish_untaint($page);
324         if (! defined $page || ! length $page ||
325                 IkiWiki::file_pruned($page, $config{srcdir})) {
326                 error(gettext("bad page name"));
327         }
328
329         my $baseurl = urlto($page, undef, 1);
330
331         $form->title(sprintf(gettext("commenting on %s"),
332                         IkiWiki::pagetitle($page)));
333
334         $form->tmpl_param('helponformattinglink',
335                 htmllink($page, $page, 'ikiwiki/formatting',
336                         noimageinline => 1,
337                         linktext => 'FormattingHelp'),
338                         allowdirectives => $config{allow_directives});
339
340         if ($form->submitted eq CANCEL) {
341                 # bounce back to the page they wanted to comment on, and exit.
342                 # CANCEL need not be considered in future
343                 IkiWiki::redirect($cgi, urlto($page, undef, 1));
344                 exit;
345         }
346
347         if (not exists $pagesources{$page}) {
348                 error(sprintf(gettext(
349                         "page '%s' doesn't exist, so you can't comment"),
350                         $page));
351         }
352
353         if (pagespec_match($page, $config{comments_closed_pagespec},
354                 location => $page)) {
355                 error(sprintf(gettext(
356                         "comments on page '%s' are closed"),
357                         $page));
358         }
359
360         # Set a flag to indicate that we're posting a comment,
361         # so that postcomment() can tell it should match.
362         $postcomment=1;
363         IkiWiki::check_canedit($page, $cgi, $session);
364         $postcomment=0;
365
366         my $location=unique_comment_location($page, $config{srcdir});
367
368         my $content = "[[!_comment format=$type\n";
369
370         # FIXME: handling of double quotes probably wrong?
371         if (defined $session->param('name')) {
372                 my $username = $session->param('name');
373                 $username =~ s/"/&quot;/g;
374                 $content .= " username=\"$username\"\n";
375         }
376         elsif (defined $ENV{REMOTE_ADDR}) {
377                 my $ip = $ENV{REMOTE_ADDR};
378                 if ($ip =~ m/^([.0-9]+)$/) {
379                         $content .= " ip=\"$1\"\n";
380                 }
381         }
382
383         if ($config{comments_allowauthor}) {
384                 my $author = $form->field('author');
385                 if (defined $author && length $author) {
386                         $author =~ s/"/&quot;/g;
387                         $content .= " claimedauthor=\"$author\"\n";
388                 }
389                 my $url = $form->field('url');
390                 if (defined $url && length $url) {
391                         $url =~ s/"/&quot;/g;
392                         $content .= " url=\"$url\"\n";
393                 }
394         }
395
396         my $subject = $form->field('subject');
397         if (defined $subject && length $subject) {
398                 $subject =~ s/"/&quot;/g;
399                 $content .= " subject=\"$subject\"\n";
400         }
401
402         $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
403
404         my $editcontent = $form->field('editcontent') || '';
405         $editcontent =~ s/\r\n/\n/g;
406         $editcontent =~ s/\r/\n/g;
407         $editcontent =~ s/"/\\"/g;
408         $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
409
410         # This is essentially a simplified version of editpage:
411         # - the user does not control the page that's created, only the parent
412         # - it's always a create operation, never an edit
413         # - this means that conflicts should never happen
414         # - this means that if they do, rocks fall and everyone dies
415
416         if ($form->submitted eq PREVIEW) {
417                 my $preview=previewcomment($content, $location, $page, time);
418                 IkiWiki::run_hooks(format => sub {
419                         $preview = shift->(page => $page,
420                                 content => $preview);
421                 });
422                 $form->tmpl_param(page_preview => $preview);
423         }
424         else {
425                 $form->tmpl_param(page_preview => "");
426         }
427
428         if ($form->submitted eq POST_COMMENT && $form->validate) {
429                 IkiWiki::checksessionexpiry($cgi, $session);
430                 
431                 $postcomment=1;
432                 my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
433                         subject => $form->field('subject'),
434                         $config{comments_allowauthor} ? (
435                                 author => $form->field('author'),
436                                 url => $form->field('url'),
437                         ) : (),
438                         page => $location,
439                         cgi => $cgi,
440                         session => $session,
441                         nonfatal => 1,
442                 );
443                 $postcomment=0;
444
445                 if (! $ok) {
446                         my $penddir=$config{wikistatedir}."/comments_pending";
447                         $location=unique_comment_location($page, $penddir);
448                         writefile("$location._comment", $penddir, $content);
449                         IkiWiki::printheader($session);
450                         print IkiWiki::misctemplate(gettext(gettext("comment stored for moderation")),
451                                 "<p>".
452                                 gettext("Your comment will be posted after moderator review").
453                                 "</p>");
454                         exit;
455                 }
456
457                 # FIXME: could probably do some sort of graceful retry
458                 # on error? Would require significant unwinding though
459                 my $file = "$location._comment";
460                 writefile($file, $config{srcdir}, $content);
461
462                 my $conflict;
463
464                 if ($config{rcs} and $config{comments_commit}) {
465                         my $message = gettext("Added a comment");
466                         if (defined $form->field('subject') &&
467                                 length $form->field('subject')) {
468                                 $message = sprintf(
469                                         gettext("Added a comment: %s"),
470                                         $form->field('subject'));
471                         }
472
473                         IkiWiki::rcs_add($file);
474                         IkiWiki::disable_commit_hook();
475                         $conflict = IkiWiki::rcs_commit_staged($message,
476                                 $session->param('name'), $ENV{REMOTE_ADDR});
477                         IkiWiki::enable_commit_hook();
478                         IkiWiki::rcs_update();
479                 }
480
481                 # Now we need a refresh
482                 require IkiWiki::Render;
483                 IkiWiki::refresh();
484                 IkiWiki::saveindex();
485
486                 # this should never happen, unless a committer deliberately
487                 # breaks it or something
488                 error($conflict) if defined $conflict;
489
490                 # Jump to the new comment on the page.
491                 # The trailing question mark tries to avoid broken
492                 # caches and get the most recent version of the page.
493                 IkiWiki::redirect($cgi, urlto($page, undef, 1).
494                         "?updated#".page_to_id($location));
495
496         }
497         else {
498                 IkiWiki::showform ($form, \@buttons, $session, $cgi,
499                         forcebaseurl => $baseurl);
500         }
501
502         exit;
503 }
504
505 sub commentmoderation ($$) {
506         my $cgi=shift;
507         my $session=shift;
508
509         IkiWiki::needsignin($cgi, $session);
510         if (! IkiWiki::is_admin($session->param("name"))) {
511                 error(gettext("you are not logged in as an admin"));
512         }
513
514         IkiWiki::decode_cgi_utf8($cgi);
515         
516         if (defined $cgi->param('sid')) {
517                 IkiWiki::checksessionexpiry($cgi, $session);
518
519                 my $rejectalldefer=$cgi->param('rejectalldefer');
520
521                 my %vars=$cgi->Vars;
522                 my $added=0;
523                 foreach my $id (keys %vars) {
524                         if ($id =~ /(.*)\Q._comment\E$/) {
525                                 my $action=$cgi->param($id);
526                                 next if $action eq 'Defer' && ! $rejectalldefer;
527
528                                 # Make sure that the id is of a legal
529                                 # pending comment before untainting.
530                                 my ($f)= $id =~ /$config{wiki_file_regexp}/;
531                                 if (! defined $f || ! length $f ||
532                                     IkiWiki::file_pruned($f, $config{srcdir})) {
533                                         error("illegal file");
534                                 }
535
536                                 my $page=IkiWiki::possibly_foolish_untaint(IkiWiki::dirname($1));
537                                 my $file="$config{wikistatedir}/comments_pending/".
538                                         IkiWiki::possibly_foolish_untaint($id);
539
540                                 if ($action eq 'Accept') {
541                                         my $content=eval { readfile($file) };
542                                         next if $@; # file vanished since form was displayed
543                                         my $dest=unique_comment_location($page, $config{srcdir})."._comment";
544                                         writefile($dest, $config{srcdir}, $content);
545                                         if ($config{rcs} and $config{comments_commit}) {
546                                                 IkiWiki::rcs_add($dest);
547                                         }
548                                         $added++;
549                                 }
550
551                                 # This removes empty subdirs, so the
552                                 # .ikiwiki/comments_pending dir will
553                                 # go away when all are moderated.
554                                 require IkiWiki::Render;
555                                 IkiWiki::prune($file);
556                         }
557                 }
558
559                 if ($added) {
560                         my $conflict;
561                         if ($config{rcs} and $config{comments_commit}) {
562                                 my $message = gettext("Comment moderation");
563                                 IkiWiki::disable_commit_hook();
564                                 $conflict=IkiWiki::rcs_commit_staged($message,
565                                         $session->param('name'), $ENV{REMOTE_ADDR});
566                                 IkiWiki::enable_commit_hook();
567                                 IkiWiki::rcs_update();
568                         }
569                 
570                         # Now we need a refresh
571                         require IkiWiki::Render;
572                         IkiWiki::refresh();
573                         IkiWiki::saveindex();
574                 
575                         error($conflict) if defined $conflict;
576                 }
577         }
578
579         my @comments=map {
580                 my ($id, $ctime)=@{$_};
581                 my $file="$config{wikistatedir}/comments_pending/$id";
582                 my $content=readfile($file);
583                 my $preview=previewcomment($content, $id,
584                         IkiWiki::dirname($_), $ctime);
585                 {
586                         id => $id,
587                         view => $preview,
588                 } 
589         } sort { $b->[1] <=> $a->[1] } comments_pending();
590
591         my $template=template("commentmoderation.tmpl");
592         $template->param(
593                 sid => $session->id,
594                 comments => \@comments,
595         );
596         IkiWiki::printheader($session);
597         my $out=$template->output;
598         IkiWiki::run_hooks(format => sub {
599                 $out = shift->(page => "", content => $out);
600         });
601         print IkiWiki::misctemplate(gettext("comment moderation"), $out);
602         exit;
603 }
604
605 sub formbuilder_setup (@) {
606         my %params=@_;
607
608         my $form=$params{form};
609         if ($form->title eq "preferences" &&
610             IkiWiki::is_admin($params{session}->param("name"))) {
611                 push @{$params{buttons}}, "Comment Moderation";
612                 if ($form->submitted && $form->submitted eq "Comment Moderation") {
613                         commentmoderation($params{cgi}, $params{session});
614                 }
615         }
616 }
617
618 sub comments_pending () {
619         my $dir="$config{wikistatedir}/comments_pending/";
620         return unless -d $dir;
621
622         my @ret;
623         eval q{use File::Find};
624         error($@) if $@;
625         find({
626                 no_chdir => 1,
627                 wanted => sub {
628                         $_=decode_utf8($_);
629                         if (IkiWiki::file_pruned($_, $dir)) {
630                                 $File::Find::prune=1;
631                         }
632                         elsif (! -l $_ && ! -d _) {
633                                 $File::Find::prune=0;
634                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
635                                 if (defined $f && $f =~ /\Q._comment\E$/) {
636                                         my $ctime=(stat($f))[10];
637                                         $f=~s/^\Q$dir\E\/?//;
638                                         push @ret, [$f, $ctime];
639                                 }
640                         }
641                 }
642         }, $dir);
643
644         return @ret;
645 }
646
647 sub previewcomment ($$$) {
648         my $content=shift;
649         my $location=shift;
650         my $page=shift;
651         my $time=shift;
652
653         my $preview = IkiWiki::htmlize($location, $page, '_comment',
654                         IkiWiki::linkify($location, $page,
655                         IkiWiki::preprocess($location, $page,
656                         IkiWiki::filter($location, $page, $content), 0, 1)));
657
658         my $template = template("comment.tmpl");
659         $template->param(content => $preview);
660         $template->param(ctime => displaytime($time));
661
662         IkiWiki::run_hooks(pagetemplate => sub {
663                 shift->(page => $location,
664                         destpage => $page,
665                         template => $template);
666         });
667
668         $template->param(have_actions => 0);
669
670         return $template->output;
671 }
672
673 sub commentsshown ($) {
674         my $page=shift;
675
676         return ! pagespec_match($page, "internal(*/$config{comments_pagename}*)",
677                                 location => $page) &&
678                pagespec_match($page, $config{comments_pagespec},
679                               location => $page);
680 }
681
682 sub commentsopen ($) {
683         my $page = shift;
684
685         return length $config{cgiurl} > 0 &&
686                (! length $config{comments_closed_pagespec} ||
687                 ! pagespec_match($page, $config{comments_closed_pagespec},
688                                  location => $page));
689 }
690
691 sub pagetemplate (@) {
692         my %params = @_;
693
694         my $page = $params{page};
695         my $template = $params{template};
696         my $shown = ($template->query(name => 'commentslink') ||
697                      $template->query(name => 'commentsurl') ||
698                      $template->query(name => 'atomcommentsurl') ||
699                      $template->query(name => 'comments')) &&
700                     commentsshown($page);
701
702         if ($template->query(name => 'comments')) {
703                 my $comments = undef;
704                 if ($shown) {
705                         $comments = IkiWiki::preprocess_inline(
706                                 pages => "internal($page/$config{comments_pagename}*)",
707                                 template => 'comment',
708                                 show => 0,
709                                 reverse => 'yes',
710                                 page => $page,
711                                 destpage => $params{destpage},
712                                 feedfile => 'comments',
713                                 emptyfeeds => 'no',
714                         );
715                 }
716
717                 if (defined $comments && length $comments) {
718                         $template->param(comments => $comments);
719                 }
720
721                 if ($shown && commentsopen($page)) {
722                         my $addcommenturl = IkiWiki::cgiurl(do => 'comment',
723                                 page => $page);
724                         $template->param(addcommenturl => $addcommenturl);
725                 }
726         }
727
728         if ($template->query(name => 'commentsurl')) {
729                 if ($shown) {
730                         $template->param(commentsurl =>
731                                 urlto($page, undef, 1).'#comments');
732                 }
733         }
734
735         if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
736                 if ($shown) {
737                         # This will 404 until there are some comments, but I
738                         # think that's probably OK...
739                         $template->param(atomcommentsurl =>
740                                 urlto($page, undef, 1).'comments.atom');
741                 }
742         }
743
744         if ($template->query(name => 'commentslink')) {
745                 # XXX Would be nice to say how many comments there are in
746                 # the link. But, to update the number, blog pages
747                 # would have to update whenever comments of any inlines
748                 # page are added, which is not currently done.
749                 if ($shown) {
750                         $template->param(commentslink =>
751                                 htmllink($page, $params{destpage}, $page,
752                                         linktext => gettext("Comments"),
753                                         anchor => "comments",
754                                         noimageinline => 1));
755                 }
756         }
757
758         # everything below this point is only relevant to the comments
759         # themselves
760         if (!exists $commentstate{$page}) {
761                 return;
762         }
763         
764         if ($template->query(name => 'commentid')) {
765                 $template->param(commentid => page_to_id($page));
766         }
767
768         if ($template->query(name => 'commentuser')) {
769                 $template->param(commentuser =>
770                         $commentstate{$page}{commentuser});
771         }
772
773         if ($template->query(name => 'commentopenid')) {
774                 $template->param(commentopenid =>
775                         $commentstate{$page}{commentopenid});
776         }
777
778         if ($template->query(name => 'commentip')) {
779                 $template->param(commentip =>
780                         $commentstate{$page}{commentip});
781         }
782
783         if ($template->query(name => 'commentauthor')) {
784                 $template->param(commentauthor =>
785                         $commentstate{$page}{commentauthor});
786         }
787
788         if ($template->query(name => 'commentauthorurl')) {
789                 $template->param(commentauthorurl =>
790                         $commentstate{$page}{commentauthorurl});
791         }
792
793         if ($template->query(name => 'removeurl') &&
794             IkiWiki::Plugin::remove->can("check_canremove") &&
795             length $config{cgiurl}) {
796                 $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
797                         page => $page));
798                 $template->param(have_actions => 1);
799         }
800 }
801
802 sub unique_comment_location ($) {
803         my $page=shift;
804         my $dir=shift;
805
806         my $location;
807         my $i = 0;
808         do {
809                 $i++;
810                 $location = "$page/$config{comments_pagename}$i";
811         } while (-e "$dir/$location._comment");
812
813         return $location;
814 }
815
816 sub page_to_id ($) {
817         # Converts a comment page name into a unique, legal html id
818         # addtibute value, that can be used as an anchor to link to the
819         # comment.
820         my $page=shift;
821
822         eval q{use Digest::MD5 'md5_hex'};
823         error($@) if $@;
824
825         return "comment-".md5_hex($page);
826 }
827         
828 package IkiWiki::PageSpec;
829
830 sub match_postcomment ($$;@) {
831         my $page = shift;
832         my $glob = shift;
833
834         if (! $postcomment) {
835                 return IkiWiki::FailReason->new("not posting a comment");
836         }
837         return match_glob($page, $glob);
838 }
839
840 1