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");
31 sub getsetup () { #{{{
37 # Pages where comments are shown, but new comments are not
38 # allowed, will show "Comments are closed".
39 comments_shown_pagespec => {
43 description => 'PageSpec for pages where comments will be shown inline',
44 link => 'ikiwiki/PageSpec',
48 comments_open_pagespec => {
50 example => 'blog/* and created_after(close_old_comments)',
52 description => 'PageSpec for pages where new comments can be posted',
53 link => 'ikiwiki/PageSpec',
57 comments_pagename => {
59 example => 'comment_',
60 default => 'comment_',
61 description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
62 safe => 0, # manual page moving required
65 comments_allowdirectives => {
69 description => 'Interpret directives in comments?',
73 comments_allowauthor => {
77 description => 'Allow anonymous commenters to set an author name?',
85 description => 'commit comments to the VCS',
86 # old uncommitted comments are likely to cause
87 # confusion if this is changed
95 return $params{content};
98 # FIXME: copied verbatim from meta
99 sub safeurl ($) { #{{{
101 if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
102 defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
103 return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
110 sub preprocess { # {{{
112 my $page = $params{page};
114 my $format = $params{format};
115 if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
116 error(sprintf(gettext("unsupported page format %s"), $format));
119 my $content = $params{content};
120 if (! defined $content) {
121 error(gettext("comment must have content"));
123 $content =~ s/\\"/"/g;
125 $content = IkiWiki::filter($page, $params{destpage}, $content);
127 if ($config{comments_allowdirectives}) {
128 $content = IkiWiki::preprocess($page, $params{destpage},
132 # no need to bother with htmlize if it's just HTML
133 $content = IkiWiki::htmlize($page, $params{destpage}, $format,
134 $content) if defined $format;
136 IkiWiki::run_hooks(sanitize => sub {
139 destpage => $params{destpage},
144 # set metadata, possibly overriding [[!meta]] directives from the
150 my $commentauthorurl;
152 if (defined $params{username}) {
153 $commentuser = $params{username};
154 ($commentauthorurl, $commentauthor) =
155 linkuser($params{username});
158 if (defined $params{ip}) {
159 $commentip = $params{ip};
161 $commentauthor = gettext("Anonymous");
164 $pagestate{$page}{comments}{commentuser} = $commentuser;
165 $pagestate{$page}{comments}{commentip} = $commentip;
166 $pagestate{$page}{comments}{commentauthor} = $commentauthor;
167 $pagestate{$page}{comments}{commentauthorurl} = $commentauthorurl;
168 if (! defined $pagestate{$page}{meta}{author}) {
169 $pagestate{$page}{meta}{author} = $commentauthor;
171 if (! defined $pagestate{$page}{meta}{authorurl}) {
172 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
175 if ($config{comments_allowauthor}) {
176 if (defined $params{claimedauthor}) {
177 $pagestate{$page}{meta}{author} = $params{claimedauthor};
180 if (defined $params{url} and safeurl($params{url})) {
181 $pagestate{$page}{meta}{authorurl} = $params{url};
185 $pagestate{$page}{meta}{author} = $commentauthor;
186 $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
189 if (defined $params{subject}) {
190 $pagestate{$page}{meta}{title} = $params{subject};
193 my $baseurl = urlto($params{destpage}, undef, 1);
195 if ($params{page} =~ m/\/(\Q$config{comments_pagename}\E\d+)$/) {
198 $pagestate{$page}{meta}{permalink} = "${baseurl}#${anchor}";
200 eval q{use Date::Parse};
202 my $time = str2time($params{date});
203 $IkiWiki::pagectime{$page} = $time if defined $time;
206 # FIXME: hard-coded HTML (although it's just to set an ID)
207 return "<div id=\"$anchor\">$content</div>" if $anchor;
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 $form->field(name => 'do', type => 'hidden');
316 $form->field(name => 'sid', type => 'hidden', value => $session->id,
318 $form->field(name => 'page', type => 'hidden');
319 $form->field(name => 'subject', type => 'text', size => 72);
320 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
321 $form->field(name => "type", value => $type, force => 1,
322 type => 'select', options => \@page_types);
324 $form->tmpl_param(username => $session->param('name'));
326 if ($config{comments_allowauthor} and
327 ! 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 # FIXME: is this right? Or should we be using the candidate subpage
350 # (whatever that might mean) as the base URL?
351 my $baseurl = urlto($page, undef, 1);
353 $form->title(sprintf(gettext("commenting on %s"),
354 IkiWiki::pagetitle($page)));
356 $form->tmpl_param('helponformattinglink',
357 htmllink($page, $page, 'ikiwiki/formatting',
359 linktext => 'FormattingHelp'),
360 allowdirectives => $config{allow_directives});
362 if ($form->submitted eq CANCEL) {
363 # bounce back to the page they wanted to comment on, and exit.
364 # CANCEL need not be considered in future
365 IkiWiki::redirect($cgi, urlto($page, undef, 1));
369 if (not exists $pagesources{$page}) {
370 error(sprintf(gettext(
371 "page '%s' doesn't exist, so you can't comment"),
375 if (not pagespec_match($page, $config{comments_open_pagespec},
376 location => $page)) {
377 error(sprintf(gettext(
378 "comments on page '%s' are closed"),
382 # Set a flag to indicate that we're posting a comment,
383 # so that postcomment() can tell it should match.
385 IkiWiki::check_canedit($page, $cgi, $session);
388 # FIXME: rather a simplistic way to make the comments...
394 $location = "$page/$config{comments_pagename}$i";
395 } while (-e "$config{srcdir}/$location._comment");
397 my $content = "[[!_comment format=$type\n";
399 # FIXME: handling of double quotes probably wrong?
400 if (defined $session->param('name')) {
401 my $username = $session->param('name');
402 $username =~ s/"/"/g;
403 $content .= " username=\"$username\"\n";
405 elsif (defined $ENV{REMOTE_ADDR}) {
406 my $ip = $ENV{REMOTE_ADDR};
407 if ($ip =~ m/^([.0-9]+)$/) {
408 $content .= " ip=\"$1\"\n";
412 if ($config{comments_allowauthor}) {
413 my $author = $form->field('author');
414 if (length $author) {
415 $author =~ s/"/"/g;
416 $content .= " claimedauthor=\"$author\"\n";
418 my $url = $form->field('url');
420 $url =~ s/"/"/g;
421 $content .= " url=\"$url\"\n";
425 my $subject = $form->field('subject');
426 if (length $subject) {
427 $subject =~ s/"/"/g;
428 $content .= " subject=\"$subject\"\n";
431 $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
433 my $editcontent = $form->field('editcontent') || '';
434 $editcontent =~ s/\r\n/\n/g;
435 $editcontent =~ s/\r/\n/g;
436 $editcontent =~ s/"/\\"/g;
437 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
439 # This is essentially a simplified version of editpage:
440 # - the user does not control the page that's created, only the parent
441 # - it's always a create operation, never an edit
442 # - this means that conflicts should never happen
443 # - this means that if they do, rocks fall and everyone dies
445 if ($form->submitted eq PREVIEW) {
446 my $preview = IkiWiki::htmlize($location, $page, '_comment',
447 IkiWiki::linkify($page, $page,
448 IkiWiki::preprocess($page, $page,
449 IkiWiki::filter($location,
452 IkiWiki::run_hooks(format => sub {
453 $preview = shift->(page => $page,
454 content => $preview);
457 my $template = template("comments_display.tmpl");
458 $template->param(content => $preview);
459 $template->param(title => $form->field('subject'));
460 $template->param(ctime => displaytime(time));
462 $form->tmpl_param(page_preview => $template->output);
465 $form->tmpl_param(page_preview => "");
468 if ($form->submitted eq POST_COMMENT && $form->validate) {
469 my $file = "$location._comment";
471 IkiWiki::checksessionexpiry($cgi, $session);
473 # FIXME: could probably do some sort of graceful retry
474 # on error? Would require significant unwinding though
475 writefile($file, $config{srcdir}, $content);
479 if ($config{rcs} and $config{comments_commit}) {
480 my $message = gettext("Added a comment");
481 if (defined $form->field('subject') &&
482 length $form->field('subject')) {
484 gettext("Added a comment: %s"),
485 $form->field('subject'));
488 IkiWiki::rcs_add($file);
489 IkiWiki::disable_commit_hook();
490 $conflict = IkiWiki::rcs_commit_staged($message,
491 $session->param('name'), $ENV{REMOTE_ADDR});
492 IkiWiki::enable_commit_hook();
493 IkiWiki::rcs_update();
496 # Now we need a refresh
497 require IkiWiki::Render;
499 IkiWiki::saveindex();
501 # this should never happen, unless a committer deliberately
502 # breaks it or something
503 error($conflict) if defined $conflict;
505 # Bounce back to where we were, but defeat broken caches
506 my $anticache = "?updated=$page/$config{comments_pagename}$i";
507 IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
510 IkiWiki::showform ($form, \@buttons, $session, $cgi,
511 forcebaseurl => $baseurl);
517 sub pagetemplate (@) { #{{{
520 my $page = $params{page};
521 my $template = $params{template};
523 if ($template->query(name => 'comments')) {
524 my $comments = undef;
527 my $shown = pagespec_match($page,
528 $config{comments_shown_pagespec},
531 if (pagespec_match($page, "*/$config{comments_pagename}*",
532 location => $page)) {
537 if (length $config{cgiurl}) {
538 $open = pagespec_match($page,
539 $config{comments_open_pagespec},
544 $comments = IkiWiki::preprocess_inline(
545 pages => "internal($page/$config{comments_pagename}*)",
546 template => 'comments_display',
550 destpage => $params{destpage},
551 feedfile => 'comments',
556 if (defined $comments && length $comments) {
557 $template->param(comments => $comments);
561 my $commenturl = IkiWiki::cgiurl(do => 'comment',
563 $template->param(commenturl => $commenturl);
567 if ($template->query(name => 'commentuser')) {
568 $template->param(commentuser =>
569 $pagestate{$page}{comments}{commentuser});
572 if ($template->query(name => 'commentip')) {
573 $template->param(commentip =>
574 $pagestate{$page}{comments}{commentip});
577 if ($template->query(name => 'commentauthor')) {
578 $template->param(commentauthor =>
579 $pagestate{$page}{comments}{commentauthor});
582 if ($template->query(name => 'commentauthorurl')) {
583 $template->param(commentauthorurl =>
584 $pagestate{$page}{comments}{commentauthorurl});
588 package IkiWiki::PageSpec;
590 sub match_postcomment ($$;@) {
594 if (! $postcomment) {
595 return IkiWiki::FailReason->new("not posting a comment");
597 return match_glob($page, $glob);