summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: 4fa82f4678da6fb1e211d2c0a9ff9374eaa73377 (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 constant PREVIEW => "Preview";
  12. use constant POST_COMMENT => "Post comment";
  13. use constant CANCEL => "Cancel";
  14. sub import { #{{{
  15. hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
  16. hook(type => "getsetup", id => 'comments', call => \&getsetup);
  17. hook(type => "preprocess", id => 'comment', call => \&preprocess);
  18. hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
  19. hook(type => "htmlize", id => "_comment", call => \&htmlize);
  20. hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
  21. hook(type => "cgi", id => "comments", call => \&linkcgi);
  22. IkiWiki::loadplugin("inline");
  23. } # }}}
  24. sub htmlize { # {{{
  25. my %params = @_;
  26. return $params{content};
  27. } # }}}
  28. # FIXME: copied verbatim from meta
  29. sub safeurl ($) { #{{{
  30. my $url=shift;
  31. if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
  32. defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
  33. return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
  34. }
  35. else {
  36. return 1;
  37. }
  38. } #}}}
  39. sub preprocess { # {{{
  40. my %params = @_;
  41. my $page = $params{page};
  42. my $format = $params{format};
  43. if (defined $format && !exists $IkiWiki::hooks{htmlize}{$format}) {
  44. error(sprintf(gettext("unsupported page format %s"), $format));
  45. }
  46. my $content = $params{content};
  47. if (!defined $content) {
  48. error(gettext("comment must have content"));
  49. }
  50. $content =~ s/\\"/"/g;
  51. $content = IkiWiki::filter($page, $params{destpage}, $content);
  52. if ($config{comments_allowdirectives}) {
  53. $content = IkiWiki::preprocess($page, $params{destpage},
  54. $content);
  55. }
  56. # no need to bother with htmlize if it's just HTML
  57. $content = IkiWiki::htmlize($page, $params{destpage}, $format,
  58. $content) if defined $format;
  59. IkiWiki::run_hooks(sanitize => sub {
  60. $content = shift->(
  61. page => $page,
  62. destpage => $params{destpage},
  63. content => $content,
  64. );
  65. });
  66. # set metadata, possibly overriding [[!meta]] directives from the
  67. # comment itself
  68. my $commentuser;
  69. my $commentip;
  70. my $commentauthor;
  71. my $commentauthorurl;
  72. if (defined $params{username}) {
  73. $commentuser = $params{username};
  74. ($commentauthorurl, $commentauthor) =
  75. linkuser($params{username});
  76. }
  77. elsif (defined $params{ip}) {
  78. $commentip = $params{ip};
  79. $commentauthor = sprintf(
  80. gettext("Anonymous (IP: %s)"), $params{ip});
  81. }
  82. else {
  83. $commentauthor = gettext("Anonymous");
  84. }
  85. $pagestate{$page}{comments}{commentuser} = $commentuser;
  86. $pagestate{$page}{comments}{commentip} = $commentip;
  87. $pagestate{$page}{comments}{commentauthor} = $commentauthor;
  88. $pagestate{$page}{comments}{commentauthorurl} = $commentauthorurl;
  89. if (!defined $pagestate{$page}{meta}{author}) {
  90. $pagestate{$page}{meta}{author} = $commentauthor;
  91. }
  92. if (!defined $pagestate{$page}{meta}{authorurl}) {
  93. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  94. }
  95. if ($config{comments_allowauthor}) {
  96. if (defined $params{claimedauthor}) {
  97. $pagestate{$page}{meta}{author} = $params{claimedauthor};
  98. }
  99. if (defined $params{url} and safeurl($params{url})) {
  100. $pagestate{$page}{meta}{authorurl} = $params{url};
  101. }
  102. }
  103. else {
  104. $pagestate{$page}{meta}{author} = $commentauthor;
  105. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  106. }
  107. if (defined $params{subject}) {
  108. $pagestate{$page}{meta}{title} = $params{subject};
  109. }
  110. my $baseurl = urlto($params{destpage}, undef, 1);
  111. my $anchor = "";
  112. my $comments_pagename = $config{comments_pagename};
  113. if ($params{page} =~ m/\/(\Q${comments_pagename}\E\d+)$/) {
  114. $anchor = $1;
  115. }
  116. $pagestate{$page}{meta}{permalink} = "${baseurl}#${anchor}";
  117. eval q{use Date::Parse};
  118. if (! $@) {
  119. my $time = str2time($params{date});
  120. $IkiWiki::pagectime{$page} = $time if defined $time;
  121. }
  122. # FIXME: hard-coded HTML (although it's just to set an ID)
  123. return "<div id=\"$anchor\">$content</div>" if $anchor;
  124. return $content;
  125. } # }}}
  126. sub getsetup () { #{{{
  127. return
  128. plugin => {
  129. safe => 1,
  130. rebuild => 1,
  131. },
  132. # Pages where comments are shown, but new comments are not
  133. # allowed, will show "Comments are closed".
  134. comments_shown_pagespec => {
  135. type => 'pagespec',
  136. example => 'blog/*',
  137. default => '',
  138. description => 'PageSpec for pages where comments will be shown inline',
  139. link => 'ikiwiki/PageSpec',
  140. safe => 1,
  141. rebuild => 1,
  142. },
  143. comments_open_pagespec => {
  144. type => 'pagespec',
  145. example => 'blog/* and created_after(close_old_comments)',
  146. default => '',
  147. description => 'PageSpec for pages where new comments can be posted',
  148. link => 'ikiwiki/PageSpec',
  149. safe => 1,
  150. rebuild => 1,
  151. },
  152. comments_pagename => {
  153. type => 'string',
  154. example => 'comment_',
  155. default => 'comment_',
  156. description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
  157. safe => 0, # manual page moving will required
  158. rebuild => undef,
  159. },
  160. comments_allowdirectives => {
  161. type => 'boolean',
  162. default => 0,
  163. example => 0,
  164. description => 'Interpret directives in comments?',
  165. safe => 1,
  166. rebuild => 0,
  167. },
  168. comments_allowauthor => {
  169. type => 'boolean',
  170. default => 0,
  171. example => 0,
  172. description => 'Allow anonymous commenters to set an author name?',
  173. safe => 1,
  174. rebuild => 0,
  175. },
  176. comments_commit => {
  177. type => 'boolean',
  178. example => 1,
  179. default => 1,
  180. description => 'commit comments to the VCS',
  181. # old uncommitted comments are likely to cause
  182. # confusion if this is changed
  183. safe => 0,
  184. rebuild => 0,
  185. },
  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. IkiWiki::check_canedit($page . "[postcomment]", $cgi, $session);
  337. my $editcontent = $form->field('editcontent') || '';
  338. $editcontent =~ s/\r\n/\n/g;
  339. $editcontent =~ s/\r/\n/g;
  340. # FIXME: check that the wiki is locked right now, because
  341. # if it's not, there are mad race conditions!
  342. # FIXME: rather a simplistic way to make the comments...
  343. my $i = 0;
  344. my $file;
  345. my $location;
  346. do {
  347. $i++;
  348. $location = "$page/${comments_pagename}${i}";
  349. } while (-e "$config{srcdir}/$location._comment");
  350. my $anchor = "${comments_pagename}${i}";
  351. $editcontent =~ s/"/\\"/g;
  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=\"" . IkiWiki::formattime(time, '%X %x') . "\"\n";
  383. $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
  384. # This is essentially a simplified version of editpage:
  385. # - the user does not control the page that's created, only the parent
  386. # - it's always a create operation, never an edit
  387. # - this means that conflicts should never happen
  388. # - this means that if they do, rocks fall and everyone dies
  389. if ($form->submitted eq PREVIEW) {
  390. my $preview = IkiWiki::htmlize($location, $page, '_comment',
  391. IkiWiki::linkify($page, $page,
  392. IkiWiki::preprocess($page, $page,
  393. IkiWiki::filter($location,
  394. $page, $content),
  395. 0, 1)));
  396. IkiWiki::run_hooks(format => sub {
  397. $preview = shift->(page => $page,
  398. content => $preview);
  399. });
  400. my $template = template("comments_display.tmpl");
  401. $template->param(content => $preview);
  402. $template->param(title => $form->field('subject'));
  403. $template->param(ctime => displaytime(time));
  404. $form->tmpl_param(page_preview => $template->output);
  405. }
  406. else {
  407. $form->tmpl_param(page_preview => "");
  408. }
  409. if ($form->submitted eq POST_COMMENT && $form->validate) {
  410. my $file = "$location._comment";
  411. IkiWiki::checksessionexpiry($session, $cgi->param('sid'));
  412. # FIXME: could probably do some sort of graceful retry
  413. # on error? Would require significant unwinding though
  414. writefile($file, $config{srcdir}, $content);
  415. my $conflict;
  416. if ($config{rcs} and $commit_comments) {
  417. my $message = gettext("Added a comment");
  418. if (defined $form->field('subject') &&
  419. length $form->field('subject')) {
  420. $message = sprintf(
  421. gettext("Added a comment: %s"),
  422. $form->field('subject'));
  423. }
  424. IkiWiki::rcs_add($file);
  425. IkiWiki::disable_commit_hook();
  426. $conflict = IkiWiki::rcs_commit_staged($message,
  427. $session->param('name'), $ENV{REMOTE_ADDR});
  428. IkiWiki::enable_commit_hook();
  429. IkiWiki::rcs_update();
  430. }
  431. # Now we need a refresh
  432. require IkiWiki::Render;
  433. IkiWiki::refresh();
  434. IkiWiki::saveindex();
  435. # this should never happen, unless a committer deliberately
  436. # breaks it or something
  437. error($conflict) if defined $conflict;
  438. # Bounce back to where we were, but defeat broken caches
  439. my $anticache = "?updated=$page/${comments_pagename}${i}";
  440. IkiWiki::redirect($cgi, urlto($page, undef, 1).$anticache);
  441. }
  442. else {
  443. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  444. forcebaseurl => $baseurl);
  445. }
  446. exit;
  447. } #}}}
  448. sub pagetemplate (@) { #{{{
  449. my %params = @_;
  450. my $page = $params{page};
  451. my $template = $params{template};
  452. if ($template->query(name => 'comments')) {
  453. my $comments = undef;
  454. my $comments_pagename = $config{comments_pagename};
  455. my $open = 0;
  456. my $shown = pagespec_match($page,
  457. $config{comments_shown_pagespec},
  458. location => $page);
  459. if (pagespec_match($page, "*/${comments_pagename}*",
  460. location => $page)) {
  461. $shown = 0;
  462. $open = 0;
  463. }
  464. if (length $config{cgiurl}) {
  465. $open = pagespec_match($page,
  466. $config{comments_open_pagespec},
  467. location => $page);
  468. }
  469. if ($shown) {
  470. eval q{use IkiWiki::Plugin::inline};
  471. error($@) if $@;
  472. my @args = (
  473. pages => "internal($page/${comments_pagename}*)",
  474. template => 'comments_display',
  475. show => 0,
  476. reverse => 'yes',
  477. page => $page,
  478. destpage => $params{destpage},
  479. );
  480. $comments = IkiWiki::preprocess_inline(@args);
  481. }
  482. if (defined $comments && length $comments) {
  483. $template->param(comments => $comments);
  484. }
  485. if ($open) {
  486. my $commenturl = IkiWiki::cgiurl(do => 'comment',
  487. page => $page);
  488. $template->param(commenturl => $commenturl);
  489. }
  490. }
  491. if ($template->query(name => 'commentuser')) {
  492. $template->param(commentuser =>
  493. $pagestate{$page}{comments}{commentuser});
  494. }
  495. if ($template->query(name => 'commentip')) {
  496. $template->param(commentip =>
  497. $pagestate{$page}{comments}{commentip});
  498. }
  499. if ($template->query(name => 'commentauthor')) {
  500. $template->param(commentauthor =>
  501. $pagestate{$page}{comments}{commentauthor});
  502. }
  503. if ($template->query(name => 'commentauthorurl')) {
  504. $template->param(commentauthorurl =>
  505. $pagestate{$page}{comments}{commentauthorurl});
  506. }
  507. } # }}}
  508. package IkiWiki::PageSpec;
  509. sub match_postcomment ($$;@) {
  510. my $page = shift;
  511. my $glob = shift;
  512. unless ($page =~ s/\[postcomment\]$//) {
  513. return IkiWiki::FailReason->new("not posting a comment");
  514. }
  515. return match_glob($page, $glob);
  516. }
  517. 1