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 IkiWiki::loadplugin("inline");
22 IkiWiki::loadplugin("mdwn");
26 eval q{use IkiWiki::Plugin::mdwn};
28 return IkiWiki::Plugin::mdwn::htmlize(@_)
31 sub getsetup () { #{{{
39 # Somewhat based on IkiWiki::Plugin::inline blog posting support
40 sub preprocess (@) { #{{{
43 unless (length $config{cgiurl}) {
44 error(gettext("[[!comments plugin requires CGI enabled]]"));
47 my $page = $params{page};
48 $pagestate{$page}{comments}{comments} = defined $params{closed}
49 ? (not IkiWiki::yesno($params{closed}))
51 $pagestate{$page}{comments}{allowdirectives} = IkiWiki::yesno($params{allowdirectives});
52 $pagestate{$page}{comments}{commit} = defined $params{commit}
53 ? IkiWiki::yesno($params{commit})
56 my $formtemplate = IkiWiki::template("comments_embed.tmpl",
58 $formtemplate->param(cgiurl => $config{cgiurl});
59 $formtemplate->param(page => $params{page});
61 if (not $pagestate{$page}{comments}{comments}) {
62 $formtemplate->param("disabled" =>
63 gettext('comments are closed'));
65 elsif ($params{preview}) {
66 $formtemplate->param("disabled" =>
67 gettext('not available during Preview'));
70 debug("page $params{page} => destpage $params{destpage}");
72 unless (defined $params{inline} && !IkiWiki::yesno($params{inline})) {
74 eval q{use IkiWiki::Plugin::inline};
77 pages => "internal($params{page}/_comment_*)",
78 template => "comments_display",
81 # special stuff passed through
82 page => $params{page},
83 destpage => $params{destpage},
84 preview => $params{preview},
86 push @args, atom => $params{atom} if defined $params{atom};
87 push @args, rss => $params{rss} if defined $params{rss};
88 push @args, feeds => $params{feeds} if defined $params{feeds};
89 push @args, feedshow => $params{feedshow} if defined $params{feedshow};
90 push @args, timeformat => $params{timeformat} if defined $params{timeformat};
91 push @args, feedonly => $params{feedonly} if defined $params{feedonly};
92 $posts = IkiWiki::preprocess_inline(@args);
93 $formtemplate->param("comments" => $posts);
96 return $formtemplate->output;
99 # FIXME: logic taken from editpage, should be common code?
100 sub getcgiuser ($) { # {{{
102 my $user = $session->param('name');
103 $user = $ENV{REMOTE_ADDR} unless defined $user;
104 debug("getcgiuser() -> $user");
108 # FIXME: logic adapted from recentchanges, should be common code?
109 sub linkuser ($) { # {{{
111 my $oiduser = eval { IkiWiki::openiduser($user) };
113 if (defined $oiduser) {
114 return ($user, $oiduser);
117 my $page = bestlink('', (length $config{userdir}
118 ? "$config{userdir}/"
120 return (urlto($page, undef, 1), $user);
124 # Mostly cargo-culted from IkiWiki::plugin::editpage
125 sub sessioncgi ($$) { #{{{
129 my $do = $cgi->param('do');
130 return unless $do eq 'comment';
132 IkiWiki::decode_cgi_utf8($cgi);
134 eval q{use CGI::FormBuilder};
137 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
138 my $form = CGI::FormBuilder->new(
139 fields => [qw{do sid page subject body}],
142 required => [qw{body}],
145 action => $config{cgiurl},
148 template => scalar IkiWiki::template_params('comments_form.tmpl'),
149 # wtf does this do in editpage?
150 wikiname => $config{wikiname},
153 IkiWiki::decode_form_utf8($form);
154 IkiWiki::run_hooks(formbuilder_setup => sub {
155 shift->(title => "comment", form => $form, cgi => $cgi,
156 session => $session, buttons => \@buttons);
158 IkiWiki::decode_form_utf8($form);
160 $form->field(name => 'do', type => 'hidden');
161 $form->field(name => 'sid', type => 'hidden', value => $session->id,
163 $form->field(name => 'page', type => 'hidden');
164 $form->field(name => 'subject', type => 'text', size => 72);
165 $form->field(name => 'body', type => 'textarea', rows => 5,
168 # The untaint is OK (as in editpage) because we're about to pass
169 # it to file_pruned anyway
170 my $page = $form->field('page');
171 $page = IkiWiki::possibly_foolish_untaint($page);
172 if (!defined $page || !length $page ||
173 IkiWiki::file_pruned($page, $config{srcdir})) {
174 error(gettext("bad page name"));
177 my $allow_directives = $pagestate{$page}{comments}{allowdirectives};
178 my $commit_comments = defined $pagestate{$page}{comments}{commit}
179 ? $pagestate{$page}{comments}{commit}
182 # FIXME: is this right? Or should we be using the candidate subpage
183 # (whatever that might mean) as the base URL?
184 my $baseurl = urlto($page, undef, 1);
186 $form->title(sprintf(gettext("commenting on %s"),
187 IkiWiki::pagetitle($page)));
189 $form->tmpl_param('helponformattinglink',
190 htmllink($page, $page, 'ikiwiki/formatting',
192 linktext => 'FormattingHelp'),
193 allowdirectives => $allow_directives);
195 if (not exists $pagesources{$page}) {
196 error(sprintf(gettext(
197 "page '%s' doesn't exist, so you can't comment"),
200 if (not $pagestate{$page}{comments}{comments}) {
201 error(sprintf(gettext(
202 "comments are not enabled on page '%s'"),
206 if ($form->submitted eq CANCEL) {
207 # bounce back to the page they wanted to comment on, and exit.
208 # CANCEL need not be considered in future
209 IkiWiki::redirect($cgi, urlto($page, undef, 1));
213 IkiWiki::check_canedit($page . "[postcomment]", $cgi, $session);
215 my ($authorurl, $author) = linkuser(getcgiuser($session));
217 my $body = $form->field('body') || '';
218 $body =~ s/\r\n/\n/g;
220 $body .= "\n" if $body !~ /\n$/;
222 unless ($allow_directives) {
223 # don't allow new-style directives at all
224 $body =~ s/(^|[^\\])\[\[!/$1\\[[!/g;
226 # don't allow [[ unless it begins an old-style
227 # wikilink, if prefix_directives is off
228 $body =~ s/(^|[^\\])\[\[(?![^\n\s\]+]\]\])/$1\\[[!/g
229 unless $config{prefix_directives};
232 IkiWiki::run_hooks(sanitize => sub {
233 # $fake is a possible location for this comment. We don't
234 # know yet what the comment number *actually* is.
235 my $fake = "$page/_comment_1";
243 # In this template, the [[!meta]] directives should stay at the end,
244 # so that they will override anything the user specifies. (For
245 # instance, [[!meta author="I can fake the author"]]...)
246 my $content_tmpl = template('comments_comment.tmpl');
247 $content_tmpl->param(author => $author);
248 $content_tmpl->param(authorurl => $authorurl);
249 $content_tmpl->param(subject => $form->field('subject'));
250 $content_tmpl->param(body => $body);
252 my $content = $content_tmpl->output;
254 # This is essentially a simplified version of editpage:
255 # - the user does not control the page that's created, only the parent
256 # - it's always a create operation, never an edit
257 # - this means that conflicts should never happen
258 # - this means that if they do, rocks fall and everyone dies
260 if ($form->submitted eq PREVIEW) {
261 # $fake is a possible location for this comment. We don't
262 # know yet what the comment number *actually* is.
263 my $fake = "$page/_comment_1";
264 my $preview = IkiWiki::htmlize($fake, $page, 'mdwn',
265 IkiWiki::linkify($page, $page,
266 IkiWiki::preprocess($page, $page,
267 IkiWiki::filter($fake, $page,
270 IkiWiki::run_hooks(format => sub {
271 $preview = shift->(page => $page,
272 content => $preview);
275 my $template = template("comments_display.tmpl");
276 $template->param(content => $preview);
277 $template->param(title => $form->field('subject'));
278 $template->param(ctime => displaytime(time));
279 $template->param(author => $author);
280 $template->param(authorurl => $authorurl);
282 $form->tmpl_param(page_preview => $template->output);
285 $form->tmpl_param(page_preview => "");
288 if ($form->submitted eq POST_COMMENT && $form->validate) {
289 # Let's get posting. We don't check_canedit here because
290 # that somewhat defeats the point of this plugin.
292 IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
294 # FIXME: check that the wiki is locked right now, because
295 # if it's not, there are mad race conditions!
297 # FIXME: rather a simplistic way to make the comments...
302 $file = "$page/_comment_${i}._comment";
303 } while (-e "$config{srcdir}/$file");
305 # FIXME: could probably do some sort of graceful retry
306 # if I could be bothered
307 writefile($file, $config{srcdir}, $content);
311 if ($config{rcs} and $commit_comments) {
312 my $message = gettext("Added a comment");
313 if (defined $form->field('subject') &&
314 length $form->field('subject')) {
315 $message .= ": ".$form->field('subject');
318 IkiWiki::rcs_add($file);
319 IkiWiki::disable_commit_hook();
320 $conflict = IkiWiki::rcs_commit_staged($message,
321 $session->param('name'), $ENV{REMOTE_ADDR});
322 IkiWiki::enable_commit_hook();
323 IkiWiki::rcs_update();
326 # Now we need a refresh
327 require IkiWiki::Render;
329 IkiWiki::saveindex();
331 # this should never happen, unless a committer deliberately
332 # breaks it or something
333 error($conflict) if defined $conflict;
335 # Bounce back to where we were, but defeat broken caches
336 my $anticache = "?updated=$page/_comment_$i";
337 IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
340 IkiWiki::showform ($form, \@buttons, $session, $cgi,
341 forcebaseurl => $baseurl);
347 package IkiWiki::PageSpec;
349 sub match_postcomment ($$;@) {
353 unless ($page =~ s/\[postcomment\]$//) {
354 return IkiWiki::FailReason->new("not posting a comment");
356 return match_glob($page, $glob);