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 () { #{{{
40 # Somewhat based on IkiWiki::Plugin::inline blog posting support
41 sub preprocess (@) { #{{{
44 unless (length $config{cgiurl}) {
45 error(gettext("[[!comments plugin requires CGI enabled]]"));
48 my $page = $params{page};
49 $pagestate{$page}{comments}{comments} = defined $params{closed}
50 ? (not IkiWiki::yesno($params{closed}))
52 $pagestate{$page}{comments}{allowdirectives} = IkiWiki::yesno($params{allowdirectives});
53 $pagestate{$page}{comments}{commit} = defined $params{commit}
54 ? IkiWiki::yesno($params{commit})
57 my $formtemplate = IkiWiki::template("comments_embed.tmpl",
59 $formtemplate->param(cgiurl => $config{cgiurl});
60 $formtemplate->param(page => $params{page});
62 if (not $pagestate{$page}{comments}{comments}) {
63 $formtemplate->param("disabled" =>
64 gettext('comments are closed'));
66 elsif ($params{preview}) {
67 $formtemplate->param("disabled" =>
68 gettext('not available during Preview'));
71 debug("page $params{page} => destpage $params{destpage}");
73 unless (defined $params{inline} && !IkiWiki::yesno($params{inline})) {
75 eval q{use IkiWiki::Plugin::inline};
78 pages => "internal($params{page}/_comment_*)",
79 template => "comments_display",
82 # special stuff passed through
83 page => $params{page},
84 destpage => $params{destpage},
85 preview => $params{preview},
87 push @args, atom => $params{atom} if defined $params{atom};
88 push @args, rss => $params{rss} if defined $params{rss};
89 push @args, feeds => $params{feeds} if defined $params{feeds};
90 push @args, feedshow => $params{feedshow} if defined $params{feedshow};
91 push @args, timeformat => $params{timeformat} if defined $params{timeformat};
92 push @args, feedonly => $params{feedonly} if defined $params{feedonly};
93 $posts = IkiWiki::preprocess_inline(@args);
94 $formtemplate->param("comments" => $posts);
97 return $formtemplate->output;
100 # FIXME: logic taken from editpage, should be common code?
101 sub getcgiuser ($) { # {{{
103 my $user = $session->param('name');
104 $user = $ENV{REMOTE_ADDR} unless defined $user;
105 debug("getcgiuser() -> $user");
109 # FIXME: logic adapted from recentchanges, should be common code?
110 sub linkuser ($) { # {{{
112 my $oiduser = eval { IkiWiki::openiduser($user) };
114 if (defined $oiduser) {
115 return ($user, $oiduser);
118 my $page = bestlink('', (length $config{userdir}
119 ? "$config{userdir}/"
121 return (urlto($page, undef, 1), $user);
125 # Mostly cargo-culted from IkiWiki::plugin::editpage
126 sub sessioncgi ($$) { #{{{
130 my $do = $cgi->param('do');
131 return unless $do eq 'comment';
133 IkiWiki::decode_cgi_utf8($cgi);
135 eval q{use CGI::FormBuilder};
138 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
139 my $form = CGI::FormBuilder->new(
140 fields => [qw{do sid page subject body}],
143 required => [qw{body}],
146 action => $config{cgiurl},
149 template => scalar IkiWiki::template_params('comments_form.tmpl'),
150 # wtf does this do in editpage?
151 wikiname => $config{wikiname},
154 IkiWiki::decode_form_utf8($form);
155 IkiWiki::run_hooks(formbuilder_setup => sub {
156 shift->(title => "comment", form => $form, cgi => $cgi,
157 session => $session, buttons => \@buttons);
159 IkiWiki::decode_form_utf8($form);
161 $form->field(name => 'do', type => 'hidden');
162 $form->field(name => 'sid', type => 'hidden', value => $session->id,
164 $form->field(name => 'page', type => 'hidden');
165 $form->field(name => 'subject', type => 'text', size => 72);
166 $form->field(name => 'body', type => 'textarea', rows => 5,
169 # The untaint is OK (as in editpage) because we're about to pass
170 # it to file_pruned anyway
171 my $page = $form->field('page');
172 $page = IkiWiki::possibly_foolish_untaint($page);
173 if (!defined $page || !length $page ||
174 IkiWiki::file_pruned($page, $config{srcdir})) {
175 error(gettext("bad page name"));
178 my $allow_directives = $pagestate{$page}{comments}{allowdirectives};
179 my $commit_comments = defined $pagestate{$page}{comments}{commit}
180 ? $pagestate{$page}{comments}{commit}
183 # FIXME: is this right? Or should we be using the candidate subpage
184 # (whatever that might mean) as the base URL?
185 my $baseurl = urlto($page, undef, 1);
187 $form->title(sprintf(gettext("commenting on %s"),
188 IkiWiki::pagetitle($page)));
190 $form->tmpl_param('helponformattinglink',
191 htmllink($page, $page, 'ikiwiki/formatting',
193 linktext => 'FormattingHelp'),
194 allowdirectives => $allow_directives);
196 if (not exists $pagesources{$page}) {
197 error(sprintf(gettext(
198 "page '%s' doesn't exist, so you can't comment"),
201 if (not $pagestate{$page}{comments}{comments}) {
202 error(sprintf(gettext(
203 "comments are not enabled on page '%s'"),
207 if ($form->submitted eq CANCEL) {
208 # bounce back to the page they wanted to comment on, and exit.
209 # CANCEL need not be considered in future
210 IkiWiki::redirect($cgi, urlto($page, undef, 1));
214 IkiWiki::check_canedit($page . "[postcomment]", $cgi, $session);
216 my ($authorurl, $author) = linkuser(getcgiuser($session));
218 my $body = $form->field('body') || '';
219 $body =~ s/\r\n/\n/g;
221 $body .= "\n" if $body !~ /\n$/;
223 unless ($allow_directives) {
224 # don't allow new-style directives at all
225 $body =~ s/(^|[^\\])\[\[!/$1[[!/g;
227 # don't allow [[ unless it begins an old-style
228 # wikilink, if prefix_directives is off
229 $body =~ s/(^|[^\\])\[\[(?![^\n\s\]+]\]\])/$1[[!/g
230 unless $config{prefix_directives};
233 IkiWiki::run_hooks(sanitize => sub {
234 # $fake is a possible location for this comment. We don't
235 # know yet what the comment number *actually* is.
236 my $fake = "$page/_comment_1";
244 # In this template, the [[!meta]] directives should stay at the end,
245 # so that they will override anything the user specifies. (For
246 # instance, [[!meta author="I can fake the author"]]...)
247 my $content_tmpl = template('comments_comment.tmpl');
248 $content_tmpl->param(author => $author);
249 $content_tmpl->param(authorurl => $authorurl);
250 $content_tmpl->param(subject => $form->field('subject'));
251 $content_tmpl->param(body => $body);
253 my $content = $content_tmpl->output;
255 # This is essentially a simplified version of editpage:
256 # - the user does not control the page that's created, only the parent
257 # - it's always a create operation, never an edit
258 # - this means that conflicts should never happen
259 # - this means that if they do, rocks fall and everyone dies
261 if ($form->submitted eq PREVIEW) {
262 # $fake is a possible location for this comment. We don't
263 # know yet what the comment number *actually* is.
264 my $fake = "$page/_comment_1";
265 my $preview = IkiWiki::htmlize($fake, $page, 'mdwn',
266 IkiWiki::linkify($page, $page,
267 IkiWiki::preprocess($page, $page,
268 IkiWiki::filter($fake, $page,
271 IkiWiki::run_hooks(format => sub {
272 $preview = shift->(page => $page,
273 content => $preview);
276 my $template = template("comments_display.tmpl");
277 $template->param(content => $preview);
278 $template->param(title => $form->field('subject'));
279 $template->param(ctime => displaytime(time));
280 $template->param(author => $author);
281 $template->param(authorurl => $authorurl);
283 $form->tmpl_param(page_preview => $template->output);
286 $form->tmpl_param(page_preview => "");
289 if ($form->submitted eq POST_COMMENT && $form->validate) {
290 # Let's get posting. We don't check_canedit here because
291 # that somewhat defeats the point of this plugin.
293 IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
295 # FIXME: check that the wiki is locked right now, because
296 # if it's not, there are mad race conditions!
298 # FIXME: rather a simplistic way to make the comments...
303 $file = "$page/_comment_${i}._comment";
304 } while (-e "$config{srcdir}/$file");
306 # FIXME: could probably do some sort of graceful retry
307 # if I could be bothered
308 writefile($file, $config{srcdir}, $content);
312 if ($config{rcs} and $commit_comments) {
313 my $message = gettext("Added a comment");
314 if (defined $form->field('subject') &&
315 length $form->field('subject')) {
316 $message .= ": ".$form->field('subject');
319 IkiWiki::rcs_add($file);
320 IkiWiki::disable_commit_hook();
321 $conflict = IkiWiki::rcs_commit_staged($message,
322 $session->param('name'), $ENV{REMOTE_ADDR});
323 IkiWiki::enable_commit_hook();
324 IkiWiki::rcs_update();
327 # Now we need a refresh
328 require IkiWiki::Render;
330 IkiWiki::saveindex();
332 # this should never happen, unless a committer deliberately
333 # breaks it or something
334 error($conflict) if defined $conflict;
336 # Bounce back to where we were, but defeat broken caches
337 my $anticache = "?updated=$page/_comment_$i";
338 IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
341 IkiWiki::showform ($form, \@buttons, $session, $cgi,
342 forcebaseurl => $baseurl);
348 sub pagetemplate (@) { #{{{
351 my $page = $params{page};
352 my $template = $params{template};
354 if ($template->query(name => 'comments')) {
355 my $comments = undef;
357 if (defined $comments && length $comments) {
358 $template->param(name => $comments);
363 package IkiWiki::PageSpec;
365 sub match_postcomment ($$;@) {
369 unless ($page =~ s/\[postcomment\]$//) {
370 return IkiWiki::FailReason->new("not posting a comment");
372 return match_glob($page, $glob);