robustness fix
[ikiwiki] / 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 => "preprocess", id => 'commentmoderation', call => \&preprocess_moderation);
26         # here for backwards compatability with old comments
27         hook(type => "preprocess", id => '_comment', call => \&preprocess);
28         hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
29         hook(type => "htmlize", id => "_comment", call => \&htmlize);
30         hook(type => "htmlize", id => "_comment_pending",
31                 call => \&htmlize_pending);
32         hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
33         hook(type => "formbuilder_setup", id => "comments",
34                 call => \&formbuilder_setup);
35         # Load goto to fix up user page links for logged-in commenters
36         IkiWiki::loadplugin("goto");
37         IkiWiki::loadplugin("inline");
38 }
39
40 sub getsetup () {
41         return
42                 plugin => {
43                         safe => 1,
44                         rebuild => 1,
45                         section => "web",
46                 },
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,
54                 },
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,
62                 },
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,
69                 },
70                 comments_allowdirectives => {
71                         type => 'boolean',
72                         example => 0,
73                         description => 'Interpret directives in comments?',
74                         safe => 1,
75                         rebuild => 0,
76                 },
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,
83                 },
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,
92                 },
93 }
94
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};
104 }
105
106 sub htmlize {
107         my %params = @_;
108         return $params{content};
109 }
110
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>');
117 }
118
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/;
125         }
126         else {
127                 return 1;
128         }
129 }
130
131 sub preprocess {
132         my %params = @_;
133         my $page = $params{page};
134
135         my $format = $params{format};
136         if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
137                 error(sprintf(gettext("unsupported page format %s"), $format));
138         }
139
140         my $content = $params{content};
141         if (! defined $content) {
142                 error(gettext("comment must have content"));
143         }
144         $content =~ s/\\"/"/g;
145
146         if ($config{comments_allowdirectives}) {
147                 $content = IkiWiki::preprocess($page, $params{destpage},
148                         $content);
149         }
150
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;
154
155         IkiWiki::run_hooks(sanitize => sub {
156                 $content = shift->(
157                         page => $page,
158                         destpage => $params{destpage},
159                         content => $content,
160                 );
161         });
162
163         # set metadata, possibly overriding [[!meta]] directives from the
164         # comment itself
165
166         my $commentuser;
167         my $commentip;
168         my $commentauthor;
169         my $commentauthorurl;
170         my $commentauthoravatar;
171         my $commentopenid;
172         if (defined $params{username}) {
173                 $commentuser = $params{username};
174
175                 my $oiduser = eval { IkiWiki::openiduser($commentuser) };
176
177                 if (defined $oiduser) {
178                         # looks like an OpenID
179                         $commentauthorurl = $commentuser;
180                         $commentauthor = (defined $params{nickname} && length $params{nickname}) ? $params{nickname} : $oiduser;
181                         $commentopenid = $commentuser;
182                 }
183                 else {
184                         $commentauthorurl = IkiWiki::cgiurl(
185                                 do => 'goto',
186                                 page => IkiWiki::userpage($commentuser)
187                         );
188
189                         $commentauthor = $commentuser;
190                 }
191
192                 eval q{use Libravatar::URL};
193                 if (! $@) {
194                         if (defined $commentopenid) {
195                                 eval {
196                                         $commentauthoravatar = libravatar_url(openid => $commentopenid, https => $ENV{HTTPS});
197                                 }
198                         }
199                         if (! defined $commentauthoravatar &&
200                             (my $email = IkiWiki::userinfo_get($commentuser, 'email'))) {
201                                 eval {
202                                         $commentauthoravatar = libravatar_url(email => $email, https => $ENV{HTTPS});
203                                 }
204                         }
205                 }
206         }
207         else {
208                 if (defined $params{ip}) {
209                         $commentip = $params{ip};
210                 }
211                 $commentauthor = gettext("Anonymous");
212         }
213
214         $commentstate{$page}{commentuser} = $commentuser;
215         $commentstate{$page}{commentopenid} = $commentopenid;
216         $commentstate{$page}{commentip} = $commentip;
217         $commentstate{$page}{commentauthor} = $commentauthor;
218         $commentstate{$page}{commentauthorurl} = $commentauthorurl;
219         $commentstate{$page}{commentauthoravatar} = $commentauthoravatar;
220         if (! defined $pagestate{$page}{meta}{author}) {
221                 $pagestate{$page}{meta}{author} = $commentauthor;
222         }
223         if (! defined $pagestate{$page}{meta}{authorurl}) {
224                 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
225         }
226
227         if ($config{comments_allowauthor}) {
228                 if (defined $params{claimedauthor}) {
229                         $pagestate{$page}{meta}{author} = $params{claimedauthor};
230                 }
231
232                 if (defined $params{url}) {
233                         my $url=$params{url};
234
235                         eval q{use URI::Heuristic}; 
236                         if (! $@) {
237                                 $url=URI::Heuristic::uf_uristr($url);
238                         }
239
240                         if (safeurl($url)) {
241                                 $pagestate{$page}{meta}{authorurl} = $url;
242                         }
243                 }
244         }
245         else {
246                 $pagestate{$page}{meta}{author} = $commentauthor;
247                 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
248         }
249
250         if (defined $params{subject}) {
251                 # decode title the same way meta does
252                 eval q{use HTML::Entities};
253                 $pagestate{$page}{meta}{title} = decode_entities($params{subject});
254         }
255
256         if ($params{page} =~ m/\/\Q$config{comments_pagename}\E\d+_/) {
257                 $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page})).
258                         "#".page_to_id($params{page});
259         }
260
261         eval q{use Date::Parse};
262         if (! $@) {
263                 my $time = str2time($params{date});
264                 $IkiWiki::pagectime{$page} = $time if defined $time;
265         }
266
267         return $content;
268 }
269
270 sub preprocess_moderation {
271         my %params = @_;
272
273         $params{desc}=gettext("Comment Moderation")
274                 unless defined $params{desc};
275
276         if (length $config{cgiurl}) {
277                 return '<a href="'.
278                         IkiWiki::cgiurl(do => 'commentmoderation').
279                         '">'.$params{desc}.'</a>';
280         }
281         else {
282                 return $params{desc};
283         }
284 }
285
286 sub sessioncgi ($$) {
287         my $cgi=shift;
288         my $session=shift;
289
290         my $do = $cgi->param('do');
291         if ($do eq 'comment') {
292                 editcomment($cgi, $session);
293         }
294         elsif ($do eq 'commentmoderation') {
295                 commentmoderation($cgi, $session);
296         }
297         elsif ($do eq 'commentsignin') {
298                 IkiWiki::cgi_signin($cgi, $session);
299                 exit;
300         }
301 }
302
303 # Mostly cargo-culted from IkiWiki::plugin::editpage
304 sub editcomment ($$) {
305         my $cgi=shift;
306         my $session=shift;
307
308         IkiWiki::decode_cgi_utf8($cgi);
309
310         eval q{use CGI::FormBuilder};
311         error($@) if $@;
312
313         my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
314         my $form = CGI::FormBuilder->new(
315                 fields => [qw{do sid page subject editcontent type author url}],
316                 charset => 'utf-8',
317                 method => 'POST',
318                 required => [qw{editcontent}],
319                 javascript => 0,
320                 params => $cgi,
321                 action => IkiWiki::cgiurl(),
322                 header => 0,
323                 table => 0,
324                 template => { template('editcomment.tmpl') },
325         );
326
327         IkiWiki::decode_form_utf8($form);
328         IkiWiki::run_hooks(formbuilder_setup => sub {
329                         shift->(title => "comment", form => $form, cgi => $cgi,
330                                 session => $session, buttons => \@buttons);
331                 });
332         IkiWiki::decode_form_utf8($form);
333
334         my $type = $form->param('type');
335         if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
336                 $type = IkiWiki::possibly_foolish_untaint($type);
337         }
338         else {
339                 $type = $config{default_pageext};
340         }
341
342
343         my @page_types;
344         if (exists $IkiWiki::hooks{htmlize}) {
345                 foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
346                         push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
347                 }
348         }
349         @page_types=sort @page_types;
350
351         $form->field(name => 'do', type => 'hidden');
352         $form->field(name => 'sid', type => 'hidden', value => $session->id,
353                 force => 1);
354         $form->field(name => 'page', type => 'hidden');
355         $form->field(name => 'subject', type => 'text', size => 72);
356         $form->field(name => 'editcontent', type => 'textarea', rows => 10);
357         $form->field(name => "type", value => $type, force => 1,
358                 type => 'select', options => \@page_types);
359
360         $form->tmpl_param(username => $session->param('name'));
361
362         if ($config{comments_allowauthor} and
363             ! defined $session->param('name')) {
364                 $form->tmpl_param(allowauthor => 1);
365                 $form->field(name => 'author', type => 'text', size => '40');
366                 $form->field(name => 'url', type => 'text', size => '40');
367         }
368         else {
369                 $form->tmpl_param(allowauthor => 0);
370                 $form->field(name => 'author', type => 'hidden', value => '',
371                         force => 1);
372                 $form->field(name => 'url', type => 'hidden', value => '',
373                         force => 1);
374         }
375
376         if (! defined $session->param('name')) {
377                 # Make signinurl work and return here.
378                 $form->tmpl_param(signinurl => IkiWiki::cgiurl(do => 'commentsignin'));
379                 $session->param(postsignin => $ENV{QUERY_STRING});
380                 IkiWiki::cgi_savesession($session);
381         }
382
383         # The untaint is OK (as in editpage) because we're about to pass
384         # it to file_pruned and wiki_file_regexp anyway.
385         my ($page) = $form->field('page')=~/$config{wiki_file_regexp}/;
386         $page = IkiWiki::possibly_foolish_untaint($page);
387         if (! defined $page || ! length $page ||
388                 IkiWiki::file_pruned($page)) {
389                 error(gettext("bad page name"));
390         }
391
392         $form->title(sprintf(gettext("commenting on %s"),
393                         IkiWiki::pagetitle(IkiWiki::basename($page))));
394
395         $form->tmpl_param('helponformattinglink',
396                 htmllink($page, $page, 'ikiwiki/formatting',
397                         noimageinline => 1,
398                         linktext => 'FormattingHelp'),
399                         allowdirectives => $config{allow_directives});
400
401         if ($form->submitted eq CANCEL) {
402                 # bounce back to the page they wanted to comment on, and exit.
403                 IkiWiki::redirect($cgi, urlto($page));
404                 exit;
405         }
406
407         if (not exists $pagesources{$page}) {
408                 error(sprintf(gettext(
409                         "page '%s' doesn't exist, so you can't comment"),
410                         $page));
411         }
412
413         if (pagespec_match($page, $config{comments_closed_pagespec},
414                 location => $page)) {
415                 error(sprintf(gettext(
416                         "comments on page '%s' are closed"),
417                         $page));
418         }
419
420         # Set a flag to indicate that we're posting a comment,
421         # so that postcomment() can tell it should match.
422         $postcomment=1;
423         IkiWiki::check_canedit($page, $cgi, $session);
424         $postcomment=0;
425
426         my $content = "[[!comment format=$type\n";
427
428         if (defined $session->param('name')) {
429                 my $username = $session->param('name');
430                 $username =~ s/"/&quot;/g;
431                 $content .= " username=\"$username\"\n";
432         }
433         if (defined $session->param('nickname')) {
434                 my $nickname = $session->param('nickname');
435                 $nickname =~ s/"/&quot;/g;
436                 $content .= " nickname=\"$nickname\"\n";
437         }
438         elsif (defined $session->remote_addr()) {
439                 my $ip = $session->remote_addr();
440                 if ($ip =~ m/^([.0-9]+)$/) {
441                         $content .= " ip=\"$1\"\n";
442                 }
443         }
444
445         if ($config{comments_allowauthor}) {
446                 my $author = $form->field('author');
447                 if (defined $author && length $author) {
448                         $author =~ s/"/&quot;/g;
449                         $content .= " claimedauthor=\"$author\"\n";
450                 }
451                 my $url = $form->field('url');
452                 if (defined $url && length $url) {
453                         $url =~ s/"/&quot;/g;
454                         $content .= " url=\"$url\"\n";
455                 }
456         }
457
458         my $subject = $form->field('subject');
459         if (defined $subject && length $subject) {
460                 $subject =~ s/"/&quot;/g;
461         }
462         else {
463                 $subject = "comment ".(num_comments($page, $config{srcdir}) + 1);
464         }
465         $content .= " subject=\"$subject\"\n";
466
467         $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
468
469         my $editcontent = $form->field('editcontent');
470         $editcontent="" if ! defined $editcontent;
471         $editcontent =~ s/\r\n/\n/g;
472         $editcontent =~ s/\r/\n/g;
473         $editcontent =~ s/"/\\"/g;
474         $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
475
476         my $location=unique_comment_location($page, $content, $config{srcdir});
477
478         # This is essentially a simplified version of editpage:
479         # - the user does not control the page that's created, only the parent
480         # - it's always a create operation, never an edit
481         # - this means that conflicts should never happen
482         # - this means that if they do, rocks fall and everyone dies
483
484         if ($form->submitted eq PREVIEW) {
485                 my $preview=previewcomment($content, $location, $page, time);
486                 IkiWiki::run_hooks(format => sub {
487                         $preview = shift->(page => $page,
488                                 content => $preview);
489                 });
490                 $form->tmpl_param(page_preview => $preview);
491         }
492         else {
493                 $form->tmpl_param(page_preview => "");
494         }
495
496         if ($form->submitted eq POST_COMMENT && $form->validate) {
497                 IkiWiki::checksessionexpiry($cgi, $session);
498                 
499                 $postcomment=1;
500                 my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
501                         subject => $form->field('subject'),
502                         $config{comments_allowauthor} ? (
503                                 author => $form->field('author'),
504                                 url => $form->field('url'),
505                         ) : (),
506                         page => $location,
507                         cgi => $cgi,
508                         session => $session,
509                         nonfatal => 1,
510                 );
511                 $postcomment=0;
512
513                 if (! $ok) {
514                         $location=unique_comment_location($page, $content, $config{srcdir}, "._comment_pending");
515                         writefile("$location._comment_pending", $config{srcdir}, $content);
516
517                         # Refresh so anything that deals with pending
518                         # comments can be updated.
519                         require IkiWiki::Render;
520                         IkiWiki::refresh();
521                         IkiWiki::saveindex();
522
523                         IkiWiki::printheader($session);
524                         print IkiWiki::cgitemplate($cgi, gettext(gettext("comment stored for moderation")),
525                                 "<p>".
526                                 gettext("Your comment will be posted after moderator review").
527                                 "</p>");
528                         exit;
529                 }
530
531                 # FIXME: could probably do some sort of graceful retry
532                 # on error? Would require significant unwinding though
533                 my $file = "$location._comment";
534                 writefile($file, $config{srcdir}, $content);
535
536                 my $conflict;
537
538                 if ($config{rcs} and $config{comments_commit}) {
539                         my $message = gettext("Added a comment");
540                         if (defined $form->field('subject') &&
541                                 length $form->field('subject')) {
542                                 $message = sprintf(
543                                         gettext("Added a comment: %s"),
544                                         $form->field('subject'));
545                         }
546
547                         IkiWiki::rcs_add($file);
548                         IkiWiki::disable_commit_hook();
549                         $conflict = IkiWiki::rcs_commit_staged(
550                                 message => $message,
551                                 session => $session,
552                         );
553                         IkiWiki::enable_commit_hook();
554                         IkiWiki::rcs_update();
555                 }
556
557                 # Now we need a refresh
558                 require IkiWiki::Render;
559                 IkiWiki::refresh();
560                 IkiWiki::saveindex();
561
562                 # this should never happen, unless a committer deliberately
563                 # breaks it or something
564                 error($conflict) if defined $conflict;
565
566                 # Jump to the new comment on the page.
567                 # The trailing question mark tries to avoid broken
568                 # caches and get the most recent version of the page.
569                 IkiWiki::redirect($cgi, urlto($page).
570                         "?updated#".page_to_id($location));
571
572         }
573         else {
574                 IkiWiki::showform($form, \@buttons, $session, $cgi,
575                         page => $page);
576         }
577
578         exit;
579 }
580
581 sub commentmoderation ($$) {
582         my $cgi=shift;
583         my $session=shift;
584
585         IkiWiki::needsignin($cgi, $session);
586         if (! IkiWiki::is_admin($session->param("name"))) {
587                 error(gettext("you are not logged in as an admin"));
588         }
589
590         IkiWiki::decode_cgi_utf8($cgi);
591         
592         if (defined $cgi->param('sid')) {
593                 IkiWiki::checksessionexpiry($cgi, $session);
594
595                 my $rejectalldefer=$cgi->param('rejectalldefer');
596
597                 my %vars=$cgi->Vars;
598                 my $added=0;
599                 foreach my $id (keys %vars) {
600                         if ($id =~ /(.*)\._comment(?:_pending)?$/) {
601                                 $id=decode_utf8($id);
602                                 my $action=$cgi->param($id);
603                                 next if $action eq 'Defer' && ! $rejectalldefer;
604
605                                 # Make sure that the id is of a legal
606                                 # pending comment.
607                                 my ($f) = $id =~ /$config{wiki_file_regexp}/;
608                                 if (! defined $f || ! length $f ||
609                                     IkiWiki::file_pruned($f)) {
610                                         error("illegal file");
611                                 }
612
613                                 my $page=IkiWiki::dirname($f);
614                                 my $file="$config{srcdir}/$f";
615                                 if (! -e $file) {
616                                         # old location
617                                         $file="$config{wikistatedir}/comments_pending/".$f;
618                                 }
619
620                                 if ($action eq 'Accept') {
621                                         my $content=eval { readfile($file) };
622                                         next if $@; # file vanished since form was displayed
623                                         my $dest=unique_comment_location($page, $content, $config{srcdir})."._comment";
624                                         writefile($dest, $config{srcdir}, $content);
625                                         if ($config{rcs} and $config{comments_commit}) {
626                                                 IkiWiki::rcs_add($dest);
627                                         }
628                                         $added++;
629                                 }
630
631                                 require IkiWiki::Render;
632                                 IkiWiki::prune($file);
633                         }
634                 }
635
636                 if ($added) {
637                         my $conflict;
638                         if ($config{rcs} and $config{comments_commit}) {
639                                 my $message = gettext("Comment moderation");
640                                 IkiWiki::disable_commit_hook();
641                                 $conflict=IkiWiki::rcs_commit_staged(
642                                         message => $message,
643                                         session => $session,
644                                 );
645                                 IkiWiki::enable_commit_hook();
646                                 IkiWiki::rcs_update();
647                         }
648                 
649                         # Now we need a refresh
650                         require IkiWiki::Render;
651                         IkiWiki::refresh();
652                         IkiWiki::saveindex();
653                 
654                         error($conflict) if defined $conflict;
655                 }
656         }
657
658         my @comments=map {
659                 my ($id, $dir, $ctime)=@{$_};
660                 my $content=readfile("$dir/$id");
661                 my $preview=previewcomment($content, $id,
662                         $id, $ctime);
663                 {
664                         id => $id,
665                         view => $preview,
666                 }
667         } sort { $b->[2] <=> $a->[2] } comments_pending();
668
669         my $template=template("commentmoderation.tmpl");
670         $template->param(
671                 sid => $session->id,
672                 comments => \@comments,
673                 cgiurl => IkiWiki::cgiurl(),
674         );
675         IkiWiki::printheader($session);
676         my $out=$template->output;
677         IkiWiki::run_hooks(format => sub {
678                 $out = shift->(page => "", content => $out);
679         });
680         print IkiWiki::cgitemplate($cgi, gettext("comment moderation"), $out);
681         exit;
682 }
683
684 sub formbuilder_setup (@) {
685         my %params=@_;
686
687         my $form=$params{form};
688         if ($form->title eq "preferences" &&
689             IkiWiki::is_admin($params{session}->param("name"))) {
690                 push @{$params{buttons}}, "Comment Moderation";
691                 if ($form->submitted && $form->submitted eq "Comment Moderation") {
692                         commentmoderation($params{cgi}, $params{session});
693                 }
694         }
695 }
696
697 sub comments_pending () {
698         my @ret;
699
700         eval q{use File::Find};
701         error($@) if $@;
702         eval q{use Cwd};
703         error($@) if $@;
704         my $origdir=getcwd();
705
706         my $find_comments=sub {
707                 my $dir=shift;
708                 my $extension=shift;
709                 return unless -d $dir;
710
711                 chdir($dir) || die "chdir $dir: $!";
712
713                 find({
714                         no_chdir => 1,
715                         wanted => sub {
716                                 my $file=decode_utf8($_);
717                                 $file=~s/^\.\///;
718                                 return if ! length $file || IkiWiki::file_pruned($file)
719                                         || -l $_ || -d _ || $file !~ /\Q$extension\E$/;
720                                 my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
721                                 if (defined $f) {
722                                         my $ctime=(stat($_))[10];
723                                         push @ret, [$f, $dir, $ctime];
724                                 }
725                         }
726                 }, ".");
727
728                 chdir($origdir) || die "chdir $origdir: $!";
729         };
730         
731         $find_comments->($config{srcdir}, "._comment_pending");
732         # old location
733         $find_comments->("$config{wikistatedir}/comments_pending/",
734                 "._comment");
735
736         return @ret;
737 }
738
739 sub previewcomment ($$$) {
740         my $content=shift;
741         my $location=shift;
742         my $page=shift;
743         my $time=shift;
744
745         # Previewing a comment should implicitly enable comment posting mode.
746         my $oldpostcomment=$postcomment;
747         $postcomment=1;
748
749         my $preview = IkiWiki::htmlize($location, $page, '_comment',
750                         IkiWiki::linkify($location, $page,
751                         IkiWiki::preprocess($location, $page,
752                         IkiWiki::filter($location, $page, $content), 0, 1)));
753
754         my $template = template("comment.tmpl");
755         $template->param(content => $preview);
756         $template->param(ctime => displaytime($time, undef, 1));
757         $template->param(html5 => $config{html5});
758
759         IkiWiki::run_hooks(pagetemplate => sub {
760                 shift->(page => $location,
761                         destpage => $page,
762                         template => $template);
763         });
764
765         $template->param(have_actions => 0);
766
767         $postcomment=$oldpostcomment;
768
769         return $template->output;
770 }
771
772 sub commentsshown ($) {
773         my $page=shift;
774
775         return pagespec_match($page, $config{comments_pagespec},
776                 location => $page);
777 }
778
779 sub commentsopen ($) {
780         my $page = shift;
781
782         return length $config{cgiurl} > 0 &&
783                (! length $config{comments_closed_pagespec} ||
784                 ! pagespec_match($page, $config{comments_closed_pagespec},
785                                  location => $page));
786 }
787
788 sub pagetemplate (@) {
789         my %params = @_;
790
791         my $page = $params{page};
792         my $template = $params{template};
793         my $shown = ($template->query(name => 'commentslink') ||
794                      $template->query(name => 'commentsurl') ||
795                      $template->query(name => 'atomcommentsurl') ||
796                      $template->query(name => 'comments')) &&
797                     commentsshown($page);
798
799         if ($template->query(name => 'comments')) {
800                 my $comments = undef;
801                 if ($shown) {
802                         $comments = IkiWiki::preprocess_inline(
803                                 pages => "comment($page) and !comment($page/*)",
804                                 template => 'comment',
805                                 show => 0,
806                                 reverse => 'yes',
807                                 page => $page,
808                                 destpage => $params{destpage},
809                                 feedfile => 'comments',
810                                 emptyfeeds => 'no',
811                         );
812                 }
813
814                 if (defined $comments && length $comments) {
815                         $template->param(comments => $comments);
816                 }
817
818                 if ($shown && commentsopen($page)) {
819                         $template->param(addcommenturl => addcommenturl($page));
820                 }
821         }
822
823         if ($shown) {
824                 if ($template->query(name => 'commentsurl')) {
825                         $template->param(commentsurl =>
826                                 urlto($page).'#comments');
827                 }
828
829                 if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
830                         # This will 404 until there are some comments, but I
831                         # think that's probably OK...
832                         $template->param(atomcommentsurl =>
833                                 urlto($page).'comments.atom');
834                 }
835
836                 if ($template->query(name => 'commentslink')) {
837                         my $num=num_comments($page, $config{srcdir});
838                         my $link;
839                         if ($num > 0) {
840                                 $link = htmllink($page, $params{destpage}, $page,
841                                         linktext => sprintf(ngettext("%i comment", "%i comments", $num), $num),
842                                         anchor => "comments",
843                                         noimageinline => 1
844                                 );
845                         }
846                         elsif (commentsopen($page)) {
847                                 $link = "<a href=\"".addcommenturl($page)."\">".
848                                         #translators: Here "Comment" is a verb;
849                                         #translators: the user clicks on it to
850                                         #translators: post a comment.
851                                         gettext("Comment").
852                                         "</a>";
853                         }
854                         $template->param(commentslink => $link)
855                                 if defined $link;
856                 }
857         }
858
859         # everything below this point is only relevant to the comments
860         # themselves
861         if (!exists $commentstate{$page}) {
862                 return;
863         }
864         
865         if ($template->query(name => 'commentid')) {
866                 $template->param(commentid => page_to_id($page));
867         }
868
869         if ($template->query(name => 'commentuser')) {
870                 $template->param(commentuser =>
871                         $commentstate{$page}{commentuser});
872         }
873
874         if ($template->query(name => 'commentopenid')) {
875                 $template->param(commentopenid =>
876                         $commentstate{$page}{commentopenid});
877         }
878
879         if ($template->query(name => 'commentip')) {
880                 $template->param(commentip =>
881                         $commentstate{$page}{commentip});
882         }
883
884         if ($template->query(name => 'commentauthor')) {
885                 $template->param(commentauthor =>
886                         $commentstate{$page}{commentauthor});
887         }
888
889         if ($template->query(name => 'commentauthorurl')) {
890                 $template->param(commentauthorurl =>
891                         $commentstate{$page}{commentauthorurl});
892         }
893
894         if ($template->query(name => 'commentauthoravatar')) {
895                 $template->param(commentauthoravatar =>
896                         $commentstate{$page}{commentauthoravatar});
897         }
898
899         if ($template->query(name => 'removeurl') &&
900             IkiWiki::Plugin::remove->can("check_canremove") &&
901             length $config{cgiurl}) {
902                 $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
903                         page => $page));
904                 $template->param(have_actions => 1);
905         }
906 }
907
908 sub addcommenturl ($) {
909         my $page=shift;
910
911         return IkiWiki::cgiurl(do => 'comment', page => $page);
912 }
913
914 sub num_comments ($$) {
915         my $page=shift;
916         my $dir=shift;
917
918         my @comments=glob("$dir/$page/$config{comments_pagename}*._comment");
919         return int @comments;
920 }
921
922 sub unique_comment_location ($$$$) {
923         my $page=shift;
924         eval q{use Digest::MD5 'md5_hex'};
925         error($@) if $@;
926         my $content_md5=md5_hex(Encode::encode_utf8(shift));
927         my $dir=shift;
928         my $ext=shift || "._comment";
929
930         my $location;
931         my $i = num_comments($page, $dir);
932         do {
933                 $i++;
934                 $location = "$page/$config{comments_pagename}${i}_${content_md5}";
935         } while (-e "$dir/$location$ext");
936
937         return $location;
938 }
939
940 sub page_to_id ($) {
941         # Converts a comment page name into a unique, legal html id
942         # attribute value, that can be used as an anchor to link to the
943         # comment.
944         my $page=shift;
945
946         eval q{use Digest::MD5 'md5_hex'};
947         error($@) if $@;
948
949         return "comment-".md5_hex(Encode::encode_utf8(($page)));
950 }
951         
952 package IkiWiki::PageSpec;
953
954 sub match_postcomment ($$;@) {
955         my $page = shift;
956         my $glob = shift;
957
958         if (! $postcomment) {
959                 return IkiWiki::FailReason->new("not posting a comment");
960         }
961         return match_glob($page, $glob, @_);
962 }
963
964 sub match_comment ($$;@) {
965         my $page = shift;
966         my $glob = shift;
967
968         if (! $postcomment) {
969                 # To see if it's a comment, check the source file type.
970                 # Deal with comments that were just deleted.
971                 my $source=exists $IkiWiki::pagesources{$page} ?
972                         $IkiWiki::pagesources{$page} :
973                         $IkiWiki::delpagesources{$page};
974                 my $type=defined $source ? IkiWiki::pagetype($source) : undef;
975                 if (! defined $type || $type ne "_comment") {
976                         return IkiWiki::FailReason->new("$page is not a comment");
977                 }
978         }
979
980         return match_glob($page, "$glob/*", internal => 1, @_);
981 }
982
983 sub match_comment_pending ($$;@) {
984         my $page = shift;
985         my $glob = shift;
986         
987         my $source=exists $IkiWiki::pagesources{$page} ?
988                 $IkiWiki::pagesources{$page} :
989                 $IkiWiki::delpagesources{$page};
990         my $type=defined $source ? IkiWiki::pagetype($source) : undef;
991         if (! defined $type || $type ne "_comment_pending") {
992                 return IkiWiki::FailReason->new("$page is not a pending comment");
993         }
994
995         return match_glob($page, "$glob/*", internal => 1, @_);
996 }
997
998 1