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