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 # FIXME: basically the same logic as recentchanges
258 # returns (author URL, pretty-printed version)
261 my $oiduser = eval { IkiWiki::openiduser($user) };
263 if (defined $oiduser) {
264 return ($user, $oiduser);
266 # FIXME: it'd be good to avoid having such a link for anonymous
269 return (IkiWiki::cgiurl(
271 page => (length $config{userdir}
272 ? "$config{userdir}/$user"
278 # Mostly cargo-culted from IkiWiki::plugin::editpage
279 sub sessioncgi ($$) {
283 my $do = $cgi->param('do');
284 return unless $do eq 'comment';
286 IkiWiki::decode_cgi_utf8($cgi);
288 eval q{use CGI::FormBuilder};
291 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
292 my $form = CGI::FormBuilder->new(
293 fields => [qw{do sid page subject editcontent type author url}],
296 required => [qw{editcontent}],
299 action => $config{cgiurl},
302 template => scalar IkiWiki::template_params('editcomment.tmpl'),
305 IkiWiki::decode_form_utf8($form);
306 IkiWiki::run_hooks(formbuilder_setup => sub {
307 shift->(title => "comment", form => $form, cgi => $cgi,
308 session => $session, buttons => \@buttons);
310 IkiWiki::decode_form_utf8($form);
312 my $type = $form->param('type');
313 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
314 $type = IkiWiki::possibly_foolish_untaint($type);
317 $type = $config{default_pageext};
320 if (exists $IkiWiki::hooks{htmlize}) {
321 @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
324 $form->field(name => 'do', type => 'hidden');
325 $form->field(name => 'sid', type => 'hidden', value => $session->id,
327 $form->field(name => 'page', type => 'hidden');
328 $form->field(name => 'subject', type => 'text', size => 72);
329 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
330 $form->field(name => "type", value => $type, force => 1,
331 type => 'select', options => \@page_types);
333 $form->tmpl_param(username => $session->param('name'));
335 if ($config{comments_allowauthor} and
336 ! defined $session->param('name')) {
337 $form->tmpl_param(allowauthor => 1);
338 $form->field(name => 'author', type => 'text', size => '40');
339 $form->field(name => 'url', type => 'text', size => '40');
342 $form->tmpl_param(allowauthor => 0);
343 $form->field(name => 'author', type => 'hidden', value => '',
345 $form->field(name => 'url', type => 'hidden', value => '',
349 # The untaint is OK (as in editpage) because we're about to pass
350 # it to file_pruned anyway
351 my $page = $form->field('page');
352 $page = IkiWiki::possibly_foolish_untaint($page);
353 if (! defined $page || ! length $page ||
354 IkiWiki::file_pruned($page, $config{srcdir})) {
355 error(gettext("bad page name"));
358 # FIXME: is this right? Or should we be using the candidate subpage
359 # (whatever that might mean) as the base URL?
360 my $baseurl = urlto($page, undef, 1);
362 $form->title(sprintf(gettext("commenting on %s"),
363 IkiWiki::pagetitle($page)));
365 $form->tmpl_param('helponformattinglink',
366 htmllink($page, $page, 'ikiwiki/formatting',
368 linktext => 'FormattingHelp'),
369 allowdirectives => $config{allow_directives});
371 if ($form->submitted eq CANCEL) {
372 # bounce back to the page they wanted to comment on, and exit.
373 # CANCEL need not be considered in future
374 IkiWiki::redirect($cgi, urlto($page, undef, 1));
378 if (not exists $pagesources{$page}) {
379 error(sprintf(gettext(
380 "page '%s' doesn't exist, so you can't comment"),
384 if (pagespec_match($page, $config{comments_closed_pagespec},
385 location => $page)) {
386 error(sprintf(gettext(
387 "comments on page '%s' are closed"),
391 # Set a flag to indicate that we're posting a comment,
392 # so that postcomment() can tell it should match.
394 IkiWiki::check_canedit($page, $cgi, $session);
397 # FIXME: rather a simplistic way to make the comments...
403 $location = "$page/$config{comments_pagename}$i";
404 } while (-e "$config{srcdir}/$location._comment");
406 my $content = "[[!_comment format=$type\n";
408 # FIXME: handling of double quotes probably wrong?
409 if (defined $session->param('name')) {
410 my $username = $session->param('name');
411 $username =~ s/"/"/g;
412 $content .= " username=\"$username\"\n";
414 elsif (defined $ENV{REMOTE_ADDR}) {
415 my $ip = $ENV{REMOTE_ADDR};
416 if ($ip =~ m/^([.0-9]+)$/) {
417 $content .= " ip=\"$1\"\n";
421 if ($config{comments_allowauthor}) {
422 my $author = $form->field('author');
423 if (length $author) {
424 $author =~ s/"/"/g;
425 $content .= " claimedauthor=\"$author\"\n";
427 my $url = $form->field('url');
429 $url =~ s/"/"/g;
430 $content .= " url=\"$url\"\n";
434 my $subject = $form->field('subject');
435 if (length $subject) {
436 $subject =~ s/"/"/g;
437 $content .= " subject=\"$subject\"\n";
440 $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
442 my $editcontent = $form->field('editcontent') || '';
443 $editcontent =~ s/\r\n/\n/g;
444 $editcontent =~ s/\r/\n/g;
445 $editcontent =~ s/"/\\"/g;
446 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
448 # This is essentially a simplified version of editpage:
449 # - the user does not control the page that's created, only the parent
450 # - it's always a create operation, never an edit
451 # - this means that conflicts should never happen
452 # - this means that if they do, rocks fall and everyone dies
454 if ($form->submitted eq PREVIEW) {
455 my $preview = IkiWiki::htmlize($location, $page, '_comment',
456 IkiWiki::linkify($page, $page,
457 IkiWiki::preprocess($page, $page,
458 IkiWiki::filter($location,
461 IkiWiki::run_hooks(format => sub {
462 $preview = shift->(page => $page,
463 content => $preview);
466 my $template = template("comment.tmpl");
467 $template->param(content => $preview);
468 $template->param(title => $form->field('subject'));
469 $template->param(ctime => displaytime(time));
471 $form->tmpl_param(page_preview => $template->output);
474 $form->tmpl_param(page_preview => "");
477 if ($form->submitted eq POST_COMMENT && $form->validate) {
478 my $file = "$location._comment";
480 IkiWiki::checksessionexpiry($cgi, $session);
482 # FIXME: could probably do some sort of graceful retry
483 # on error? Would require significant unwinding though
484 writefile($file, $config{srcdir}, $content);
488 if ($config{rcs} and $config{comments_commit}) {
489 my $message = gettext("Added a comment");
490 if (defined $form->field('subject') &&
491 length $form->field('subject')) {
493 gettext("Added a comment: %s"),
494 $form->field('subject'));
497 IkiWiki::rcs_add($file);
498 IkiWiki::disable_commit_hook();
499 $conflict = IkiWiki::rcs_commit_staged($message,
500 $session->param('name'), $ENV{REMOTE_ADDR});
501 IkiWiki::enable_commit_hook();
502 IkiWiki::rcs_update();
505 # Now we need a refresh
506 require IkiWiki::Render;
508 IkiWiki::saveindex();
510 # this should never happen, unless a committer deliberately
511 # breaks it or something
512 error($conflict) if defined $conflict;
514 # Jump to the new comment on the page.
515 IkiWiki::redirect($cgi, urlto($page, undef, 1)."#$location");
518 IkiWiki::showform ($form, \@buttons, $session, $cgi,
519 forcebaseurl => $baseurl);
525 sub commentsshown ($) {
528 return ! pagespec_match($page, "*/$config{comments_pagename}*",
529 location => $page) &&
530 pagespec_match($page, $config{comments_pagespec},
534 sub commentsopen ($) {
537 return length $config{cgiurl} > 0 &&
538 (! length $config{comments_closed_pagespec} ||
539 ! pagespec_match($page, $config{comments_closed_pagespec},
543 sub pagetemplate (@) {
546 my $page = $params{page};
547 my $template = $params{template};
548 my $shown = ($template->query(name => 'commentslink') ||
549 $template->query(name => 'comments')) &&
550 commentsshown($page);
552 if ($template->query(name => 'comments')) {
553 my $comments = undef;
555 $comments = IkiWiki::preprocess_inline(
556 pages => "internal($page/$config{comments_pagename}*)",
557 template => 'comment',
561 destpage => $params{destpage},
562 feedfile => 'comments',
567 if (defined $comments && length $comments) {
568 $template->param(comments => $comments);
571 if ($shown && commentsopen($page)) {
572 my $commenturl = IkiWiki::cgiurl(do => 'comment',
574 $template->param(commenturl => $commenturl);
578 if ($template->query(name => 'commentslink')) {
579 # XXX Would be nice to say how many comments there are in
580 # the link. But, to update the number, blog pages
581 # would have to update whenever comments of any inlines
582 # page are added, which is not currently done.
584 $template->param(commentslink =>
585 htmllink($page, $params{destpage}, $page,
586 linktext => gettext("Comments"),
587 anchor => "comments",
588 noimageinline => 1));
592 if ($template->query(name => 'commentuser')) {
593 $template->param(commentuser =>
594 $pagestate{$page}{comments}{commentuser});
597 if ($template->query(name => 'commentip')) {
598 $template->param(commentip =>
599 $pagestate{$page}{comments}{commentip});
602 if ($template->query(name => 'commentauthor')) {
603 $template->param(commentauthor =>
604 $pagestate{$page}{comments}{commentauthor});
607 if ($template->query(name => 'commentauthorurl')) {
608 $template->param(commentauthorurl =>
609 $pagestate{$page}{comments}{commentauthorurl});
613 package IkiWiki::PageSpec;
615 sub match_postcomment ($$;@) {
619 if (! $postcomment) {
620 return IkiWiki::FailReason->new("not posting a comment");
622 return match_glob($page, $glob);