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