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::smcvpostcomment;
11 use IkiWiki::Plugin::inline;
12 use IkiWiki::Plugin::mdwn;
15 use constant PLUGIN => "smcvpostcomment";
16 use constant PREVIEW => "Preview";
17 use constant POST_COMMENT => "Post comment";
18 use constant CANCEL => "Cancel";
21 hook(type => "getsetup", id => PLUGIN, call => \&getsetup);
22 hook(type => "preprocess", id => PLUGIN, call => \&preprocess);
23 hook(type => "sessioncgi", id => PLUGIN, call => \&sessioncgi);
24 hook(type => "htmlize", id => "_".PLUGIN,
25 call => \&IkiWiki::Plugin::mdwn::htmlize);
26 IkiWiki::loadplugin("inline");
29 sub getsetup () { #{{{
37 # Somewhat based on IkiWiki::Plugin::inline blog posting support
38 sub preprocess (@) { #{{{
41 unless (length $config{cgiurl}) {
42 error(sprintf (gettext("[[!%s plugin requires CGI enabled]]"),
46 my $page = $params{page};
47 $pagestate{$page}{PLUGIN()}{comments} = 1;
48 $pagestate{$page}{PLUGIN()}{allowhtml} = IkiWiki::yesno($params{allowhtml});
49 $pagestate{$page}{PLUGIN()}{allowdirectives} = IkiWiki::yesno($params{allowdirectives});
50 $pagestate{$page}{PLUGIN()}{commit} = defined $params{commit}
51 ? IkiWiki::yesno($params{commit})
54 my $formtemplate = IkiWiki::template(PLUGIN . "_embed.tmpl",
56 $formtemplate->param(cgiurl => $config{cgiurl});
57 $formtemplate->param(page => $params{page});
59 if ($params{preview}) {
60 $formtemplate->param("disabled" =>
61 gettext('not available during Preview'));
64 debug("page $params{page} => destpage $params{destpage}");
67 unless (defined $params{inline} && !IkiWiki::yesno($params{inline})) {
69 pages => "internal($params{page}/_comment_*)",
70 template => PLUGIN . "_display",
73 # special stuff passed through
74 page => $params{page},
75 destpage => $params{destpage},
76 preview => $params{preview},
78 push @args, atom => $params{atom} if defined $params{atom};
79 push @args, rss => $params{rss} if defined $params{rss};
80 push @args, feeds => $params{feeds} if defined $params{feeds};
81 push @args, feedshow => $params{feedshow} if defined $params{feedshow};
82 push @args, timeformat => $params{timeformat} if defined $params{timeformat};
83 push @args, feedonly => $params{feedonly} if defined $params{feedonly};
84 $posts = "\n" . IkiWiki::preprocess_inline(@args);
87 return $formtemplate->output . $posts;
90 # FIXME: logic taken from editpage, should be common code?
91 sub getcgiuser ($) { # {{{
93 my $user = $session->param('name');
94 $user = $ENV{REMOTE_ADDR} unless defined $user;
95 debug("getcgiuser() -> $user");
99 # FIXME: logic adapted from recentchanges, should be common code?
100 sub linkuser ($) { # {{{
102 my $oiduser = eval { IkiWiki::openiduser($user) };
104 if (defined $oiduser) {
105 return ($user, $oiduser);
108 my $page = bestlink('', (length $config{userdir}
109 ? "$config{userdir}/"
111 return (urlto($page, undef, 1), $user);
115 # FIXME: taken from IkiWiki::Plugin::editpage, should be common?
116 sub checksessionexpiry ($$) { # {{{
120 if (defined $session->param("name")) {
121 if (! defined $sid || $sid ne $session->id) {
122 error(gettext("Your login session has expired."));
127 # Mostly cargo-culted from IkiWiki::plugin::editpage
128 sub sessioncgi ($$) { #{{{
132 my $do = $cgi->param('do');
133 return unless $do eq PLUGIN;
135 IkiWiki::decode_cgi_utf8($cgi);
137 eval q{use CGI::FormBuilder};
140 my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
141 my $form = CGI::FormBuilder->new(
142 fields => [qw{do sid page subject body}],
145 required => [qw{body}],
148 action => $config{cgiurl},
151 template => scalar IkiWiki::template_params(PLUGIN . '_form.tmpl'),
152 # wtf does this do in editpage?
153 wikiname => $config{wikiname},
156 IkiWiki::decode_form_utf8($form);
157 IkiWiki::run_hooks(formbuilder_setup => sub {
158 shift->(title => PLUGIN, form => $form, cgi => $cgi,
159 session => $session, buttons => \@buttons);
161 IkiWiki::decode_form_utf8($form);
163 $form->field(name => 'do', type => 'hidden');
164 $form->field(name => 'sid', type => 'hidden', value => $session->id,
166 $form->field(name => 'page', type => 'hidden');
167 $form->field(name => 'subject', type => 'text', size => 72);
168 $form->field(name => 'body', type => 'textarea', rows => 5,
171 # The untaint is OK (as in editpage) because we're about to pass
172 # it to file_pruned anyway
173 my $page = $form->field('page');
174 $page = IkiWiki::possibly_foolish_untaint($page);
175 if (!defined $page || !length $page ||
176 IkiWiki::file_pruned($page, $config{srcdir})) {
177 error(gettext("bad page name"));
180 my $allow_directives = $pagestate{$page}{PLUGIN()}{allowdirectives};
181 my $allow_html = $pagestate{$page}{PLUGIN()}{allowdirectives};
182 my $commit_comments = defined $pagestate{$page}{PLUGIN()}{commit}
183 ? $pagestate{$page}{PLUGIN()}{commit}
186 # FIXME: is this right? Or should we be using the candidate subpage
187 # (whatever that might mean) as the base URL?
188 my $baseurl = urlto($page, undef, 1);
190 $form->title(sprintf(gettext("commenting on %s"),
191 IkiWiki::pagetitle($page)));
193 $form->tmpl_param('helponformattinglink',
194 htmllink($page, $page, 'ikiwiki/formatting',
196 linktext => 'FormattingHelp'),
197 allowhtml => $allow_html,
198 allowdirectives => $allow_directives);
200 if (not exists $pagesources{$page}) {
201 error(sprintf(gettext(
202 "page '%s' doesn't exist, so you can't comment"),
205 if (not $pagestate{$page}{PLUGIN()}{comments}) {
206 error(sprintf(gettext(
207 "comments are not enabled on page '%s'"),
211 if ($form->submitted eq CANCEL) {
212 # bounce back to the page they wanted to comment on, and exit.
213 # CANCEL need not be considered in future
214 IkiWiki::redirect($cgi, urlto($page, undef, 1));
218 IkiWiki::check_canedit($page . "[" . PLUGIN . "]", $cgi, $session);
220 my ($authorurl, $author) = linkuser(getcgiuser($session));
222 my $body = $form->field('body') || '';
223 $body =~ s/\r\n/\n/g;
225 $body = "\n" if $body !~ /\n$/;
227 unless ($allow_directives) {
228 # don't allow new-style directives at all
229 $body =~ s/(^|[^\\])\[\[!/$1\\[[!/g;
231 # don't allow [[ unless it begins an old-style
232 # wikilink, if prefix_directives is off
233 $body =~ s/(^|[^\\])\[\[(?![^\n\s\]+]\]\])/$1\\[[!/g
234 unless $config{prefix_directives};
237 unless ($allow_html) {
238 $body =~ s/&(\w|#)/&$1/g;
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(PLUGIN . '_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 location that has the same number of slashes
262 # as the eventual location of this comment.
263 my $fake = "$page/_" . PLUGIN . "hypothetical";
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(PLUGIN . "_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 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}._" . PLUGIN;
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_smcvpostcomment ($$;@) {
353 unless ($page =~ s/\[smcvpostcomment\]$//) {
354 return IkiWiki::FailReason->new("not posting a comment");
356 return match_glob($page, $glob);