summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 6184c6031281f5c0bd719b2b9dab95ae8337a100 (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. if ($params{page} =~ m/\/(\Q$config{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. $form->field(name => 'do', type => 'hidden');
  276. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  277. force => 1);
  278. $form->field(name => 'page', type => 'hidden');
  279. $form->field(name => 'subject', type => 'text', size => 72);
  280. $form->field(name => 'editcontent', type => 'textarea', rows => 10);
  281. $form->field(name => "type", value => $type, force => 1,
  282. type => 'select', options => \@page_types);
  283. $form->tmpl_param(username => $session->param('name'));
  284. if ($config{comments_allowauthor} and
  285. ! 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. # FIXME: is this right? Or should we be using the candidate subpage
  306. # (whatever that might mean) as the base URL?
  307. my $baseurl = urlto($page, undef, 1);
  308. $form->title(sprintf(gettext("commenting on %s"),
  309. IkiWiki::pagetitle($page)));
  310. $form->tmpl_param('helponformattinglink',
  311. htmllink($page, $page, 'ikiwiki/formatting',
  312. noimageinline => 1,
  313. linktext => 'FormattingHelp'),
  314. allowdirectives => $config{allow_directives});
  315. if ($form->submitted eq CANCEL) {
  316. # bounce back to the page they wanted to comment on, and exit.
  317. # CANCEL need not be considered in future
  318. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  319. exit;
  320. }
  321. if (not exists $pagesources{$page}) {
  322. error(sprintf(gettext(
  323. "page '%s' doesn't exist, so you can't comment"),
  324. $page));
  325. }
  326. if (not pagespec_match($page, $config{comments_open_pagespec},
  327. location => $page)) {
  328. error(sprintf(gettext(
  329. "comments on page '%s' are closed"),
  330. $page));
  331. }
  332. # Set a flag to indicate that we're posting a comment,
  333. # so that postcomment() can tell it should match.
  334. $postcomment=1;
  335. IkiWiki::check_canedit($page, $cgi, $session);
  336. $postcomment=0;
  337. # FIXME: rather a simplistic way to make the comments...
  338. my $i = 0;
  339. my $file;
  340. my $location;
  341. do {
  342. $i++;
  343. $location = "$page/$config{comments_pagename}$i";
  344. } while (-e "$config{srcdir}/$location._comment");
  345. my $content = "[[!_comment format=$type\n";
  346. # FIXME: handling of double quotes probably wrong?
  347. if (defined $session->param('name')) {
  348. my $username = $session->param('name');
  349. $username =~ s/"/&quot;/g;
  350. $content .= " username=\"$username\"\n";
  351. }
  352. elsif (defined $ENV{REMOTE_ADDR}) {
  353. my $ip = $ENV{REMOTE_ADDR};
  354. if ($ip =~ m/^([.0-9]+)$/) {
  355. $content .= " ip=\"$1\"\n";
  356. }
  357. }
  358. if ($config{comments_allowauthor}) {
  359. my $author = $form->field('author');
  360. if (length $author) {
  361. $author =~ s/"/&quot;/g;
  362. $content .= " claimedauthor=\"$author\"\n";
  363. }
  364. my $url = $form->field('url');
  365. if (length $url) {
  366. $url =~ s/"/&quot;/g;
  367. $content .= " url=\"$url\"\n";
  368. }
  369. }
  370. my $subject = $form->field('subject');
  371. if (length $subject) {
  372. $subject =~ s/"/&quot;/g;
  373. $content .= " subject=\"$subject\"\n";
  374. }
  375. $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
  376. my $editcontent = $form->field('editcontent') || '';
  377. $editcontent =~ s/\r\n/\n/g;
  378. $editcontent =~ s/\r/\n/g;
  379. $editcontent =~ s/"/\\"/g;
  380. $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
  381. # This is essentially a simplified version of editpage:
  382. # - the user does not control the page that's created, only the parent
  383. # - it's always a create operation, never an edit
  384. # - this means that conflicts should never happen
  385. # - this means that if they do, rocks fall and everyone dies
  386. if ($form->submitted eq PREVIEW) {
  387. my $preview = IkiWiki::htmlize($location, $page, '_comment',
  388. IkiWiki::linkify($page, $page,
  389. IkiWiki::preprocess($page, $page,
  390. IkiWiki::filter($location,
  391. $page, $content),
  392. 0, 1)));
  393. IkiWiki::run_hooks(format => sub {
  394. $preview = shift->(page => $page,
  395. content => $preview);
  396. });
  397. my $template = template("comments_display.tmpl");
  398. $template->param(content => $preview);
  399. $template->param(title => $form->field('subject'));
  400. $template->param(ctime => displaytime(time));
  401. $form->tmpl_param(page_preview => $template->output);
  402. }
  403. else {
  404. $form->tmpl_param(page_preview => "");
  405. }
  406. if ($form->submitted eq POST_COMMENT && $form->validate) {
  407. my $file = "$location._comment";
  408. IkiWiki::checksessionexpiry($cgi, $session);
  409. # FIXME: could probably do some sort of graceful retry
  410. # on error? Would require significant unwinding though
  411. writefile($file, $config{srcdir}, $content);
  412. my $conflict;
  413. if ($config{rcs} and $config{comments_commit}) {
  414. my $message = gettext("Added a comment");
  415. if (defined $form->field('subject') &&
  416. length $form->field('subject')) {
  417. $message = sprintf(
  418. gettext("Added a comment: %s"),
  419. $form->field('subject'));
  420. }
  421. IkiWiki::rcs_add($file);
  422. IkiWiki::disable_commit_hook();
  423. $conflict = IkiWiki::rcs_commit_staged($message,
  424. $session->param('name'), $ENV{REMOTE_ADDR});
  425. IkiWiki::enable_commit_hook();
  426. IkiWiki::rcs_update();
  427. }
  428. # Now we need a refresh
  429. require IkiWiki::Render;
  430. IkiWiki::refresh();
  431. IkiWiki::saveindex();
  432. # this should never happen, unless a committer deliberately
  433. # breaks it or something
  434. error($conflict) if defined $conflict;
  435. # Bounce back to where we were, but defeat broken caches
  436. my $anticache = "?updated=$page/$config{comments_pagename}$i";
  437. IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
  438. }
  439. else {
  440. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  441. forcebaseurl => $baseurl);
  442. }
  443. exit;
  444. }
  445. sub pagetemplate (@) {
  446. my %params = @_;
  447. my $page = $params{page};
  448. my $template = $params{template};
  449. if ($template->query(name => 'comments')) {
  450. my $comments = undef;
  451. my $open = 0;
  452. my $shown = pagespec_match($page,
  453. $config{comments_shown_pagespec},
  454. location => $page);
  455. if (pagespec_match($page, "*/$config{comments_pagename}*",
  456. location => $page)) {
  457. $shown = 0;
  458. $open = 0;
  459. }
  460. if (length $config{cgiurl}) {
  461. $open = pagespec_match($page,
  462. $config{comments_open_pagespec},
  463. location => $page);
  464. }
  465. if ($shown) {
  466. $comments = IkiWiki::preprocess_inline(
  467. pages => "internal($page/$config{comments_pagename}*)",
  468. template => 'comments_display',
  469. show => 0,
  470. reverse => 'yes',
  471. page => $page,
  472. destpage => $params{destpage},
  473. feedfile => 'comments',
  474. emptyfeeds => 'no',
  475. );
  476. }
  477. if (defined $comments && length $comments) {
  478. $template->param(comments => $comments);
  479. }
  480. if ($open) {
  481. my $commenturl = IkiWiki::cgiurl(do => 'comment',
  482. page => $page);
  483. $template->param(commenturl => $commenturl);
  484. }
  485. }
  486. if ($template->query(name => 'commentuser')) {
  487. $template->param(commentuser =>
  488. $pagestate{$page}{comments}{commentuser});
  489. }
  490. if ($template->query(name => 'commentip')) {
  491. $template->param(commentip =>
  492. $pagestate{$page}{comments}{commentip});
  493. }
  494. if ($template->query(name => 'commentauthor')) {
  495. $template->param(commentauthor =>
  496. $pagestate{$page}{comments}{commentauthor});
  497. }
  498. if ($template->query(name => 'commentauthorurl')) {
  499. $template->param(commentauthorurl =>
  500. $pagestate{$page}{comments}{commentauthorurl});
  501. }
  502. }
  503. package IkiWiki::PageSpec;
  504. sub match_postcomment ($$;@) {
  505. my $page = shift;
  506. my $glob = shift;
  507. if (! $postcomment) {
  508. return IkiWiki::FailReason->new("not posting a comment");
  509. }
  510. return match_glob($page, $glob);
  511. }
  512. 1