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;
13 use constant PREVIEW => "Preview";
14 use constant POST_COMMENT => "Post comment";
15 use constant CANCEL => "Cancel";
18 hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
19 hook(type => "getsetup", id => 'comments', call => \&getsetup);
20 hook(type => "preprocess", id => 'comment', call => \&preprocess);
21 hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
22 hook(type => "htmlize", id => "_comment", call => \&htmlize);
23 hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
24 hook(type => "cgi", id => "comments", call => \&linkcgi);
25 IkiWiki::loadplugin("inline");
30 return $params{content};
33 # FIXME: copied verbatim from meta
34 sub safeurl ($) { #{{{
36 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
37 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
38 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
45 sub preprocess { # {{{
47 my $page = $params{page};
49 my $format = $params{format};
50 if (defined $format && !exists $IkiWiki::hooks{htmlize}{$format}) {
51 error(sprintf(gettext("unsupported page format %s"), $format));
54 my $content = $params{content};
55 if (!defined $content) {
56 error(gettext("comment must have content"));
58 $content =~ s/\\"/"/g;
60 $content = IkiWiki::filter($page, $params{destpage}, $content);
62 if ($config{comments_allowdirectives}) {
63 $content = IkiWiki::preprocess($page, $params{destpage},
67 # no need to bother with htmlize if it's just HTML
68 $content = IkiWiki::htmlize($page, $params{destpage}, $format,
69 $content) if defined $format;
71 IkiWiki::run_hooks(sanitize => sub {
74 destpage => $params{destpage},
79 # set metadata, possibly overriding [[!meta]] directives from the
87 if (defined $params{username}) {
88 $commentuser = $params{username};
89 ($commentauthorurl, $commentauthor) =
90 linkuser($params{username});
92 elsif (defined $params{ip}) {
93 $commentip = $params{ip};
94 $commentauthor = sprintf(
95 gettext("Anonymous (IP: %s)"), $params{ip});
98 $commentauthor = gettext("Anonymous");
101 $pagestate{$page}{comments}{commentuser} = $commentuser;
102 $pagestate{$page}{comments}{commentip} = $commentip;
103 $pagestate{$page}{comments}{commentauthor} = $commentauthor;
104 $pagestate{$page}{comments}{commentauthorurl} = $commentauthorurl;
105 if (!defined $pagestate{$page}{meta}{author}) {
106 $pagestate{$page}{meta}{author} = $commentauthor;
108 if (!defined $pagestate{$page}{meta}{authorurl}) {
109 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
112 if ($config{comments_allowauthor}) {
113 if (defined $params{claimedauthor}) {
114 $pagestate{$page}{meta}{author} = $params{claimedauthor};
117 if (defined $params{url} and safeurl($params{url})) {
118 $pagestate{$page}{meta}{authorurl} = $params{url};
122 $pagestate{$page}{meta}{author} = $commentauthor;
123 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
126 if (defined $params{subject}) {
127 $pagestate{$page}{meta}{title} = $params{subject};
130 my $baseurl = urlto($params{destpage}, undef, 1);
132 my $comments_pagename = $config{comments_pagename};
133 if ($params{page} =~ m/\/(\Q${comments_pagename}\E\d+)$/) {
136 $pagestate{$page}{meta}{permalink} = "${baseurl}#${anchor}";
138 eval q{use Date::Parse};
140 my $time = str2time($params{date});
141 $IkiWiki::pagectime{$page} = $time if defined $time;
144 # FIXME: hard-coded HTML (although it's just to set an ID)
145 return "<div id=\"$anchor\">$content</div>" if $anchor;
149 sub getsetup () { #{{{
155 # Pages where comments are shown, but new comments are not
156 # allowed, will show "Comments are closed".
157 comments_shown_pagespec => {
161 description => 'PageSpec for pages where comments will be shown inline',
162 link => 'ikiwiki/PageSpec',
166 comments_open_pagespec => {
168 example => 'blog/* and created_after(close_old_comments)',
170 description => 'PageSpec for pages where new comments can be posted',
171 link => 'ikiwiki/PageSpec',
175 comments_pagename => {
177 example => 'comment_',
178 default => 'comment_',
179 description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
180 safe => 0, # manual page moving will required
183 comments_allowdirectives => {
187 description => 'Interpret directives in comments?',
191 comments_allowauthor => {
195 description => 'Allow anonymous commenters to set an author name?',
203 description => 'commit comments to the VCS',
204 # old uncommitted comments are likely to cause
205 # confusion if this is changed
211 sub checkconfig () { #{{{
212 $config{comments_commit} = 1 unless defined $config{comments_commit};
213 $config{comments_pagename} = 'comment_'
214 unless defined $config{comments_pagename};
217 # This is exactly the same as recentchanges_link :-(
218 sub linkcgi ($) { #{{{
220 if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
222 my $page=decode_utf8($cgi->param("page"));
223 if (!defined $page) {
224 error("missing page parameter");
227 IkiWiki::loadindex();
229 my $link=bestlink("", $page);
230 if (! length $link) {
231 print "Content-type: text/html\n\n";
232 print IkiWiki::misctemplate(gettext(gettext("missing page")),
234 sprintf(gettext("The page %s does not exist."),
235 htmllink("", "", $page)).
239 IkiWiki::redirect($cgi, urlto($link, undef, 1));
246 # FIXME: basically the same logic as recentchanges
247 # returns (author URL, pretty-printed version)
248 sub linkuser ($) { # {{{
250 my $oiduser = eval { IkiWiki::openiduser($user) };
252 if (defined $oiduser) {
253 return ($user, $oiduser);
255 # FIXME: it'd be good to avoid having such a link for anonymous
258 return (IkiWiki::cgiurl(
260 page => (length $config{userdir}
261 ? "$config{userdir}/$user"
267 # Mostly cargo-culted from IkiWiki::plugin::editpage
268 sub sessioncgi ($$) { #{{{
272 my $do = $cgi->param('do');
273 return unless $do eq 'comment';
275 IkiWiki::decode_cgi_utf8($cgi);
277 eval q{use CGI::FormBuilder};
280 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
281 my $form = CGI::FormBuilder->new(
282 fields => [qw{do sid page subject editcontent type author url}],
285 required => [qw{editcontent}],
288 action => $config{cgiurl},
291 template => scalar IkiWiki::template_params('comments_form.tmpl'),
292 # wtf does this do in editpage?
293 wikiname => $config{wikiname},
296 IkiWiki::decode_form_utf8($form);
297 IkiWiki::run_hooks(formbuilder_setup => sub {
298 shift->(title => "comment", form => $form, cgi => $cgi,
299 session => $session, buttons => \@buttons);
301 IkiWiki::decode_form_utf8($form);
303 my $type = $form->param('type');
304 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
305 $type = IkiWiki::possibly_foolish_untaint($type);
308 $type = $config{default_pageext};
311 if (exists $IkiWiki::hooks{htmlize}) {
312 @page_types = grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}};
315 my $allow_author = $config{comments_allowauthor};
317 $form->field(name => 'do', type => 'hidden');
318 $form->field(name => 'sid', type => 'hidden', value => $session->id,
320 $form->field(name => 'page', type => 'hidden');
321 $form->field(name => 'subject', type => 'text', size => 72);
322 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
323 $form->field(name => "type", value => $type, force => 1,
324 type => 'select', options => \@page_types);
326 $form->tmpl_param(username => $session->param('name'));
328 if ($allow_author and !defined $session->param('name')) {
329 $form->tmpl_param(allowauthor => 1);
330 $form->field(name => 'author', type => 'text', size => '40');
331 $form->field(name => 'url', type => 'text', size => '40');
334 $form->tmpl_param(allowauthor => 0);
335 $form->field(name => 'author', type => 'hidden', value => '',
337 $form->field(name => 'url', type => 'hidden', value => '',
341 # The untaint is OK (as in editpage) because we're about to pass
342 # it to file_pruned anyway
343 my $page = $form->field('page');
344 $page = IkiWiki::possibly_foolish_untaint($page);
345 if (!defined $page || !length $page ||
346 IkiWiki::file_pruned($page, $config{srcdir})) {
347 error(gettext("bad page name"));
350 my $allow_directives = $config{comments_allowdirectives};
351 my $commit_comments = $config{comments_commit};
352 my $comments_pagename = $config{comments_pagename};
354 # FIXME: is this right? Or should we be using the candidate subpage
355 # (whatever that might mean) as the base URL?
356 my $baseurl = urlto($page, undef, 1);
358 $form->title(sprintf(gettext("commenting on %s"),
359 IkiWiki::pagetitle($page)));
361 $form->tmpl_param('helponformattinglink',
362 htmllink($page, $page, 'ikiwiki/formatting',
364 linktext => 'FormattingHelp'),
365 allowdirectives => $allow_directives);
367 if ($form->submitted eq CANCEL) {
368 # bounce back to the page they wanted to comment on, and exit.
369 # CANCEL need not be considered in future
370 IkiWiki::redirect($cgi, urlto($page, undef, 1));
374 if (not exists $pagesources{$page}) {
375 error(sprintf(gettext(
376 "page '%s' doesn't exist, so you can't comment"),
380 if (not pagespec_match($page, $config{comments_open_pagespec},
381 location => $page)) {
382 error(sprintf(gettext(
383 "comments on page '%s' are closed"),
387 IkiWiki::check_canedit($page . "[postcomment]", $cgi, $session);
389 my $editcontent = $form->field('editcontent') || '';
390 $editcontent =~ s/\r\n/\n/g;
391 $editcontent =~ s/\r/\n/g;
393 # FIXME: check that the wiki is locked right now, because
394 # if it's not, there are mad race conditions!
396 # FIXME: rather a simplistic way to make the comments...
402 $location = "$page/${comments_pagename}${i}";
403 } while (-e "$config{srcdir}/$location._comment");
405 my $anchor = "${comments_pagename}${i}";
407 $editcontent =~ s/"/\\"/g;
408 my $content = "[[!comment format=$type\n";
410 # FIXME: handling of double quotes probably wrong?
411 if (defined $session->param('name')) {
412 my $username = $session->param('name');
413 $username =~ s/"/"/g;
414 $content .= " username=\"$username\"\n";
416 elsif (defined $ENV{REMOTE_ADDR}) {
417 my $ip = $ENV{REMOTE_ADDR};
418 if ($ip =~ m/^([.0-9]+)$/) {
419 $content .= " ip=\"$1\"\n";
424 my $author = $form->field('author');
425 if (length $author) {
426 $author =~ s/"/"/g;
427 $content .= " claimedauthor=\"$author\"\n";
429 my $url = $form->field('url');
431 $url =~ s/"/"/g;
432 $content .= " url=\"$url\"\n";
436 my $subject = $form->field('subject');
437 if (length $subject) {
438 $subject =~ s/"/"/g;
439 $content .= " subject=\"$subject\"\n";
442 $content .= " date=\"" . IkiWiki::formattime(time, '%X %x') . "\"\n";
444 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
446 # This is essentially a simplified version of editpage:
447 # - the user does not control the page that's created, only the parent
448 # - it's always a create operation, never an edit
449 # - this means that conflicts should never happen
450 # - this means that if they do, rocks fall and everyone dies
452 if ($form->submitted eq PREVIEW) {
453 my $preview = IkiWiki::htmlize($location, $page, '_comment',
454 IkiWiki::linkify($page, $page,
455 IkiWiki::preprocess($page, $page,
456 IkiWiki::filter($location,
459 IkiWiki::run_hooks(format => sub {
460 $preview = shift->(page => $page,
461 content => $preview);
464 my $template = template("comments_display.tmpl");
465 $template->param(content => $preview);
466 $template->param(title => $form->field('subject'));
467 $template->param(ctime => displaytime(time));
469 $form->tmpl_param(page_preview => $template->output);
472 $form->tmpl_param(page_preview => "");
475 if ($form->submitted eq POST_COMMENT && $form->validate) {
476 my $file = "$location._comment";
478 IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
480 # FIXME: could probably do some sort of graceful retry
481 # on error? Would require significant unwinding though
482 writefile($file, $config{srcdir}, $content);
486 if ($config{rcs} and $commit_comments) {
487 my $message = gettext("Added a comment");
488 if (defined $form->field('subject') &&
489 length $form->field('subject')) {
491 gettext("Added a comment: %s"),
492 $form->field('subject'));
495 IkiWiki::rcs_add($file);
496 IkiWiki::disable_commit_hook();
497 $conflict = IkiWiki::rcs_commit_staged($message,
498 $session->param('name'), $ENV{REMOTE_ADDR});
499 IkiWiki::enable_commit_hook();
500 IkiWiki::rcs_update();
503 # Now we need a refresh
504 require IkiWiki::Render;
506 IkiWiki::saveindex();
508 # this should never happen, unless a committer deliberately
509 # breaks it or something
510 error($conflict) if defined $conflict;
512 # Bounce back to where we were, but defeat broken caches
513 my $anticache = "?updated=$page/${comments_pagename}${i}";
514 IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
517 IkiWiki::showform ($form, \@buttons, $session, $cgi,
518 forcebaseurl => $baseurl);
524 sub pagetemplate (@) { #{{{
527 my $page = $params{page};
528 my $template = $params{template};
530 if ($template->query(name => 'comments')) {
531 my $comments = undef;
533 my $comments_pagename = $config{comments_pagename};
536 my $shown = pagespec_match($page,
537 $config{comments_shown_pagespec},
540 if (pagespec_match($page, "*/${comments_pagename}*",
541 location => $page)) {
546 if (length $config{cgiurl}) {
547 $open = pagespec_match($page,
548 $config{comments_open_pagespec},
553 eval q{use IkiWiki::Plugin::inline};
557 pages => "internal($page/${comments_pagename}*)",
558 template => 'comments_display',
562 destpage => $params{destpage},
564 $comments = IkiWiki::preprocess_inline(@args);
567 if (defined $comments && length $comments) {
568 $template->param(comments => $comments);
572 my $commenturl = IkiWiki::cgiurl(do => 'comment',
574 $template->param(commenturl => $commenturl);
578 if ($template->query(name => 'commentuser')) {
579 $template->param(commentuser =>
580 $pagestate{$page}{comments}{commentuser});
583 if ($template->query(name => 'commentip')) {
584 $template->param(commentip =>
585 $pagestate{$page}{comments}{commentip});
588 if ($template->query(name => 'commentauthor')) {
589 $template->param(commentauthor =>
590 $pagestate{$page}{comments}{commentauthor});
593 if ($template->query(name => 'commentauthorurl')) {
594 $template->param(commentauthorurl =>
595 $pagestate{$page}{comments}{commentauthorurl});
599 package IkiWiki::PageSpec;
601 sub match_postcomment ($$;@) {
605 unless ($page =~ s/\[postcomment\]$//) {
606 return IkiWiki::FailReason->new("not posting a comment");
608 return match_glob($page, $glob);