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