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