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