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 sub preprocess { # {{{
35 my $page = $params{page};
37 my $format = $params{format};
38 if (defined $format && !exists $IkiWiki::hooks{htmlize}{$format}) {
39 error(sprintf(gettext("unsupported page format %s"), $format));
42 my $content = $params{content};
43 if (!defined $content) {
44 error(gettext("comment must have content"));
46 $content =~ s/\\"/"/g;
48 $content = IkiWiki::filter($page, $params{destpage}, $content);
50 if ($config{comments_allowdirectives}) {
51 $content = IkiWiki::preprocess($page, $params{destpage},
55 # no need to bother with htmlize if it's just HTML
56 $content = IkiWiki::htmlize($page, $params{destpage}, $format,
57 $content) if defined $format;
59 IkiWiki::run_hooks(sanitize => sub {
62 destpage => $params{destpage},
67 # override any metadata
69 if (defined $params{username}) {
70 my ($authorurl, $author) = linkuser($params{username});
71 $pagestate{$page}{meta}{author} = $author;
72 $pagestate{$page}{meta}{authorurl} = $authorurl;
74 elsif (defined $params{ip}) {
75 $pagestate{$page}{meta}{author} = sprintf(
76 gettext("Anonymous (IP: %s)"),
78 delete $pagestate{$page}{meta}{authorurl};
81 $pagestate{$page}{meta}{author} = gettext("Anonymous");
82 delete $pagestate{$page}{meta}{authorurl};
85 if (defined $params{subject}) {
86 $pagestate{$page}{meta}{title} = $params{subject};
89 delete $pagestate{$page}{meta}{title};
92 my $baseurl = urlto($params{destpage}, undef, 1);
94 my $comments_pagename = $config{comments_pagename};
95 if ($params{page} =~ m/\/(\Q${comments_pagename}\E\d+)$/) {
98 $pagestate{$page}{meta}{permalink} = "${baseurl}#${anchor}";
100 eval q{use Date::Parse};
102 my $time = str2time($params{date});
103 $IkiWiki::pagectime{$page} = $time if defined $time;
106 # FIXME: hard-coded HTML (although it's just to set an ID)
107 return "<div id=\"$anchor\">$content</div>" if $anchor;
111 sub getsetup () { #{{{
117 # Pages where comments are shown, but new comments are not
118 # allowed, will show "Comments are closed".
119 comments_shown_pagespec => {
123 description => 'PageSpec for pages where comments will be shown inline',
124 link => 'ikiwiki/PageSpec',
128 comments_open_pagespec => {
130 example => 'blog/* and created_after(close_old_comments)',
132 description => 'PageSpec for pages where new comments can be posted',
133 link => 'ikiwiki/PageSpec',
137 comments_pagename => {
139 example => 'comment_',
140 default => 'comment_',
141 description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
142 safe => 0, # manual page moving will required
145 comments_allowdirectives => {
149 description => 'Allow directives in newly posted comments?',
157 description => 'commit comments to the VCS',
158 # old uncommitted comments are likely to cause
159 # confusion if this is changed
165 sub checkconfig () { #{{{
166 $config{comments_commit} = 1 unless defined $config{comments_commit};
167 $config{comments_pagename} = 'comment_'
168 unless defined $config{comments_pagename};
171 # This is exactly the same as recentchanges_link :-(
172 sub linkcgi ($) { #{{{
174 if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
176 my $page=decode_utf8($cgi->param("page"));
177 if (!defined $page) {
178 error("missing page parameter");
181 IkiWiki::loadindex();
183 my $link=bestlink("", $page);
184 if (! length $link) {
185 print "Content-type: text/html\n\n";
186 print IkiWiki::misctemplate(gettext(gettext("missing page")),
188 sprintf(gettext("The page %s does not exist."),
189 htmllink("", "", $page)).
193 IkiWiki::redirect($cgi, urlto($link, undef, 1));
200 # FIXME: basically the same logic as recentchanges
201 # returns (author URL, pretty-printed version)
202 sub linkuser ($) { # {{{
204 my $oiduser = eval { IkiWiki::openiduser($user) };
206 if (defined $oiduser) {
207 return ($user, $oiduser);
209 # FIXME: it'd be good to avoid having such a link for anonymous
212 return (IkiWiki::cgiurl(
214 page => (length $config{userdir}
215 ? "$config{userdir}/$user"
221 # Mostly cargo-culted from IkiWiki::plugin::editpage
222 sub sessioncgi ($$) { #{{{
226 my $do = $cgi->param('do');
227 return unless $do eq 'comment';
229 IkiWiki::decode_cgi_utf8($cgi);
231 eval q{use CGI::FormBuilder};
234 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
235 my $form = CGI::FormBuilder->new(
236 fields => [qw{do sid page subject editcontent type}],
239 required => [qw{editcontent}],
242 action => $config{cgiurl},
245 template => scalar IkiWiki::template_params('comments_form.tmpl'),
246 # wtf does this do in editpage?
247 wikiname => $config{wikiname},
250 IkiWiki::decode_form_utf8($form);
251 IkiWiki::run_hooks(formbuilder_setup => sub {
252 shift->(title => "comment", form => $form, cgi => $cgi,
253 session => $session, buttons => \@buttons);
255 IkiWiki::decode_form_utf8($form);
257 my $type = $form->param('type');
258 if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
259 $type = possibly_foolish_untaint($type);
262 $type = $config{default_pageext};
265 if (exists $IkiWiki::hooks{htmlize}) {
266 @page_types = grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}};
269 $form->field(name => 'do', type => 'hidden');
270 $form->field(name => 'sid', type => 'hidden', value => $session->id,
272 $form->field(name => 'page', type => 'hidden');
273 $form->field(name => 'subject', type => 'text', size => 72);
274 $form->field(name => 'editcontent', type => 'textarea', rows => 10);
275 $form->field(name => "type", value => $type, force => 1,
276 type => 'select', options => \@page_types);
278 # The untaint is OK (as in editpage) because we're about to pass
279 # it to file_pruned anyway
280 my $page = $form->field('page');
281 $page = IkiWiki::possibly_foolish_untaint($page);
282 if (!defined $page || !length $page ||
283 IkiWiki::file_pruned($page, $config{srcdir})) {
284 error(gettext("bad page name"));
287 my $allow_directives = $config{comments_allowdirectives};
288 my $commit_comments = $config{comments_commit};
289 my $comments_pagename = $config{comments_pagename};
291 # FIXME: is this right? Or should we be using the candidate subpage
292 # (whatever that might mean) as the base URL?
293 my $baseurl = urlto($page, undef, 1);
295 $form->title(sprintf(gettext("commenting on %s"),
296 IkiWiki::pagetitle($page)));
298 $form->tmpl_param('helponformattinglink',
299 htmllink($page, $page, 'ikiwiki/formatting',
301 linktext => 'FormattingHelp'),
302 allowdirectives => $allow_directives);
304 if ($form->submitted eq CANCEL) {
305 # bounce back to the page they wanted to comment on, and exit.
306 # CANCEL need not be considered in future
307 IkiWiki::redirect($cgi, urlto($page, undef, 1));
311 if (not exists $pagesources{$page}) {
312 error(sprintf(gettext(
313 "page '%s' doesn't exist, so you can't comment"),
317 if (not pagespec_match($page, $config{comments_open_pagespec},
318 location => $page)) {
319 error(sprintf(gettext(
320 "comments on page '%s' are closed"),
324 IkiWiki::check_canedit($page . "[postcomment]", $cgi, $session);
326 my $editcontent = $form->field('editcontent') || '';
327 $editcontent =~ s/\r\n/\n/g;
328 $editcontent =~ s/\r/\n/g;
330 # FIXME: check that the wiki is locked right now, because
331 # if it's not, there are mad race conditions!
333 # FIXME: rather a simplistic way to make the comments...
339 $location = "$page/${comments_pagename}${i}";
340 } while (-e "$config{srcdir}/$location._comment");
342 my $anchor = "${comments_pagename}${i}";
344 $editcontent =~ s/"/\\"/g;
345 my $content = "[[!comment format=$type\n";
347 # FIXME: handling of double quotes probably wrong?
348 if (defined $session->param('name')) {
349 my $username = $session->param('name');
350 $username =~ s/"/"/g;
351 $content .= " username=\"$username\"\n";
353 elsif (defined $ENV{REMOTE_ADDR}) {
354 my $ip = $ENV{REMOTE_ADDR};
355 if ($ip =~ m/^([.0-9]+)$/) {
356 $content .= " ip=\"$1\"\n";
360 my $subject = $form->field('subject');
361 $subject =~ s/"/"/g;
362 $content .= " subject=\"$subject\"\n";
364 $content .= " date=\"" . IkiWiki::formattime(time, '%X %x') . "\"\n";
366 $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
368 # This is essentially a simplified version of editpage:
369 # - the user does not control the page that's created, only the parent
370 # - it's always a create operation, never an edit
371 # - this means that conflicts should never happen
372 # - this means that if they do, rocks fall and everyone dies
374 if ($form->submitted eq PREVIEW) {
375 my $preview = IkiWiki::htmlize($location, $page, '_comment',
376 IkiWiki::linkify($page, $page,
377 IkiWiki::preprocess($page, $page,
378 IkiWiki::filter($location,
381 IkiWiki::run_hooks(format => sub {
382 $preview = shift->(page => $page,
383 content => $preview);
386 my $template = template("comments_display.tmpl");
387 $template->param(content => $preview);
388 $template->param(title => $form->field('subject'));
389 $template->param(ctime => displaytime(time));
391 $form->tmpl_param(page_preview => $template->output);
394 $form->tmpl_param(page_preview => "");
397 if ($form->submitted eq POST_COMMENT && $form->validate) {
398 my $file = "$location._comment";
400 IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
402 # FIXME: could probably do some sort of graceful retry
403 # on error? Would require significant unwinding though
404 writefile($file, $config{srcdir}, $content);
408 if ($config{rcs} and $commit_comments) {
409 my $message = gettext("Added a comment");
410 if (defined $form->field('subject') &&
411 length $form->field('subject')) {
413 gettext("Added a comment: %s"),
414 $form->field('subject'));
417 IkiWiki::rcs_add($file);
418 IkiWiki::disable_commit_hook();
419 $conflict = IkiWiki::rcs_commit_staged($message,
420 $session->param('name'), $ENV{REMOTE_ADDR});
421 IkiWiki::enable_commit_hook();
422 IkiWiki::rcs_update();
425 # Now we need a refresh
426 require IkiWiki::Render;
428 IkiWiki::saveindex();
430 # this should never happen, unless a committer deliberately
431 # breaks it or something
432 error($conflict) if defined $conflict;
434 # Bounce back to where we were, but defeat broken caches
435 my $anticache = "?updated=$page/${comments_pagename}${i}";
436 IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
439 IkiWiki::showform ($form, \@buttons, $session, $cgi,
440 forcebaseurl => $baseurl);
446 sub pagetemplate (@) { #{{{
449 my $page = $params{page};
450 my $template = $params{template};
452 if ($template->query(name => 'comments')) {
453 my $comments = undef;
455 my $comments_pagename = $config{comments_pagename};
458 my $shown = pagespec_match($page,
459 $config{comments_shown_pagespec},
462 if (pagespec_match($page, "*/${comments_pagename}*",
463 location => $page)) {
468 if (length $config{cgiurl}) {
469 $open = pagespec_match($page,
470 $config{comments_open_pagespec},
475 eval q{use IkiWiki::Plugin::inline};
479 pages => "internal($page/${comments_pagename}*)",
480 template => 'comments_display',
484 destpage => $params{destpage},
486 $comments = IkiWiki::preprocess_inline(@args);
489 if (defined $comments && length $comments) {
490 $template->param(comments => $comments);
494 my $commenturl = IkiWiki::cgiurl(do => 'comment',
496 $template->param(commenturl => $commenturl);
501 package IkiWiki::PageSpec;
503 sub match_postcomment ($$;@) {
507 unless ($page =~ s/\[postcomment\]$//) {
508 return IkiWiki::FailReason->new("not posting a comment");
510 return match_glob($page, $glob);