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;
12 use POSIX qw(strftime);
14 use constant PREVIEW => "Preview";
15 use constant POST_COMMENT => "Post comment";
16 use constant CANCEL => "Cancel";
22 hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
23 hook(type => "getsetup", id => 'comments', call => \&getsetup);
24 hook(type => "preprocess", id => '_comment', call => \&preprocess);
25 hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
26 hook(type => "htmlize", id => "_comment", call => \&htmlize);
27 hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
28 hook(type => "cgi", id => "comments", call => \&linkcgi);
29 IkiWiki::loadplugin("inline");
38 comments_pagespec => {
40 example => 'blog/* and !*/Discussion',
41 description => 'PageSpec of pages where comments are allowed',
42 link => 'ikiwiki/PageSpec',
46 comments_closed_pagespec => {
48 example => 'blog/controversial or blog/flamewar',
49 description => 'PageSpec of pages where posting new comments is not allowed',
50 link => 'ikiwiki/PageSpec',
54 comments_pagename => {
56 default => 'comment_',
57 description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
58 safe => 0, # manual page moving required
61 comments_allowdirectives => {
64 description => 'Interpret directives in comments?',
68 comments_allowauthor => {
71 description => 'Allow anonymous commenters to set an author name?',
78 description => 'commit comments to the VCS',
79 # old uncommitted comments are likely to cause
80 # confusion if this is changed
87 $config{comments_commit} = 1
88 unless defined $config{comments_commit};
89 $config{comments_pagespec} = ''
90 unless defined $config{comments_pagespec};
91 $config{comments_closed_pagespec} = ''
92 unless defined $config{comments_closed_pagespec};
93 $config{comments_pagename} = 'comment_'
94 unless defined $config{comments_pagename};
99 return $params{content};
102 # FIXME: copied verbatim from meta
105 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
106 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
107 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
116 my $page = $params{page};
118 my $format = $params{format};
119 if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
120 error(sprintf(gettext("unsupported page format %s"), $format));
123 my $content = $params{content};
124 if (! defined $content) {
125 error(gettext("comment must have content"));
127 $content =~ s/\\"/"/g;
129 $content = IkiWiki::filter($page, $params{destpage}, $content);
131 if ($config{comments_allowdirectives}) {
132 $content = IkiWiki::preprocess($page, $params{destpage},
136 # no need to bother with htmlize if it's just HTML
137 $content = IkiWiki::htmlize($page, $params{destpage}, $format,
138 $content) if defined $format;
140 IkiWiki::run_hooks(sanitize => sub {
143 destpage => $params{destpage},
148 # set metadata, possibly overriding [[!meta]] directives from the
154 my $commentauthorurl;
156 if (defined $params{username}) {
157 $commentuser = $params{username};
159 my $oiduser = eval { IkiWiki::openiduser($commentuser) };
161 if (defined $oiduser) {
162 # looks like an OpenID
163 $commentauthorurl = $commentuser;
164 $commentauthor = $oiduser;
165 $commentopenid = $commentuser;
168 $commentauthorurl = IkiWiki::cgiurl(
170 page => (length $config{userdir}
171 ? "$config{userdir}/$commentuser"
174 $commentauthor = $commentuser;
178 if (defined $params{ip}) {
179 $commentip = $params{ip};
181 $commentauthor = gettext("Anonymous");
184 $commentstate{$page}{commentuser} = $commentuser;
185 $commentstate{$page}{commentopenid} = $commentopenid;
186 $commentstate{$page}{commentip} = $commentip;
187 $commentstate{$page}{commentauthor} = $commentauthor;
188 $commentstate{$page}{commentauthorurl} = $commentauthorurl;
189 if (! defined $pagestate{$page}{meta}{author}) {
190 $pagestate{$page}{meta}{author} = $commentauthor;
192 if (! defined $pagestate{$page}{meta}{authorurl}) {
193 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
196 if ($config{comments_allowauthor}) {
197 if (defined $params{claimedauthor}) {
198 $pagestate{$page}{meta}{author} = $params{claimedauthor};
201 if (defined $params{url}) {
202 my $url=$params{url};
204 eval q{use URI::Heuristic};
206 $url=URI::Heuristic::uf_uristr($url);
210 $pagestate{$page}{meta}{authorurl} = $url;
215 $pagestate{$page}{meta}{author} = $commentauthor;
216 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
219 if (defined $params{subject}) {
220 $pagestate{$page}{meta}{title} = $params{subject};
223 if ($params{page} =~ m/\/(\Q$config{comments_pagename}\E\d+)$/) {
224 $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page}), undef, 1).
228 eval q{use Date::Parse};
230 my $time = str2time($params{date});
231 $IkiWiki::pagectime{$page} = $time if defined $time;
237 # This is exactly the same as recentchanges_link :-(
240 if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
242 my $page=decode_utf8($cgi->param("page"));
243 if (! defined $page) {
244 error("missing page parameter");
247 IkiWiki::loadindex();
249 my $link=bestlink("", $page);
250 if (! length $link) {
251 print "Content-type: text/html\n\n";
252 print IkiWiki::misctemplate(gettext(gettext("missing page")),
254 sprintf(gettext("The page %s does not exist."),
255 htmllink("", "", $page)).
259 IkiWiki::redirect($cgi, urlto($link, undef, 1));
266 # Mostly cargo-culted from IkiWiki::plugin::editpage
267 sub sessioncgi ($$) {
271 my $do = $cgi->param('do');
272 return unless $do eq 'comment';
274 IkiWiki::decode_cgi_utf8($cgi);
276 eval q{use CGI::FormBuilder};
279 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
280 my $form = CGI::FormBuilder->new(
281 fields => [qw{do sid page subject editcontent type author url}],
284 required => [qw{editcontent}],
287 action => $config{cgiurl},
290 template => scalar IkiWiki::template_params('editcomment.tmpl'),
293 IkiWiki::decode_form_utf8($form);
294 IkiWiki::run_hooks(formbuilder_setup => sub {
295 shift->(title => "comment", form => $form, cgi => $cgi,
296 session => $session, buttons => \@buttons);
298 IkiWiki::decode_form_utf8($form);
300 my $type = $form->param('type');
301 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
302 $type = IkiWiki::possibly_foolish_untaint($type);
305 $type = $config{default_pageext};
308 if (exists $IkiWiki::hooks{htmlize}) {
309 @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
312 $form->field(name => 'do', type => 'hidden');
313 $form->field(name => 'sid', type => 'hidden', value => $session->id,
315 $form->field(name => 'page', type => 'hidden');
316 $form->field(name => 'subject', type => 'text', size => 72);
317 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
318 $form->field(name => "type", value => $type, force => 1,
319 type => 'select', options => \@page_types);
321 $form->tmpl_param(username => $session->param('name'));
323 if ($config{comments_allowauthor} and
324 ! defined $session->param('name')) {
325 $form->tmpl_param(allowauthor => 1);
326 $form->field(name => 'author', type => 'text', size => '40');
327 $form->field(name => 'url', type => 'text', size => '40');
330 $form->tmpl_param(allowauthor => 0);
331 $form->field(name => 'author', type => 'hidden', value => '',
333 $form->field(name => 'url', type => 'hidden', value => '',
337 # The untaint is OK (as in editpage) because we're about to pass
338 # it to file_pruned anyway
339 my $page = $form->field('page');
340 $page = IkiWiki::possibly_foolish_untaint($page);
341 if (! defined $page || ! length $page ||
342 IkiWiki::file_pruned($page, $config{srcdir})) {
343 error(gettext("bad page name"));
346 my $baseurl = urlto($page, undef, 1);
348 $form->title(sprintf(gettext("commenting on %s"),
349 IkiWiki::pagetitle($page)));
351 $form->tmpl_param('helponformattinglink',
352 htmllink($page, $page, 'ikiwiki/formatting',
354 linktext => 'FormattingHelp'),
355 allowdirectives => $config{allow_directives});
357 if ($form->submitted eq CANCEL) {
358 # bounce back to the page they wanted to comment on, and exit.
359 # CANCEL need not be considered in future
360 IkiWiki::redirect($cgi, urlto($page, undef, 1));
364 if (not exists $pagesources{$page}) {
365 error(sprintf(gettext(
366 "page '%s' doesn't exist, so you can't comment"),
370 if (pagespec_match($page, $config{comments_closed_pagespec},
371 location => $page)) {
372 error(sprintf(gettext(
373 "comments on page '%s' are closed"),
377 # Set a flag to indicate that we're posting a comment,
378 # so that postcomment() can tell it should match.
380 IkiWiki::check_canedit($page, $cgi, $session);
383 # FIXME: rather a simplistic way to make the comments...
389 $location = "$page/$config{comments_pagename}$i";
390 } while (-e "$config{srcdir}/$location._comment");
392 my $content = "[[!_comment format=$type\n";
394 # FIXME: handling of double quotes probably wrong?
395 if (defined $session->param('name')) {
396 my $username = $session->param('name');
397 $username =~ s/"/"/g;
398 $content .= " username=\"$username\"\n";
400 elsif (defined $ENV{REMOTE_ADDR}) {
401 my $ip = $ENV{REMOTE_ADDR};
402 if ($ip =~ m/^([.0-9]+)$/) {
403 $content .= " ip=\"$1\"\n";
407 if ($config{comments_allowauthor}) {
408 my $author = $form->field('author');
409 if (length $author) {
410 $author =~ s/"/"/g;
411 $content .= " claimedauthor=\"$author\"\n";
413 my $url = $form->field('url');
415 $url =~ s/"/"/g;
416 $content .= " url=\"$url\"\n";
420 my $subject = $form->field('subject');
421 if (length $subject) {
422 $subject =~ s/"/"/g;
423 $content .= " subject=\"$subject\"\n";
426 $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
428 my $editcontent = $form->field('editcontent') || '';
429 $editcontent =~ s/\r\n/\n/g;
430 $editcontent =~ s/\r/\n/g;
431 $editcontent =~ s/"/\\"/g;
432 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
434 # This is essentially a simplified version of editpage:
435 # - the user does not control the page that's created, only the parent
436 # - it's always a create operation, never an edit
437 # - this means that conflicts should never happen
438 # - this means that if they do, rocks fall and everyone dies
440 if ($form->submitted eq PREVIEW) {
441 my $preview = IkiWiki::htmlize($location, $page, '_comment',
442 IkiWiki::linkify($location, $page,
443 IkiWiki::preprocess($location, $page,
444 IkiWiki::filter($location,
447 IkiWiki::run_hooks(format => sub {
448 $preview = shift->(page => $page,
449 content => $preview);
452 my $template = template("comment.tmpl");
453 $template->param(content => $preview);
454 $template->param(title => $form->field('subject'));
455 $template->param(ctime => displaytime(time));
457 IkiWiki::run_hooks(pagetemplate => sub {
458 shift->(page => $location,
460 template => $template);
463 $form->tmpl_param(page_preview => $template->output);
466 $form->tmpl_param(page_preview => "");
469 if ($form->submitted eq POST_COMMENT && $form->validate) {
470 IkiWiki::checksessionexpiry($cgi, $session);
473 IkiWiki::check_content(content => $form->field('editcontent'),
474 subject => $form->field('subject'),
475 $config{comments_allowauthor} ? (
476 author => $form->field('author'),
477 url => $form->field('url'),
480 cgi => $cgi, session => $session
484 my $file = "$location._comment";
486 # FIXME: could probably do some sort of graceful retry
487 # on error? Would require significant unwinding though
488 writefile($file, $config{srcdir}, $content);
492 if ($config{rcs} and $config{comments_commit}) {
493 my $message = gettext("Added a comment");
494 if (defined $form->field('subject') &&
495 length $form->field('subject')) {
497 gettext("Added a comment: %s"),
498 $form->field('subject'));
501 IkiWiki::rcs_add($file);
502 IkiWiki::disable_commit_hook();
503 $conflict = IkiWiki::rcs_commit_staged($message,
504 $session->param('name'), $ENV{REMOTE_ADDR});
505 IkiWiki::enable_commit_hook();
506 IkiWiki::rcs_update();
509 # Now we need a refresh
510 require IkiWiki::Render;
512 IkiWiki::saveindex();
514 # this should never happen, unless a committer deliberately
515 # breaks it or something
516 error($conflict) if defined $conflict;
518 # Jump to the new comment on the page.
519 # The trailing question mark tries to avoid broken
520 # caches and get the most recent version of the page.
521 IkiWiki::redirect($cgi, urlto($page, undef, 1)."?updated#$location");
525 IkiWiki::showform ($form, \@buttons, $session, $cgi,
526 forcebaseurl => $baseurl);
532 sub commentsshown ($) {
535 return ! pagespec_match($page, "*/$config{comments_pagename}*",
536 location => $page) &&
537 pagespec_match($page, $config{comments_pagespec},
541 sub commentsopen ($) {
544 return length $config{cgiurl} > 0 &&
545 (! length $config{comments_closed_pagespec} ||
546 ! pagespec_match($page, $config{comments_closed_pagespec},
550 sub pagetemplate (@) {
553 my $page = $params{page};
554 my $template = $params{template};
555 my $shown = ($template->query(name => 'commentslink') ||
556 $template->query(name => 'commentsurl') ||
557 $template->query(name => 'atomcommentsurl') ||
558 $template->query(name => 'comments')) &&
559 commentsshown($page);
561 if ($template->query(name => 'comments')) {
562 my $comments = undef;
564 $comments = IkiWiki::preprocess_inline(
565 pages => "internal($page/$config{comments_pagename}*)",
566 template => 'comment',
570 destpage => $params{destpage},
571 feedfile => 'comments',
576 if (defined $comments && length $comments) {
577 $template->param(comments => $comments);
580 if ($shown && commentsopen($page)) {
581 my $addcommenturl = IkiWiki::cgiurl(do => 'comment',
583 $template->param(addcommenturl => $addcommenturl);
587 if ($template->query(name => 'commentsurl')) {
589 $template->param(commentsurl =>
590 urlto($page, undef, 1).'#comments');
594 if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
596 # This will 404 until there are some comments, but I
597 # think that's probably OK...
598 $template->param(atomcommentsurl =>
599 urlto($page, undef, 1).'comments.atom');
603 if ($template->query(name => 'commentslink')) {
604 # XXX Would be nice to say how many comments there are in
605 # the link. But, to update the number, blog pages
606 # would have to update whenever comments of any inlines
607 # page are added, which is not currently done.
609 $template->param(commentslink =>
610 htmllink($page, $params{destpage}, $page,
611 linktext => gettext("Comments"),
612 anchor => "comments",
613 noimageinline => 1));
617 # everything below this point is only relevant to the comments
619 if (!exists $commentstate{$page}) {
623 if ($template->query(name => 'commentuser')) {
624 $template->param(commentuser =>
625 $commentstate{$page}{commentuser});
628 if ($template->query(name => 'commentopenid')) {
629 $template->param(commentopenid =>
630 $commentstate{$page}{commentopenid});
633 if ($template->query(name => 'commentip')) {
634 $template->param(commentip =>
635 $commentstate{$page}{commentip});
638 if ($template->query(name => 'commentauthor')) {
639 $template->param(commentauthor =>
640 $commentstate{$page}{commentauthor});
643 if ($template->query(name => 'commentauthorurl')) {
644 $template->param(commentauthorurl =>
645 $commentstate{$page}{commentauthorurl});
648 if ($template->query(name => 'removeurl') &&
649 IkiWiki::Plugin::remove->can("check_canremove") &&
650 length $config{cgiurl}) {
651 $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
653 $template->param(have_actions => 1);
657 package IkiWiki::PageSpec;
659 sub match_postcomment ($$;@) {
663 if (! $postcomment) {
664 return IkiWiki::FailReason->new("not posting a comment");
666 return match_glob($page, $glob);