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