+ my @comments=map {
+ my ($id, $dir, $ctime)=@{$_};
+ my $content=readfile("$dir/$id");
+ my $preview=previewcomment($content, $id,
+ $id, $ctime);
+ {
+ id => $id,
+ view => $preview,
+ }
+ } sort { $b->[2] <=> $a->[2] } comments_pending();
+
+ my $template=template("commentmoderation.tmpl");
+ $template->param(
+ sid => $session->id,
+ comments => \@comments,
+ cgiurl => IkiWiki::cgiurl(),
+ );
+ IkiWiki::printheader($session);
+ my $out=$template->output;
+ IkiWiki::run_hooks(format => sub {
+ $out = shift->(page => "", content => $out);
+ });
+ print IkiWiki::cgitemplate($cgi, gettext("comment moderation"), $out);
+ exit;
+}
+
+sub formbuilder_setup (@) {
+ my %params=@_;
+
+ my $form=$params{form};
+ if ($form->title eq "preferences" &&
+ IkiWiki::is_admin($params{session}->param("name"))) {
+ push @{$params{buttons}}, "Comment Moderation";
+ if ($form->submitted && $form->submitted eq "Comment Moderation") {
+ commentmoderation($params{cgi}, $params{session});
+ }
+ }
+}
+
+sub comments_pending () {
+ my @ret;
+
+ eval q{use File::Find};
+ error($@) if $@;
+ eval q{use Cwd};
+ error($@) if $@;
+ my $origdir=getcwd();
+
+ my $find_comments=sub {
+ my $dir=shift;
+ my $extension=shift;
+ return unless -d $dir;
+
+ chdir($dir) || die "chdir $dir: $!";
+
+ find({
+ no_chdir => 1,
+ wanted => sub {
+ my $file=decode_utf8($_);
+ $file=~s/^\.\///;
+ return if ! length $file || IkiWiki::file_pruned($file)
+ || -l $_ || -d _ || $file !~ /\Q$extension\E$/;
+ my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
+ if (defined $f) {
+ my $ctime=(stat($_))[10];
+ push @ret, [$f, $dir, $ctime];
+ }
+ }
+ }, ".");
+
+ chdir($origdir) || die "chdir $origdir: $!";
+ };
+
+ $find_comments->($config{srcdir}, "._comment_pending");
+ # old location
+ $find_comments->("$config{wikistatedir}/comments_pending/",
+ "._comment");
+
+ return @ret;
+}
+
+sub previewcomment ($$$) {
+ my $content=shift;
+ my $location=shift;
+ my $page=shift;
+ my $time=shift;
+
+ # Previewing a comment should implicitly enable comment posting mode.
+ my $oldpostcomment=$postcomment;
+ $postcomment=1;
+
+ my $preview = IkiWiki::htmlize($location, $page, '_comment',
+ IkiWiki::linkify($location, $page,
+ IkiWiki::preprocess($location, $page,
+ IkiWiki::filter($location, $page, $content), 0, 1)));
+
+ my $template = template("comment.tmpl");
+ $template->param(content => $preview);
+ $template->param(ctime => displaytime($time, undef, 1));
+ $template->param(html5 => $config{html5});
+
+ IkiWiki::run_hooks(pagetemplate => sub {
+ shift->(page => $location,
+ destpage => $page,
+ template => $template);
+ });
+
+ $template->param(have_actions => 0);
+
+ $postcomment=$oldpostcomment;
+
+ return $template->output;
+}
+
+sub commentsshown ($) {
+ my $page=shift;
+
+ return pagespec_match($page, $config{comments_pagespec},
+ location => $page);
+}
+
+sub commentsopen ($) {
+ my $page = shift;
+
+ return length $config{cgiurl} > 0 &&
+ (! length $config{comments_closed_pagespec} ||
+ ! pagespec_match($page, $config{comments_closed_pagespec},
+ location => $page));
+}
+
+sub pagetemplate (@) {
+ my %params = @_;
+
+ my $page = $params{page};
+ my $template = $params{template};
+ my $shown = ($template->query(name => 'commentslink') ||
+ $template->query(name => 'commentsurl') ||
+ $template->query(name => 'atomcommentsurl') ||
+ $template->query(name => 'comments')) &&
+ commentsshown($page);