]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/comments.pm
Revert "pagespec_match_list * optimisation"
[git.ikiwiki.info.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;
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 => "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 }
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 }
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 }
99 sub htmlize {
100         my %params = @_;
101         return $params{content};
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         }
116 sub preprocess {
117         my %params = @_;
118         my $page = $params{page};
120         my $format = $params{format};
121         if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
122                 error(sprintf(gettext("unsupported page format %s"), $format));
123         }
125         my $content = $params{content};
126         if (! defined $content) {
127                 error(gettext("comment must have content"));
128         }
129         $content =~ s/\\"/"/g;
131         $content = IkiWiki::filter($page, $params{destpage}, $content);
133         if ($config{comments_allowdirectives}) {
134                 $content = IkiWiki::preprocess($page, $params{destpage},
135                         $content);
136         }
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;
142         IkiWiki::run_hooks(sanitize => sub {
143                 $content = shift->(
144                         page => $page,
145                         destpage => $params{destpage},
146                         content => $content,
147                 );
148         });
150         # set metadata, possibly overriding [[!meta]] directives from the
151         # comment itself
153         my $commentuser;
154         my $commentip;
155         my $commentauthor;
156         my $commentauthorurl;
157         my $commentopenid;
158         if (defined $params{username}) {
159                 $commentuser = $params{username};
161                 my $oiduser = eval { IkiWiki::openiduser($commentuser) };
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"));
176                         $commentauthor = $commentuser;
177                 }
178         }
179         else {
180                 if (defined $params{ip}) {
181                         $commentip = $params{ip};
182                 }
183                 $commentauthor = gettext("Anonymous");
184         }
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         }
198         if ($config{comments_allowauthor}) {
199                 if (defined $params{claimedauthor}) {
200                         $pagestate{$page}{meta}{author} = $params{claimedauthor};
201                 }
203                 if (defined $params{url}) {
204                         my $url=$params{url};
206                         eval q{use URI::Heuristic}; 
207                         if (! $@) {
208                                 $url=URI::Heuristic::uf_uristr($url);
209                         }
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         }
221         if (defined $params{subject}) {
222                 $pagestate{$page}{meta}{title} = $params{subject};
223         }
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         }
230         eval q{use Date::Parse};
231         if (! $@) {
232                 my $time = str2time($params{date});
233                 $IkiWiki::pagectime{$page} = $time if defined $time;
234         }
236         return $content;
239 sub sessioncgi ($$) {
240         my $cgi=shift;
241         my $session=shift;
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         }
252 # Mostly cargo-culted from IkiWiki::plugin::editpage
253 sub editcomment ($$) {
254         my $cgi=shift;
255         my $session=shift;
257         IkiWiki::decode_cgi_utf8($cgi);
259         eval q{use CGI::FormBuilder};
260         error($@) if $@;
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         );
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);
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         }
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);
304         $form->tmpl_param(username => $session->param('name'));
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         }
320         if (! defined $session->param('name')) {
321                 # Make signinurl work and return here.
322                 $form->tmpl_param(signinurl => IkiWiki::cgiurl(do => 'signin'));
323                 $session->param(postsignin => $ENV{QUERY_STRING});
324                 IkiWiki::cgi_savesession($session);
325         }
327         # The untaint is OK (as in editpage) because we're about to pass
328         # it to file_pruned anyway
329         my $page = $form->field('page');
330         $page = IkiWiki::possibly_foolish_untaint($page);
331         if (! defined $page || ! length $page ||
332                 IkiWiki::file_pruned($page, $config{srcdir})) {
333                 error(gettext("bad page name"));
334         }
336         my $baseurl = urlto($page, undef, 1);
338         $form->title(sprintf(gettext("commenting on %s"),
339                         IkiWiki::pagetitle($page)));
341         $form->tmpl_param('helponformattinglink',
342                 htmllink($page, $page, 'ikiwiki/formatting',
343                         noimageinline => 1,
344                         linktext => 'FormattingHelp'),
345                         allowdirectives => $config{allow_directives});
347         if ($form->submitted eq CANCEL) {
348                 # bounce back to the page they wanted to comment on, and exit.
349                 # CANCEL need not be considered in future
350                 IkiWiki::redirect($cgi, urlto($page, undef, 1));
351                 exit;
352         }
354         if (not exists $pagesources{$page}) {
355                 error(sprintf(gettext(
356                         "page '%s' doesn't exist, so you can't comment"),
357                         $page));
358         }
360         if (pagespec_match($page, $config{comments_closed_pagespec},
361                 location => $page)) {
362                 error(sprintf(gettext(
363                         "comments on page '%s' are closed"),
364                         $page));
365         }
367         # Set a flag to indicate that we're posting a comment,
368         # so that postcomment() can tell it should match.
369         $postcomment=1;
370         IkiWiki::check_canedit($page, $cgi, $session);
371         $postcomment=0;
373         my $location=unique_comment_location($page, $config{srcdir});
375         my $content = "[[!_comment format=$type\n";
377         # FIXME: handling of double quotes probably wrong?
378         if (defined $session->param('name')) {
379                 my $username = $session->param('name');
380                 $username =~ s/"/&quot;/g;
381                 $content .= " username=\"$username\"\n";
382         }
383         elsif (defined $ENV{REMOTE_ADDR}) {
384                 my $ip = $ENV{REMOTE_ADDR};
385                 if ($ip =~ m/^([.0-9]+)$/) {
386                         $content .= " ip=\"$1\"\n";
387                 }
388         }
390         if ($config{comments_allowauthor}) {
391                 my $author = $form->field('author');
392                 if (defined $author && length $author) {
393                         $author =~ s/"/&quot;/g;
394                         $content .= " claimedauthor=\"$author\"\n";
395                 }
396                 my $url = $form->field('url');
397                 if (defined $url && length $url) {
398                         $url =~ s/"/&quot;/g;
399                         $content .= " url=\"$url\"\n";
400                 }
401         }
403         my $subject = $form->field('subject');
404         if (defined $subject && length $subject) {
405                 $subject =~ s/"/&quot;/g;
406                 $content .= " subject=\"$subject\"\n";
407         }
409         $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
411         my $editcontent = $form->field('editcontent') || '';
412         $editcontent =~ s/\r\n/\n/g;
413         $editcontent =~ s/\r/\n/g;
414         $editcontent =~ s/"/\\"/g;
415         $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
417         # This is essentially a simplified version of editpage:
418         # - the user does not control the page that's created, only the parent
419         # - it's always a create operation, never an edit
420         # - this means that conflicts should never happen
421         # - this means that if they do, rocks fall and everyone dies
423         if ($form->submitted eq PREVIEW) {
424                 my $preview=previewcomment($content, $location, $page, time);
425                 IkiWiki::run_hooks(format => sub {
426                         $preview = shift->(page => $page,
427                                 content => $preview);
428                 });
429                 $form->tmpl_param(page_preview => $preview);
430         }
431         else {
432                 $form->tmpl_param(page_preview => "");
433         }
435         if ($form->submitted eq POST_COMMENT && $form->validate) {
436                 IkiWiki::checksessionexpiry($cgi, $session);
437                 
438                 $postcomment=1;
439                 my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
440                         subject => $form->field('subject'),
441                         $config{comments_allowauthor} ? (
442                                 author => $form->field('author'),
443                                 url => $form->field('url'),
444                         ) : (),
445                         page => $location,
446                         cgi => $cgi,
447                         session => $session,
448                         nonfatal => 1,
449                 );
450                 $postcomment=0;
452                 if (! $ok) {
453                         my $penddir=$config{wikistatedir}."/comments_pending";
454                         $location=unique_comment_location($page, $penddir);
455                         writefile("$location._comment", $penddir, $content);
456                         IkiWiki::printheader($session);
457                         print IkiWiki::misctemplate(gettext(gettext("comment stored for moderation")),
458                                 "<p>".
459                                 gettext("Your comment will be posted after moderator review").
460                                 "</p>");
461                         exit;
462                 }
464                 # FIXME: could probably do some sort of graceful retry
465                 # on error? Would require significant unwinding though
466                 my $file = "$location._comment";
467                 writefile($file, $config{srcdir}, $content);
469                 my $conflict;
471                 if ($config{rcs} and $config{comments_commit}) {
472                         my $message = gettext("Added a comment");
473                         if (defined $form->field('subject') &&
474                                 length $form->field('subject')) {
475                                 $message = sprintf(
476                                         gettext("Added a comment: %s"),
477                                         $form->field('subject'));
478                         }
480                         IkiWiki::rcs_add($file);
481                         IkiWiki::disable_commit_hook();
482                         $conflict = IkiWiki::rcs_commit_staged($message,
483                                 $session->param('name'), $ENV{REMOTE_ADDR});
484                         IkiWiki::enable_commit_hook();
485                         IkiWiki::rcs_update();
486                 }
488                 # Now we need a refresh
489                 require IkiWiki::Render;
490                 IkiWiki::refresh();
491                 IkiWiki::saveindex();
493                 # this should never happen, unless a committer deliberately
494                 # breaks it or something
495                 error($conflict) if defined $conflict;
497                 # Jump to the new comment on the page.
498                 # The trailing question mark tries to avoid broken
499                 # caches and get the most recent version of the page.
500                 IkiWiki::redirect($cgi, urlto($page, undef, 1).
501                         "?updated#".page_to_id($location));
503         }
504         else {
505                 IkiWiki::showform ($form, \@buttons, $session, $cgi,
506                         forcebaseurl => $baseurl);
507         }
509         exit;
512 sub commentmoderation ($$) {
513         my $cgi=shift;
514         my $session=shift;
516         IkiWiki::needsignin($cgi, $session);
517         if (! IkiWiki::is_admin($session->param("name"))) {
518                 error(gettext("you are not logged in as an admin"));
519         }
521         IkiWiki::decode_cgi_utf8($cgi);
522         
523         if (defined $cgi->param('sid')) {
524                 IkiWiki::checksessionexpiry($cgi, $session);
526                 my $rejectalldefer=$cgi->param('rejectalldefer');
528                 my %vars=$cgi->Vars;
529                 my $added=0;
530                 foreach my $id (keys %vars) {
531                         if ($id =~ /(.*)\Q._comment\E$/) {
532                                 my $action=$cgi->param($id);
533                                 next if $action eq 'Defer' && ! $rejectalldefer;
535                                 # Make sure that the id is of a legal
536                                 # pending comment before untainting.
537                                 my ($f)= $id =~ /$config{wiki_file_regexp}/;
538                                 if (! defined $f || ! length $f ||
539                                     IkiWiki::file_pruned($f, $config{srcdir})) {
540                                         error("illegal file");
541                                 }
543                                 my $page=IkiWiki::possibly_foolish_untaint(IkiWiki::dirname($1));
544                                 my $file="$config{wikistatedir}/comments_pending/".
545                                         IkiWiki::possibly_foolish_untaint($id);
547                                 if ($action eq 'Accept') {
548                                         my $content=eval { readfile($file) };
549                                         next if $@; # file vanished since form was displayed
550                                         my $dest=unique_comment_location($page, $config{srcdir})."._comment";
551                                         writefile($dest, $config{srcdir}, $content);
552                                         if ($config{rcs} and $config{comments_commit}) {
553                                                 IkiWiki::rcs_add($dest);
554                                         }
555                                         $added++;
556                                 }
558                                 # This removes empty subdirs, so the
559                                 # .ikiwiki/comments_pending dir will
560                                 # go away when all are moderated.
561                                 require IkiWiki::Render;
562                                 IkiWiki::prune($file);
563                         }
564                 }
566                 if ($added) {
567                         my $conflict;
568                         if ($config{rcs} and $config{comments_commit}) {
569                                 my $message = gettext("Comment moderation");
570                                 IkiWiki::disable_commit_hook();
571                                 $conflict=IkiWiki::rcs_commit_staged($message,
572                                         $session->param('name'), $ENV{REMOTE_ADDR});
573                                 IkiWiki::enable_commit_hook();
574                                 IkiWiki::rcs_update();
575                         }
576                 
577                         # Now we need a refresh
578                         require IkiWiki::Render;
579                         IkiWiki::refresh();
580                         IkiWiki::saveindex();
581                 
582                         error($conflict) if defined $conflict;
583                 }
584         }
586         my @comments=map {
587                 my ($id, $ctime)=@{$_};
588                 my $file="$config{wikistatedir}/comments_pending/$id";
589                 my $content=readfile($file);
590                 my $preview=previewcomment($content, $id,
591                         IkiWiki::dirname($_), $ctime);
592                 {
593                         id => $id,
594                         view => $preview,
595                 } 
596         } sort { $b->[1] <=> $a->[1] } comments_pending();
598         my $template=template("commentmoderation.tmpl");
599         $template->param(
600                 sid => $session->id,
601                 comments => \@comments,
602         );
603         IkiWiki::printheader($session);
604         my $out=$template->output;
605         IkiWiki::run_hooks(format => sub {
606                 $out = shift->(page => "", content => $out);
607         });
608         print IkiWiki::misctemplate(gettext("comment moderation"), $out);
609         exit;
612 sub formbuilder_setup (@) {
613         my %params=@_;
615         my $form=$params{form};
616         if ($form->title eq "preferences" &&
617             IkiWiki::is_admin($params{session}->param("name"))) {
618                 push @{$params{buttons}}, "Comment Moderation";
619                 if ($form->submitted && $form->submitted eq "Comment Moderation") {
620                         commentmoderation($params{cgi}, $params{session});
621                 }
622         }
625 sub comments_pending () {
626         my $dir="$config{wikistatedir}/comments_pending/";
627         return unless -d $dir;
629         my @ret;
630         eval q{use File::Find};
631         error($@) if $@;
632         find({
633                 no_chdir => 1,
634                 wanted => sub {
635                         $_=decode_utf8($_);
636                         if (IkiWiki::file_pruned($_, $dir)) {
637                                 $File::Find::prune=1;
638                         }
639                         elsif (! -l $_ && ! -d _) {
640                                 $File::Find::prune=0;
641                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
642                                 if (defined $f && $f =~ /\Q._comment\E$/) {
643                                         my $ctime=(stat($f))[10];
644                                         $f=~s/^\Q$dir\E\/?//;
645                                         push @ret, [$f, $ctime];
646                                 }
647                         }
648                 }
649         }, $dir);
651         return @ret;
654 sub previewcomment ($$$) {
655         my $content=shift;
656         my $location=shift;
657         my $page=shift;
658         my $time=shift;
660         my $preview = IkiWiki::htmlize($location, $page, '_comment',
661                         IkiWiki::linkify($location, $page,
662                         IkiWiki::preprocess($location, $page,
663                         IkiWiki::filter($location, $page, $content), 0, 1)));
665         my $template = template("comment.tmpl");
666         $template->param(content => $preview);
667         $template->param(ctime => displaytime($time));
669         IkiWiki::run_hooks(pagetemplate => sub {
670                 shift->(page => $location,
671                         destpage => $page,
672                         template => $template);
673         });
675         $template->param(have_actions => 0);
677         return $template->output;
680 sub commentsshown ($) {
681         my $page=shift;
683         return ! pagespec_match($page, "internal(*/$config{comments_pagename}*)",
684                                 location => $page) &&
685                pagespec_match($page, $config{comments_pagespec},
686                               location => $page);
689 sub commentsopen ($) {
690         my $page = shift;
692         return length $config{cgiurl} > 0 &&
693                (! length $config{comments_closed_pagespec} ||
694                 ! pagespec_match($page, $config{comments_closed_pagespec},
695                                  location => $page));
698 sub pagetemplate (@) {
699         my %params = @_;
701         my $page = $params{page};
702         my $template = $params{template};
703         my $shown = ($template->query(name => 'commentslink') ||
704                      $template->query(name => 'commentsurl') ||
705                      $template->query(name => 'atomcommentsurl') ||
706                      $template->query(name => 'comments')) &&
707                     commentsshown($page);
709         if ($template->query(name => 'comments')) {
710                 my $comments = undef;
711                 if ($shown) {
712                         $comments = IkiWiki::preprocess_inline(
713                                 pages => "internal($page/$config{comments_pagename}*)",
714                                 template => 'comment',
715                                 show => 0,
716                                 reverse => 'yes',
717                                 page => $page,
718                                 destpage => $params{destpage},
719                                 feedfile => 'comments',
720                                 emptyfeeds => 'no',
721                         );
722                 }
724                 if (defined $comments && length $comments) {
725                         $template->param(comments => $comments);
726                 }
728                 if ($shown && commentsopen($page)) {
729                         my $addcommenturl = IkiWiki::cgiurl(do => 'comment',
730                                 page => $page);
731                         $template->param(addcommenturl => $addcommenturl);
732                 }
733         }
735         if ($template->query(name => 'commentsurl')) {
736                 if ($shown) {
737                         $template->param(commentsurl =>
738                                 urlto($page, undef, 1).'#comments');
739                 }
740         }
742         if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
743                 if ($shown) {
744                         # This will 404 until there are some comments, but I
745                         # think that's probably OK...
746                         $template->param(atomcommentsurl =>
747                                 urlto($page, undef, 1).'comments.atom');
748                 }
749         }
751         if ($template->query(name => 'commentslink')) {
752                 # XXX Would be nice to say how many comments there are in
753                 # the link. But, to update the number, blog pages
754                 # would have to update whenever comments of any inlines
755                 # page are added, which is not currently done.
756                 if ($shown) {
757                         $template->param(commentslink =>
758                                 htmllink($page, $params{destpage}, $page,
759                                         linktext => gettext("Comments"),
760                                         anchor => "comments",
761                                         noimageinline => 1));
762                 }
763         }
765         # everything below this point is only relevant to the comments
766         # themselves
767         if (!exists $commentstate{$page}) {
768                 return;
769         }
770         
771         if ($template->query(name => 'commentid')) {
772                 $template->param(commentid => page_to_id($page));
773         }
775         if ($template->query(name => 'commentuser')) {
776                 $template->param(commentuser =>
777                         $commentstate{$page}{commentuser});
778         }
780         if ($template->query(name => 'commentopenid')) {
781                 $template->param(commentopenid =>
782                         $commentstate{$page}{commentopenid});
783         }
785         if ($template->query(name => 'commentip')) {
786                 $template->param(commentip =>
787                         $commentstate{$page}{commentip});
788         }
790         if ($template->query(name => 'commentauthor')) {
791                 $template->param(commentauthor =>
792                         $commentstate{$page}{commentauthor});
793         }
795         if ($template->query(name => 'commentauthorurl')) {
796                 $template->param(commentauthorurl =>
797                         $commentstate{$page}{commentauthorurl});
798         }
800         if ($template->query(name => 'removeurl') &&
801             IkiWiki::Plugin::remove->can("check_canremove") &&
802             length $config{cgiurl}) {
803                 $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
804                         page => $page));
805                 $template->param(have_actions => 1);
806         }
809 sub unique_comment_location ($) {
810         my $page=shift;
811         my $dir=shift;
813         my $location;
814         my $i = 0;
815         do {
816                 $i++;
817                 $location = "$page/$config{comments_pagename}$i";
818         } while (-e "$dir/$location._comment");
820         return $location;
823 sub page_to_id ($) {
824         # Converts a comment page name into a unique, legal html id
825         # addtibute value, that can be used as an anchor to link to the
826         # comment.
827         my $page=shift;
829         eval q{use Digest::MD5 'md5_hex'};
830         error($@) if $@;
832         return "comment-".md5_hex($page);
834         
835 package IkiWiki::PageSpec;
837 sub match_postcomment ($$;@) {
838         my $page = shift;
839         my $glob = shift;
841         if (! $postcomment) {
842                 return IkiWiki::FailReason->new("not posting a comment");
843         }
844         return match_glob($page, $glob);