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";
21 hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
22 hook(type => "getsetup", id => 'comments', call => \&getsetup);
23 hook(type => "preprocess", id => '_comment', call => \&preprocess);
24 hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
25 hook(type => "htmlize", id => "_comment", call => \&htmlize);
26 hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
27 hook(type => "cgi", id => "comments", call => \&linkcgi);
28 IkiWiki::loadplugin("inline");
37 comments_pagespec => {
39 example => 'blog/* and !*/Discussion',
40 description => 'PageSpec of pages where comments are allowed',
41 link => 'ikiwiki/PageSpec',
45 comments_closed_pagespec => {
47 example => 'blog/controversial or blog/flamewar',
48 description => 'PageSpec of pages where posting new comments is not allowed',
49 link => 'ikiwiki/PageSpec',
53 comments_pagename => {
55 default => 'comment_',
56 description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
57 safe => 0, # manual page moving required
60 comments_allowdirectives => {
63 description => 'Interpret directives in comments?',
67 comments_allowauthor => {
70 description => 'Allow anonymous commenters to set an author name?',
77 description => 'commit comments to the VCS',
78 # old uncommitted comments are likely to cause
79 # confusion if this is changed
86 $config{comments_commit} = 1
87 unless defined $config{comments_commit};
88 $config{comments_pagespec} = ''
89 unless defined $config{comments_pagespec};
90 $config{comments_closed_pagespec} = ''
91 unless defined $config{comments_closed_pagespec};
92 $config{comments_pagename} = 'comment_'
93 unless defined $config{comments_pagename};
98 return $params{content};
101 # FIXME: copied verbatim from meta
104 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
105 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
106 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
115 my $page = $params{page};
117 my $format = $params{format};
118 if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
119 error(sprintf(gettext("unsupported page format %s"), $format));
122 my $content = $params{content};
123 if (! defined $content) {
124 error(gettext("comment must have content"));
126 $content =~ s/\\"/"/g;
128 $content = IkiWiki::filter($page, $params{destpage}, $content);
130 if ($config{comments_allowdirectives}) {
131 $content = IkiWiki::preprocess($page, $params{destpage},
135 # no need to bother with htmlize if it's just HTML
136 $content = IkiWiki::htmlize($page, $params{destpage}, $format,
137 $content) if defined $format;
139 IkiWiki::run_hooks(sanitize => sub {
142 destpage => $params{destpage},
147 # set metadata, possibly overriding [[!meta]] directives from the
153 my $commentauthorurl;
155 if (defined $params{username}) {
156 $commentuser = $params{username};
157 ($commentauthorurl, $commentauthor) =
158 linkuser($params{username});
161 if (defined $params{ip}) {
162 $commentip = $params{ip};
164 $commentauthor = gettext("Anonymous");
167 $pagestate{$page}{comments}{commentuser} = $commentuser;
168 $pagestate{$page}{comments}{commentip} = $commentip;
169 $pagestate{$page}{comments}{commentauthor} = $commentauthor;
170 $pagestate{$page}{comments}{commentauthorurl} = $commentauthorurl;
171 if (! defined $pagestate{$page}{meta}{author}) {
172 $pagestate{$page}{meta}{author} = $commentauthor;
174 if (! defined $pagestate{$page}{meta}{authorurl}) {
175 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
178 if ($config{comments_allowauthor}) {
179 if (defined $params{claimedauthor}) {
180 $pagestate{$page}{meta}{author} = $params{claimedauthor};
183 if (defined $params{url} and safeurl($params{url})) {
184 $pagestate{$page}{meta}{authorurl} = $params{url};
188 $pagestate{$page}{meta}{author} = $commentauthor;
189 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
192 if (defined $params{subject}) {
193 $pagestate{$page}{meta}{title} = $params{subject};
196 if ($params{page} =~ m/\/(\Q$config{comments_pagename}\E\d+)$/) {
197 $pagestate{$page}{meta}{permalink} = urlto($params{destpage}, undef, 1).
201 eval q{use Date::Parse};
203 my $time = str2time($params{date});
204 $IkiWiki::pagectime{$page} = $time if defined $time;
210 # This is exactly the same as recentchanges_link :-(
213 if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
215 my $page=decode_utf8($cgi->param("page"));
216 if (! defined $page) {
217 error("missing page parameter");
220 IkiWiki::loadindex();
222 my $link=bestlink("", $page);
223 if (! length $link) {
224 print "Content-type: text/html\n\n";
225 print IkiWiki::misctemplate(gettext(gettext("missing page")),
227 sprintf(gettext("The page %s does not exist."),
228 htmllink("", "", $page)).
232 IkiWiki::redirect($cgi, urlto($link, undef, 1));
239 # FIXME: basically the same logic as recentchanges
240 # returns (author URL, pretty-printed version)
243 my $oiduser = eval { IkiWiki::openiduser($user) };
245 if (defined $oiduser) {
246 return ($user, $oiduser);
248 # FIXME: it'd be good to avoid having such a link for anonymous
251 return (IkiWiki::cgiurl(
253 page => (length $config{userdir}
254 ? "$config{userdir}/$user"
260 # Mostly cargo-culted from IkiWiki::plugin::editpage
261 sub sessioncgi ($$) {
265 my $do = $cgi->param('do');
266 return unless $do eq 'comment';
268 IkiWiki::decode_cgi_utf8($cgi);
270 eval q{use CGI::FormBuilder};
273 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
274 my $form = CGI::FormBuilder->new(
275 fields => [qw{do sid page subject editcontent type author url}],
278 required => [qw{editcontent}],
281 action => $config{cgiurl},
284 template => scalar IkiWiki::template_params('comments_form.tmpl'),
285 # wtf does this do in editpage?
286 wikiname => $config{wikiname},
289 IkiWiki::decode_form_utf8($form);
290 IkiWiki::run_hooks(formbuilder_setup => sub {
291 shift->(title => "comment", form => $form, cgi => $cgi,
292 session => $session, buttons => \@buttons);
294 IkiWiki::decode_form_utf8($form);
296 my $type = $form->param('type');
297 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
298 $type = IkiWiki::possibly_foolish_untaint($type);
301 $type = $config{default_pageext};
304 if (exists $IkiWiki::hooks{htmlize}) {
305 @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
308 $form->field(name => 'do', type => 'hidden');
309 $form->field(name => 'sid', type => 'hidden', value => $session->id,
311 $form->field(name => 'page', type => 'hidden');
312 $form->field(name => 'subject', type => 'text', size => 72);
313 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
314 $form->field(name => "type", value => $type, force => 1,
315 type => 'select', options => \@page_types);
317 $form->tmpl_param(username => $session->param('name'));
319 if ($config{comments_allowauthor} and
320 ! defined $session->param('name')) {
321 $form->tmpl_param(allowauthor => 1);
322 $form->field(name => 'author', type => 'text', size => '40');
323 $form->field(name => 'url', type => 'text', size => '40');
326 $form->tmpl_param(allowauthor => 0);
327 $form->field(name => 'author', type => 'hidden', value => '',
329 $form->field(name => 'url', type => 'hidden', value => '',
333 # The untaint is OK (as in editpage) because we're about to pass
334 # it to file_pruned anyway
335 my $page = $form->field('page');
336 $page = IkiWiki::possibly_foolish_untaint($page);
337 if (! defined $page || ! length $page ||
338 IkiWiki::file_pruned($page, $config{srcdir})) {
339 error(gettext("bad page name"));
342 # FIXME: is this right? Or should we be using the candidate subpage
343 # (whatever that might mean) as the base URL?
344 my $baseurl = urlto($page, undef, 1);
346 $form->title(sprintf(gettext("commenting on %s"),
347 IkiWiki::pagetitle($page)));
349 $form->tmpl_param('helponformattinglink',
350 htmllink($page, $page, 'ikiwiki/formatting',
352 linktext => 'FormattingHelp'),
353 allowdirectives => $config{allow_directives});
355 if ($form->submitted eq CANCEL) {
356 # bounce back to the page they wanted to comment on, and exit.
357 # CANCEL need not be considered in future
358 IkiWiki::redirect($cgi, urlto($page, undef, 1));
362 if (not exists $pagesources{$page}) {
363 error(sprintf(gettext(
364 "page '%s' doesn't exist, so you can't comment"),
368 if (pagespec_match($page, $config{comments_closed_pagespec},
369 location => $page)) {
370 error(sprintf(gettext(
371 "comments on page '%s' are closed"),
375 # Set a flag to indicate that we're posting a comment,
376 # so that postcomment() can tell it should match.
378 IkiWiki::check_canedit($page, $cgi, $session);
381 # FIXME: rather a simplistic way to make the comments...
387 $location = "$page/$config{comments_pagename}$i";
388 } while (-e "$config{srcdir}/$location._comment");
390 my $content = "[[!_comment format=$type\n";
392 # FIXME: handling of double quotes probably wrong?
393 if (defined $session->param('name')) {
394 my $username = $session->param('name');
395 $username =~ s/"/"/g;
396 $content .= " username=\"$username\"\n";
398 elsif (defined $ENV{REMOTE_ADDR}) {
399 my $ip = $ENV{REMOTE_ADDR};
400 if ($ip =~ m/^([.0-9]+)$/) {
401 $content .= " ip=\"$1\"\n";
405 if ($config{comments_allowauthor}) {
406 my $author = $form->field('author');
407 if (length $author) {
408 $author =~ s/"/"/g;
409 $content .= " claimedauthor=\"$author\"\n";
411 my $url = $form->field('url');
413 $url =~ s/"/"/g;
414 $content .= " url=\"$url\"\n";
418 my $subject = $form->field('subject');
419 if (length $subject) {
420 $subject =~ s/"/"/g;
421 $content .= " subject=\"$subject\"\n";
424 $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
426 my $editcontent = $form->field('editcontent') || '';
427 $editcontent =~ s/\r\n/\n/g;
428 $editcontent =~ s/\r/\n/g;
429 $editcontent =~ s/"/\\"/g;
430 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
432 # This is essentially a simplified version of editpage:
433 # - the user does not control the page that's created, only the parent
434 # - it's always a create operation, never an edit
435 # - this means that conflicts should never happen
436 # - this means that if they do, rocks fall and everyone dies
438 if ($form->submitted eq PREVIEW) {
439 my $preview = IkiWiki::htmlize($location, $page, '_comment',
440 IkiWiki::linkify($page, $page,
441 IkiWiki::preprocess($page, $page,
442 IkiWiki::filter($location,
445 IkiWiki::run_hooks(format => sub {
446 $preview = shift->(page => $page,
447 content => $preview);
450 my $template = template("comment.tmpl");
451 $template->param(content => $preview);
452 $template->param(title => $form->field('subject'));
453 $template->param(ctime => displaytime(time));
455 $form->tmpl_param(page_preview => $template->output);
458 $form->tmpl_param(page_preview => "");
461 if ($form->submitted eq POST_COMMENT && $form->validate) {
462 my $file = "$location._comment";
464 IkiWiki::checksessionexpiry($cgi, $session);
466 # FIXME: could probably do some sort of graceful retry
467 # on error? Would require significant unwinding though
468 writefile($file, $config{srcdir}, $content);
472 if ($config{rcs} and $config{comments_commit}) {
473 my $message = gettext("Added a comment");
474 if (defined $form->field('subject') &&
475 length $form->field('subject')) {
477 gettext("Added a comment: %s"),
478 $form->field('subject'));
481 IkiWiki::rcs_add($file);
482 IkiWiki::disable_commit_hook();
483 $conflict = IkiWiki::rcs_commit_staged($message,
484 $session->param('name'), $ENV{REMOTE_ADDR});
485 IkiWiki::enable_commit_hook();
486 IkiWiki::rcs_update();
489 # Now we need a refresh
490 require IkiWiki::Render;
492 IkiWiki::saveindex();
494 # this should never happen, unless a committer deliberately
495 # breaks it or something
496 error($conflict) if defined $conflict;
498 # Jump to the new comment on the page.
499 IkiWiki::redirect($cgi, urlto($page, undef, 1)."#$location");
502 IkiWiki::showform ($form, \@buttons, $session, $cgi,
503 forcebaseurl => $baseurl);
509 sub commentsshown ($) {
512 return ! pagespec_match($page, "*/$config{comments_pagename}*",
513 location => $page) &&
514 pagespec_match($page, $config{comments_pagespec},
518 sub commentsopen ($) {
521 return length $config{cgiurl} > 0 &&
522 (! length $config{comments_closed_pagespec} ||
523 ! pagespec_match($page, $config{comments_closed_pagespec},
527 sub pagetemplate (@) {
530 my $page = $params{page};
531 my $template = $params{template};
532 my $shown = ($template->query(name => 'commentslink') ||
533 $template->query(name => 'comments')) &&
534 commentsshown($page);
536 if ($template->query(name => 'comments')) {
537 my $comments = undef;
539 $comments = IkiWiki::preprocess_inline(
540 pages => "internal($page/$config{comments_pagename}*)",
541 template => 'comment',
545 destpage => $params{destpage},
546 feedfile => 'comments',
551 if (defined $comments && length $comments) {
552 $template->param(comments => $comments);
555 if ($shown && commentsopen($page)) {
556 my $commenturl = IkiWiki::cgiurl(do => 'comment',
558 $template->param(commenturl => $commenturl);
562 if ($template->query(name => 'commentslink')) {
563 # XXX Would be nice to say how many comments there are in
564 # the link. But, to update the number, blog pages
565 # would have to update whenever comments of any inlines
566 # page are added, which is not currently done.
568 $template->param(commentslink =>
569 htmllink($page, $params{destpage}, $page,
570 linktext => gettext("Comments"),
571 anchor => "comments",
572 noimageinline => 1));
576 if ($template->query(name => 'commentuser')) {
577 $template->param(commentuser =>
578 $pagestate{$page}{comments}{commentuser});
581 if ($template->query(name => 'commentip')) {
582 $template->param(commentip =>
583 $pagestate{$page}{comments}{commentip});
586 if ($template->query(name => 'commentauthor')) {
587 $template->param(commentauthor =>
588 $pagestate{$page}{comments}{commentauthor});
591 if ($template->query(name => 'commentauthorurl')) {
592 $template->param(commentauthorurl =>
593 $pagestate{$page}{comments}{commentauthorurl});
597 package IkiWiki::PageSpec;
599 sub match_postcomment ($$;@) {
603 if (! $postcomment) {
604 return IkiWiki::FailReason->new("not posting a comment");
606 return match_glob($page, $glob);