summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/comments.pm
blob: a39dab36cdb81e6c06049d9c0bc312a6e58b1dd2 (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 3.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. my %commentstate;
  17. sub import {
  18. hook(type => "checkconfig", id => 'comments', call => \&checkconfig);
  19. hook(type => "getsetup", id => 'comments', call => \&getsetup);
  20. hook(type => "preprocess", id => 'comment', call => \&preprocess);
  21. hook(type => "preprocess", id => 'commentmoderation', call => \&preprocess_moderation);
  22. # here for backwards compatability with old comments
  23. hook(type => "preprocess", id => '_comment', call => \&preprocess);
  24. hook(type => "sessioncgi", id => 'comment', call => \&sessioncgi);
  25. hook(type => "htmlize", id => "_comment", call => \&htmlize);
  26. hook(type => "htmlize", id => "_comment_pending",
  27. call => \&htmlize_pending);
  28. hook(type => "pagetemplate", id => "comments", call => \&pagetemplate);
  29. hook(type => "formbuilder_setup", id => "comments",
  30. call => \&formbuilder_setup);
  31. # Load goto to fix up user page links for logged-in commenters
  32. IkiWiki::loadplugin("goto");
  33. IkiWiki::loadplugin("inline");
  34. }
  35. sub getsetup () {
  36. return
  37. plugin => {
  38. safe => 1,
  39. rebuild => 1,
  40. section => "web",
  41. },
  42. comments_pagespec => {
  43. type => 'pagespec',
  44. example => 'blog/* and !*/Discussion',
  45. description => 'PageSpec of pages where comments are allowed',
  46. link => 'ikiwiki/PageSpec',
  47. safe => 1,
  48. rebuild => 1,
  49. },
  50. comments_closed_pagespec => {
  51. type => 'pagespec',
  52. example => 'blog/controversial or blog/flamewar',
  53. description => 'PageSpec of pages where posting new comments is not allowed',
  54. link => 'ikiwiki/PageSpec',
  55. safe => 1,
  56. rebuild => 1,
  57. },
  58. comments_pagename => {
  59. type => 'string',
  60. default => 'comment_',
  61. description => 'Base name for comments, e.g. "comment_" for pages like "sandbox/comment_12"',
  62. safe => 0, # manual page moving required
  63. rebuild => undef,
  64. },
  65. comments_allowdirectives => {
  66. type => 'boolean',
  67. example => 0,
  68. description => 'Interpret directives in comments?',
  69. safe => 1,
  70. rebuild => 0,
  71. },
  72. comments_allowauthor => {
  73. type => 'boolean',
  74. example => 0,
  75. description => 'Allow anonymous commenters to set an author name?',
  76. safe => 1,
  77. rebuild => 0,
  78. },
  79. comments_commit => {
  80. type => 'boolean',
  81. example => 1,
  82. description => 'commit comments to the VCS',
  83. # old uncommitted comments are likely to cause
  84. # confusion if this is changed
  85. safe => 0,
  86. rebuild => 0,
  87. },
  88. }
  89. sub checkconfig () {
  90. $config{comments_commit} = 1
  91. unless defined $config{comments_commit};
  92. $config{comments_pagespec} = ''
  93. unless defined $config{comments_pagespec};
  94. $config{comments_closed_pagespec} = ''
  95. unless defined $config{comments_closed_pagespec};
  96. $config{comments_pagename} = 'comment_'
  97. unless defined $config{comments_pagename};
  98. }
  99. sub htmlize {
  100. my %params = @_;
  101. return $params{content};
  102. }
  103. sub htmlize_pending {
  104. my %params = @_;
  105. return sprintf(gettext("this comment needs %s"),
  106. '<a href="'.
  107. IkiWiki::cgiurl(do => "commentmoderation").'">'.
  108. gettext("moderation").'</a>');
  109. }
  110. # FIXME: copied verbatim from meta
  111. sub safeurl ($) {
  112. my $url=shift;
  113. if (exists $IkiWiki::Plugin::htmlscrubber::{safe_url_regexp} &&
  114. defined $IkiWiki::Plugin::htmlscrubber::safe_url_regexp) {
  115. return $url=~/$IkiWiki::Plugin::htmlscrubber::safe_url_regexp/;
  116. }
  117. else {
  118. return 1;
  119. }
  120. }
  121. sub preprocess {
  122. my %params = @_;
  123. my $page = $params{page};
  124. my $format = $params{format};
  125. if (defined $format && ! exists $IkiWiki::hooks{htmlize}{$format}) {
  126. error(sprintf(gettext("unsupported page format %s"), $format));
  127. }
  128. my $content = $params{content};
  129. if (! defined $content) {
  130. error(gettext("comment must have content"));
  131. }
  132. $content =~ s/\\"/"/g;
  133. if ($config{comments_allowdirectives}) {
  134. $content = IkiWiki::preprocess($page, $params{destpage},
  135. $content);
  136. }
  137. # no need to bother with htmlize if it's just HTML
  138. $content = IkiWiki::htmlize($page, $params{destpage}, $format, $content)
  139. if defined $format;
  140. IkiWiki::run_hooks(sanitize => sub {
  141. $content = shift->(
  142. page => $page,
  143. destpage => $params{destpage},
  144. content => $content,
  145. );
  146. });
  147. # set metadata, possibly overriding [[!meta]] directives from the
  148. # comment itself
  149. my $commentuser;
  150. my $commentip;
  151. my $commentauthor;
  152. my $commentauthorurl;
  153. my $commentopenid;
  154. if (defined $params{username}) {
  155. $commentuser = $params{username};
  156. my $oiduser = eval { IkiWiki::openiduser($commentuser) };
  157. if (defined $oiduser) {
  158. # looks like an OpenID
  159. $commentauthorurl = $commentuser;
  160. $commentauthor = (defined $params{nickname} && length $params{nickname}) ? $params{nickname} : $oiduser;
  161. $commentopenid = $commentuser;
  162. }
  163. else {
  164. $commentauthorurl = IkiWiki::cgiurl(
  165. do => 'goto',
  166. page => IkiWiki::userpage($commentuser)
  167. );
  168. $commentauthor = $commentuser;
  169. }
  170. }
  171. else {
  172. if (defined $params{ip}) {
  173. $commentip = $params{ip};
  174. }
  175. $commentauthor = gettext("Anonymous");
  176. }
  177. $commentstate{$page}{commentuser} = $commentuser;
  178. $commentstate{$page}{commentopenid} = $commentopenid;
  179. $commentstate{$page}{commentip} = $commentip;
  180. $commentstate{$page}{commentauthor} = $commentauthor;
  181. $commentstate{$page}{commentauthorurl} = $commentauthorurl;
  182. if (! defined $pagestate{$page}{meta}{author}) {
  183. $pagestate{$page}{meta}{author} = $commentauthor;
  184. }
  185. if (! defined $pagestate{$page}{meta}{authorurl}) {
  186. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  187. }
  188. if ($config{comments_allowauthor}) {
  189. if (defined $params{claimedauthor}) {
  190. $pagestate{$page}{meta}{author} = $params{claimedauthor};
  191. }
  192. if (defined $params{url}) {
  193. my $url=$params{url};
  194. eval q{use URI::Heuristic};
  195. if (! $@) {
  196. $url=URI::Heuristic::uf_uristr($url);
  197. }
  198. if (safeurl($url)) {
  199. $pagestate{$page}{meta}{authorurl} = $url;
  200. }
  201. }
  202. }
  203. else {
  204. $pagestate{$page}{meta}{author} = $commentauthor;
  205. $pagestate{$page}{meta}{authorurl} = $commentauthorurl;
  206. }
  207. if (defined $params{subject}) {
  208. # decode title the same way meta does
  209. eval q{use HTML::Entities};
  210. $pagestate{$page}{meta}{title} = decode_entities($params{subject});
  211. }
  212. if ($params{page} =~ m/\/\Q$config{comments_pagename}\E\d+_/) {
  213. $pagestate{$page}{meta}{permalink} = urlto(IkiWiki::dirname($params{page}), undef, 1).
  214. "#".page_to_id($params{page});
  215. }
  216. eval q{use Date::Parse};
  217. if (! $@) {
  218. my $time = str2time($params{date});
  219. $IkiWiki::pagectime{$page} = $time if defined $time;
  220. }
  221. return $content;
  222. }
  223. sub preprocess_moderation {
  224. my %params = @_;
  225. $params{desc}=gettext("Comment Moderation")
  226. unless defined $params{desc};
  227. if (length $config{cgiurl}) {
  228. return '<a href="'.
  229. IkiWiki::cgiurl(do => 'commentmoderation').
  230. '">'.$params{desc}.'</a>';
  231. }
  232. else {
  233. return $params{desc};
  234. }
  235. }
  236. sub sessioncgi ($$) {
  237. my $cgi=shift;
  238. my $session=shift;
  239. my $do = $cgi->param('do');
  240. if ($do eq 'comment') {
  241. editcomment($cgi, $session);
  242. }
  243. elsif ($do eq 'commentmoderation') {
  244. commentmoderation($cgi, $session);
  245. }
  246. elsif ($do eq 'commentsignin') {
  247. IkiWiki::cgi_signin($cgi, $session);
  248. exit;
  249. }
  250. }
  251. # Mostly cargo-culted from IkiWiki::plugin::editpage
  252. sub editcomment ($$) {
  253. my $cgi=shift;
  254. my $session=shift;
  255. IkiWiki::decode_cgi_utf8($cgi);
  256. eval q{use CGI::FormBuilder};
  257. error($@) if $@;
  258. my @buttons = (POST_COMMENT, PREVIEW, CANCEL);
  259. my $form = CGI::FormBuilder->new(
  260. fields => [qw{do sid page subject editcontent type author url}],
  261. charset => 'utf-8',
  262. method => 'POST',
  263. required => [qw{editcontent}],
  264. javascript => 0,
  265. params => $cgi,
  266. action => $config{cgiurl},
  267. header => 0,
  268. table => 0,
  269. template => { template('editcomment.tmpl') },
  270. );
  271. IkiWiki::decode_form_utf8($form);
  272. IkiWiki::run_hooks(formbuilder_setup => sub {
  273. shift->(title => "comment", form => $form, cgi => $cgi,
  274. session => $session, buttons => \@buttons);
  275. });
  276. IkiWiki::decode_form_utf8($form);
  277. my $type = $form->param('type');
  278. if (defined $type && length $type && $IkiWiki::hooks{htmlize}{$type}) {
  279. $type = IkiWiki::possibly_foolish_untaint($type);
  280. }
  281. else {
  282. $type = $config{default_pageext};
  283. }
  284. my @page_types;
  285. if (exists $IkiWiki::hooks{htmlize}) {
  286. foreach my $key (grep { !/^_/ } keys %{$IkiWiki::hooks{htmlize}}) {
  287. push @page_types, [$key, $IkiWiki::hooks{htmlize}{$key}{longname} || $key];
  288. }
  289. }
  290. @page_types=sort @page_types;
  291. $form->field(name => 'do', type => 'hidden');
  292. $form->field(name => 'sid', type => 'hidden', value => $session->id,
  293. force => 1);
  294. $form->field(name => 'page', type => 'hidden');
  295. $form->field(name => 'subject', type => 'text', size => 72);
  296. $form->field(name => 'editcontent', type => 'textarea', rows => 10);
  297. $form->field(name => "type", value => $type, force => 1,
  298. type => 'select', options => \@page_types);
  299. $form->tmpl_param(username => $session->param('name'));
  300. if ($config{comments_allowauthor} and
  301. ! defined $session->param('name')) {
  302. $form->tmpl_param(allowauthor => 1);
  303. $form->field(name => 'author', type => 'text', size => '40');
  304. $form->field(name => 'url', type => 'text', size => '40');
  305. }
  306. else {
  307. $form->tmpl_param(allowauthor => 0);
  308. $form->field(name => 'author', type => 'hidden', value => '',
  309. force => 1);
  310. $form->field(name => 'url', type => 'hidden', value => '',
  311. force => 1);
  312. }
  313. if (! defined $session->param('name')) {
  314. # Make signinurl work and return here.
  315. $form->tmpl_param(signinurl => IkiWiki::cgiurl(do => 'commentsignin'));
  316. $session->param(postsignin => $ENV{QUERY_STRING});
  317. IkiWiki::cgi_savesession($session);
  318. }
  319. # The untaint is OK (as in editpage) because we're about to pass
  320. # it to file_pruned anyway
  321. my $page = $form->field('page');
  322. $page = IkiWiki::possibly_foolish_untaint($page);
  323. if (! defined $page || ! length $page ||
  324. IkiWiki::file_pruned($page)) {
  325. error(gettext("bad page name"));
  326. }
  327. my $baseurl = urlto($page, undef, 1);
  328. $form->title(sprintf(gettext("commenting on %s"),
  329. IkiWiki::pagetitle($page)));
  330. $form->tmpl_param('helponformattinglink',
  331. htmllink($page, $page, 'ikiwiki/formatting',
  332. noimageinline => 1,
  333. linktext => 'FormattingHelp'),
  334. allowdirectives => $config{allow_directives});
  335. if ($form->submitted eq CANCEL) {
  336. # bounce back to the page they wanted to comment on, and exit.
  337. # CANCEL need not be considered in future
  338. IkiWiki::redirect($cgi, urlto($page, undef, 1));
  339. exit;
  340. }
  341. if (not exists $pagesources{$page}) {
  342. error(sprintf(gettext(
  343. "page '%s' doesn't exist, so you can't comment"),
  344. $page));
  345. }
  346. if (pagespec_match($page, $config{comments_closed_pagespec},
  347. location => $page)) {
  348. error(sprintf(gettext(
  349. "comments on page '%s' are closed"),
  350. $page));
  351. }
  352. # Set a flag to indicate that we're posting a comment,
  353. # so that postcomment() can tell it should match.
  354. $postcomment=1;
  355. IkiWiki::check_canedit($page, $cgi, $session);
  356. $postcomment=0;
  357. my $content = "[[!comment format=$type\n";
  358. if (defined $session->param('name')) {
  359. my $username = $session->param('name');
  360. $username =~ s/"/&quot;/g;
  361. $content .= " username=\"$username\"\n";
  362. }
  363. if (defined $session->param('nickname')) {
  364. my $nickname = $session->param('nickname');
  365. $nickname =~ s/"/&quot;/g;
  366. $content .= " nickname=\"$nickname\"\n";
  367. }
  368. elsif (defined $session->remote_addr()) {
  369. my $ip = $session->remote_addr();
  370. if ($ip =~ m/^([.0-9]+)$/) {
  371. $content .= " ip=\"$1\"\n";
  372. }
  373. }
  374. if ($config{comments_allowauthor}) {
  375. my $author = $form->field('author');
  376. if (defined $author && length $author) {
  377. $author =~ s/"/&quot;/g;
  378. $content .= " claimedauthor=\"$author\"\n";
  379. }
  380. my $url = $form->field('url');
  381. if (defined $url && length $url) {
  382. $url =~ s/"/&quot;/g;
  383. $content .= " url=\"$url\"\n";
  384. }
  385. }
  386. my $subject = $form->field('subject');
  387. if (defined $subject && length $subject) {
  388. $subject =~ s/"/&quot;/g;
  389. }
  390. else {
  391. $subject = "comment ".(num_comments($page, $config{srcdir}) + 1);
  392. }
  393. $content .= " subject=\"$subject\"\n";
  394. $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n";
  395. my $editcontent = $form->field('editcontent');
  396. $editcontent="" if ! defined $editcontent;
  397. $editcontent =~ s/\r\n/\n/g;
  398. $editcontent =~ s/\r/\n/g;
  399. $editcontent =~ s/"/\\"/g;
  400. $content .= " content=\"\"\"\n$editcontent\n\"\"\"]]\n";
  401. my $location=unique_comment_location($page, $content, $config{srcdir});
  402. # This is essentially a simplified version of editpage:
  403. # - the user does not control the page that's created, only the parent
  404. # - it's always a create operation, never an edit
  405. # - this means that conflicts should never happen
  406. # - this means that if they do, rocks fall and everyone dies
  407. if ($form->submitted eq PREVIEW) {
  408. my $preview=previewcomment($content, $location, $page, time);
  409. IkiWiki::run_hooks(format => sub {
  410. $preview = shift->(page => $page,
  411. content => $preview);
  412. });
  413. $form->tmpl_param(page_preview => $preview);
  414. }
  415. else {
  416. $form->tmpl_param(page_preview => "");
  417. }
  418. if ($form->submitted eq POST_COMMENT && $form->validate) {
  419. IkiWiki::checksessionexpiry($cgi, $session);
  420. $postcomment=1;
  421. my $ok=IkiWiki::check_content(content => $form->field('editcontent'),
  422. subject => $form->field('subject'),
  423. $config{comments_allowauthor} ? (
  424. author => $form->field('author'),
  425. url => $form->field('url'),
  426. ) : (),
  427. page => $location,
  428. cgi => $cgi,
  429. session => $session,
  430. nonfatal => 1,
  431. );
  432. $postcomment=0;
  433. if (! $ok) {
  434. $location=unique_comment_location($page, $content, $config{srcdir}, "._comment_pending");
  435. writefile("$location._comment_pending", $config{srcdir}, $content);
  436. # Refresh so anything that deals with pending
  437. # comments can be updated.
  438. require IkiWiki::Render;
  439. IkiWiki::refresh();
  440. IkiWiki::saveindex();
  441. IkiWiki::printheader($session);
  442. print IkiWiki::misctemplate(gettext(gettext("comment stored for moderation")),
  443. "<p>".
  444. gettext("Your comment will be posted after moderator review").
  445. "</p>");
  446. exit;
  447. }
  448. # FIXME: could probably do some sort of graceful retry
  449. # on error? Would require significant unwinding though
  450. my $file = "$location._comment";
  451. writefile($file, $config{srcdir}, $content);
  452. my $conflict;
  453. if ($config{rcs} and $config{comments_commit}) {
  454. my $message = gettext("Added a comment");
  455. if (defined $form->field('subject') &&
  456. length $form->field('subject')) {
  457. $message = sprintf(
  458. gettext("Added a comment: %s"),
  459. $form->field('subject'));
  460. }
  461. IkiWiki::rcs_add($file);
  462. IkiWiki::disable_commit_hook();
  463. $conflict = IkiWiki::rcs_commit_staged(
  464. message => $message,
  465. session => $session,
  466. );
  467. IkiWiki::enable_commit_hook();
  468. IkiWiki::rcs_update();
  469. }
  470. # Now we need a refresh
  471. require IkiWiki::Render;
  472. IkiWiki::refresh();
  473. IkiWiki::saveindex();
  474. # this should never happen, unless a committer deliberately
  475. # breaks it or something
  476. error($conflict) if defined $conflict;
  477. # Jump to the new comment on the page.
  478. # The trailing question mark tries to avoid broken
  479. # caches and get the most recent version of the page.
  480. IkiWiki::redirect($cgi, urlto($page, undef, 1).
  481. "?updated#".page_to_id($location));
  482. }
  483. else {
  484. IkiWiki::showform ($form, \@buttons, $session, $cgi,
  485. forcebaseurl => $baseurl, page => $page);
  486. }
  487. exit;
  488. }
  489. sub commentmoderation ($$) {
  490. my $cgi=shift;
  491. my $session=shift;
  492. IkiWiki::needsignin($cgi, $session);
  493. if (! IkiWiki::is_admin($session->param("name"))) {
  494. error(gettext("you are not logged in as an admin"));
  495. }
  496. IkiWiki::decode_cgi_utf8($cgi);
  497. if (defined $cgi->param('sid')) {
  498. IkiWiki::checksessionexpiry($cgi, $session);
  499. my $rejectalldefer=$cgi->param('rejectalldefer');
  500. my %vars=$cgi->Vars;
  501. my $added=0;
  502. foreach my $id (keys %vars) {
  503. if ($id =~ /(.*)\._comment(?:_pending)?$/) {
  504. $id=decode_utf8($id);
  505. my $action=$cgi->param($id);
  506. next if $action eq 'Defer' && ! $rejectalldefer;
  507. # Make sure that the id is of a legal
  508. # pending comment.
  509. my ($f) = $id =~ /$config{wiki_file_regexp}/;
  510. if (! defined $f || ! length $f ||
  511. IkiWiki::file_pruned($f)) {
  512. error("illegal file");
  513. }
  514. my $page=IkiWiki::dirname($f);
  515. my $file="$config{srcdir}/$f";
  516. if (! -e $file) {
  517. # old location
  518. $file="$config{wikistatedir}/comments_pending/".$f;
  519. }
  520. if ($action eq 'Accept') {
  521. my $content=eval { readfile($file) };
  522. next if $@; # file vanished since form was displayed
  523. my $dest=unique_comment_location($page, $content, $config{srcdir})."._comment";
  524. writefile($dest, $config{srcdir}, $content);
  525. if ($config{rcs} and $config{comments_commit}) {
  526. IkiWiki::rcs_add($dest);
  527. }
  528. $added++;
  529. }
  530. require IkiWiki::Render;
  531. IkiWiki::prune($file);
  532. }
  533. }
  534. if ($added) {
  535. my $conflict;
  536. if ($config{rcs} and $config{comments_commit}) {
  537. my $message = gettext("Comment moderation");
  538. IkiWiki::disable_commit_hook();
  539. $conflict=IkiWiki::rcs_commit_staged(
  540. message => $message,
  541. session => $session,
  542. );
  543. IkiWiki::enable_commit_hook();
  544. IkiWiki::rcs_update();
  545. }
  546. # Now we need a refresh
  547. require IkiWiki::Render;
  548. IkiWiki::refresh();
  549. IkiWiki::saveindex();
  550. error($conflict) if defined $conflict;
  551. }
  552. }
  553. my @comments=map {
  554. my ($id, $dir, $ctime)=@{$_};
  555. my $content=readfile("$dir/$id");
  556. my $preview=previewcomment($content, $id,
  557. $id, $ctime);
  558. {
  559. id => $id,
  560. view => $preview,
  561. }
  562. } sort { $b->[2] <=> $a->[2] } comments_pending();
  563. my $template=template("commentmoderation.tmpl");
  564. $template->param(
  565. sid => $session->id,
  566. comments => \@comments,
  567. );
  568. IkiWiki::printheader($session);
  569. my $out=$template->output;
  570. IkiWiki::run_hooks(format => sub {
  571. $out = shift->(page => "", content => $out);
  572. });
  573. print IkiWiki::misctemplate(gettext("comment moderation"), $out);
  574. exit;
  575. }
  576. sub formbuilder_setup (@) {
  577. my %params=@_;
  578. my $form=$params{form};
  579. if ($form->title eq "preferences" &&
  580. IkiWiki::is_admin($params{session}->param("name"))) {
  581. push @{$params{buttons}}, "Comment Moderation";
  582. if ($form->submitted && $form->submitted eq "Comment Moderation") {
  583. commentmoderation($params{cgi}, $params{session});
  584. }
  585. }
  586. }
  587. sub comments_pending () {
  588. my @ret;
  589. eval q{use File::Find};
  590. error($@) if $@;
  591. eval q{use Cwd};
  592. error($@) if $@;
  593. my $origdir=getcwd();
  594. my $find_comments=sub {
  595. my $dir=shift;
  596. my $extension=shift;
  597. return unless -d $dir;
  598. chdir($dir) || die "chdir $dir: $!";
  599. find({
  600. no_chdir => 1,
  601. wanted => sub {
  602. my $file=decode_utf8($_);
  603. $file=~s/^\.\///;
  604. return if ! length $file || IkiWiki::file_pruned($file)
  605. || -l $_ || -d _ || $file !~ /\Q$extension\E$/;
  606. my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
  607. if (defined $f) {
  608. my $ctime=(stat($_))[10];
  609. push @ret, [$f, $dir, $ctime];
  610. }
  611. }
  612. }, ".");
  613. chdir($origdir) || die "chdir $origdir: $!";
  614. };
  615. $find_comments->($config{srcdir}, "._comment_pending");
  616. # old location
  617. $find_comments->("$config{wikistatedir}/comments_pending/",
  618. "._comment");
  619. return @ret;
  620. }
  621. sub previewcomment ($$$) {
  622. my $content=shift;
  623. my $location=shift;
  624. my $page=shift;
  625. my $time=shift;
  626. # Previewing a comment should implicitly enable comment posting mode.
  627. my $oldpostcomment=$postcomment;
  628. $postcomment=1;
  629. my $preview = IkiWiki::htmlize($location, $page, '_comment',
  630. IkiWiki::linkify($location, $page,
  631. IkiWiki::preprocess($location, $page,
  632. IkiWiki::filter($location, $page, $content), 0, 1)));
  633. my $template = template("comment.tmpl");
  634. $template->param(content => $preview);
  635. $template->param(ctime => displaytime($time, undef, 1));
  636. $template->param(html5 => $config{html5});
  637. IkiWiki::run_hooks(pagetemplate => sub {
  638. shift->(page => $location,
  639. destpage => $page,
  640. template => $template);
  641. });
  642. $template->param(have_actions => 0);
  643. $postcomment=$oldpostcomment;
  644. return $template->output;
  645. }
  646. sub commentsshown ($) {
  647. my $page=shift;
  648. return ! pagespec_match($page, "comment(*)",
  649. location => $page) &&
  650. pagespec_match($page, $config{comments_pagespec},
  651. location => $page);
  652. }
  653. sub commentsopen ($) {
  654. my $page = shift;
  655. return length $config{cgiurl} > 0 &&
  656. (! length $config{comments_closed_pagespec} ||
  657. ! pagespec_match($page, $config{comments_closed_pagespec},
  658. location => $page));
  659. }
  660. sub pagetemplate (@) {
  661. my %params = @_;
  662. my $page = $params{page};
  663. my $template = $params{template};
  664. my $shown = ($template->query(name => 'commentslink') ||
  665. $template->query(name => 'commentsurl') ||
  666. $template->query(name => 'atomcommentsurl') ||
  667. $template->query(name => 'comments')) &&
  668. commentsshown($page);
  669. if ($template->query(name => 'comments')) {
  670. my $comments = undef;
  671. if ($shown) {
  672. $comments = IkiWiki::preprocess_inline(
  673. pages => "comment($page)",
  674. template => 'comment',
  675. show => 0,
  676. reverse => 'yes',
  677. page => $page,
  678. destpage => $params{destpage},
  679. feedfile => 'comments',
  680. emptyfeeds => 'no',
  681. );
  682. }
  683. if (defined $comments && length $comments) {
  684. $template->param(comments => $comments);
  685. }
  686. if ($shown && commentsopen($page)) {
  687. $template->param(addcommenturl => addcommenturl($page));
  688. }
  689. }
  690. if ($shown) {
  691. if ($template->query(name => 'commentsurl')) {
  692. $template->param(commentsurl =>
  693. urlto($page, undef, 1).'#comments');
  694. }
  695. if ($template->query(name => 'atomcommentsurl') && $config{usedirs}) {
  696. # This will 404 until there are some comments, but I
  697. # think that's probably OK...
  698. $template->param(atomcommentsurl =>
  699. urlto($page, undef, 1).'comments.atom');
  700. }
  701. if ($template->query(name => 'commentslink')) {
  702. my $num=num_comments($page, $config{srcdir});
  703. my $link;
  704. if ($num > 0) {
  705. $link = htmllink($page, $params{destpage}, $page,
  706. linktext => sprintf(ngettext("%i comment", "%i comments", $num), $num),
  707. anchor => "comments",
  708. noimageinline => 1
  709. );
  710. }
  711. elsif (commentsopen($page)) {
  712. $link = "<a href=\"".addcommenturl($page)."\">".
  713. #translators: Here "Comment" is a verb;
  714. #translators: the user clicks on it to
  715. #translators: post a comment.
  716. gettext("Comment").
  717. "</a>";
  718. }
  719. $template->param(commentslink => $link)
  720. if defined $link;
  721. }
  722. }
  723. # everything below this point is only relevant to the comments
  724. # themselves
  725. if (!exists $commentstate{$page}) {
  726. return;
  727. }
  728. if ($template->query(name => 'commentid')) {
  729. $template->param(commentid => page_to_id($page));
  730. }
  731. if ($template->query(name => 'commentuser')) {
  732. $template->param(commentuser =>
  733. $commentstate{$page}{commentuser});
  734. }
  735. if ($template->query(name => 'commentopenid')) {
  736. $template->param(commentopenid =>
  737. $commentstate{$page}{commentopenid});
  738. }
  739. if ($template->query(name => 'commentip')) {
  740. $template->param(commentip =>
  741. $commentstate{$page}{commentip});
  742. }
  743. if ($template->query(name => 'commentauthor')) {
  744. $template->param(commentauthor =>
  745. $commentstate{$page}{commentauthor});
  746. }
  747. if ($template->query(name => 'commentauthorurl')) {
  748. $template->param(commentauthorurl =>
  749. $commentstate{$page}{commentauthorurl});
  750. }
  751. if ($template->query(name => 'removeurl') &&
  752. IkiWiki::Plugin::remove->can("check_canremove") &&
  753. length $config{cgiurl}) {
  754. $template->param(removeurl => IkiWiki::cgiurl(do => 'remove',
  755. page => $page));
  756. $template->param(have_actions => 1);
  757. }
  758. }
  759. sub addcommenturl ($) {
  760. my $page=shift;
  761. return IkiWiki::cgiurl(do => 'comment', page => $page);
  762. }
  763. sub num_comments ($$) {
  764. my $page=shift;
  765. my $dir=shift;
  766. my @comments=glob("$dir/$page/$config{comments_pagename}*._comment");
  767. return int @comments;
  768. }
  769. sub unique_comment_location ($$$$) {
  770. my $page=shift;
  771. eval q{use Digest::MD5 'md5_hex'};
  772. error($@) if $@;
  773. my $content_md5=md5_hex(Encode::encode_utf8(shift));
  774. my $dir=shift;
  775. my $ext=shift || "._comment";
  776. my $location;
  777. my $i = num_comments($page, $dir);
  778. do {
  779. $i++;
  780. $location = "$page/$config{comments_pagename}${i}_${content_md5}";
  781. } while (-e "$dir/$location$ext");
  782. return $location;
  783. }
  784. sub page_to_id ($) {
  785. # Converts a comment page name into a unique, legal html id
  786. # attribute value, that can be used as an anchor to link to the
  787. # comment.
  788. my $page=shift;
  789. eval q{use Digest::MD5 'md5_hex'};
  790. error($@) if $@;
  791. return "comment-".md5_hex(Encode::encode_utf8(($page)));
  792. }
  793. package IkiWiki::PageSpec;
  794. sub match_postcomment ($$;@) {
  795. my $page = shift;
  796. my $glob = shift;
  797. if (! $postcomment) {
  798. return IkiWiki::FailReason->new("not posting a comment");
  799. }
  800. return match_glob($page, $glob, @_);
  801. }
  802. sub match_comment ($$;@) {
  803. my $page = shift;
  804. my $glob = shift;
  805. if (! $postcomment) {
  806. # To see if it's a comment, check the source file type.
  807. # Deal with comments that were just deleted.
  808. my $source=exists $IkiWiki::pagesources{$page} ?
  809. $IkiWiki::pagesources{$page} :
  810. $IkiWiki::delpagesources{$page};
  811. my $type=defined $source ? IkiWiki::pagetype($source) : undef;
  812. if (! defined $type || $type ne "_comment") {
  813. return IkiWiki::FailReason->new("$page is not a comment");
  814. }
  815. }
  816. return match_glob($page, "$glob/*", internal => 1, @_);
  817. }
  818. sub match_comment_pending ($$;@) {
  819. my $page = shift;
  820. my $glob = shift;
  821. my $source=exists $IkiWiki::pagesources{$page} ?
  822. $IkiWiki::pagesources{$page} :
  823. $IkiWiki::delpagesources{$page};
  824. my $type=defined $source ? IkiWiki::pagetype($source) : undef;
  825. if (! defined $type || $type ne "_comment_pending") {
  826. return IkiWiki::FailReason->new("$page is not a pending comment");
  827. }
  828. return match_glob($page, "$glob/*", internal => 1, @_);
  829. }
  830. 1