summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 33d8ca8e212aaf4b907c922b6e75cc6897b6b86a (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 = $config{comments_allowdirectives};
  199. my $commit_comments = $config{comments_commit};
  200. my $comments_pagename = $config{comments_pagename};
  201. # FIXME: is this right? Or should we be using the candidate subpage
  202. # (whatever that might mean) as the base URL?
  203. my $baseurl = urlto($page, undef, 1);
  204. $form->title(sprintf(gettext("commenting on %s"),
  205. IkiWiki::pagetitle($page)));
  206. $form->tmpl_param('helponformattinglink',
  207. htmllink($page, $page, 'ikiwiki/formatting',
  208. noimageinline => 1,
  209. linktext => 'FormattingHelp'),
  210. allowdirectives => $allow_directives);
  211. if ($form->submitted eq CANCEL) {
  212. # bounce back to the page they wanted to comment on, and exit.
  213. # CANCEL need not be considered in future
  214. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  215. exit;
  216. }
  217. if (not exists $pagesources{$page}) {
  218. error(sprintf(gettext(
  219. "page '%s' doesn't exist, so you can't comment"),
  220. $page));
  221. }
  222. if (not pagespec_match($page, $config{comments_open_pagespec},
  223. location => $page)) {
  224. error(sprintf(gettext(
  225. "comments on page '%s' are closed"),
  226. $page));
  227. }
  228. IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
  229. IkiWiki::check_canedit($page . "[postcomment]", $cgi, $session);
  230. my ($authorurl, $author) = linkuser(getcgiuser($session));
  231. my $body = $form->field('body') || '';
  232. $body =~ s/\r\n/\n/g;
  233. $body =~ s/\r/\n/g;
  234. $body .= "\n" if $body !~ /\n$/;
  235. unless ($allow_directives) {
  236. # don't allow new-style directives at all
  237. $body =~ s/(^|[^\\])\[\[!/$1&#91;&#91;!/g;
  238. # don't allow [[ unless it begins an old-style
  239. # wikilink, if prefix_directives is off
  240. $body =~ s/(^|[^\\])\[\[(?![^\n\s\]+]\]\])/$1&#91;&#91;!/g
  241. unless $config{prefix_directives};
  242. }
  243. # FIXME: check that the wiki is locked right now, because
  244. # if it's not, there are mad race conditions!
  245. # FIXME: rather a simplistic way to make the comments...
  246. my $i = 0;
  247. my $file;
  248. my $location;
  249. do {
  250. $i++;
  251. $location = "$page/${comments_pagename}${i}";
  252. } while (-e "$config{srcdir}/$location._comment");
  253. my $anchor = "${comments_pagename}${i}";
  254. IkiWiki::run_hooks(sanitize => sub {
  255. $body=shift->(
  256. page => $location,
  257. destpage => $location,
  258. content => $body,
  259. );
  260. });
  261. # In this template, the [[!meta]] directives should stay at the end,
  262. # so that they will override anything the user specifies. (For
  263. # instance, [[!meta author="I can fake the author"]]...)
  264. my $content_tmpl = template('comments_comment.tmpl');
  265. $content_tmpl->param(author => $author);
  266. $content_tmpl->param(authorurl => $authorurl);
  267. $content_tmpl->param(subject => $form->field('subject'));
  268. $content_tmpl->param(body => $body);
  269. $content_tmpl->param(anchor => "$anchor");
  270. $content_tmpl->param(permalink => "$baseurl#$anchor");
  271. my $content = $content_tmpl->output;
  272. # This is essentially a simplified version of editpage:
  273. # - the user does not control the page that's created, only the parent
  274. # - it's always a create operation, never an edit
  275. # - this means that conflicts should never happen
  276. # - this means that if they do, rocks fall and everyone dies
  277. if ($form->submitted eq PREVIEW) {
  278. my $preview = IkiWiki::htmlize($location, $page, 'mdwn',
  279. IkiWiki::linkify($page, $page,
  280. IkiWiki::preprocess($page, $page,
  281. IkiWiki::filter($location,
  282. $page, $content),
  283. 0, 1)));
  284. IkiWiki::run_hooks(format => sub {
  285. $preview = shift->(page => $page,
  286. content => $preview);
  287. });
  288. my $template = template("comments_display.tmpl");
  289. $template->param(content => $preview);
  290. $template->param(title => $form->field('subject'));
  291. $template->param(ctime => displaytime(time));
  292. $template->param(author => $author);
  293. $template->param(authorurl => $authorurl);
  294. $form->tmpl_param(page_preview => $template->output);
  295. }
  296. else {
  297. $form->tmpl_param(page_preview => "");
  298. }
  299. if ($form->submitted eq POST_COMMENT && $form->validate) {
  300. my $file = "$location._comment";
  301. # FIXME: could probably do some sort of graceful retry
  302. # on error? Would require significant unwinding though
  303. writefile($file, $config{srcdir}, $content);
  304. my $conflict;
  305. if ($config{rcs} and $commit_comments) {
  306. my $message = gettext("Added a comment");
  307. if (defined $form->field('subject') &&
  308. length $form->field('subject')) {
  309. $message = sprintf(
  310. gettext("Added a comment: %s"),
  311. $form->field('subject'));
  312. }
  313. IkiWiki::rcs_add($file);
  314. IkiWiki::disable_commit_hook();
  315. $conflict = IkiWiki::rcs_commit_staged($message,
  316. $session->param('name'), $ENV{REMOTE_ADDR});
  317. IkiWiki::enable_commit_hook();
  318. IkiWiki::rcs_update();
  319. }
  320. # Now we need a refresh
  321. require IkiWiki::Render;
  322. IkiWiki::refresh();
  323. IkiWiki::saveindex();
  324. # this should never happen, unless a committer deliberately
  325. # breaks it or something
  326. error($conflict) if defined $conflict;
  327. # Bounce back to where we were, but defeat broken caches
  328. my $anticache = "?updated=$page/${comments_pagename}${i}";
  329. IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
  330. }
  331. else {
  332. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  333. forcebaseurl => $baseurl);
  334. }
  335. exit;
  336. } #}}}
  337. sub pagetemplate (@) { #{{{
  338. my %params = @_;
  339. my $page = $params{page};
  340. my $template = $params{template};
  341. if ($template->query(name => 'comments')) {
  342. my $comments = undef;
  343. my $comments_pagename = $config{comments_pagename};
  344. my $open = 0;
  345. my $shown = pagespec_match($page,
  346. $config{comments_shown_pagespec},
  347. location => $page);
  348. if (pagespec_match($page, "*/${comments_pagename}*",
  349. location => $page)) {
  350. $shown = 0;
  351. $open = 0;
  352. }
  353. if (length $config{cgiurl}) {
  354. $open = pagespec_match($page,
  355. $config{comments_open_pagespec},
  356. location => $page);
  357. }
  358. if ($shown) {
  359. eval q{use IkiWiki::Plugin::inline};
  360. error($@) if $@;
  361. my @args = (
  362. pages => "internal($page/${comments_pagename}*)",
  363. template => 'comments_display',
  364. show => 0,
  365. reverse => 'yes',
  366. page => $page,
  367. destpage => $params{destpage},
  368. );
  369. $comments = IkiWiki::preprocess_inline(@args);
  370. }
  371. if (defined $comments && length $comments) {
  372. $template->param(comments => $comments);
  373. }
  374. if ($open) {
  375. my $commenturl = IkiWiki::cgiurl(do => 'comment',
  376. page => $page);
  377. $template->param(commenturl => $commenturl);
  378. }
  379. }
  380. } # }}}
  381. package IkiWiki::PageSpec;
  382. sub match_postcomment ($$;@) {
  383. my $page = shift;
  384. my $glob = shift;
  385. unless ($page =~ s/\[postcomment\]$//) {
  386. return IkiWiki::FailReason->new("not posting a comment");
  387. }
  388. return match_glob($page, $glob);
  389. }
  390. 1