summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 45b13168ab9a65694f70028973440a27c9598195 (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::check_canedit($page . "[postcomment]", $cgi, $session);
  235. my ($authorurl, $author) = linkuser(getcgiuser($session));
  236. my $body = $form->field('body') || '';
  237. $body =~ s/\r\n/\n/g;
  238. $body =~ s/\r/\n/g;
  239. $body .= "\n" if $body !~ /\n$/;
  240. unless ($allow_directives) {
  241. # don't allow new-style directives at all
  242. $body =~ s/(^|[^\\])\[\[!/$1&#91;&#91;!/g;
  243. # don't allow [[ unless it begins an old-style
  244. # wikilink, if prefix_directives is off
  245. $body =~ s/(^|[^\\])\[\[(?![^\n\s\]+]\]\])/$1&#91;&#91;!/g
  246. unless $config{prefix_directives};
  247. }
  248. # FIXME: check that the wiki is locked right now, because
  249. # if it's not, there are mad race conditions!
  250. # FIXME: rather a simplistic way to make the comments...
  251. my $i = 0;
  252. my $file;
  253. my $location;
  254. do {
  255. $i++;
  256. $location = "$page/${comments_pagename}${i}";
  257. } while (-e "$config{srcdir}/$location._comment");
  258. my $anchor = "${comments_pagename}${i}";
  259. IkiWiki::run_hooks(sanitize => sub {
  260. $body=shift->(
  261. page => $location,
  262. destpage => $location,
  263. content => $body,
  264. );
  265. });
  266. # In this template, the [[!meta]] directives should stay at the end,
  267. # so that they will override anything the user specifies. (For
  268. # instance, [[!meta author="I can fake the author"]]...)
  269. my $content_tmpl = template('comments_comment.tmpl');
  270. $content_tmpl->param(author => $author);
  271. $content_tmpl->param(authorurl => $authorurl);
  272. $content_tmpl->param(subject => $form->field('subject'));
  273. $content_tmpl->param(body => $body);
  274. $content_tmpl->param(anchor => "$anchor");
  275. $content_tmpl->param(permalink => "$baseurl#$anchor");
  276. $content_tmpl->param(date => IkiWiki::formattime(time, "%X %x"));
  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. IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
  308. # FIXME: could probably do some sort of graceful retry
  309. # on error? Would require significant unwinding though
  310. writefile($file, $config{srcdir}, $content);
  311. my $conflict;
  312. if ($config{rcs} and $commit_comments) {
  313. my $message = gettext("Added a comment");
  314. if (defined $form->field('subject') &&
  315. length $form->field('subject')) {
  316. $message = sprintf(
  317. gettext("Added a comment: %s"),
  318. $form->field('subject'));
  319. }
  320. IkiWiki::rcs_add($file);
  321. IkiWiki::disable_commit_hook();
  322. $conflict = IkiWiki::rcs_commit_staged($message,
  323. $session->param('name'), $ENV{REMOTE_ADDR});
  324. IkiWiki::enable_commit_hook();
  325. IkiWiki::rcs_update();
  326. }
  327. # Now we need a refresh
  328. require IkiWiki::Render;
  329. IkiWiki::refresh();
  330. IkiWiki::saveindex();
  331. # this should never happen, unless a committer deliberately
  332. # breaks it or something
  333. error($conflict) if defined $conflict;
  334. # Bounce back to where we were, but defeat broken caches
  335. my $anticache = "?updated=$page/${comments_pagename}${i}";
  336. IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
  337. }
  338. else {
  339. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  340. forcebaseurl => $baseurl);
  341. }
  342. exit;
  343. } #}}}
  344. sub pagetemplate (@) { #{{{
  345. my %params = @_;
  346. my $page = $params{page};
  347. my $template = $params{template};
  348. if ($template->query(name => 'comments')) {
  349. my $comments = undef;
  350. my $comments_pagename = $config{comments_pagename};
  351. my $open = 0;
  352. my $shown = pagespec_match($page,
  353. $config{comments_shown_pagespec},
  354. location => $page);
  355. if (pagespec_match($page, "*/${comments_pagename}*",
  356. location => $page)) {
  357. $shown = 0;
  358. $open = 0;
  359. }
  360. if (length $config{cgiurl}) {
  361. $open = pagespec_match($page,
  362. $config{comments_open_pagespec},
  363. location => $page);
  364. }
  365. if ($shown) {
  366. eval q{use IkiWiki::Plugin::inline};
  367. error($@) if $@;
  368. my @args = (
  369. pages => "internal($page/${comments_pagename}*)",
  370. template => 'comments_display',
  371. show => 0,
  372. reverse => 'yes',
  373. page => $page,
  374. destpage => $params{destpage},
  375. );
  376. $comments = IkiWiki::preprocess_inline(@args);
  377. }
  378. if (defined $comments && length $comments) {
  379. $template->param(comments => $comments);
  380. }
  381. if ($open) {
  382. my $commenturl = IkiWiki::cgiurl(do => 'comment',
  383. page => $page);
  384. $template->param(commenturl => $commenturl);
  385. }
  386. }
  387. } # }}}
  388. package IkiWiki::PageSpec;
  389. sub match_postcomment ($$;@) {
  390. my $page = shift;
  391. my $glob = shift;
  392. unless ($page =~ s/\[postcomment\]$//) {
  393. return IkiWiki::FailReason->new("not posting a comment");
  394. }
  395. return match_glob($page, $glob);
  396. }
  397. 1