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