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 constant PREVIEW => "Preview";
13 use constant POST_COMMENT => "Post comment";
14 use constant CANCEL => "Cancel";
17 hook(type => "getsetup", id => 'comments', call => \&getsetup);
18 hook(type => "preprocess", id => 'comments', call => \&preprocess);
19 hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
20 hook(type => "htmlize", id => "_comment", call => \&htmlize);
21 hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
22 IkiWiki::loadplugin("inline");
23 IkiWiki::loadplugin("mdwn");
27 eval q{use IkiWiki::Plugin::mdwn};
29 return IkiWiki::Plugin::mdwn::htmlize(@_)
32 sub getsetup () { #{{{
38 # Pages where comments are shown, but new comments are not
39 # allowed, will show "Comments are closed".
40 comments_shown_pagespec => {
44 description => 'PageSpec for pages where comments will be shown inline',
45 link => 'ikiwiki/PageSpec',
49 comments_open_pagespec => {
51 example => 'blog/* and created_after(close_old_comments)',
53 description => 'PageSpec for pages where new comments can be posted',
54 link => 'ikiwiki/PageSpec',
58 comments_pagename => {
60 example => 'comment_',
61 default => 'comment_',
62 description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
63 safe => 0, # manual page moving will required
66 comments_allowdirectives => {
70 description => 'Allow directives in newly posted comments?',
78 description => 'commit comments to the VCS',
79 # old uncommitted comments are likely to cause
80 # confusion if this is changed
86 # Somewhat based on IkiWiki::Plugin::inline blog posting support
87 sub preprocess (@) { #{{{
92 my $page = $params{page};
93 $pagestate{$page}{comments}{comments} = defined $params{closed}
94 ? (not IkiWiki::yesno($params{closed}))
96 $pagestate{$page}{comments}{allowdirectives} = IkiWiki::yesno($params{allowdirectives});
97 $pagestate{$page}{comments}{commit} = defined $params{commit}
98 ? IkiWiki::yesno($params{commit})
101 my $formtemplate = IkiWiki::template("comments_embed.tmpl",
103 $formtemplate->param(cgiurl => $config{cgiurl});
104 $formtemplate->param(page => $params{page});
106 if (not $pagestate{$page}{comments}{comments}) {
107 $formtemplate->param("disabled" =>
108 gettext('comments are closed'));
110 elsif ($params{preview}) {
111 $formtemplate->param("disabled" =>
112 gettext('not available during Preview'));
115 debug("page $params{page} => destpage $params{destpage}");
117 unless (defined $params{inline} && !IkiWiki::yesno($params{inline})) {
119 eval q{use IkiWiki::Plugin::inline};
122 pages => "internal($params{page}/_comment_*)",
123 template => "comments_display",
126 # special stuff passed through
127 page => $params{page},
128 destpage => $params{destpage},
129 preview => $params{preview},
131 push @args, atom => $params{atom} if defined $params{atom};
132 push @args, rss => $params{rss} if defined $params{rss};
133 push @args, feeds => $params{feeds} if defined $params{feeds};
134 push @args, feedshow => $params{feedshow} if defined $params{feedshow};
135 push @args, timeformat => $params{timeformat} if defined $params{timeformat};
136 push @args, feedonly => $params{feedonly} if defined $params{feedonly};
137 $posts = IkiWiki::preprocess_inline(@args);
138 $formtemplate->param("comments" => $posts);
141 return $formtemplate->output;
144 # FIXME: logic taken from editpage, should be common code?
145 sub getcgiuser ($) { # {{{
147 my $user = $session->param('name');
148 $user = $ENV{REMOTE_ADDR} unless defined $user;
149 debug("getcgiuser() -> $user");
153 # FIXME: logic adapted from recentchanges, should be common code?
154 # returns (author URL, pretty-printed version)
155 sub linkuser ($) { # {{{
157 my $oiduser = eval { IkiWiki::openiduser($user) };
159 if (defined $oiduser) {
160 return ($user, $oiduser);
163 my $page = bestlink('', (length $config{userdir}
164 ? "$config{userdir}/"
166 return (urlto($page, undef, 1), $user);
170 # Mostly cargo-culted from IkiWiki::plugin::editpage
171 sub sessioncgi ($$) { #{{{
175 my $do = $cgi->param('do');
176 return unless $do eq 'comment';
178 IkiWiki::decode_cgi_utf8($cgi);
180 eval q{use CGI::FormBuilder};
183 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
184 my $form = CGI::FormBuilder->new(
185 fields => [qw{do sid page subject body}],
188 required => [qw{body}],
191 action => $config{cgiurl},
194 template => scalar IkiWiki::template_params('comments_form.tmpl'),
195 # wtf does this do in editpage?
196 wikiname => $config{wikiname},
199 IkiWiki::decode_form_utf8($form);
200 IkiWiki::run_hooks(formbuilder_setup => sub {
201 shift->(title => "comment", form => $form, cgi => $cgi,
202 session => $session, buttons => \@buttons);
204 IkiWiki::decode_form_utf8($form);
206 $form->field(name => 'do', type => 'hidden');
207 $form->field(name => 'sid', type => 'hidden', value => $session->id,
209 $form->field(name => 'page', type => 'hidden');
210 $form->field(name => 'subject', type => 'text', size => 72);
211 $form->field(name => 'body', type => 'textarea', rows => 5,
214 # The untaint is OK (as in editpage) because we're about to pass
215 # it to file_pruned anyway
216 my $page = $form->field('page');
217 $page = IkiWiki::possibly_foolish_untaint($page);
218 if (!defined $page || !length $page ||
219 IkiWiki::file_pruned($page, $config{srcdir})) {
220 error(gettext("bad page name"));
223 my $allow_directives = $config{comments_allowdirectives};
224 my $commit_comments = $config{comments_commit};
225 my $comments_pagename = $config{comments_pagename};
227 # FIXME: is this right? Or should we be using the candidate subpage
228 # (whatever that might mean) as the base URL?
229 my $baseurl = urlto($page, undef, 1);
231 $form->title(sprintf(gettext("commenting on %s"),
232 IkiWiki::pagetitle($page)));
234 $form->tmpl_param('helponformattinglink',
235 htmllink($page, $page, 'ikiwiki/formatting',
237 linktext => 'FormattingHelp'),
238 allowdirectives => $allow_directives);
240 if ($form->submitted eq CANCEL) {
241 # bounce back to the page they wanted to comment on, and exit.
242 # CANCEL need not be considered in future
243 IkiWiki::redirect($cgi, urlto($page, undef, 1));
247 if (not exists $pagesources{$page}) {
248 error(sprintf(gettext(
249 "page '%s' doesn't exist, so you can't comment"),
253 if (not pagespec_match($page, $config{comments_open_pagespec},
254 location => $page)) {
255 error(sprintf(gettext(
256 "comments on page '%s' are closed"),
260 IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
261 IkiWiki::check_canedit($page . "[postcomment]", $cgi, $session);
263 my ($authorurl, $author) = linkuser(getcgiuser($session));
265 my $body = $form->field('body') || '';
266 $body =~ s/\r\n/\n/g;
268 $body .= "\n" if $body !~ /\n$/;
270 unless ($allow_directives) {
271 # don't allow new-style directives at all
272 $body =~ s/(^|[^\\])\[\[!/$1[[!/g;
274 # don't allow [[ unless it begins an old-style
275 # wikilink, if prefix_directives is off
276 $body =~ s/(^|[^\\])\[\[(?![^\n\s\]+]\]\])/$1[[!/g
277 unless $config{prefix_directives};
280 # FIXME: check that the wiki is locked right now, because
281 # if it's not, there are mad race conditions!
283 # FIXME: rather a simplistic way to make the comments...
289 $location = "$page/${comments_pagename}${i}";
290 } while (-e "$config{srcdir}/$location._comment");
292 my $anchor = "${comments_pagename}${i}";
294 IkiWiki::run_hooks(sanitize => sub {
297 destpage => $location,
302 # In this template, the [[!meta]] directives should stay at the end,
303 # so that they will override anything the user specifies. (For
304 # instance, [[!meta author="I can fake the author"]]...)
305 my $content_tmpl = template('comments_comment.tmpl');
306 $content_tmpl->param(author => $author);
307 $content_tmpl->param(authorurl => $authorurl);
308 $content_tmpl->param(subject => $form->field('subject'));
309 $content_tmpl->param(body => $body);
310 $content_tmpl->param(anchor => "$anchor");
311 $content_tmpl->param(permalink => "$baseurl#$anchor");
313 my $content = $content_tmpl->output;
315 # This is essentially a simplified version of editpage:
316 # - the user does not control the page that's created, only the parent
317 # - it's always a create operation, never an edit
318 # - this means that conflicts should never happen
319 # - this means that if they do, rocks fall and everyone dies
321 if ($form->submitted eq PREVIEW) {
322 my $preview = IkiWiki::htmlize($location, $page, 'mdwn',
323 IkiWiki::linkify($page, $page,
324 IkiWiki::preprocess($page, $page,
325 IkiWiki::filter($location,
328 IkiWiki::run_hooks(format => sub {
329 $preview = shift->(page => $page,
330 content => $preview);
333 my $template = template("comments_display.tmpl");
334 $template->param(content => $preview);
335 $template->param(title => $form->field('subject'));
336 $template->param(ctime => displaytime(time));
337 $template->param(author => $author);
338 $template->param(authorurl => $authorurl);
340 $form->tmpl_param(page_preview => $template->output);
343 $form->tmpl_param(page_preview => "");
346 if ($form->submitted eq POST_COMMENT && $form->validate) {
347 my $file = "$location._comment";
349 # FIXME: could probably do some sort of graceful retry
350 # on error? Would require significant unwinding though
351 writefile($file, $config{srcdir}, $content);
355 if ($config{rcs} and $commit_comments) {
356 my $message = gettext("Added a comment");
357 if (defined $form->field('subject') &&
358 length $form->field('subject')) {
360 gettext("Added a comment: %s"),
361 $form->field('subject'));
364 IkiWiki::rcs_add($file);
365 IkiWiki::disable_commit_hook();
366 $conflict = IkiWiki::rcs_commit_staged($message,
367 $session->param('name'), $ENV{REMOTE_ADDR});
368 IkiWiki::enable_commit_hook();
369 IkiWiki::rcs_update();
372 # Now we need a refresh
373 require IkiWiki::Render;
375 IkiWiki::saveindex();
377 # this should never happen, unless a committer deliberately
378 # breaks it or something
379 error($conflict) if defined $conflict;
381 # Bounce back to where we were, but defeat broken caches
382 my $anticache = "?updated=$page/${comments_pagename}${i}";
383 IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
386 IkiWiki::showform ($form, \@buttons, $session, $cgi,
387 forcebaseurl => $baseurl);
393 sub pagetemplate (@) { #{{{
396 my $page = $params{page};
397 my $template = $params{template};
399 if ($template->query(name => 'comments')) {
400 my $comments = undef;
402 if (defined $comments && length $comments) {
403 $template->param(name => $comments);
408 package IkiWiki::PageSpec;
410 sub match_postcomment ($$;@) {
414 unless ($page =~ s/\[postcomment\]$//) {
415 return IkiWiki::FailReason->new("not posting a comment");
417 return match_glob($page, $glob);