summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: c458ea9dab55ef2b20e18cbe48b6191066dcfafd (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 Encode;
  11. use POSIX qw(strftime);
  12. use constant PREVIEW => "Preview";
  13. use constant POST_COMMENT => "Post comment";
  14. use constant CANCEL => "Cancel";
  15. my $postcomment;
  16. sub import { #{{{
  17. hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
  18. hook(type => "getsetup", id => 'comments', call => \&getsetup);
  19. hook(type => "preprocess", id => '_comment', call => \&preprocess);
  20. hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
  21. hook(type => "htmlize", id => "_comment", call => \&htmlize);
  22. hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
  23. hook(type => "cgi", id => "comments", call => \&linkcgi);
  24. IkiWiki::loadplugin("inline");
  25. } # }}}
  26. sub getsetup () { #{{{
  27. return
  28. plugin => {
  29. safe => 1,
  30. rebuild => 1,
  31. },
  32. # Pages where comments are shown, but new comments are not
  33. # allowed, will show "Comments are closed".
  34. comments_shown_pagespec => {
  35. type => 'pagespec',
  36. example => 'blog/*',
  37. default => '',
  38. description => 'PageSpec for pages where comments will be shown inline',
  39. link => 'ikiwiki/PageSpec',
  40. safe => 1,
  41. rebuild => 1,
  42. },
  43. comments_open_pagespec => {
  44. type => 'pagespec',
  45. example => 'blog/* and created_after(close_old_comments)',
  46. default => '',
  47. description => 'PageSpec for pages where new comments can be posted',
  48. link => 'ikiwiki/PageSpec',
  49. safe => 1,
  50. rebuild => 1,
  51. },
  52. comments_pagename => {
  53. type => 'string',
  54. example => 'comment_',
  55. default => 'comment_',
  56. description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
  57. safe => 0, # manual page moving required
  58. rebuild => undef,
  59. },
  60. comments_allowdirectives => {
  61. type => 'boolean',
  62. default => 0,
  63. example => 0,
  64. description => 'Interpret directives in comments?',
  65. safe => 1,
  66. rebuild => 0,
  67. },
  68. comments_allowauthor => {
  69. type => 'boolean',
  70. default => 0,
  71. example => 0,
  72. description => 'Allow anonymous commenters to set an author name?',
  73. safe => 1,
  74. rebuild => 0,
  75. },
  76. comments_commit => {
  77. type => 'boolean',
  78. example => 1,
  79. default => 1,
  80. description => 'commit comments to the VCS',
  81. # old uncommitted comments are likely to cause
  82. # confusion if this is changed
  83. safe => 0,
  84. rebuild => 0,
  85. },
  86. } #}}}
  87. sub htmlize { # {{{
  88. my %params = @_;
  89. return $params{content};
  90. } # }}}
  91. # FIXME: copied verbatim from meta
  92. sub safeurl ($) { #{{{
  93. my $url=shift;
  94. if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
  95. defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
  96. return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
  97. }
  98. else {
  99. return 1;
  100. }
  101. } #}}}
  102. sub preprocess { # {{{
  103. my %params = @_;
  104. my $page = $params{page};
  105. my $format = $params{format};
  106. if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
  107. error(sprintf(gettext("unsupported page format %s"), $format));
  108. }
  109. my $content = $params{content};
  110. if (! defined $content) {
  111. error(gettext("comment must have content"));
  112. }
  113. $content =~ s/\\"/"/g;
  114. $content = IkiWiki::filter($page, $params{destpage}, $content);
  115. if ($config{comments_allowdirectives}) {
  116. $content = IkiWiki::preprocess($page, $params{destpage},
  117. $content);
  118. }
  119. # no need to bother with htmlize if it's just HTML
  120. $content = IkiWiki::htmlize($page, $params{destpage}, $format,
  121. $content) if defined $format;
  122. IkiWiki::run_hooks(sanitize => sub {
  123. $content = shift->(
  124. page => $page,
  125. destpage => $params{destpage},
  126. content => $content,
  127. );
  128. });
  129. # set metadata, possibly overriding [[!meta]] directives from the
  130. # comment itself
  131. my $commentuser;
  132. my $commentip;
  133. my $commentauthor;
  134. my $commentauthorurl;
  135. if (defined $params{username}) {
  136. $commentuser = $params{username};
  137. ($commentauthorurl, $commentauthor) =
  138. linkuser($params{username});
  139. }
  140. else {
  141. if (defined $params{ip}) {
  142. $commentip = $params{ip};
  143. }
  144. $commentauthor = gettext("Anonymous");
  145. }
  146. $pagestate{$page}{comments}{commentuser} = $commentuser;
  147. $pagestate{$page}{comments}{commentip} = $commentip;
  148. $pagestate{$page}{comments}{commentauthor} = $commentauthor;
  149. $pagestate{$page}{comments}{commentauthorurl} = $commentauthorurl;
  150. if (! defined $pagestate{$page}{meta}{author}) {
  151. $pagestate{$page}{meta}{author} = $commentauthor;
  152. }
  153. if (! defined $pagestate{$page}{meta}{authorurl}) {
  154. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  155. }
  156. if ($config{comments_allowauthor}) {
  157. if (defined $params{claimedauthor}) {
  158. $pagestate{$page}{meta}{author} = $params{claimedauthor};
  159. }
  160. if (defined $params{url} and safeurl($params{url})) {
  161. $pagestate{$page}{meta}{authorurl} = $params{url};
  162. }
  163. }
  164. else {
  165. $pagestate{$page}{meta}{author} = $commentauthor;
  166. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  167. }
  168. if (defined $params{subject}) {
  169. $pagestate{$page}{meta}{title} = $params{subject};
  170. }
  171. my $baseurl = urlto($params{destpage}, undef, 1);
  172. my $anchor = "";
  173. my $comments_pagename = $config{comments_pagename};
  174. if ($params{page} =~ m/\/(\Q${comments_pagename}\E\d+)$/) {
  175. $anchor = $1;
  176. }
  177. $pagestate{$page}{meta}{permalink} = "${baseurl}#${anchor}";
  178. eval q{use Date::Parse};
  179. if (! $@) {
  180. my $time = str2time($params{date});
  181. $IkiWiki::pagectime{$page} = $time if defined $time;
  182. }
  183. # FIXME: hard-coded HTML (although it's just to set an ID)
  184. return "<div id=\"$anchor\">$content</div>" if $anchor;
  185. return $content;
  186. } # }}}
  187. sub checkconfig () { #{{{
  188. $config{comments_commit} = 1 unless defined $config{comments_commit};
  189. $config{comments_pagename} = 'comment_'
  190. unless defined $config{comments_pagename};
  191. } #}}}
  192. # This is exactly the same as recentchanges_link :-(
  193. sub linkcgi ($) { #{{{
  194. my $cgi=shift;
  195. if (defined $cgi->param('do') && $cgi->param('do') eq "commenter") {
  196. my $page=decode_utf8($cgi->param("page"));
  197. if (! defined $page) {
  198. error("missing page parameter");
  199. }
  200. IkiWiki::loadindex();
  201. my $link=bestlink("", $page);
  202. if (! length $link) {
  203. print "Content-type: text/html\n\n";
  204. print IkiWiki::misctemplate(gettext(gettext("missing page")),
  205. "<p>".
  206. sprintf(gettext("The page %s does not exist."),
  207. htmllink("", "", $page)).
  208. "</p>");
  209. }
  210. else {
  211. IkiWiki::redirect($cgi, urlto($link, undef, 1));
  212. }
  213. exit;
  214. }
  215. }
  216. # FIXME: basically the same logic as recentchanges
  217. # returns (author URL, pretty-printed version)
  218. sub linkuser ($) { # {{{
  219. my $user = shift;
  220. my $oiduser = eval { IkiWiki::openiduser($user) };
  221. if (defined $oiduser) {
  222. return ($user, $oiduser);
  223. }
  224. # FIXME: it'd be good to avoid having such a link for anonymous
  225. # posts
  226. else {
  227. return (IkiWiki::cgiurl(
  228. do => 'commenter',
  229. page => (length $config{userdir}
  230. ? "$config{userdir}/$user"
  231. : "$user")
  232. ), $user);
  233. }
  234. } # }}}
  235. # Mostly cargo-culted from IkiWiki::plugin::editpage
  236. sub sessioncgi ($$) { #{{{
  237. my $cgi=shift;
  238. my $session=shift;
  239. my $do = $cgi->param('do');
  240. return unless $do eq 'comment';
  241. IkiWiki::decode_cgi_utf8($cgi);
  242. eval q{use CGI::FormBuilder};
  243. error($@) if $@;
  244. my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
  245. my $form = CGI::FormBuilder->new(
  246. fields => [qw{do sid page subject editcontent type author url}],
  247. charset => 'utf-8',
  248. method => 'POST',
  249. required => [qw{editcontent}],
  250. javascript => 0,
  251. params => $cgi,
  252. action => $config{cgiurl},
  253. header => 0,
  254. table => 0,
  255. template => scalar IkiWiki::template_params('comments_form.tmpl'),
  256. # wtf does this do in editpage?
  257. wikiname => $config{wikiname},
  258. );
  259. IkiWiki::decode_form_utf8($form);
  260. IkiWiki::run_hooks(formbuilder_setup => sub {
  261. shift->(title => "comment", form => $form, cgi => $cgi,
  262. session => $session, buttons => \@buttons);
  263. });
  264. IkiWiki::decode_form_utf8($form);
  265. my $type = $form->param('type');
  266. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  267. $type = IkiWiki::possibly_foolish_untaint($type);
  268. }
  269. else {
  270. $type = $config{default_pageext};
  271. }
  272. my @page_types;
  273. if (exists $IkiWiki::hooks{htmlize}) {
  274. @page_types = grep { ! /^_/ } keys %{$IkiWiki::hooks{htmlize}};
  275. }
  276. my $allow_author = $config{comments_allowauthor};
  277. $form->field(name => 'do', type => 'hidden');
  278. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  279. force => 1);
  280. $form->field(name => 'page', type => 'hidden');
  281. $form->field(name => 'subject', type => 'text', size => 72);
  282. $form->field(name => 'editcontent', type => 'textarea', rows => 10);
  283. $form->field(name => "type", value => $type, force => 1,
  284. type => 'select', options => \@page_types);
  285. $form->tmpl_param(username => $session->param('name'));
  286. if ($allow_author and ! defined $session->param('name')) {
  287. $form->tmpl_param(allowauthor => 1);
  288. $form->field(name => 'author', type => 'text', size => '40');
  289. $form->field(name => 'url', type => 'text', size => '40');
  290. }
  291. else {
  292. $form->tmpl_param(allowauthor => 0);
  293. $form->field(name => 'author', type => 'hidden', value => '',
  294. force => 1);
  295. $form->field(name => 'url', type => 'hidden', value => '',
  296. force => 1);
  297. }
  298. # The untaint is OK (as in editpage) because we're about to pass
  299. # it to file_pruned anyway
  300. my $page = $form->field('page');
  301. $page = IkiWiki::possibly_foolish_untaint($page);
  302. if (! defined $page || ! length $page ||
  303. IkiWiki::file_pruned($page, $config{srcdir})) {
  304. error(gettext("bad page name"));
  305. }
  306. my $allow_directives = $config{comments_allowdirectives};
  307. my $commit_comments = $config{comments_commit};
  308. my $comments_pagename = $config{comments_pagename};
  309. # FIXME: is this right? Or should we be using the candidate subpage
  310. # (whatever that might mean) as the base URL?
  311. my $baseurl = urlto($page, undef, 1);
  312. $form->title(sprintf(gettext("commenting on %s"),
  313. IkiWiki::pagetitle($page)));
  314. $form->tmpl_param('helponformattinglink',
  315. htmllink($page, $page, 'ikiwiki/formatting',
  316. noimageinline => 1,
  317. linktext => 'FormattingHelp'),
  318. allowdirectives => $allow_directives);
  319. if ($form->submitted eq CANCEL) {
  320. # bounce back to the page they wanted to comment on, and exit.
  321. # CANCEL need not be considered in future
  322. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  323. exit;
  324. }
  325. if (not exists $pagesources{$page}) {
  326. error(sprintf(gettext(
  327. "page '%s' doesn't exist, so you can't comment"),
  328. $page));
  329. }
  330. if (not pagespec_match($page, $config{comments_open_pagespec},
  331. location => $page)) {
  332. error(sprintf(gettext(
  333. "comments on page '%s' are closed"),
  334. $page));
  335. }
  336. # Set a flag to indicate that we're posting a comment,
  337. # so that postcomment() can tell it should match.
  338. $postcomment=1;
  339. IkiWiki::check_canedit($page, $cgi, $session);
  340. $postcomment=0;
  341. # FIXME: check that the wiki is locked right now, because
  342. # if it's not, there are mad race conditions!
  343. # FIXME: rather a simplistic way to make the comments...
  344. my $i = 0;
  345. my $file;
  346. my $location;
  347. do {
  348. $i++;
  349. $location = "$page/${comments_pagename}${i}";
  350. } while (-e "$config{srcdir}/$location._comment");
  351. my $anchor = "${comments_pagename}${i}";
  352. my $content = "[[!_comment format=$type\n";
  353. # FIXME: handling of double quotes probably wrong?
  354. if (defined $session->param('name')) {
  355. my $username = $session->param('name');
  356. $username =~ s/"/&quot;/g;
  357. $content .= " username=\"$username\"\n";
  358. }
  359. elsif (defined $ENV{REMOTE_ADDR}) {
  360. my $ip = $ENV{REMOTE_ADDR};
  361. if ($ip =~ m/^([.0-9]+)$/) {
  362. $content .= " ip=\"$1\"\n";
  363. }
  364. }
  365. if ($allow_author) {
  366. my $author = $form->field('author');
  367. if (length $author) {
  368. $author =~ s/"/&quot;/g;
  369. $content .= " claimedauthor=\"$author\"\n";
  370. }
  371. my $url = $form->field('url');
  372. if (length $url) {
  373. $url =~ s/"/&quot;/g;
  374. $content .= " url=\"$url\"\n";
  375. }
  376. }
  377. my $subject = $form->field('subject');
  378. if (length $subject) {
  379. $subject =~ s/"/&quot;/g;
  380. $content .= " subject=\"$subject\"\n";
  381. }
  382. $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
  383. my $editcontent = $form->field('editcontent') || '';
  384. $editcontent =~ s/\r\n/\n/g;
  385. $editcontent =~ s/\r/\n/g;
  386. $editcontent =~ s/"/\\"/g;
  387. $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
  388. # This is essentially a simplified version of editpage:
  389. # - the user does not control the page that's created, only the parent
  390. # - it's always a create operation, never an edit
  391. # - this means that conflicts should never happen
  392. # - this means that if they do, rocks fall and everyone dies
  393. if ($form->submitted eq PREVIEW) {
  394. my $preview = IkiWiki::htmlize($location, $page, '_comment',
  395. IkiWiki::linkify($page, $page,
  396. IkiWiki::preprocess($page, $page,
  397. IkiWiki::filter($location,
  398. $page, $content),
  399. 0, 1)));
  400. IkiWiki::run_hooks(format => sub {
  401. $preview = shift->(page => $page,
  402. content => $preview);
  403. });
  404. my $template = template("comments_display.tmpl");
  405. $template->param(content => $preview);
  406. $template->param(title => $form->field('subject'));
  407. $template->param(ctime => displaytime(time));
  408. $form->tmpl_param(page_preview => $template->output);
  409. }
  410. else {
  411. $form->tmpl_param(page_preview => "");
  412. }
  413. if ($form->submitted eq POST_COMMENT && $form->validate) {
  414. my $file = "$location._comment";
  415. IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
  416. # FIXME: could probably do some sort of graceful retry
  417. # on error? Would require significant unwinding though
  418. writefile($file, $config{srcdir}, $content);
  419. my $conflict;
  420. if ($config{rcs} and $commit_comments) {
  421. my $message = gettext("Added a comment");
  422. if (defined $form->field('subject') &&
  423. length $form->field('subject')) {
  424. $message = sprintf(
  425. gettext("Added a comment: %s"),
  426. $form->field('subject'));
  427. }
  428. IkiWiki::rcs_add($file);
  429. IkiWiki::disable_commit_hook();
  430. $conflict = IkiWiki::rcs_commit_staged($message,
  431. $session->param('name'), $ENV{REMOTE_ADDR});
  432. IkiWiki::enable_commit_hook();
  433. IkiWiki::rcs_update();
  434. }
  435. # Now we need a refresh
  436. require IkiWiki::Render;
  437. IkiWiki::refresh();
  438. IkiWiki::saveindex();
  439. # this should never happen, unless a committer deliberately
  440. # breaks it or something
  441. error($conflict) if defined $conflict;
  442. # Bounce back to where we were, but defeat broken caches
  443. my $anticache = "?updated=$page/${comments_pagename}${i}";
  444. IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
  445. }
  446. else {
  447. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  448. forcebaseurl => $baseurl);
  449. }
  450. exit;
  451. } #}}}
  452. sub pagetemplate (@) { #{{{
  453. my %params = @_;
  454. my $page = $params{page};
  455. my $template = $params{template};
  456. if ($template->query(name => 'comments')) {
  457. my $comments = undef;
  458. my $comments_pagename = $config{comments_pagename};
  459. my $open = 0;
  460. my $shown = pagespec_match($page,
  461. $config{comments_shown_pagespec},
  462. location => $page);
  463. if (pagespec_match($page, "*/${comments_pagename}*",
  464. location => $page)) {
  465. $shown = 0;
  466. $open = 0;
  467. }
  468. if (length $config{cgiurl}) {
  469. $open = pagespec_match($page,
  470. $config{comments_open_pagespec},
  471. location => $page);
  472. }
  473. if ($shown) {
  474. $comments = IkiWiki::preprocess_inline(
  475. pages => "internal($page/${comments_pagename}*)",
  476. template => 'comments_display',
  477. show => 0,
  478. reverse => 'yes',
  479. page => $page,
  480. destpage => $params{destpage},
  481. feedfile => 'comments',
  482. emptyfeeds => 'no',
  483. );
  484. }
  485. if (defined $comments && length $comments) {
  486. $template->param(comments => $comments);
  487. }
  488. if ($open) {
  489. my $commenturl = IkiWiki::cgiurl(do => 'comment',
  490. page => $page);
  491. $template->param(commenturl => $commenturl);
  492. }
  493. }
  494. if ($template->query(name => 'commentuser')) {
  495. $template->param(commentuser =>
  496. $pagestate{$page}{comments}{commentuser});
  497. }
  498. if ($template->query(name => 'commentip')) {
  499. $template->param(commentip =>
  500. $pagestate{$page}{comments}{commentip});
  501. }
  502. if ($template->query(name => 'commentauthor')) {
  503. $template->param(commentauthor =>
  504. $pagestate{$page}{comments}{commentauthor});
  505. }
  506. if ($template->query(name => 'commentauthorurl')) {
  507. $template->param(commentauthorurl =>
  508. $pagestate{$page}{comments}{commentauthorurl});
  509. }
  510. } # }}}
  511. package IkiWiki::PageSpec;
  512. sub match_postcomment ($$;@) {
  513. my $page = shift;
  514. my $glob = shift;
  515. if (! $postcomment) {
  516. return IkiWiki::FailReason->new("not posting a comment");
  517. }
  518. return match_glob($page, $glob);
  519. }
  520. 1