summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/smcvpostcomment.pm
blob: cb55ff94fddf77c655a39a6b3835873b458017a1 (plain)
  1. #!/usr/bin/perl
  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;
  7. use warnings;
  8. use strict;
  9. use IkiWiki 2.00;
  10. use IkiWiki::Plugin::mdwn;
  11. use CGI 'escapeHTML';
  12. use constant PLUGIN => "smcvpostcomment";
  13. use constant PREVIEW => "Preview";
  14. use constant POST_COMMENT => "Post comment";
  15. use constant CANCEL => "Cancel";
  16. sub import { #{{{
  17. hook(type => "getsetup", id => PLUGIN, call => \&getsetup);
  18. hook(type => "preprocess", id => PLUGIN, call => \&preprocess);
  19. hook(type => "sessioncgi", id => PLUGIN, call => \&sessioncgi);
  20. hook(type => "htmlize", id => "_".PLUGIN,
  21. call => \&IkiWiki::Plugin::mdwn::htmlize);
  22. IkiWiki::loadplugin("inline");
  23. } # }}}
  24. sub getsetup () { #{{{
  25. return
  26. plugin => {
  27. safe => 1,
  28. rebuild => undef,
  29. },
  30. } #}}}
  31. # Somewhat based on IkiWiki::Plugin::inline blog posting support
  32. sub preprocess (@) { #{{{
  33. my %params=@_;
  34. unless (length $config{cgiurl}) {
  35. error("this plugin makes no sense if you have no CGI");
  36. }
  37. my $formtemplate = IkiWiki::template(PLUGIN . "_embed.tmpl",
  38. blind_cache => 1);
  39. $formtemplate->param(cgiurl => $config{cgiurl});
  40. $formtemplate->param(page => $params{page});
  41. if ($params{preview}) {
  42. $formtemplate->param("disabled" =>
  43. 'not available during Preview');
  44. }
  45. debug("page $params{page} => destpage $params{page}");
  46. # I'm reasonably sure that this counts as abuse of [[!inline]]
  47. return $formtemplate->output . "\n" .
  48. IkiWiki::preprocess_inline(
  49. pages => "internal(/$params{page}/comment_*)",
  50. template => PLUGIN . "_display",
  51. show => 0,
  52. reverse => "yes",
  53. page => $params{page},
  54. destpage => $params{destpage},
  55. preview => $params{preview});
  56. } # }}}
  57. # FIXME: logic taken from editpage, should be common code?
  58. sub getcgiuser ($) { # {{{
  59. my $session = shift;
  60. my $user = $session->param('name');
  61. $user = $ENV{REMOTE_ADDR} unless defined $user;
  62. debug("getcgiuser() -> $user");
  63. return $user;
  64. } # }}}
  65. # FIXME: logic adapted from recentchanges, should be common code?
  66. sub linkuser ($) { # {{{
  67. my $user = shift;
  68. my $oiduser = eval { IkiWiki::openiduser($user) };
  69. if (defined $oiduser) {
  70. return ($user, $oiduser);
  71. }
  72. else {
  73. my $page = bestlink('', (length $config{userdir}
  74. ? "$config{userdir}/"
  75. : "").$user);
  76. return (urlto($page, undef, 1), $user);
  77. }
  78. } # }}}
  79. # FIXME: taken from IkiWiki::Plugin::editpage, should be common?
  80. sub checksessionexpiry ($$) { # {{{
  81. my $session = shift;
  82. my $sid = shift;
  83. if (defined $session->param("name")) {
  84. if (! defined $sid || $sid ne $session->id) {
  85. error(gettext("Your login session has expired."));
  86. }
  87. }
  88. } # }}}
  89. # Mostly cargo-culted from IkiWiki::plugin::editpage
  90. sub sessioncgi ($$) { #{{{
  91. my $cgi=shift;
  92. my $session=shift;
  93. my $do = $cgi->param('do');
  94. return unless $do eq PLUGIN;
  95. # These are theoretically configurable, but currently hard-coded
  96. my $allow_wikilinks = 0;
  97. my $allow_directives = 0;
  98. my $commit_comments = 1;
  99. IkiWiki::decode_cgi_utf8($cgi);
  100. eval q{use CGI::FormBuilder};
  101. error($@) if $@;
  102. my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
  103. my $form = CGI::FormBuilder->new(
  104. fields => [qw{do sid page subject body}],
  105. charset => 'utf-8',
  106. method => 'POST',
  107. required => [qw{body}],
  108. javascript => 0,
  109. params => $cgi,
  110. action => $config{cgiurl},
  111. header => 0,
  112. table => 0,
  113. template => scalar IkiWiki::template_params(PLUGIN . '_form.tmpl'),
  114. # wtf does this do in editpage?
  115. wikiname => $config{wikiname},
  116. );
  117. IkiWiki::decode_form_utf8($form);
  118. IkiWiki::run_hooks(formbuilder_setup => sub {
  119. shift->(title => PLUGIN, form => $form, cgi => $cgi,
  120. session => $session, buttons => \@buttons);
  121. });
  122. IkiWiki::decode_form_utf8($form);
  123. $form->field(name => 'do', type => 'hidden');
  124. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  125. force => 1);
  126. $form->field(name => 'page', type => 'hidden');
  127. $form->field(name => 'subject', type => 'text', size => 80);
  128. $form->field(name => 'body', type => 'textarea', rows => 5,
  129. cols => 80);
  130. # The untaint is OK (as in editpage) because we're about to pass
  131. # it to file_pruned anyway
  132. my $page = $form->field('page');
  133. $page = IkiWiki::possibly_foolish_untaint($page);
  134. if (!defined $page || !length $page ||
  135. IkiWiki::file_pruned($page, $config{srcdir})) {
  136. error ("bad page name");
  137. }
  138. # FIXME: is this right? Or should we be using the candidate subpage
  139. # (whatever that might mean) as the base URL?
  140. my $baseurl = urlto($page, undef, 1);
  141. $form->title(sprintf(gettext("commenting on %s"),
  142. IkiWiki::pagetitle($page)));
  143. $form->tmpl_param('helponformattinglink',
  144. htmllink($page, $page, 'ikiwiki/formatting',
  145. noimageinline => 1,
  146. linktext => 'FormattingHelp'));
  147. if (not exists $pagesources{$page}) {
  148. error ("page '$page' doesn't exist, so you can't comment");
  149. }
  150. if ($form->submitted eq CANCEL) {
  151. # bounce back to the page they wanted to comment on, and exit.
  152. # CANCEL need not be considered in future
  153. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  154. exit;
  155. }
  156. my ($authorurl, $author) = linkuser(getcgiuser($session));
  157. my $body = $form->field('body');
  158. $body =~ s/\r\n/\n/g;
  159. $body =~ s/\r/\n/g;
  160. $body .= "\n" if $body !~ /\n$/;
  161. $body =~ s/\[\[([^!])/&#91;&#91;$1/g unless $allow_wikilinks;
  162. $body =~ s/\[\[!/&#91;&#91;!/g unless $allow_directives;
  163. # In this template, the [[!meta]] directives should stay at the end,
  164. # so that they will override anything the user specifies. (For
  165. # instance, [[!meta author="I can fake the author"]]...)
  166. my $content_tmpl = template(PLUGIN . '_comment.tmpl');
  167. $content_tmpl->param(author => $author);
  168. $content_tmpl->param(authorurl => $authorurl);
  169. $content_tmpl->param(subject => $form->field('subject'));
  170. $content_tmpl->param(body => $body);
  171. my $content = $content_tmpl->output;
  172. # This is essentially a simplified version of editpage:
  173. # - the user does not control the page that's created, only the parent
  174. # - it's always a create operation, never an edit
  175. # - this means that conflicts should never happen
  176. # - this means that if they do, rocks fall and everyone dies
  177. if ($form->submitted eq PREVIEW) {
  178. my $fake = "$page/_" . PLUGIN . "hypothetical";
  179. my $preview = IkiWiki::htmlize($fake, $page, 'mdwn',
  180. IkiWiki::linkify($page, $page,
  181. IkiWiki::preprocess($page, $page,
  182. IkiWiki::filter($fake, $page,
  183. $content),
  184. 0, 1)));
  185. IkiWiki::run_hooks(format => sub {
  186. $preview = shift->(page => $page,
  187. content => $preview);
  188. });
  189. my $template = template(PLUGIN . "_display.tmpl");
  190. $template->param(content => $preview);
  191. $template->param(title => $form->field('subject'));
  192. $template->param(ctime => displaytime(time));
  193. $template->param(author => $author);
  194. $template->param(authorurl => $authorurl);
  195. $form->tmpl_param(page_preview => $template->output);
  196. }
  197. else {
  198. $form->tmpl_param(page_preview => "");
  199. }
  200. if ($form->submitted eq POST_COMMENT && $form->validate) {
  201. # Let's get posting. We don't check_canedit here because
  202. # that somewhat defeats the point of this plugin.
  203. checksessionexpiry($session, $cgi->param('sid'));
  204. # FIXME: check that the wiki is locked right now, because
  205. # if it's not, there are mad race conditions!
  206. # FIXME: rather a simplistic way to make the comments...
  207. my $i = 0;
  208. my $file;
  209. do {
  210. $i++;
  211. $file = "$page/comment_${i}._" . PLUGIN;
  212. } while (-e "$config{srcdir}/$file");
  213. # FIXME: could probably do some sort of graceful retry
  214. # if I could be bothered
  215. writefile($file, $config{srcdir}, $content);
  216. my $conflict;
  217. if ($config{rcs} and $commit_comments) {
  218. my $message = "Added a comment";
  219. if (defined $form->field('subject') &&
  220. length $form->field('subject')) {
  221. $message = "Added a comment: " .
  222. $form->field('subject');
  223. }
  224. IkiWiki::rcs_add($file);
  225. IkiWiki::disable_commit_hook();
  226. $conflict = IkiWiki::rcs_commit_staged($message,
  227. $session->param('name'), $ENV{REMOTE_ADDR});
  228. IkiWiki::enable_commit_hook();
  229. IkiWiki::rcs_update();
  230. }
  231. # Now we need a refresh
  232. require IkiWiki::Render;
  233. IkiWiki::refresh();
  234. IkiWiki::saveindex();
  235. # this should never happen, unless a committer deliberately
  236. # breaks it or something
  237. error($conflict) if defined $conflict;
  238. # Bounce back to where we were, but defeat broken caches
  239. my $anticache = "?updated=$page/comment_$i";
  240. IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
  241. }
  242. else {
  243. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  244. forcebaseurl => $baseurl);
  245. }
  246. exit;
  247. } #}}}
  248. 1