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";
19 hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
20 hook(type => "getsetup", id => 'comments', call => \&getsetup);
21 hook(type => "preprocess", id => '_comment', call => \&preprocess);
22 hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
23 hook(type => "htmlize", id => "_comment", call => \&htmlize);
24 hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
25 hook(type => "cgi", id => "comments", call => \&linkcgi);
26 IkiWiki::loadplugin("inline");
31 return $params{content};
34 # FIXME: copied verbatim from meta
35 sub safeurl ($) { #{{{
37 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
38 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
39 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
46 sub preprocess { # {{{
48 my $page = $params{page};
50 my $format = $params{format};
51 if (defined $format && !exists $IkiWiki::hooks{htmlize}{$format}) {
52 error(sprintf(gettext("unsupported page format %s"), $format));
55 my $content = $params{content};
56 if (!defined $content) {
57 error(gettext("comment must have content"));
59 $content =~ s/\\"/"/g;
61 $content = IkiWiki::filter($page, $params{destpage}, $content);
63 if ($config{comments_allowdirectives}) {
64 $content = IkiWiki::preprocess($page, $params{destpage},
68 # no need to bother with htmlize if it's just HTML
69 $content = IkiWiki::htmlize($page, $params{destpage}, $format,
70 $content) if defined $format;
72 IkiWiki::run_hooks(sanitize => sub {
75 destpage => $params{destpage},
80 # set metadata, possibly overriding [[!meta]] directives from the
88 if (defined $params{username}) {
89 $commentuser = $params{username};
90 ($commentauthorurl, $commentauthor) =
91 linkuser($params{username});
94 if (defined $params{ip}) {
95 $commentip = $params{ip};
97 $commentauthor = gettext("Anonymous");
100 $pagestate{$page}{comments}{commentuser} = $commentuser;
101 $pagestate{$page}{comments}{commentip} = $commentip;
102 $pagestate{$page}{comments}{commentauthor} = $commentauthor;
103 $pagestate{$page}{comments}{commentauthorurl} = $commentauthorurl;
104 if (!defined $pagestate{$page}{meta}{author}) {
105 $pagestate{$page}{meta}{author} = $commentauthor;
107 if (!defined $pagestate{$page}{meta}{authorurl}) {
108 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
111 if ($config{comments_allowauthor}) {
112 if (defined $params{claimedauthor}) {
113 $pagestate{$page}{meta}{author} = $params{claimedauthor};
116 if (defined $params{url} and safeurl($params{url})) {
117 $pagestate{$page}{meta}{authorurl} = $params{url};
121 $pagestate{$page}{meta}{author} = $commentauthor;
122 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
125 if (defined $params{subject}) {
126 $pagestate{$page}{meta}{title} = $params{subject};
129 my $baseurl = urlto($params{destpage}, undef, 1);
131 my $comments_pagename = $config{comments_pagename};
132 if ($params{page} =~ m/\/(\Q${comments_pagename}\E\d+)$/) {
135 $pagestate{$page}{meta}{permalink} = "${baseurl}#${anchor}";
137 eval q{use Date::Parse};
139 my $time = str2time($params{date});
140 $IkiWiki::pagectime{$page} = $time if defined $time;
143 # FIXME: hard-coded HTML (although it's just to set an ID)
144 return "<div id=\"$anchor\">$content</div>" if $anchor;
148 sub getsetup () { #{{{
154 # Pages where comments are shown, but new comments are not
155 # allowed, will show "Comments are closed".
156 comments_shown_pagespec => {
160 description => 'PageSpec for pages where comments will be shown inline',
161 link => 'ikiwiki/PageSpec',
165 comments_open_pagespec => {
167 example => 'blog/* and created_after(close_old_comments)',
169 description => 'PageSpec for pages where new comments can be posted',
170 link => 'ikiwiki/PageSpec',
174 comments_pagename => {
176 example => 'comment_',
177 default => 'comment_',
178 description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
179 safe => 0, # manual page moving will required
182 comments_allowdirectives => {
186 description => 'Interpret directives in comments?',
190 comments_allowauthor => {
194 description => 'Allow anonymous commenters to set an author name?',
202 description => 'commit comments to the VCS',
203 # old uncommitted comments are likely to cause
204 # confusion if this is changed
210 sub checkconfig () { #{{{
211 $config{comments_commit} = 1 unless defined $config{comments_commit};
212 $config{comments_pagename} = 'comment_'
213 unless defined $config{comments_pagename};
216 # This is exactly the same as recentchanges_link :-(
217 sub linkcgi ($) { #{{{
219 if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
221 my $page=decode_utf8($cgi->param("page"));
222 if (!defined $page) {
223 error("missing page parameter");
226 IkiWiki::loadindex();
228 my $link=bestlink("", $page);
229 if (! length $link) {
230 print "Content-type: text/html\n\n";
231 print IkiWiki::misctemplate(gettext(gettext("missing page")),
233 sprintf(gettext("The page %s does not exist."),
234 htmllink("", "", $page)).
238 IkiWiki::redirect($cgi, urlto($link, undef, 1));
245 # FIXME: basically the same logic as recentchanges
246 # returns (author URL, pretty-printed version)
247 sub linkuser ($) { # {{{
249 my $oiduser = eval { IkiWiki::openiduser($user) };
251 if (defined $oiduser) {
252 return ($user, $oiduser);
254 # FIXME: it'd be good to avoid having such a link for anonymous
257 return (IkiWiki::cgiurl(
259 page => (length $config{userdir}
260 ? "$config{userdir}/$user"
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('comments_form.tmpl'),
291 # wtf does this do in editpage?
292 wikiname => $config{wikiname},
295 IkiWiki::decode_form_utf8($form);
296 IkiWiki::run_hooks(formbuilder_setup => sub {
297 shift->(title => "comment", form => $form, cgi => $cgi,
298 session => $session, buttons => \@buttons);
300 IkiWiki::decode_form_utf8($form);
302 my $type = $form->param('type');
303 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
304 $type = IkiWiki::possibly_foolish_untaint($type);
307 $type = $config{default_pageext};
310 if (exists $IkiWiki::hooks{htmlize}) {
311 @page_types = grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}};
314 my $allow_author = $config{comments_allowauthor};
316 $form->field(name => 'do', type => 'hidden');
317 $form->field(name => 'sid', type => 'hidden', value => $session->id,
319 $form->field(name => 'page', type => 'hidden');
320 $form->field(name => 'subject', type => 'text', size => 72);
321 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
322 $form->field(name => "type", value => $type, force => 1,
323 type => 'select', options => \@page_types);
325 $form->tmpl_param(username => $session->param('name'));
327 if ($allow_author and !defined $session->param('name')) {
328 $form->tmpl_param(allowauthor => 1);
329 $form->field(name => 'author', type => 'text', size => '40');
330 $form->field(name => 'url', type => 'text', size => '40');
333 $form->tmpl_param(allowauthor => 0);
334 $form->field(name => 'author', type => 'hidden', value => '',
336 $form->field(name => 'url', type => 'hidden', value => '',
340 # The untaint is OK (as in editpage) because we're about to pass
341 # it to file_pruned anyway
342 my $page = $form->field('page');
343 $page = IkiWiki::possibly_foolish_untaint($page);
344 if (!defined $page || !length $page ||
345 IkiWiki::file_pruned($page, $config{srcdir})) {
346 error(gettext("bad page name"));
349 my $allow_directives = $config{comments_allowdirectives};
350 my $commit_comments = $config{comments_commit};
351 my $comments_pagename = $config{comments_pagename};
353 # FIXME: is this right? Or should we be using the candidate subpage
354 # (whatever that might mean) as the base URL?
355 my $baseurl = urlto($page, undef, 1);
357 $form->title(sprintf(gettext("commenting on %s"),
358 IkiWiki::pagetitle($page)));
360 $form->tmpl_param('helponformattinglink',
361 htmllink($page, $page, 'ikiwiki/formatting',
363 linktext => 'FormattingHelp'),
364 allowdirectives => $allow_directives);
366 if ($form->submitted eq CANCEL) {
367 # bounce back to the page they wanted to comment on, and exit.
368 # CANCEL need not be considered in future
369 IkiWiki::redirect($cgi, urlto($page, undef, 1));
373 if (not exists $pagesources{$page}) {
374 error(sprintf(gettext(
375 "page '%s' doesn't exist, so you can't comment"),
379 if (not pagespec_match($page, $config{comments_open_pagespec},
380 location => $page)) {
381 error(sprintf(gettext(
382 "comments on page '%s' are closed"),
386 IkiWiki::check_canedit($page . "[postcomment]", $cgi, $session);
388 my $editcontent = $form->field('editcontent') || '';
389 $editcontent =~ s/\r\n/\n/g;
390 $editcontent =~ s/\r/\n/g;
392 # FIXME: check that the wiki is locked right now, because
393 # if it's not, there are mad race conditions!
395 # FIXME: rather a simplistic way to make the comments...
401 $location = "$page/${comments_pagename}${i}";
402 } while (-e "$config{srcdir}/$location._comment");
404 my $anchor = "${comments_pagename}${i}";
406 $editcontent =~ s/"/\\"/g;
407 my $content = "[[!_comment format=$type\n";
409 # FIXME: handling of double quotes probably wrong?
410 if (defined $session->param('name')) {
411 my $username = $session->param('name');
412 $username =~ s/"/"/g;
413 $content .= " username=\"$username\"\n";
415 elsif (defined $ENV{REMOTE_ADDR}) {
416 my $ip = $ENV{REMOTE_ADDR};
417 if ($ip =~ m/^([.0-9]+)$/) {
418 $content .= " ip=\"$1\"\n";
423 my $author = $form->field('author');
424 if (length $author) {
425 $author =~ s/"/"/g;
426 $content .= " claimedauthor=\"$author\"\n";
428 my $url = $form->field('url');
430 $url =~ s/"/"/g;
431 $content .= " url=\"$url\"\n";
435 my $subject = $form->field('subject');
436 if (length $subject) {
437 $subject =~ s/"/"/g;
438 $content .= " subject=\"$subject\"\n";
441 $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
443 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
445 # This is essentially a simplified version of editpage:
446 # - the user does not control the page that's created, only the parent
447 # - it's always a create operation, never an edit
448 # - this means that conflicts should never happen
449 # - this means that if they do, rocks fall and everyone dies
451 if ($form->submitted eq PREVIEW) {
452 my $preview = IkiWiki::htmlize($location, $page, '_comment',
453 IkiWiki::linkify($page, $page,
454 IkiWiki::preprocess($page, $page,
455 IkiWiki::filter($location,
458 IkiWiki::run_hooks(format => sub {
459 $preview = shift->(page => $page,
460 content => $preview);
463 my $template = template("comments_display.tmpl");
464 $template->param(content => $preview);
465 $template->param(title => $form->field('subject'));
466 $template->param(ctime => displaytime(time));
468 $form->tmpl_param(page_preview => $template->output);
471 $form->tmpl_param(page_preview => "");
474 if ($form->submitted eq POST_COMMENT && $form->validate) {
475 my $file = "$location._comment";
477 IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
479 # FIXME: could probably do some sort of graceful retry
480 # on error? Would require significant unwinding though
481 writefile($file, $config{srcdir}, $content);
485 if ($config{rcs} and $commit_comments) {
486 my $message = gettext("Added a comment");
487 if (defined $form->field('subject') &&
488 length $form->field('subject')) {
490 gettext("Added a comment: %s"),
491 $form->field('subject'));
494 IkiWiki::rcs_add($file);
495 IkiWiki::disable_commit_hook();
496 $conflict = IkiWiki::rcs_commit_staged($message,
497 $session->param('name'), $ENV{REMOTE_ADDR});
498 IkiWiki::enable_commit_hook();
499 IkiWiki::rcs_update();
502 # Now we need a refresh
503 require IkiWiki::Render;
505 IkiWiki::saveindex();
507 # this should never happen, unless a committer deliberately
508 # breaks it or something
509 error($conflict) if defined $conflict;
511 # Bounce back to where we were, but defeat broken caches
512 my $anticache = "?updated=$page/${comments_pagename}${i}";
513 IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
516 IkiWiki::showform ($form, \@buttons, $session, $cgi,
517 forcebaseurl => $baseurl);
523 sub pagetemplate (@) { #{{{
526 my $page = $params{page};
527 my $template = $params{template};
529 if ($template->query(name => 'comments')) {
530 my $comments = undef;
532 my $comments_pagename = $config{comments_pagename};
535 my $shown = pagespec_match($page,
536 $config{comments_shown_pagespec},
539 if (pagespec_match($page, "*/${comments_pagename}*",
540 location => $page)) {
545 if (length $config{cgiurl}) {
546 $open = pagespec_match($page,
547 $config{comments_open_pagespec},
552 eval q{use IkiWiki::Plugin::inline};
556 pages => "internal($page/${comments_pagename}*)",
557 template => 'comments_display',
561 destpage => $params{destpage},
562 feedfile => 'comments',
565 $comments = IkiWiki::preprocess_inline(@args);
568 if (defined $comments && length $comments) {
569 $template->param(comments => $comments);
573 my $commenturl = IkiWiki::cgiurl(do => 'comment',
575 $template->param(commenturl => $commenturl);
579 if ($template->query(name => 'commentuser')) {
580 $template->param(commentuser =>
581 $pagestate{$page}{comments}{commentuser});
584 if ($template->query(name => 'commentip')) {
585 $template->param(commentip =>
586 $pagestate{$page}{comments}{commentip});
589 if ($template->query(name => 'commentauthor')) {
590 $template->param(commentauthor =>
591 $pagestate{$page}{comments}{commentauthor});
594 if ($template->query(name => 'commentauthorurl')) {
595 $template->param(commentauthorurl =>
596 $pagestate{$page}{comments}{commentauthorurl});
600 package IkiWiki::PageSpec;
602 sub match_postcomment ($$;@) {
606 unless ($page =~ s/\[postcomment\]$//) {
607 return IkiWiki::FailReason->new("not posting a comment");
609 return match_glob($page, $glob);