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;
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 $pagestate{$page}{comments}{commentuser} = $commentuser;
185 $pagestate{$page}{comments}{commentopenid} = $commentopenid;
186 $pagestate{$page}{comments}{commentip} = $commentip;
187 $pagestate{$page}{comments}{commentauthor} = $commentauthor;
188 $pagestate{$page}{comments}{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} and safeurl($params{url})) {
202 $pagestate{$page}{meta}{authorurl} = $params{url};
206 $pagestate{$page}{meta}{author} = $commentauthor;
207 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
210 if (defined $params{subject}) {
211 $pagestate{$page}{meta}{title} = $params{subject};
214 if ($params{page} =~ m/\/(\Q$config{comments_pagename}\E\d+)$/) {
215 $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page}), undef, 1).
219 eval q{use Date::Parse};
221 my $time = str2time($params{date});
222 $IkiWiki::pagectime{$page} = $time if defined $time;
228 # This is exactly the same as recentchanges_link :-(
231 if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
233 my $page=decode_utf8($cgi->param("page"));
234 if (! defined $page) {
235 error("missing page parameter");
238 IkiWiki::loadindex();
240 my $link=bestlink("", $page);
241 if (! length $link) {
242 print "Content-type: text/html\n\n";
243 print IkiWiki::misctemplate(gettext(gettext("missing page")),
245 sprintf(gettext("The page %s does not exist."),
246 htmllink("", "", $page)).
250 IkiWiki::redirect($cgi, urlto($link, undef, 1));
257 # Mostly cargo-culted from IkiWiki::plugin::editpage
258 sub sessioncgi ($$) {
262 my $do = $cgi->param('do');
263 return unless $do eq 'comment';
265 IkiWiki::decode_cgi_utf8($cgi);
267 eval q{use CGI::FormBuilder};
270 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
271 my $form = CGI::FormBuilder->new(
272 fields => [qw{do sid page subject editcontent type author url}],
275 required => [qw{editcontent}],
278 action => $config{cgiurl},
281 template => scalar IkiWiki::template_params('editcomment.tmpl'),
284 IkiWiki::decode_form_utf8($form);
285 IkiWiki::run_hooks(formbuilder_setup => sub {
286 shift->(title => "comment", form => $form, cgi => $cgi,
287 session => $session, buttons => \@buttons);
289 IkiWiki::decode_form_utf8($form);
291 my $type = $form->param('type');
292 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
293 $type = IkiWiki::possibly_foolish_untaint($type);
296 $type = $config{default_pageext};
299 if (exists $IkiWiki::hooks{htmlize}) {
300 @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
303 $form->field(name => 'do', type => 'hidden');
304 $form->field(name => 'sid', type => 'hidden', value => $session->id,
306 $form->field(name => 'page', type => 'hidden');
307 $form->field(name => 'subject', type => 'text', size => 72);
308 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
309 $form->field(name => "type", value => $type, force => 1,
310 type => 'select', options => \@page_types);
312 $form->tmpl_param(username => $session->param('name'));
314 if ($config{comments_allowauthor} and
315 ! defined $session->param('name')) {
316 $form->tmpl_param(allowauthor => 1);
317 $form->field(name => 'author', type => 'text', size => '40');
318 $form->field(name => 'url', type => 'text', size => '40');
321 $form->tmpl_param(allowauthor => 0);
322 $form->field(name => 'author', type => 'hidden', value => '',
324 $form->field(name => 'url', type => 'hidden', value => '',
328 # The untaint is OK (as in editpage) because we're about to pass
329 # it to file_pruned anyway
330 my $page = $form->field('page');
331 $page = IkiWiki::possibly_foolish_untaint($page);
332 if (! defined $page || ! length $page ||
333 IkiWiki::file_pruned($page, $config{srcdir})) {
334 error(gettext("bad page name"));
337 # FIXME: is this right? Or should we be using the candidate subpage
338 # (whatever that might mean) as the base URL?
339 my $baseurl = urlto($page, undef, 1);
341 $form->title(sprintf(gettext("commenting on %s"),
342 IkiWiki::pagetitle($page)));
344 $form->tmpl_param('helponformattinglink',
345 htmllink($page, $page, 'ikiwiki/formatting',
347 linktext => 'FormattingHelp'),
348 allowdirectives => $config{allow_directives});
350 if ($form->submitted eq CANCEL) {
351 # bounce back to the page they wanted to comment on, and exit.
352 # CANCEL need not be considered in future
353 IkiWiki::redirect($cgi, urlto($page, undef, 1));
357 if (not exists $pagesources{$page}) {
358 error(sprintf(gettext(
359 "page '%s' doesn't exist, so you can't comment"),
363 if (pagespec_match($page, $config{comments_closed_pagespec},
364 location => $page)) {
365 error(sprintf(gettext(
366 "comments on page '%s' are closed"),
370 # Set a flag to indicate that we're posting a comment,
371 # so that postcomment() can tell it should match.
373 IkiWiki::check_canedit($page, $cgi, $session);
376 # FIXME: rather a simplistic way to make the comments...
382 $location = "$page/$config{comments_pagename}$i";
383 } while (-e "$config{srcdir}/$location._comment");
385 my $content = "[[!_comment format=$type\n";
387 # FIXME: handling of double quotes probably wrong?
388 if (defined $session->param('name')) {
389 my $username = $session->param('name');
390 $username =~ s/"/"/g;
391 $content .= " username=\"$username\"\n";
393 elsif (defined $ENV{REMOTE_ADDR}) {
394 my $ip = $ENV{REMOTE_ADDR};
395 if ($ip =~ m/^([.0-9]+)$/) {
396 $content .= " ip=\"$1\"\n";
400 if ($config{comments_allowauthor}) {
401 my $author = $form->field('author');
402 if (length $author) {
403 $author =~ s/"/"/g;
404 $content .= " claimedauthor=\"$author\"\n";
406 my $url = $form->field('url');
408 $url =~ s/"/"/g;
409 $content .= " url=\"$url\"\n";
413 my $subject = $form->field('subject');
414 if (length $subject) {
415 $subject =~ s/"/"/g;
416 $content .= " subject=\"$subject\"\n";
419 $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
421 my $editcontent = $form->field('editcontent') || '';
422 $editcontent =~ s/\r\n/\n/g;
423 $editcontent =~ s/\r/\n/g;
424 $editcontent =~ s/"/\\"/g;
425 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
427 # This is essentially a simplified version of editpage:
428 # - the user does not control the page that's created, only the parent
429 # - it's always a create operation, never an edit
430 # - this means that conflicts should never happen
431 # - this means that if they do, rocks fall and everyone dies
433 if ($form->submitted eq PREVIEW) {
434 my $preview = IkiWiki::htmlize($location, $page, '_comment',
435 IkiWiki::linkify($page, $page,
436 IkiWiki::preprocess($page, $page,
437 IkiWiki::filter($location,
440 IkiWiki::run_hooks(format => sub {
441 $preview = shift->(page => $page,
442 content => $preview);
445 my $template = template("comment.tmpl");
446 $template->param(content => $preview);
447 $template->param(title => $form->field('subject'));
448 $template->param(ctime => displaytime(time));
450 $form->tmpl_param(page_preview => $template->output);
453 $form->tmpl_param(page_preview => "");
456 if ($form->submitted eq POST_COMMENT && $form->validate) {
457 my $file = "$location._comment";
459 IkiWiki::checksessionexpiry($cgi, $session);
461 # FIXME: could probably do some sort of graceful retry
462 # on error? Would require significant unwinding though
463 writefile($file, $config{srcdir}, $content);
467 if ($config{rcs} and $config{comments_commit}) {
468 my $message = gettext("Added a comment");
469 if (defined $form->field('subject') &&
470 length $form->field('subject')) {
472 gettext("Added a comment: %s"),
473 $form->field('subject'));
476 IkiWiki::rcs_add($file);
477 IkiWiki::disable_commit_hook();
478 $conflict = IkiWiki::rcs_commit_staged($message,
479 $session->param('name'), $ENV{REMOTE_ADDR});
480 IkiWiki::enable_commit_hook();
481 IkiWiki::rcs_update();
484 # Now we need a refresh
485 require IkiWiki::Render;
487 IkiWiki::saveindex();
489 # this should never happen, unless a committer deliberately
490 # breaks it or something
491 error($conflict) if defined $conflict;
493 # Jump to the new comment on the page.
494 IkiWiki::redirect($cgi, urlto($page, undef, 1)."#$location");
497 IkiWiki::showform ($form, \@buttons, $session, $cgi,
498 forcebaseurl => $baseurl);
504 sub commentsshown ($) {
507 return ! pagespec_match($page, "*/$config{comments_pagename}*",
508 location => $page) &&
509 pagespec_match($page, $config{comments_pagespec},
513 sub commentsopen ($) {
516 return length $config{cgiurl} > 0 &&
517 (! length $config{comments_closed_pagespec} ||
518 ! pagespec_match($page, $config{comments_closed_pagespec},
522 sub pagetemplate (@) {
525 my $page = $params{page};
526 my $template = $params{template};
527 my $shown = ($template->query(name => 'commentslink') ||
528 $template->query(name => 'comments')) &&
529 commentsshown($page);
531 if ($template->query(name => 'comments')) {
532 my $comments = undef;
534 $comments = IkiWiki::preprocess_inline(
535 pages => "internal($page/$config{comments_pagename}*)",
536 template => 'comment',
540 destpage => $params{destpage},
541 feedfile => 'comments',
546 if (defined $comments && length $comments) {
547 $template->param(comments => $comments);
550 if ($shown && commentsopen($page)) {
551 my $commenturl = IkiWiki::cgiurl(do => 'comment',
553 $template->param(commenturl => $commenturl);
557 if ($template->query(name => 'commentslink')) {
558 # XXX Would be nice to say how many comments there are in
559 # the link. But, to update the number, blog pages
560 # would have to update whenever comments of any inlines
561 # page are added, which is not currently done.
563 $template->param(commentslink =>
564 htmllink($page, $params{destpage}, $page,
565 linktext => gettext("Comments"),
566 anchor => "comments",
567 noimageinline => 1));
571 if ($template->query(name => 'commentuser')) {
572 $template->param(commentuser =>
573 $pagestate{$page}{comments}{commentuser});
576 if ($template->query(name => 'commentip')) {
577 $template->param(commentip =>
578 $pagestate{$page}{comments}{commentip});
581 if ($template->query(name => 'commentauthor')) {
582 $template->param(commentauthor =>
583 $pagestate{$page}{comments}{commentauthor});
586 if ($template->query(name => 'commentauthorurl')) {
587 $template->param(commentauthorurl =>
588 $pagestate{$page}{comments}{commentauthorurl});
592 package IkiWiki::PageSpec;
594 sub match_postcomment ($$;@) {
598 if (! $postcomment) {
599 return IkiWiki::FailReason->new("not posting a comment");
601 return match_glob($page, $glob);