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