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