summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 972d069d50605d22afed0e79e23a6a9df5fb8e51 (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 $content = "[[!_comment format=$type\n";
  352. # FIXME: handling of double quotes probably wrong?
  353. if (defined $session->param('name')) {
  354. my $username = $session->param('name');
  355. $username =~ s/"/&quot;/g;
  356. $content .= " username=\"$username\"\n";
  357. }
  358. elsif (defined $ENV{REMOTE_ADDR}) {
  359. my $ip = $ENV{REMOTE_ADDR};
  360. if ($ip =~ m/^([.0-9]+)$/) {
  361. $content .= " ip=\"$1\"\n";
  362. }
  363. }
  364. if ($allow_author) {
  365. my $author = $form->field('author');
  366. if (length $author) {
  367. $author =~ s/"/&quot;/g;
  368. $content .= " claimedauthor=\"$author\"\n";
  369. }
  370. my $url = $form->field('url');
  371. if (length $url) {
  372. $url =~ s/"/&quot;/g;
  373. $content .= " url=\"$url\"\n";
  374. }
  375. }
  376. my $subject = $form->field('subject');
  377. if (length $subject) {
  378. $subject =~ s/"/&quot;/g;
  379. $content .= " subject=\"$subject\"\n";
  380. }
  381. $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
  382. my $editcontent = $form->field('editcontent') || '';
  383. $editcontent =~ s/\r\n/\n/g;
  384. $editcontent =~ s/\r/\n/g;
  385. $editcontent =~ s/"/\\"/g;
  386. $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
  387. # This is essentially a simplified version of editpage:
  388. # - the user does not control the page that's created, only the parent
  389. # - it's always a create operation, never an edit
  390. # - this means that conflicts should never happen
  391. # - this means that if they do, rocks fall and everyone dies
  392. if ($form->submitted eq PREVIEW) {
  393. my $preview = IkiWiki::htmlize($location, $page, '_comment',
  394. IkiWiki::linkify($page, $page,
  395. IkiWiki::preprocess($page, $page,
  396. IkiWiki::filter($location,
  397. $page, $content),
  398. 0, 1)));
  399. IkiWiki::run_hooks(format => sub {
  400. $preview = shift->(page => $page,
  401. content => $preview);
  402. });
  403. my $template = template("comments_display.tmpl");
  404. $template->param(content => $preview);
  405. $template->param(title => $form->field('subject'));
  406. $template->param(ctime => displaytime(time));
  407. $form->tmpl_param(page_preview => $template->output);
  408. }
  409. else {
  410. $form->tmpl_param(page_preview => "");
  411. }
  412. if ($form->submitted eq POST_COMMENT && $form->validate) {
  413. my $file = "$location._comment";
  414. IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
  415. # FIXME: could probably do some sort of graceful retry
  416. # on error? Would require significant unwinding though
  417. writefile($file, $config{srcdir}, $content);
  418. my $conflict;
  419. if ($config{rcs} and $commit_comments) {
  420. my $message = gettext("Added a comment");
  421. if (defined $form->field('subject') &&
  422. length $form->field('subject')) {
  423. $message = sprintf(
  424. gettext("Added a comment: %s"),
  425. $form->field('subject'));
  426. }
  427. IkiWiki::rcs_add($file);
  428. IkiWiki::disable_commit_hook();
  429. $conflict = IkiWiki::rcs_commit_staged($message,
  430. $session->param('name'), $ENV{REMOTE_ADDR});
  431. IkiWiki::enable_commit_hook();
  432. IkiWiki::rcs_update();
  433. }
  434. # Now we need a refresh
  435. require IkiWiki::Render;
  436. IkiWiki::refresh();
  437. IkiWiki::saveindex();
  438. # this should never happen, unless a committer deliberately
  439. # breaks it or something
  440. error($conflict) if defined $conflict;
  441. # Bounce back to where we were, but defeat broken caches
  442. my $anticache = "?updated=$page/${comments_pagename}${i}";
  443. IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
  444. }
  445. else {
  446. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  447. forcebaseurl => $baseurl);
  448. }
  449. exit;
  450. } #}}}
  451. sub pagetemplate (@) { #{{{
  452. my %params = @_;
  453. my $page = $params{page};
  454. my $template = $params{template};
  455. if ($template->query(name => 'comments')) {
  456. my $comments = undef;
  457. my $comments_pagename = $config{comments_pagename};
  458. my $open = 0;
  459. my $shown = pagespec_match($page,
  460. $config{comments_shown_pagespec},
  461. location => $page);
  462. if (pagespec_match($page, "*/${comments_pagename}*",
  463. location => $page)) {
  464. $shown = 0;
  465. $open = 0;
  466. }
  467. if (length $config{cgiurl}) {
  468. $open = pagespec_match($page,
  469. $config{comments_open_pagespec},
  470. location => $page);
  471. }
  472. if ($shown) {
  473. $comments = IkiWiki::preprocess_inline(
  474. pages => "internal($page/${comments_pagename}*)",
  475. template => 'comments_display',
  476. show => 0,
  477. reverse => 'yes',
  478. page => $page,
  479. destpage => $params{destpage},
  480. feedfile => 'comments',
  481. emptyfeeds => 'no',
  482. );
  483. }
  484. if (defined $comments && length $comments) {
  485. $template->param(comments => $comments);
  486. }
  487. if ($open) {
  488. my $commenturl = IkiWiki::cgiurl(do => 'comment',
  489. page => $page);
  490. $template->param(commenturl => $commenturl);
  491. }
  492. }
  493. if ($template->query(name => 'commentuser')) {
  494. $template->param(commentuser =>
  495. $pagestate{$page}{comments}{commentuser});
  496. }
  497. if ($template->query(name => 'commentip')) {
  498. $template->param(commentip =>
  499. $pagestate{$page}{comments}{commentip});
  500. }
  501. if ($template->query(name => 'commentauthor')) {
  502. $template->param(commentauthor =>
  503. $pagestate{$page}{comments}{commentauthor});
  504. }
  505. if ($template->query(name => 'commentauthorurl')) {
  506. $template->param(commentauthorurl =>
  507. $pagestate{$page}{comments}{commentauthorurl});
  508. }
  509. } # }}}
  510. package IkiWiki::PageSpec;
  511. sub match_postcomment ($$;@) {
  512. my $page = shift;
  513. my $glob = shift;
  514. if (! $postcomment) {
  515. return IkiWiki::FailReason->new("not posting a comment");
  516. }
  517. return match_glob($page, $glob);
  518. }
  519. 1