]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/comments.pm
use md5sum for page_to_id
[ikiwiki.git] / IkiWiki / Plugin / comments.pm
index 32f3712f2423a05b04f91cd43da77af8adfa89b8..98f9f8b3de8ec224b0fa23a033b476e852a700a9 100644 (file)
@@ -25,8 +25,9 @@ sub import {
        hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
        hook(type => "htmlize", id => "_comment", call => \&htmlize);
        hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
-       hook(type => "cgi", id => "comments", call => \&linkcgi);
        hook(type => "formbuilder_setup", id => "comments", call => \&formbuilder_setup);
+       # Load goto to fix up user page links for logged-in commenters
+       IkiWiki::loadplugin("goto");
        IkiWiki::loadplugin("inline");
 }
 
@@ -167,7 +168,7 @@ sub preprocess {
                }
                else {
                        $commentauthorurl = IkiWiki::cgiurl(
-                               do => 'commenter',
+                               do => 'goto',
                                page => (length $config{userdir}
                                        ? "$config{userdir}/$commentuser"
                                        : "$commentuser"));
@@ -223,7 +224,7 @@ sub preprocess {
 
        if ($params{page} =~ m/\/(\Q$config{comments_pagename}\E\d+)$/) {
                $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page}), undef, 1).
-                       "#".$params{page};
+                       "#".page_to_id($params{page});
        }
 
        eval q{use Date::Parse};
@@ -235,35 +236,6 @@ sub preprocess {
        return $content;
 }
 
-# This is exactly the same as recentchanges_link :-(
-sub linkcgi ($) {
-       my $cgi=shift;
-       if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
-
-               my $page=decode_utf8($cgi->param("page"));
-               if (! defined $page) {
-                       error("missing page parameter");
-               }
-
-               IkiWiki::loadindex();
-
-               my $link=bestlink("", $page);
-               if (! length $link) {
-                       print "Content-type: text/html\n\n";
-                       print IkiWiki::misctemplate(gettext(gettext("missing page")),
-                               "<p>".
-                               sprintf(gettext("The page %s does not exist."),
-                                       htmllink("", "", $page)).
-                               "</p>");
-               }
-               else {
-                       IkiWiki::redirect($cgi, urlto($link, undef, 1));
-               }
-
-               exit;
-       }
-}
-
 sub sessioncgi ($$) {
        my $cgi=shift;
        my $session=shift;
@@ -442,8 +414,12 @@ sub editcomment ($$) {
        # - this means that if they do, rocks fall and everyone dies
 
        if ($form->submitted eq PREVIEW) {
-               $form->tmpl_param(page_preview => 
-                       previewcomment($content, $location, $page, time));
+               my $preview=previewcomment($content, $location, $page, time);
+               IkiWiki::run_hooks(format => sub {
+                       $preview = shift->(page => $page,
+                               content => $preview);
+               });
+               $form->tmpl_param(page_preview => $preview);
        }
        else {
                $form->tmpl_param(page_preview => "");
@@ -473,7 +449,7 @@ sub editcomment ($$) {
                        IkiWiki::printheader($session);
                        print IkiWiki::misctemplate(gettext(gettext("comment stored for moderation")),
                                "<p>".
-                               gettext("Your comment will be posted after moderator review"),
+                               gettext("Your comment will be posted after moderator review").
                                "</p>");
                        exit;
                }
@@ -514,7 +490,8 @@ sub editcomment ($$) {
                # Jump to the new comment on the page.
                # The trailing question mark tries to avoid broken
                # caches and get the most recent version of the page.
-               IkiWiki::redirect($cgi, urlto($page, undef, 1)."?updated#$location");
+               IkiWiki::redirect($cgi, urlto($page, undef, 1).
+                       "?updated#".page_to_id($location));
 
        }
        else {
@@ -603,10 +580,11 @@ sub commentmoderation ($$) {
                my ($id, $ctime)=@{$_};
                my $file="$config{wikistatedir}/comments_pending/$id";
                my $content=readfile($file);
+               my $preview=previewcomment($content, $id,
+                       IkiWiki::dirname($_), $ctime);
                {
                        id => $id,
-                       view => previewcomment($content, $id,
-                                       IkiWiki::dirname($_), $ctime),
+                       view => $preview,
                } 
        } sort { $b->[1] <=> $a->[1] } comments_pending();
 
@@ -616,7 +594,11 @@ sub commentmoderation ($$) {
                comments => \@comments,
        );
        IkiWiki::printheader($session);
-       print IkiWiki::misctemplate(gettext("comment moderation"), $template->output);
+       my $out=$template->output;
+       IkiWiki::run_hooks(format => sub {
+               $out = shift->(page => "", content => $out);
+       });
+       print IkiWiki::misctemplate(gettext("comment moderation"), $out);
        exit;
 }
 
@@ -624,7 +606,8 @@ sub formbuilder_setup (@) {
        my %params=@_;
 
        my $form=$params{form};
-       if ($form->title eq "preferences") {
+       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});
@@ -671,10 +654,6 @@ sub previewcomment ($$$) {
                        IkiWiki::linkify($location, $page,
                        IkiWiki::preprocess($location, $page,
                        IkiWiki::filter($location, $page, $content), 0, 1)));
-       IkiWiki::run_hooks(format => sub {
-               $preview = shift->(page => $page,
-                       content => $preview);
-       });
 
        my $template = template("comment.tmpl");
        $template->param(content => $preview);
@@ -694,7 +673,7 @@ sub previewcomment ($$$) {
 sub commentsshown ($) {
        my $page=shift;
 
-       return ! pagespec_match($page, "*/$config{comments_pagename}*",
+       return ! pagespec_match($page, "internal(*/$config{comments_pagename}*)",
                                location => $page) &&
               pagespec_match($page, $config{comments_pagespec},
                              location => $page);
@@ -781,6 +760,10 @@ sub pagetemplate (@) {
        if (!exists $commentstate{$page}) {
                return;
        }
+       
+       if ($template->query(name => 'commentid')) {
+               $template->param(commentid => page_to_id($page));
+       }
 
        if ($template->query(name => 'commentuser')) {
                $template->param(commentuser =>
@@ -830,6 +813,18 @@ sub unique_comment_location ($) {
        return $location;
 }
 
+sub page_to_id ($) {
+       # Converts a comment page name into a unique, legal html id
+       # addtibute value, that can be used as an anchor to link to the
+       # comment.
+       my $page=shift;
+
+       eval q{use Digest::MD5 'md5_hex'};
+       error($@) if $@;
+
+       return "comment-".md5_hex($page);
+}
+       
 package IkiWiki::PageSpec;
 
 sub match_postcomment ($$;@) {